Skip to content

Commit

Permalink
#272: Add unit test to cover empty base optimization when using tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
thearusable committed May 28, 2024
1 parent 5480204 commit 213c04b
Showing 1 changed file with 159 additions and 0 deletions.
159 changes: 159 additions & 0 deletions tests/unit/test_tuple.cc
Original file line number Diff line number Diff line change
@@ -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 [email protected]
//
// *****************************************************************************
//@HEADER
*/

#include <gtest/gtest.h>

#include "test_harness.h"

#include <checkpoint/checkpoint.h>
#include <tuple>

namespace checkpoint { namespace tests { namespace unit {

struct TestTuple : TestHarness { };

struct Base {
template <typename SerializerT>
void serialize(SerializerT&) { }
};
struct DerivedEmpty : Base {
template <typename SerializerT>
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 <typename SerializerT>
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 <typename SerializerT>
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 <typename SerializerT>
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 <typename SerializerT>
void serialize(SerializerT& s) {
s | base;
s | x;
}
};

template <typename T>
static void testTupleSerialization(std::tuple<T, T> before) {
auto ret = checkpoint::serialize(before);
auto after = checkpoint::deserialize<std::tuple<T, T>>(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<int>(std::make_tuple(123, 0));
testTupleSerialization<uint8_t>(std::make_tuple(1, 2));
testTupleSerialization<int64_t>(std::make_tuple(3, 4));
testTupleSerialization<double>(std::make_tuple(1.0, 2.0));
testTupleSerialization<float>(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<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)));
}

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

0 comments on commit 213c04b

Please sign in to comment.