#include "../../data/DataFixture.h" #include "../../data/FixedSizeData.h" #include "../../data/Data.h" #include "google/gtest/gtest.h" using ::testing::Test; using ::testing::WithParamInterface; using ::testing::Values; using std::string; using namespace cpputils; class FixedSizeDataTest: public Test { public: static constexpr size_t SIZE = 16; const string DATA1_AS_STRING = "1491BB4932A389EE14BC7090AC772972"; const string DATA2_AS_STRING = "272EE5517627CFA147A971A8E6E747E0"; const Data DATA3_AS_BINARY; const Data DATA4_AS_BINARY; FixedSizeDataTest() : DATA3_AS_BINARY(DataFixture::generate(SIZE, 1)), DATA4_AS_BINARY(DataFixture::generate(SIZE, 2)) {} template void EXPECT_DATA_EQ(const Data &expected, const FixedSizeData &actual) { EXPECT_EQ(expected.size(), SIZE); EXPECT_EQ(0, std::memcmp(expected.data(), actual.data(), SIZE)); } }; constexpr size_t FixedSizeDataTest::SIZE; TEST_F(FixedSizeDataTest, EqualsTrue) { FixedSizeData DATA1_1 = FixedSizeData::FromString(DATA1_AS_STRING); FixedSizeData DATA1_2 = FixedSizeData::FromString(DATA1_AS_STRING); EXPECT_TRUE(DATA1_1 == DATA1_2); EXPECT_TRUE(DATA1_2 == DATA1_1); } TEST_F(FixedSizeDataTest, EqualsFalse) { FixedSizeData DATA1_1 = FixedSizeData::FromString(DATA1_AS_STRING); FixedSizeData DATA2_1 = FixedSizeData::FromString(DATA2_AS_STRING); EXPECT_FALSE(DATA1_1 == DATA2_1); EXPECT_FALSE(DATA2_1 == DATA1_1); } TEST_F(FixedSizeDataTest, NotEqualsFalse) { FixedSizeData DATA1_1 = FixedSizeData::FromString(DATA1_AS_STRING); FixedSizeData DATA1_2 = FixedSizeData::FromString(DATA1_AS_STRING); EXPECT_FALSE(DATA1_1 != DATA1_2); EXPECT_FALSE(DATA1_2 != DATA1_1); } TEST_F(FixedSizeDataTest, NotEqualsTrue) { FixedSizeData DATA1_1 = FixedSizeData::FromString(DATA1_AS_STRING); FixedSizeData DATA2_1 = FixedSizeData::FromString(DATA2_AS_STRING); EXPECT_TRUE(DATA1_1 != DATA2_1); EXPECT_TRUE(DATA2_1 != DATA1_1); } class FixedSizeDataTestWithStringParam: public FixedSizeDataTest, public WithParamInterface {}; INSTANTIATE_TEST_CASE_P(FixedSizeDataTestWithStringParam, FixedSizeDataTestWithStringParam, Values("2898B4B8A13CA63CBE0F0278CCE465DB", "6FFEBAD90C0DAA2B79628F0627CE9841")); TEST_P(FixedSizeDataTestWithStringParam, FromAndToString) { FixedSizeData data = FixedSizeData::FromString(GetParam()); EXPECT_EQ(GetParam(), data.ToString()); } TEST_P(FixedSizeDataTestWithStringParam, ToAndFromString) { FixedSizeData data = FixedSizeData::FromString(GetParam()); FixedSizeData data2 = FixedSizeData::FromString(data.ToString()); EXPECT_EQ(data, data2); } class FixedSizeDataTestWithBinaryParam: public FixedSizeDataTest, public WithParamInterface { public: static const Data VALUE1; static const Data VALUE2; }; const Data FixedSizeDataTestWithBinaryParam::VALUE1(DataFixture::generate(SIZE, 3)); const Data FixedSizeDataTestWithBinaryParam::VALUE2(DataFixture::generate(SIZE, 4)); INSTANTIATE_TEST_CASE_P(FixedSizeDataTestWithBinaryParam, FixedSizeDataTestWithBinaryParam, Values(&FixedSizeDataTestWithBinaryParam::VALUE1, &FixedSizeDataTestWithBinaryParam::VALUE2)); TEST_P(FixedSizeDataTestWithBinaryParam, FromBinary) { FixedSizeData data = FixedSizeData::FromBinary((uint8_t*)GetParam()->data()); EXPECT_DATA_EQ(*GetParam(), data); } TEST_P(FixedSizeDataTestWithBinaryParam, FromAndToBinary) { FixedSizeData data = FixedSizeData::FromBinary((uint8_t*)GetParam()->data()); Data output(FixedSizeData::BINARY_LENGTH); data.ToBinary(output.data()); EXPECT_EQ(*GetParam(), output); } TEST_P(FixedSizeDataTestWithBinaryParam, ToAndFromBinary) { FixedSizeData data = FixedSizeData::FromBinary((uint8_t*)GetParam()->data()); Data stored(FixedSizeData::BINARY_LENGTH); data.ToBinary(stored.data()); FixedSizeData loaded = FixedSizeData::FromBinary(stored.data()); EXPECT_EQ(data, loaded); } class FixedSizeDataTestWithParam: public FixedSizeDataTest, public WithParamInterface> {}; INSTANTIATE_TEST_CASE_P(FixedSizeDataTestWithParam, FixedSizeDataTestWithParam, Values(FixedSizeData::FromString("2898B4B8A13CA63CBE0F0278CCE465DB"), FixedSizeData::FromString("6FFEBAD90C0DAA2B79628F0627CE9841"))); TEST_P(FixedSizeDataTestWithParam, CopyConstructor) { FixedSizeData copy(GetParam()); EXPECT_EQ(GetParam(), copy); } TEST_P(FixedSizeDataTestWithParam, Take_Half) { FixedSizeData source(GetParam()); FixedSizeData taken = source.take(); EXPECT_EQ(0, std::memcmp(source.data(), taken.data(), SIZE/2)); } TEST_P(FixedSizeDataTestWithParam, Drop_Half) { FixedSizeData source(GetParam()); FixedSizeData taken = source.drop(); EXPECT_EQ(0, std::memcmp(source.data() + SIZE/2, taken.data(), SIZE/2)); } TEST_P(FixedSizeDataTestWithParam, Take_One) { FixedSizeData source(GetParam()); FixedSizeData<1> taken = source.take<1>(); EXPECT_EQ(0, std::memcmp(source.data(), taken.data(), 1)); } TEST_P(FixedSizeDataTestWithParam, Drop_One) { FixedSizeData source(GetParam()); FixedSizeData taken = source.drop<1>(); EXPECT_EQ(0, std::memcmp(source.data() + 1, taken.data(), SIZE-1)); } TEST_P(FixedSizeDataTestWithParam, Take_Nothing) { FixedSizeData source(GetParam()); FixedSizeData<0> taken = source.take<0>(); } TEST_P(FixedSizeDataTestWithParam, Drop_Nothing) { FixedSizeData source(GetParam()); FixedSizeData taken = source.drop<0>(); EXPECT_EQ(0, std::memcmp(source.data(), taken.data(), SIZE)); } TEST_P(FixedSizeDataTestWithParam, Take_All) { FixedSizeData source(GetParam()); FixedSizeData taken = source.take(); EXPECT_EQ(0, std::memcmp(source.data(), taken.data(), SIZE)); } TEST_P(FixedSizeDataTestWithParam, Drop_All) { FixedSizeData source(GetParam()); FixedSizeData<0> taken = source.drop(); } TEST_F(FixedSizeDataTest, CopyConstructorDoesntChangeSource) { FixedSizeData data1 = FixedSizeData::FromString(DATA1_AS_STRING); FixedSizeData data2(data1); EXPECT_EQ(DATA1_AS_STRING, data1.ToString()); } TEST_P(FixedSizeDataTestWithParam, IsEqualAfterAssignment1) { FixedSizeData data2 = FixedSizeData::FromString(DATA2_AS_STRING); EXPECT_NE(GetParam(), data2); data2 = GetParam(); EXPECT_EQ(GetParam(), data2); } TEST_F(FixedSizeDataTest, AssignmentDoesntChangeSource) { FixedSizeData data1 = FixedSizeData::FromString(DATA1_AS_STRING); FixedSizeData data2 = FixedSizeData::FromString(DATA2_AS_STRING); data2 = data1; EXPECT_EQ(DATA1_AS_STRING, data1.ToString()); } // This tests that a FixedSizeData object is very lightweight // (it is meant to be kept on stack and passed around) TEST_F(FixedSizeDataTest, IsLightweightObject) { EXPECT_EQ(FixedSizeData::BINARY_LENGTH, sizeof(FixedSizeData)); }