Skip to content

Commit 298761d

Browse files
committed
[#491] Extend C++ tests to improve test coverage
1 parent dd81e59 commit 298761d

7 files changed

+136
-11
lines changed

compiler/extensions/cpp/runtime/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ set(ZSERIO_PROJECT_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../../../..")
1919
set(CMAKE_MODULE_PATH "${ZSERIO_PROJECT_ROOT}/cmake")
2020

2121
set(ZSERIO_CODE_COVERAGE_ENABLE OFF CACHE BOOL "Enable unit test code coverage calculation.")
22-
set(ZSERIO_CODE_COVERAGE_FAIL_ON_INCOMPLETE ON CACHE BOOL "Fail build if line coverage is not 97%.")
22+
set(ZSERIO_CODE_COVERAGE_FAIL_ON_INCOMPLETE ON CACHE BOOL "Fail build if line coverage is not 98%.")
2323

2424
# cmake helpers
2525
include(cmake_utils)
@@ -57,7 +57,7 @@ if (${ZSERIO_CODE_COVERAGE_ENABLE})
5757
set(COV_PARAMS)
5858
if (${ZSERIO_CODE_COVERAGE_FAIL_ON_INCOMPLETE})
5959
list(APPEND COV_PARAMS "INCOMPLETE_COVERAGE_FAIL")
60-
list(APPEND COV_PARAMS "97")
60+
list(APPEND COV_PARAMS "98")
6161
endif ()
6262
list(APPEND COV_PARAMS "EXCLUDE_SOURCES")
6363
list(APPEND COV_PARAMS ".*test_object.*")

compiler/extensions/cpp/runtime/test/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ set(ZSERIO_CPP_RUNTIME_TEST_HEADERS
141141
)
142142

143143
if (MSVC)
144-
set_property(SOURCE zserio/ReflectableTest.cpp APPEND PROPERTY COMPILE_OPTIONS /bigobj)
144+
set_property(SOURCE zserio/ReflectableTest.cpp zserio/ArrayTest.cpp APPEND PROPERTY COMPILE_OPTIONS /bigobj)
145145
endif ()
146146

147147
if (CMAKE_SIZEOF_VOID_P EQUAL 8)

compiler/extensions/cpp/runtime/test/zserio/AllocatorPropagatingCopyTest.cpp

+53-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,30 @@ namespace
1414
{
1515

1616
class RegularType
17-
{};
17+
{
18+
};
19+
20+
class RegularTypeWithStdAllocator
21+
{
22+
public:
23+
using allocator_type = std::allocator<RegularTypeWithStdAllocator>;
24+
25+
allocator_type get_allocator() const
26+
{
27+
return m_allocator;
28+
}
29+
30+
RegularTypeWithStdAllocator(const allocator_type& allocator = allocator_type())
31+
: m_allocator(allocator)
32+
{}
33+
34+
RegularTypeWithStdAllocator(const RegularTypeWithStdAllocator&, const allocator_type& allocator)
35+
: m_allocator(allocator)
36+
{}
37+
38+
private:
39+
allocator_type m_allocator;
40+
};
1841

1942
class RegularWithAllocatorSupport
2043
{
@@ -37,13 +60,6 @@ class RegularWithAllocatorSupport
3760

3861
RegularWithAllocatorSupport(RegularWithAllocatorSupport&&) = default;
3962

40-
RegularWithAllocatorSupport& operator=(const RegularWithAllocatorSupport& other)
41-
{
42-
if(AllocTraits::propagate_on_container_copy_assignment::value)
43-
m_allocator = other.m_allocator;
44-
return *this;
45-
}
46-
4763
RegularWithAllocatorSupport(PropagateAllocatorT, const RegularWithAllocatorSupport&,
4864
const allocator_type& allocator) :
4965
m_allocator(allocator)
@@ -64,6 +80,14 @@ TEST(AllocatorPropagatingCopyTest, copyDefault)
6480
static_cast<void>(thingCopy);
6581
}
6682

83+
TEST(AllocatorPropagatingCopyTest, copyDefaultStdAllocator)
84+
{
85+
RegularTypeWithStdAllocator::allocator_type allocator;
86+
const RegularTypeWithStdAllocator thing(allocator);
87+
RegularTypeWithStdAllocator thingCopy(allocatorPropagatingCopy(thing, allocator));
88+
ASSERT_EQ(allocator, thingCopy.get_allocator());
89+
}
90+
6791
TEST(AllocatorPropagatingCopyTest, copyDefaultAllocator)
6892
{
6993
RegularWithAllocatorSupport::allocator_type allocator;
@@ -154,5 +178,26 @@ TEST(AllocatorPropagatingCopyTest, copyVector)
154178
}));
155179
}
156180

181+
TEST(AllocatorPropagatingCopyTest, copyVectorRegular)
182+
{
183+
std::allocator<RegularType> allocator;
184+
185+
const std::vector<RegularType> emptyVec(allocator);
186+
std::vector<RegularType>
187+
emptyVecCopy(allocatorPropagatingCopy(emptyVec, allocator));
188+
ASSERT_EQ(emptyVec.get_allocator(), emptyVecCopy.get_allocator());
189+
}
190+
191+
TEST(AllocatorPropagatingCopyTest, copyString)
192+
{
193+
RegularWithAllocatorSupport::allocator_type allocator;
194+
195+
const std::basic_string<char, std::char_traits<char>, RegularWithAllocatorSupport::allocator_type>
196+
emptyString(allocator);
197+
std::basic_string<char, std::char_traits<char>, RegularWithAllocatorSupport::allocator_type>
198+
emptyStringCopy(allocatorPropagatingCopy(emptyString, allocator));
199+
ASSERT_EQ(emptyString.get_allocator(), emptyStringCopy.get_allocator());
200+
}
201+
157202
} // namespace zserio
158203

compiler/extensions/cpp/runtime/test/zserio/ArrayTest.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,9 @@ class ArrayTest : public ::testing::Test
426426
Array<RAW_ARRAY, ARRAY_TRAITS, ArrayType::NORMAL> readArray{arrayTraits};
427427
readArray.read(reader, rawArray.size(), elementFactory);
428428
ASSERT_EQ(array, readArray);
429+
430+
Array<RAW_ARRAY, ARRAY_TRAITS, ArrayType::NORMAL> arrayCopy(array);
431+
ASSERT_EQ(array, arrayCopy);
429432
}
430433
}
431434

@@ -451,6 +454,9 @@ class ArrayTest : public ::testing::Test
451454
Array<RAW_ARRAY, ARRAY_TRAITS, ArrayType::AUTO> readArray{arrayTraits};
452455
readArray.read(reader, elementFactory);
453456
ASSERT_EQ(array, readArray);
457+
458+
Array<RAW_ARRAY, ARRAY_TRAITS, ArrayType::AUTO> arrayCopy(array);
459+
ASSERT_EQ(array, arrayCopy);
454460
}
455461
}
456462

@@ -478,6 +484,10 @@ class ArrayTest : public ::testing::Test
478484
ArrayTestOffsetChecker, ArrayTestOffsetInitializer> readArray{arrayTraits};
479485
readArray.read(reader, rawArray.size(), elementFactory, ArrayTestOffsetChecker());
480486
ASSERT_EQ(array, readArray);
487+
488+
Array<RAW_ARRAY, ARRAY_TRAITS, ArrayType::ALIGNED,
489+
ArrayTestOffsetChecker, ArrayTestOffsetInitializer> arrayCopy(array);
490+
ASSERT_EQ(array, arrayCopy);
481491
}
482492
}
483493

@@ -505,6 +515,10 @@ class ArrayTest : public ::testing::Test
505515
ArrayTestOffsetChecker, ArrayTestOffsetInitializer> readArray{arrayTraits};
506516
readArray.read(reader, elementFactory, ArrayTestOffsetChecker());
507517
ASSERT_EQ(array, readArray);
518+
519+
Array<RAW_ARRAY, ARRAY_TRAITS, ArrayType::ALIGNED_AUTO,
520+
ArrayTestOffsetChecker, ArrayTestOffsetInitializer> arrayCopy(array);
521+
ASSERT_EQ(array, arrayCopy);
508522
}
509523
}
510524

@@ -533,6 +547,9 @@ class ArrayTest : public ::testing::Test
533547
Array<RAW_ARRAY, ARRAY_TRAITS, ArrayType::IMPLICIT> readArray{arrayTraits};
534548
readArray.read(reader);
535549
ASSERT_EQ(array, readArray);
550+
551+
Array<RAW_ARRAY, ARRAY_TRAITS, ArrayType::IMPLICIT> arrayCopy(array);
552+
ASSERT_EQ(array, arrayCopy);
536553
}
537554
}
538555

@@ -568,6 +585,9 @@ class ArrayTest : public ::testing::Test
568585
Array<RAW_ARRAY, ARRAY_TRAITS, ArrayType::NORMAL> readArray{arrayTraits};
569586
readArray.readPacked(reader, rawArray.size(), elementFactory);
570587
ASSERT_EQ(array, readArray);
588+
589+
Array<RAW_ARRAY, ARRAY_TRAITS, ArrayType::NORMAL> arrayCopy(array);
590+
ASSERT_EQ(array, arrayCopy);
571591
}
572592
}
573593

@@ -596,6 +616,9 @@ class ArrayTest : public ::testing::Test
596616
Array<RAW_ARRAY, ARRAY_TRAITS, ArrayType::AUTO> readArray{arrayTraits};
597617
readArray.readPacked(reader, elementFactory);
598618
ASSERT_EQ(array, readArray);
619+
620+
Array<RAW_ARRAY, ARRAY_TRAITS, ArrayType::AUTO> arrayCopy(array);
621+
ASSERT_EQ(array, arrayCopy);
599622
}
600623
}
601624

@@ -626,6 +649,10 @@ class ArrayTest : public ::testing::Test
626649
ArrayTestOffsetChecker, ArrayTestOffsetInitializer> readArray{arrayTraits};
627650
readArray.readPacked(reader, rawArray.size(), elementFactory, ArrayTestOffsetChecker());
628651
ASSERT_EQ(array, readArray);
652+
653+
Array<RAW_ARRAY, ARRAY_TRAITS, ArrayType::ALIGNED,
654+
ArrayTestOffsetChecker, ArrayTestOffsetInitializer> arrayCopy(array);
655+
ASSERT_EQ(array, arrayCopy);
629656
}
630657
}
631658

@@ -656,6 +683,10 @@ class ArrayTest : public ::testing::Test
656683
ArrayTestOffsetChecker, ArrayTestOffsetInitializer> readArray{arrayTraits};
657684
readArray.readPacked(reader, elementFactory, ArrayTestOffsetChecker());
658685
ASSERT_EQ(array, readArray);
686+
687+
Array<RAW_ARRAY, ARRAY_TRAITS, ArrayType::ALIGNED_AUTO,
688+
ArrayTestOffsetChecker, ArrayTestOffsetInitializer> arrayCopy(array);
689+
ASSERT_EQ(array, arrayCopy);
659690
}
660691
}
661692

compiler/extensions/cpp/runtime/test/zserio/DebugStringUtilTest.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,28 @@ TEST(DebugStringUtilTest, toJsonStreamDefault)
437437
// improve coverage
438438
IReflectablePtr reflectable = dummyObject.reflectable();
439439
ASSERT_TRUE(reflectable);
440+
ASSERT_EQ(0, reflectable->bitSizeOf());
441+
uint8_t buffer[1];
442+
BitStreamWriter writer(buffer, 1);
443+
reflectable->write(writer);
440444
ASSERT_EQ("test"_sv, reflectable->getField("text")->getStringView());
445+
ASSERT_THROW(reflectable->getField("wrong"), CppRuntimeException);
446+
const AnyHolder<> value(0);
447+
ASSERT_THROW(reflectable->setField("wrong", value), CppRuntimeException);
448+
IReflectableConstPtr reflectableConst = dummyObject.reflectable();
449+
ASSERT_EQ("test",
450+
reflectableConst->getAnyValue().get<std::reference_wrapper<const DummyObject<>>>().get().getText());
451+
452+
// improve coverage
453+
const DummyObject<> dummyObjectConst;
454+
reflectableConst = dummyObjectConst.reflectable();
455+
ASSERT_TRUE(reflectableConst);
456+
ASSERT_EQ(0, reflectableConst->bitSizeOf());
457+
reflectableConst->write(writer);
458+
ASSERT_EQ("test"_sv, reflectableConst->getField("text")->getStringView());
459+
ASSERT_THROW(reflectableConst->getField("wrong"), CppRuntimeException);
460+
ASSERT_EQ("test",
461+
reflectableConst->getAnyValue().get<std::reference_wrapper<const DummyObject<>>>().get().getText());
441462
}
442463

443464
TEST(DebugStringUtilTest, toJsonStreamDefaultWithAlloc)
@@ -614,6 +635,7 @@ TEST(DebugStringUtilTest, toJsonFileDefault)
614635
const DummyObject<> dummyObject;
615636
const std::string fileName = "DebugStringUtilTest_toJsonFileDefault.json";
616637
toJsonFile(dummyObject, fileName);
638+
ASSERT_THROW(toJsonFile(dummyObject, ""), CppRuntimeException);
617639

618640
std::ifstream is(fileName.c_str());
619641
std::stringstream ss;
@@ -782,6 +804,21 @@ TEST(DebugStringUtilTest, fromJsonStreamParameterizedTypeInfo)
782804

783805
ASSERT_EQ(10, reflectable->getParameter("param")->getInt32());
784806
ASSERT_EQ("something"_sv, reflectable->getField("text")->getStringView());
807+
808+
// improve coverage
809+
ASSERT_EQ(0, reflectable->bitSizeOf());
810+
uint8_t buffer[1];
811+
BitStreamWriter writer(buffer, 1);
812+
reflectable->write(writer);
813+
ASSERT_THROW(reflectable->getField("wrong"), CppRuntimeException);
814+
const AnyHolder<> value(0);
815+
ASSERT_THROW(reflectable->setField("wrong", value), CppRuntimeException);
816+
ASSERT_THROW(reflectable->getParameter("wrong"), CppRuntimeException);
817+
ASSERT_EQ("something",
818+
reflectable->getAnyValue().get<std::reference_wrapper<ParameterizedDummyObject<>>>().
819+
get().getText());
820+
ASSERT_THROW(reflectable->initialize(vector<AnyHolder<>>()), CppRuntimeException);
821+
//!@# TODO, Fabian hack and email and J&T
785822
}
786823

787824
TEST(DebugStringUtilTest, fromJsonStreamTypeInfoWithAlloc)
@@ -963,6 +1000,8 @@ TEST(DebugStringUtilTest, fromJsonFileTypeInfo)
9631000
ASSERT_TRUE(reflectable);
9641001

9651002
ASSERT_EQ("something"_sv, reflectable->getField("text")->getStringView());
1003+
1004+
ASSERT_THROW(fromJsonFile(DummyObject<>::typeInfo(), ""), CppRuntimeException);
9661005
}
9671006

9681007
TEST(DebugStringUtilTest, fromJsonFileParameterizedTypeInfo)

compiler/extensions/cpp/runtime/test/zserio/FileUtilTest.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ TEST(FileUtilTest, writeReadByteBufferBitSize)
2020
{
2121
ASSERT_EQ(buffer[i], readBitBuffer.getBuffer()[i]);
2222
}
23+
24+
const std::string invalidFileName = "";
25+
ASSERT_THROW(writeBufferToFile(&buffer[0], 20, BitsTag(), invalidFileName), CppRuntimeException);
26+
ASSERT_THROW(readBufferFromFile(invalidFileName), CppRuntimeException);
2327
}
2428

2529
TEST(FileUtilTest, writeReadByteBufferByteSize)

compiler/extensions/cpp/runtime/test/zserio/JsonWriterTest.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,10 @@ TEST(JsonWriterTest, enumValue)
236236
int8_t m_value;
237237
};
238238

239+
IReflectableConstPtr reflectableZeroConst = std::make_shared<DummyEnumReflectable>(0);
240+
ASSERT_EQ(0, reflectableZeroConst->getAnyValue().get<int8_t>()); // improve coverage
239241
IReflectablePtr reflectableZero = std::make_shared<DummyEnumReflectable>(0);
242+
ASSERT_EQ(0, reflectableZero->getAnyValue().get<int8_t>()); // improve coverage
240243
IReflectablePtr reflectableOne = std::make_shared<DummyEnumReflectable>(1);
241244
IReflectablePtr reflectableTwo = std::make_shared<DummyEnumReflectable>(2);
242245
IReflectablePtr reflectableMinusOne = std::make_shared<DummyEnumReflectable>(-1);
@@ -317,7 +320,10 @@ TEST(JsonWriterTest, bitmaskValue)
317320
uint8_t m_value;
318321
};
319322

323+
IReflectableConstPtr reflectableZeroConst = std::make_shared<DummyBitmaskReflectable>(0);
324+
ASSERT_EQ(0, reflectableZeroConst->getAnyValue().get<uint8_t>()); // improve coverage
320325
IReflectablePtr reflectableZero = std::make_shared<DummyBitmaskReflectable>(0);
326+
ASSERT_EQ(0, reflectableZero->getAnyValue().get<uint8_t>()); // improve coverage
321327
IReflectablePtr reflectableTwo = std::make_shared<DummyBitmaskReflectable>(2);
322328
IReflectablePtr reflectableThree = std::make_shared<DummyBitmaskReflectable>(3);
323329
IReflectablePtr reflectableFour = std::make_shared<DummyBitmaskReflectable>(4);

0 commit comments

Comments
 (0)