From 8d83f278d21d570652891358208285de866b14fb Mon Sep 17 00:00:00 2001 From: Arkadiusz Szczepkowicz Date: Tue, 28 May 2024 18:01:10 +0200 Subject: [PATCH 1/4] #272: Add unit test to cover empty base optimization when using tuple --- tests/unit/test_tuple.cc | 159 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 tests/unit/test_tuple.cc diff --git a/tests/unit/test_tuple.cc b/tests/unit/test_tuple.cc new file mode 100644 index 00000000..b454609e --- /dev/null +++ b/tests/unit/test_tuple.cc @@ -0,0 +1,159 @@ +/* +//@HEADER +// ***************************************************************************** +// +// test_tuple.cc +// DARMA/checkpoint => Serialization Library +// +// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC +// (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S. +// Government retains certain rights in this software. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * Neither the name of the copyright holder nor the names of its +// contributors may be used to endorse or promote products derived from this +// software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// Questions? Contact darma@sandia.gov +// +// ***************************************************************************** +//@HEADER +*/ + +#include + +#include "test_harness.h" + +#include +#include + +namespace checkpoint { namespace tests { namespace unit { + +struct TestTuple : TestHarness { }; + +struct Base { + template + void serialize(SerializerT&) { } +}; +struct DerivedEmpty : Base { + template + void serialize(SerializerT&) { } + + bool operator==(const DerivedEmpty&) const { return true; } +}; + +struct Derived : Base { + Derived(int in_x) : x(in_x){}; + Derived() : x(1){}; + int x; + + bool operator==(const Derived& rhs) const { return x == rhs.x; } + + template + void serialize(SerializerT& s) { + s | x; + } +}; + +struct Derived2 : Base { + Derived2(int in_x) : x(in_x){}; + Derived2() : x(1){}; + + Base base; + int x; + + bool operator==(const Derived2& rhs) const { return x == rhs.x; } + + template + void serialize(SerializerT& s) { + s | base; + s | x; + } +}; + +struct Derived3 : Base { + Derived3(int in_x) : x(in_x){}; + Derived3() : x(1){}; + + Derived base; + int x; + + bool operator==(const Derived3& rhs) const { return x == rhs.x; } + + template + void serialize(SerializerT& s) { + s | base; + s | x; + } +}; + +struct Derived4 : Base { + Derived4(int in_x) : x(in_x){}; + Derived4() : x(1){}; + + Derived3 base; + int x; + + bool operator==(const Derived4& rhs) const { return x == rhs.x; } + + template + void serialize(SerializerT& s) { + s | base; + s | x; + } +}; + +template +static void testTupleSerialization(std::tuple before) { + auto ret = checkpoint::serialize(before); + auto after = checkpoint::deserialize>(ret->getBuffer()); + + EXPECT_NE(after, nullptr); + EXPECT_EQ(sizeof(before), sizeof(*after)); + EXPECT_EQ(std::get<0>(before), std::get<0>(*after)); + EXPECT_EQ(std::get<1>(before), std::get<1>(*after)); +} + +TEST_F(TestTuple, test_tuple_simple_types) { + testTupleSerialization(std::make_tuple(123, 0)); + testTupleSerialization(std::make_tuple(1, 2)); + testTupleSerialization(std::make_tuple(3, 4)); + testTupleSerialization(std::make_tuple(1.0, 2.0)); + testTupleSerialization(std::make_tuple(3.0, 4.0)); +} + +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( + std::make_tuple(DerivedEmpty(), DerivedEmpty())); + testTupleSerialization(std::make_tuple(Derived(1), Derived(2))); + testTupleSerialization(std::make_tuple(Derived2(3), Derived2(4))); + testTupleSerialization(std::make_tuple(Derived3(5), Derived3(6))); + testTupleSerialization(std::make_tuple(Derived4(7), Derived4(8))); +} + +}}} // end namespace checkpoint::tests::unit From 15d30054e9ad233bb2cea83c2a28544eacc4f8f5 Mon Sep 17 00:00:00 2001 From: Arkadiusz Szczepkowicz Date: Fri, 31 May 2024 15:53:51 +0200 Subject: [PATCH 2/4] #272: Expand testing to check serialization buffer sizes --- tests/unit/test_tuple.cc | 44 +++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/tests/unit/test_tuple.cc b/tests/unit/test_tuple.cc index b454609e..fd46f8b3 100644 --- a/tests/unit/test_tuple.cc +++ b/tests/unit/test_tuple.cc @@ -55,6 +55,8 @@ struct TestTuple : TestHarness { }; struct Base { template void serialize(SerializerT&) { } + + bool operator==(const Base&) const { return true; } }; struct DerivedEmpty : Base { template @@ -125,10 +127,23 @@ struct Derived4 : Base { }; template -static void testTupleSerialization(std::tuple before) { +static void testTupleSerialization(std::tuple 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 + 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 auto ret = checkpoint::serialize(before); - auto after = checkpoint::deserialize>(ret->getBuffer()); + auto before_size = ret->getSize(); + + auto after = checkpoint::deserialize>(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)); @@ -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( - std::make_tuple(DerivedEmpty(), DerivedEmpty())); - testTupleSerialization(std::make_tuple(Derived(1), Derived(2))); - testTupleSerialization(std::make_tuple(Derived2(3), Derived2(4))); - testTupleSerialization(std::make_tuple(Derived3(5), Derived3(6))); - testTupleSerialization(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(std::make_tuple(Base(), 0)); + testTupleSerialization(std::make_tuple(DerivedEmpty(), 0)); + testTupleSerialization(std::make_tuple(Derived(1), 1)); + testTupleSerialization(std::make_tuple(Derived2(3), 2)); + testTupleSerialization(std::make_tuple(Derived3(5), 3)); + testTupleSerialization(std::make_tuple(Derived4(7), 4)); } }}} // end namespace checkpoint::tests::unit From 371a406d03544d2b0a1faaf755fb946247f7457d Mon Sep 17 00:00:00 2001 From: Arkadiusz Szczepkowicz Date: Tue, 4 Jun 2024 18:22:38 +0200 Subject: [PATCH 3/4] #272: Adjust unit tests to work properly with SERIALIZATION_ERROR_CHECKING flag --- tests/unit/test_tuple.cc | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_tuple.cc b/tests/unit/test_tuple.cc index fd46f8b3..37cae2d5 100644 --- a/tests/unit/test_tuple.cc +++ b/tests/unit/test_tuple.cc @@ -133,8 +133,12 @@ static void testTupleSerialization(std::tuple before) { auto first_part_size = checkpoint::serialize(first_part)->getSize(); // calculate serialized buffer size for std::tuple +#if defined(SERIALIZATION_ERROR_CHECKING) auto second_part = std::make_tuple(std::get<1>(before)); auto second_part_size = checkpoint::serialize(second_part)->getSize(); +#else + auto second_part_size = sizeof(std::tuple); +#endif // Calculate serialized buffer size for std::tuple auto ret = checkpoint::serialize(before); @@ -164,13 +168,18 @@ TEST_F(TestTuple, test_tuple_empty_base_optimization) { 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(); + +#if defined(SERIALIZATION_ERROR_CHECKING) + auto base = Base(); + auto base_size = checkpoint::serialize(base)->getSize(); EXPECT_EQ(base_size + some_int_size, derived_size); +#else + EXPECT_EQ(some_int_size, derived_size); +#endif testTupleSerialization(std::make_tuple(Base(), 0)); testTupleSerialization(std::make_tuple(DerivedEmpty(), 0)); From ef394a85b72d9ff3d2b512b8d90b6a94657146b9 Mon Sep 17 00:00:00 2001 From: Arkadiusz Szczepkowicz Date: Mon, 10 Jun 2024 11:33:28 +0200 Subject: [PATCH 4/4] #272: Fix license --- tests/unit/test_tuple.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_tuple.cc b/tests/unit/test_tuple.cc index 37cae2d5..76c5b5ba 100644 --- a/tests/unit/test_tuple.cc +++ b/tests/unit/test_tuple.cc @@ -2,7 +2,7 @@ //@HEADER // ***************************************************************************** // -// test_tuple.cc +// test_tuple.cc // DARMA/checkpoint => Serialization Library // // Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC