From 812a20866b078b9354f477e3e7924f0f56b35535 Mon Sep 17 00:00:00 2001 From: Milan Kriz Date: Wed, 4 Oct 2023 16:45:31 +0200 Subject: [PATCH] Extend C++ runtime tests for Traits.h --- .../extensions/cpp/runtime/src/CMakeLists.txt | 1 + .../cpp/runtime/test/zserio/TraitsTest.cpp | 155 ++++++++++++++++++ 2 files changed, 156 insertions(+) diff --git a/compiler/extensions/cpp/runtime/src/CMakeLists.txt b/compiler/extensions/cpp/runtime/src/CMakeLists.txt index 9d26ebf00..e26f1441a 100644 --- a/compiler/extensions/cpp/runtime/src/CMakeLists.txt +++ b/compiler/extensions/cpp/runtime/src/CMakeLists.txt @@ -93,6 +93,7 @@ set(ZSERIO_CPP_RUNTIME_LIB_SRCS zserio/SqliteFinalizer.h zserio/StringConvertUtil.h zserio/StringView.h + zserio/Traits.h zserio/TypeInfo.h zserio/TypeInfoUtil.cpp zserio/TypeInfoUtil.h diff --git a/compiler/extensions/cpp/runtime/test/zserio/TraitsTest.cpp b/compiler/extensions/cpp/runtime/test/zserio/TraitsTest.cpp index 9af3e3341..15e613fd9 100644 --- a/compiler/extensions/cpp/runtime/test/zserio/TraitsTest.cpp +++ b/compiler/extensions/cpp/runtime/test/zserio/TraitsTest.cpp @@ -40,6 +40,21 @@ class DummyObjectInitializeChildren void initializeChildren() {} }; +class DummyOwner +{}; + +class DummyObjectWithOwnerType +{ +public: + using OwnerType = DummyOwner; +}; + +class DummyObjectWithAllocator +{ +public: + using allocator_type = std::allocator; +}; + class DummyObjectReflectable { public: @@ -56,6 +71,30 @@ class DummyObjectWithPackingContext {}; }; +class DummyObjectInitializeOffset +{ +public: + using OwnerType = DummyOwner; + + void initializeOffset(OwnerType&, size_t, size_t) {} +}; + +class DummyObjectCheckOffset +{ +public: + using OwnerType = DummyOwner; + + void checkOffset(const OwnerType&, size_t, size_t) {} +}; + +class DummyObjectInitializeElement +{ +public: + using OwnerType = DummyOwner; + + void initializeElement(OwnerType&, DummyObjectInitializeChildren&, size_t) {} +}; + class DummyBitmask { public: @@ -89,6 +128,7 @@ TEST(TraitsTest, isAllocator) ASSERT_FALSE(is_allocator::value); ASSERT_FALSE(is_allocator>::value); ASSERT_FALSE(is_allocator>::value); + ASSERT_FALSE(is_allocator::value); } TEST(TraitsTest, isFirstAllocator) @@ -106,6 +146,49 @@ TEST(TraitsTest, isFirstAllocator) assertFalse(is_first_allocator, std::allocator>::value); assertFalse(is_first_allocator, std::allocator>::value); assertFalse(is_first_allocator>::value); + assertFalse(is_first_allocator>::value); +} + +TEST(TraitsTest, hasOwnerType) +{ + ASSERT_TRUE(has_owner_type::value); + ASSERT_TRUE(has_owner_type::value); + ASSERT_TRUE(has_owner_type::value); + ASSERT_TRUE(has_owner_type::value); + ASSERT_FALSE(has_owner_type::value); + ASSERT_FALSE(has_owner_type::value); + ASSERT_FALSE(has_owner_type::value); + ASSERT_FALSE(has_owner_type::value); + ASSERT_FALSE(has_owner_type::value); + ASSERT_FALSE(has_owner_type::value); + ASSERT_FALSE(has_owner_type>::value); + ASSERT_FALSE(has_owner_type>::value); +} + +TEST(TraitsTest, hasZserioPackingContext) +{ + ASSERT_TRUE(has_zserio_packing_context::value); + ASSERT_FALSE(has_zserio_packing_context::value); + ASSERT_FALSE(has_zserio_packing_context::value); + ASSERT_FALSE(has_zserio_packing_context::value); + ASSERT_FALSE(has_zserio_packing_context::value); + ASSERT_FALSE(has_zserio_packing_context::value); + ASSERT_FALSE(has_zserio_packing_context::value); + ASSERT_FALSE(has_zserio_packing_context>::value); + ASSERT_FALSE(has_zserio_packing_context>::value); +} + +TEST(TraitsTest, hasAllocator) +{ + ASSERT_TRUE(has_allocator::value); + ASSERT_TRUE(has_allocator::value); + ASSERT_TRUE(has_allocator>::value); + ASSERT_TRUE(has_allocator>::value); + ASSERT_FALSE(has_allocator::value); + ASSERT_FALSE(has_allocator::value); + ASSERT_FALSE(has_allocator::value); + ASSERT_FALSE(has_allocator::value); + ASSERT_FALSE(has_allocator::value); } TEST(TraitsTest, hasInitialize) @@ -113,6 +196,8 @@ TEST(TraitsTest, hasInitialize) ASSERT_TRUE(has_initialize::value); DummyObjectInitialize().initialize(); ASSERT_FALSE(has_initialize::value); + ASSERT_FALSE(has_initialize::value); + ASSERT_FALSE(has_initialize::value); ASSERT_FALSE(has_initialize::value); ASSERT_FALSE(has_initialize::value); ASSERT_FALSE(has_initialize>::value); @@ -123,6 +208,8 @@ TEST(TraitsTest, hasInitializeChildren) ASSERT_TRUE(has_initialize_children::value); DummyObjectInitializeChildren().initializeChildren(); ASSERT_FALSE(has_initialize_children::value); + ASSERT_FALSE(has_initialize_children::value); + ASSERT_FALSE(has_initialize_children::value); ASSERT_FALSE(has_initialize_children::value); ASSERT_FALSE(has_initialize_children::value); ASSERT_FALSE(has_initialize_children>::value); @@ -133,21 +220,89 @@ TEST(TraitsTest, hasReflectable) ASSERT_TRUE(has_reflectable::value); ASSERT_EQ(nullptr, DummyObjectReflectable().reflectable()); ASSERT_FALSE(has_reflectable::value); + ASSERT_FALSE(has_reflectable::value); + ASSERT_FALSE(has_reflectable::value); + ASSERT_FALSE(has_reflectable::value); ASSERT_FALSE(has_reflectable::value); ASSERT_FALSE(has_reflectable::value); ASSERT_FALSE(has_reflectable>::value); } +TEST(TraitsTest, hasInitializeOffset) +{ + ASSERT_TRUE(has_initialize_offset::value); + DummyOwner owner; + DummyObjectInitializeOffset().initializeOffset(owner, 0, 0); + ASSERT_FALSE(has_initialize_offset::value); + ASSERT_FALSE(has_initialize_offset::value); + ASSERT_FALSE(has_initialize_offset::value); + ASSERT_FALSE(has_initialize_offset::value); + ASSERT_FALSE(has_initialize_offset::value); + ASSERT_FALSE(has_initialize_offset::value); + ASSERT_FALSE(has_initialize_offset::value); + ASSERT_FALSE(has_initialize_offset::value); +} + +TEST(TraitsTest, hasCheckOffset) +{ + ASSERT_TRUE(has_check_offset::value); + DummyObjectCheckOffset().checkOffset(DummyOwner(), 0, 0); + ASSERT_FALSE(has_check_offset::value); + ASSERT_FALSE(has_check_offset::value); + ASSERT_FALSE(has_check_offset::value); + ASSERT_FALSE(has_check_offset::value); + ASSERT_FALSE(has_check_offset::value); + ASSERT_FALSE(has_check_offset::value); + ASSERT_FALSE(has_check_offset::value); + ASSERT_FALSE(has_check_offset::value); +} + +TEST(TraitsTest, hasInitializeElement) +{ + ASSERT_TRUE(has_initialize_element::value); + DummyOwner owner; + DummyObjectInitializeChildren element; + DummyObjectInitializeElement().initializeElement(owner, element, 0); + ASSERT_FALSE(has_initialize_element::value); + ASSERT_FALSE(has_initialize_element::value); + ASSERT_FALSE(has_initialize_element::value); + ASSERT_FALSE(has_initialize_element::value); + ASSERT_FALSE(has_initialize_element::value); + ASSERT_FALSE(has_initialize_element::value); + ASSERT_FALSE(has_initialize_element::value); + ASSERT_FALSE(has_initialize_element::value); +} + TEST(TraitsTest, isBitmask) { ASSERT_TRUE(is_bitmask::value); ASSERT_EQ(0, DummyBitmask().getValue()); ASSERT_FALSE(is_bitmask::value); ASSERT_FALSE(is_bitmask::value); + ASSERT_FALSE(is_bitmask::value); + ASSERT_FALSE(is_bitmask::value); + ASSERT_FALSE(is_bitmask::value); ASSERT_FALSE(is_bitmask::value); ASSERT_FALSE(is_bitmask>::value); } +TEST(TraitsTest, isSpan) +{ + ASSERT_TRUE(is_span>::value); + ASSERT_TRUE(is_span>::value); + ASSERT_TRUE(is_span>::value); + ASSERT_TRUE(is_span>::value); + assertFalse(is_span>::value); + ASSERT_FALSE(is_span>::value); + ASSERT_FALSE(is_span::value); + ASSERT_FALSE(is_span::value); + ASSERT_FALSE(is_span::value); + ASSERT_FALSE(is_span::value); + ASSERT_FALSE(is_span::value); + ASSERT_FALSE(is_span::value); + ASSERT_FALSE(is_span::value); +} + TEST(TraitsTest, isFieldConstructorEnabled) { assertTrue(has_field_ctor>::value);