Skip to content

Commit

Permalink
#272: Expand testing to check serialization buffer sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
thearusable committed May 31, 2024
1 parent 5580f49 commit ff209f0
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions tests/unit/test_tuple.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ struct TestTuple : TestHarness { };
struct Base {
template <typename SerializerT>
void serialize(SerializerT&) { }

bool operator==(const Base&) const { return true; }
};
struct DerivedEmpty : Base {
template <typename SerializerT>
Expand Down Expand Up @@ -125,10 +127,23 @@ struct Derived4 : Base {
};

template <typename T>
static void testTupleSerialization(std::tuple<T, T> before) {
static void testTupleSerialization(std::tuple<T, int> before) {
// Calculate serialized buffer size for T
auto first_part = std::get<0>(before);
auto first_part_size = checkpoint::serialize(first_part)->getSize();

// calculate serialized buffer size for std::tuple<int>
auto second_part = std::make_tuple(std::get<1>(before));
auto second_part_size = checkpoint::serialize(second_part)->getSize();

// Calculate serialized buffer size for std::tuple<T, int>
auto ret = checkpoint::serialize(before);
auto after = checkpoint::deserialize<std::tuple<T, T>>(ret->getBuffer());
auto before_size = ret->getSize();

auto after = checkpoint::deserialize<std::tuple<T, int>>(ret->getBuffer());

// Check sizes and content of the tuples
EXPECT_EQ(first_part_size + second_part_size, before_size);
EXPECT_NE(after, nullptr);
EXPECT_EQ(sizeof(before), sizeof(*after));
EXPECT_EQ(std::get<0>(before), std::get<0>(*after));
Expand All @@ -146,14 +161,23 @@ TEST_F(TestTuple, test_tuple_simple_types) {
TEST_F(TestTuple, test_tuple_empty_base_optimization) {
// Expect Empty Base Optimization to be aplied
EXPECT_GE(sizeof(Base), 1);
EXPECT_EQ(sizeof(Derived), sizeof(int));

testTupleSerialization<DerivedEmpty>(
std::make_tuple(DerivedEmpty(), DerivedEmpty()));
testTupleSerialization<Derived>(std::make_tuple(Derived(1), Derived(2)));
testTupleSerialization<Derived2>(std::make_tuple(Derived2(3), Derived2(4)));
testTupleSerialization<Derived3>(std::make_tuple(Derived3(5), Derived3(6)));
testTupleSerialization<Derived4>(std::make_tuple(Derived4(7), Derived4(8)));
EXPECT_NE(sizeof(Base) + sizeof(int), sizeof(Derived));

// Check serialization buffer sizes for the types without tuple
auto base = Base();
auto base_size = checkpoint::serialize(base)->getSize();
auto some_int = 4;
auto some_int_size = checkpoint::serialize(some_int)->getSize();
auto derived = Derived(12345);
auto derived_size = checkpoint::serialize(derived)->getSize();
EXPECT_EQ(base_size + some_int_size, derived_size);

testTupleSerialization<Base>(std::make_tuple(Base(), 0));
testTupleSerialization<DerivedEmpty>(std::make_tuple(DerivedEmpty(), 0));
testTupleSerialization<Derived>(std::make_tuple(Derived(1), 1));
testTupleSerialization<Derived2>(std::make_tuple(Derived2(3), 2));
testTupleSerialization<Derived3>(std::make_tuple(Derived3(5), 3));
testTupleSerialization<Derived4>(std::make_tuple(Derived4(7), 4));
}

}}} // end namespace checkpoint::tests::unit

0 comments on commit ff209f0

Please sign in to comment.