Skip to content

Commit

Permalink
Enable integer serialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
asoffer committed Feb 19, 2024
1 parent d33f87c commit d4294bc
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 1 deletion.
13 changes: 13 additions & 0 deletions nth/io/serialize/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ cc_library(
],
)

cc_library(
name = "round_trip",
testonly = True,
hdrs = ["round_trip.h"],
deps = [
"//nth/io/serialize",
"//nth/io/serialize:deserialize",
"//nth/io/writer:string",
"//nth/io/reader:string",
"//nth/test",
],
)

cc_test(
name = "round_trip_test",
srcs = ["round_trip_test.cc"],
Expand Down
34 changes: 34 additions & 0 deletions nth/io/serialize/round_trip.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef NTH_IO_SERIALIZE_ROUND_TRIP_H
#define NTH_IO_SERIALIZE_ROUND_TRIP_H

#include <concepts>
#include <string>

#include "nth/io/reader/string.h"
#include "nth/io/serialize/deserialize.h"
#include "nth/io/serialize/serialize.h"
#include "nth/io/writer/string.h"
#include "nth/test/test.h"

NTH_TEST("nth/io/serialize/round-trip",
std::equality_comparable auto const &original) {
std::decay_t<decltype(original)> object;

{
std::string content;

{
nth::io::string_writer s(content);
NTH_ASSERT(nth::io::serialize(s, original));
}

{
nth::io::string_reader d(content);
NTH_ASSERT(nth::io::deserialize(d, object));
}
}

NTH_EXPECT(original == object);
}

#endif // NTH_IO_SERIALIZE_ROUND_TRIP_H
7 changes: 6 additions & 1 deletion nth/numeric/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ cc_library(
name = "integer",
hdrs = ["integer.h"],
srcs = ["integer.cc"],
deps = ["//nth/debug"],
deps = [
"//nth/debug",
"//nth/io/serialize",
"//nth/io/serialize:deserialize",
],
)

cc_library(
Expand All @@ -22,6 +26,7 @@ cc_test(
deps = [
":integer",
":test_traits",
"//nth/io/serialize:round_trip",
"//nth/test:main",
],
)
40 changes: 40 additions & 0 deletions nth/numeric/integer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <string_view>

#include "nth/debug/debug.h"
#include "nth/io/serialize/deserialize.h"
#include "nth/io/serialize/serialize.h"
#include "nth/meta/concepts.h"

namespace nth {
Expand Down Expand Up @@ -124,6 +126,44 @@ struct integer {
n.words().size());
}

friend bool NthSerialize(auto &s, integer const &n) {
if (not nth::io::serialize_fixed(s, static_cast<bool>(n.sign_)) or
not nth::io::serialize_integer(s, n.size_)) {
return false;
}
for (uintptr_t w : n.words()) {
if (not nth::io::serialize_fixed(s, w)) { return false; }
}
return true;
}

friend bool NthDeserialize(auto &d, integer &n) {
bool sig;
uintptr_t size;
if (not nth::io::deserialize_fixed(d, sig) or
not nth::io::deserialize_integer(d, size)) {
return false;
}
n.sign_ = sig;
n.size_ = size;
switch (size) {
case 0:return true;
case 1: return nth::io::deserialize_fixed(d, n.data_[0]);
case 2:
return nth::io::deserialize_fixed(d, n.data_[0]) and
nth::io::deserialize_fixed(d, n.data_[1]);
default: {
uintptr_t *ptr = new uintptr_t[size];
n.data_[0] = reinterpret_cast<uintptr_t>(ptr);
n.data_[1] = size;
for (size_t i = 0; i < size; ++i) {
if (not nth::io::deserialize_fixed(d, *ptr++)) { return false; }
}
return true;
} break;
}
}

private:
void ResizeTo(uint64_t n);

Expand Down
5 changes: 5 additions & 0 deletions nth/numeric/integer_test.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "nth/numeric/integer.h"

#include "nth/io/serialize/round_trip.h"
#include "nth/numeric/test_traits.h"
#include "nth/strings/format/universal.h"
#include "nth/test/test.h"
Expand Down Expand Up @@ -454,5 +455,9 @@ NTH_TEST("integer/addition/different-signs") {
-1);
}

NTH_INVOKE_TEST("nth/io/serialize/round-trip") {
for (auto const &v : SampleValues()) { co_yield v; }
}

} // namespace
} // namespace nth

0 comments on commit d4294bc

Please sign in to comment.