From 53803c6c4157fdb7e475387e7fddf6179c92f4cc Mon Sep 17 00:00:00 2001 From: Arkadiusz Szczepkowicz Date: Fri, 7 Jun 2024 15:38:00 +0200 Subject: [PATCH 1/3] #273: Add unit tests for std::optional serializer --- tests/unit/test_footprinter.cc | 21 ++++++++ tests/unit/test_optional.cc | 93 ++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 tests/unit/test_optional.cc diff --git a/tests/unit/test_footprinter.cc b/tests/unit/test_footprinter.cc index d4845e29..ec0669df 100644 --- a/tests/unit/test_footprinter.cc +++ b/tests/unit/test_footprinter.cc @@ -533,6 +533,27 @@ TEST_F(TestFootprinter, test_vector) { } } +TEST_F(TestFootprinter, test_optional_footprint) { + { + std::optional opt = true; + EXPECT_EQ(checkpoint::getMemoryFootprint(opt), sizeof(opt) + sizeof(bool)); + opt.reset(); + EXPECT_EQ(checkpoint::getMemoryFootprint(opt), sizeof(opt)); + } + { + std::optional opt = 123; + EXPECT_EQ(checkpoint::getMemoryFootprint(opt), sizeof(opt) + sizeof(long long)); + opt.reset(); + EXPECT_EQ(checkpoint::getMemoryFootprint(opt), sizeof(opt)); + } + { + std::optional opt = "999"; + EXPECT_EQ(checkpoint::getMemoryFootprint(opt), sizeof(opt) + sizeof(std::string("999"))); + opt.reset(); + EXPECT_EQ(checkpoint::getMemoryFootprint(opt), sizeof(opt)); + } +} + TEST_F(TestFootprinter, test_virtual_serialize) { { std::unique_ptr ptr = std::make_unique(0); diff --git a/tests/unit/test_optional.cc b/tests/unit/test_optional.cc new file mode 100644 index 00000000..9feeec17 --- /dev/null +++ b/tests/unit/test_optional.cc @@ -0,0 +1,93 @@ +/* +//@HEADER +// ***************************************************************************** +// +// test_optional.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 +#include + +namespace checkpoint { namespace tests { namespace unit { + +using TestOptional = TestHarness; + +template +void testOptional(std::optional before) { + // Test serialization when containing the value + { + auto ret = checkpoint::serialize(before); + auto after = checkpoint::deserialize>(ret->getBuffer()); + + EXPECT_TRUE(nullptr != after); + EXPECT_EQ(before.has_value(), after->has_value()); + EXPECT_EQ(before, *after); + } + + before.reset(); + + // Test serialization when there is not value + { + auto ret = checkpoint::serialize(before); + auto after = checkpoint::deserialize>(ret->getBuffer()); + + EXPECT_TRUE(nullptr != after); + EXPECT_EQ(before.has_value(), after->has_value()); + EXPECT_EQ(before, *after); + } +} + +TEST_F(TestOptional, test_optional) { + testOptional({true}); + testOptional({1}); + testOptional({2}); + testOptional({3}); + testOptional({4}); + testOptional({5}); + testOptional({6}); + testOptional({"7"}); +} + +}}} // end namespace checkpoint::tests::unit From 50b297930b884cd51a329124a7d49ff3073dea18 Mon Sep 17 00:00:00 2001 From: Arkadiusz Szczepkowicz Date: Fri, 7 Jun 2024 15:38:32 +0200 Subject: [PATCH 2/3] #273: Add std::optional serializer --- src/checkpoint/checkpoint.h | 1 + src/checkpoint/container/optional_serialize.h | 95 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 src/checkpoint/container/optional_serialize.h diff --git a/src/checkpoint/checkpoint.h b/src/checkpoint/checkpoint.h index 18b20fea..0088238b 100644 --- a/src/checkpoint/checkpoint.h +++ b/src/checkpoint/checkpoint.h @@ -65,6 +65,7 @@ #include "checkpoint/container/unique_ptr_serialize.h" #include "checkpoint/container/view_serialize.h" #include "checkpoint/container/variant_serialize.h" +#include "checkpoint/container/optional_serialize.h" #include "checkpoint/container/kokkos_unordered_map_serialize.h" #include "checkpoint/container/kokkos_pair_serialize.h" diff --git a/src/checkpoint/container/optional_serialize.h b/src/checkpoint/container/optional_serialize.h new file mode 100644 index 00000000..acdfc85a --- /dev/null +++ b/src/checkpoint/container/optional_serialize.h @@ -0,0 +1,95 @@ +/* +//@HEADER +// ***************************************************************************** +// +// optional_serialize.h +// 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 +*/ + +#if !defined INCLUDED_CHECKPOINT_CONTAINER_OPTIONAL_SERIALIZE_H +#define INCLUDED_CHECKPOINT_CONTAINER_OPTIONAL_SERIALIZE_H + +#include "checkpoint/common.h" + +#include + +namespace checkpoint { + +template +void deserializeOptional(SerializerT& s, std::optional& optional) { + using BoolReconstructor = + dispatch::Reconstructor::CleanT>; + using ValReconstructor = + dispatch::Reconstructor::CleanT>; + + dispatch::Allocator boolAllocated; + dispatch::Allocator valAllocated; + + auto* has_value = BoolReconstructor::construct(boolAllocated.buf); + s | *has_value; + if (*has_value) { + auto* value = ValReconstructor::construct(valAllocated.buf); + s | *value; + optional = *value; + } else { + optional.reset(); + } +} + +template +void serialize(SerializerT& s, std::optional& optional) { + if (s.isFootprinting()) { + s.countBytes(optional); + if (optional.has_value()) { + s.addBytes(sizeof(*optional)); + } + } else if (s.isUnpacking()) { + deserializeOptional(s, optional); + } else { + bool has_value = optional.has_value(); + + s | has_value; + if (has_value) { + s | *optional; + } + } +} + +} /* end namespace checkpoint */ + +#endif /*INCLUDED_CHECKPOINT_CONTAINER_OPTIONAL_SERIALIZE_H*/ From 2a0ab461e8e5c8d2d46f1264f58252a651fc0430 Mon Sep 17 00:00:00 2001 From: Arkadiusz Szczepkowicz Date: Fri, 7 Jun 2024 15:39:13 +0200 Subject: [PATCH 3/3] #273: Fix licenses --- src/checkpoint/container/optional_serialize.h | 2 +- tests/unit/test_optional.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/checkpoint/container/optional_serialize.h b/src/checkpoint/container/optional_serialize.h index acdfc85a..16b5edae 100644 --- a/src/checkpoint/container/optional_serialize.h +++ b/src/checkpoint/container/optional_serialize.h @@ -2,7 +2,7 @@ //@HEADER // ***************************************************************************** // -// optional_serialize.h +// optional_serialize.h // DARMA/checkpoint => Serialization Library // // Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC diff --git a/tests/unit/test_optional.cc b/tests/unit/test_optional.cc index 9feeec17..69d5afd9 100644 --- a/tests/unit/test_optional.cc +++ b/tests/unit/test_optional.cc @@ -2,7 +2,7 @@ //@HEADER // ***************************************************************************** // -// test_optional.cc +// test_optional.cc // DARMA/checkpoint => Serialization Library // // Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC