Skip to content

Commit

Permalink
Extend C++ runtime tests for Traits.h
Browse files Browse the repository at this point in the history
  • Loading branch information
Mi-La committed Oct 6, 2023
1 parent 294f746 commit 812a208
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 0 deletions.
1 change: 1 addition & 0 deletions compiler/extensions/cpp/runtime/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
155 changes: 155 additions & 0 deletions compiler/extensions/cpp/runtime/test/zserio/TraitsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ class DummyObjectInitializeChildren
void initializeChildren() {}
};

class DummyOwner
{};

class DummyObjectWithOwnerType
{
public:
using OwnerType = DummyOwner;
};

class DummyObjectWithAllocator
{
public:
using allocator_type = std::allocator<uint8_t>;
};

class DummyObjectReflectable
{
public:
Expand All @@ -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:
Expand Down Expand Up @@ -89,6 +128,7 @@ TEST(TraitsTest, isAllocator)
ASSERT_FALSE(is_allocator<uint64_t>::value);
ASSERT_FALSE(is_allocator<std::vector<uint8_t>>::value);
ASSERT_FALSE(is_allocator<AnyHolder<>>::value);
ASSERT_FALSE(is_allocator<DummyObjectWithAllocator>::value);
}

TEST(TraitsTest, isFirstAllocator)
Expand All @@ -106,13 +146,58 @@ TEST(TraitsTest, isFirstAllocator)
assertFalse(is_first_allocator<int, std::allocator<uint8_t>, std::allocator<uint8_t>>::value);
assertFalse(is_first_allocator<int, pmr::PolymorphicAllocator<uint8_t>, std::allocator<uint8_t>>::value);
assertFalse(is_first_allocator<char, pmr::PolymorphicAllocator<uint8_t>>::value);
assertFalse(is_first_allocator<DummyObjectWithAllocator, pmr::PolymorphicAllocator<uint8_t>>::value);
}

TEST(TraitsTest, hasOwnerType)
{
ASSERT_TRUE(has_owner_type<DummyObjectWithOwnerType>::value);
ASSERT_TRUE(has_owner_type<DummyObjectInitializeOffset>::value);
ASSERT_TRUE(has_owner_type<DummyObjectCheckOffset>::value);
ASSERT_TRUE(has_owner_type<DummyObjectInitializeElement>::value);
ASSERT_FALSE(has_owner_type<DummyObjectInitialize>::value);
ASSERT_FALSE(has_owner_type<DummyObjectInitializeChildren>::value);
ASSERT_FALSE(has_owner_type<DummyObjectWithPackingContext>::value);
ASSERT_FALSE(has_owner_type<DummyObjectWithAllocator>::value);
ASSERT_FALSE(has_owner_type<DummyBitmask>::value);
ASSERT_FALSE(has_owner_type<std::string>::value);
ASSERT_FALSE(has_owner_type<std::vector<uint8_t>>::value);
ASSERT_FALSE(has_owner_type<std::vector<DummyObjectWithOwnerType>>::value);
}

TEST(TraitsTest, hasZserioPackingContext)
{
ASSERT_TRUE(has_zserio_packing_context<DummyObjectWithPackingContext>::value);
ASSERT_FALSE(has_zserio_packing_context<DummyObjectInitialize>::value);
ASSERT_FALSE(has_zserio_packing_context<DummyObjectInitializeChildren>::value);
ASSERT_FALSE(has_zserio_packing_context<DummyObjectWithOwnerType>::value);
ASSERT_FALSE(has_zserio_packing_context<DummyObjectWithAllocator>::value);
ASSERT_FALSE(has_zserio_packing_context<DummyBitmask>::value);
ASSERT_FALSE(has_zserio_packing_context<std::string>::value);
ASSERT_FALSE(has_zserio_packing_context<std::vector<uint8_t>>::value);
ASSERT_FALSE(has_zserio_packing_context<std::vector<DummyObjectWithPackingContext>>::value);
}

TEST(TraitsTest, hasAllocator)
{
ASSERT_TRUE(has_allocator<DummyObjectWithAllocator>::value);
ASSERT_TRUE(has_allocator<std::string>::value);
ASSERT_TRUE(has_allocator<std::vector<uint8_t>>::value);
ASSERT_TRUE(has_allocator<std::vector<DummyObjectWithAllocator>>::value);
ASSERT_FALSE(has_allocator<DummyObjectInitialize>::value);
ASSERT_FALSE(has_allocator<DummyObjectInitializeChildren>::value);
ASSERT_FALSE(has_allocator<DummyObjectWithOwnerType>::value);
ASSERT_FALSE(has_allocator<DummyObjectWithPackingContext>::value);
ASSERT_FALSE(has_allocator<DummyBitmask>::value);
}

TEST(TraitsTest, hasInitialize)
{
ASSERT_TRUE(has_initialize<DummyObjectInitialize>::value);
DummyObjectInitialize().initialize();
ASSERT_FALSE(has_initialize<DummyObjectInitializeChildren>::value);
ASSERT_FALSE(has_initialize<DummyObjectWithOwnerType>::value);
ASSERT_FALSE(has_initialize<DummyObjectWithPackingContext>::value);
ASSERT_FALSE(has_initialize<DummyBitmask>::value);
ASSERT_FALSE(has_initialize<std::string>::value);
ASSERT_FALSE(has_initialize<std::vector<uint8_t>>::value);
Expand All @@ -123,6 +208,8 @@ TEST(TraitsTest, hasInitializeChildren)
ASSERT_TRUE(has_initialize_children<DummyObjectInitializeChildren>::value);
DummyObjectInitializeChildren().initializeChildren();
ASSERT_FALSE(has_initialize_children<DummyObjectInitialize>::value);
ASSERT_FALSE(has_initialize_children<DummyObjectWithOwnerType>::value);
ASSERT_FALSE(has_initialize_children<DummyObjectWithPackingContext>::value);
ASSERT_FALSE(has_initialize_children<DummyBitmask>::value);
ASSERT_FALSE(has_initialize_children<std::string>::value);
ASSERT_FALSE(has_initialize_children<std::vector<uint8_t>>::value);
Expand All @@ -133,21 +220,89 @@ TEST(TraitsTest, hasReflectable)
ASSERT_TRUE(has_reflectable<DummyObjectReflectable>::value);
ASSERT_EQ(nullptr, DummyObjectReflectable().reflectable());
ASSERT_FALSE(has_reflectable<DummyObjectInitialize>::value);
ASSERT_FALSE(has_reflectable<DummyObjectWithOwnerType>::value);
ASSERT_FALSE(has_reflectable<DummyObjectWithPackingContext>::value);
ASSERT_FALSE(has_reflectable<DummyObjectWithAllocator>::value);
ASSERT_FALSE(has_reflectable<DummyBitmask>::value);
ASSERT_FALSE(has_reflectable<std::string>::value);
ASSERT_FALSE(has_reflectable<std::vector<uint8_t>>::value);
}

TEST(TraitsTest, hasInitializeOffset)
{
ASSERT_TRUE(has_initialize_offset<DummyObjectInitializeOffset>::value);
DummyOwner owner;
DummyObjectInitializeOffset().initializeOffset(owner, 0, 0);
ASSERT_FALSE(has_initialize_offset<DummyObjectCheckOffset>::value);
ASSERT_FALSE(has_initialize_offset<DummyObjectInitializeElement>::value);
ASSERT_FALSE(has_initialize_offset<DummyObjectInitialize>::value);
ASSERT_FALSE(has_initialize_offset<DummyObjectInitializeChildren>::value);
ASSERT_FALSE(has_initialize_offset<DummyObjectReflectable>::value);
ASSERT_FALSE(has_initialize_offset<DummyObjectWithOwnerType>::value);
ASSERT_FALSE(has_initialize_offset<DummyObjectWithAllocator>::value);
ASSERT_FALSE(has_initialize_offset<DummyObjectWithPackingContext>::value);
}

TEST(TraitsTest, hasCheckOffset)
{
ASSERT_TRUE(has_check_offset<DummyObjectCheckOffset>::value);
DummyObjectCheckOffset().checkOffset(DummyOwner(), 0, 0);
ASSERT_FALSE(has_check_offset<DummyObjectInitializeOffset>::value);
ASSERT_FALSE(has_check_offset<DummyObjectInitializeElement>::value);
ASSERT_FALSE(has_check_offset<DummyObjectInitialize>::value);
ASSERT_FALSE(has_check_offset<DummyObjectInitializeChildren>::value);
ASSERT_FALSE(has_check_offset<DummyObjectReflectable>::value);
ASSERT_FALSE(has_check_offset<DummyObjectWithOwnerType>::value);
ASSERT_FALSE(has_check_offset<DummyObjectWithAllocator>::value);
ASSERT_FALSE(has_check_offset<DummyObjectWithPackingContext>::value);
}

TEST(TraitsTest, hasInitializeElement)
{
ASSERT_TRUE(has_initialize_element<DummyObjectInitializeElement>::value);
DummyOwner owner;
DummyObjectInitializeChildren element;
DummyObjectInitializeElement().initializeElement(owner, element, 0);
ASSERT_FALSE(has_initialize_element<DummyObjectInitializeOffset>::value);
ASSERT_FALSE(has_initialize_element<DummyObjectCheckOffset>::value);
ASSERT_FALSE(has_initialize_element<DummyObjectInitialize>::value);
ASSERT_FALSE(has_initialize_element<DummyObjectInitializeChildren>::value);
ASSERT_FALSE(has_initialize_element<DummyObjectReflectable>::value);
ASSERT_FALSE(has_initialize_element<DummyObjectWithOwnerType>::value);
ASSERT_FALSE(has_initialize_element<DummyObjectWithAllocator>::value);
ASSERT_FALSE(has_initialize_element<DummyObjectWithPackingContext>::value);
}

TEST(TraitsTest, isBitmask)
{
ASSERT_TRUE(is_bitmask<DummyBitmask>::value);
ASSERT_EQ(0, DummyBitmask().getValue());
ASSERT_FALSE(is_bitmask<DummyObjectInitializeChildren>::value);
ASSERT_FALSE(is_bitmask<DummyObjectInitialize>::value);
ASSERT_FALSE(is_bitmask<DummyObjectWithOwnerType>::value);
ASSERT_FALSE(is_bitmask<DummyObjectWithPackingContext>::value);
ASSERT_FALSE(is_bitmask<DummyObjectWithAllocator>::value);
ASSERT_FALSE(is_bitmask<std::string>::value);
ASSERT_FALSE(is_bitmask<std::vector<uint8_t>>::value);
}

TEST(TraitsTest, isSpan)
{
ASSERT_TRUE(is_span<zserio::Span<uint8_t>>::value);
ASSERT_TRUE(is_span<zserio::Span<const uint8_t>>::value);
ASSERT_TRUE(is_span<zserio::Span<char* const>>::value);
ASSERT_TRUE(is_span<zserio::Span<char*>>::value);
assertFalse(is_span<std::array<uint8_t, 2>>::value);
ASSERT_FALSE(is_span<std::vector<uint8_t>>::value);
ASSERT_FALSE(is_span<uint8_t>::value);
ASSERT_FALSE(is_span<std::string>::value);
ASSERT_FALSE(is_span<DummyObjectInitializeChildren>::value);
ASSERT_FALSE(is_span<DummyObjectInitialize>::value);
ASSERT_FALSE(is_span<DummyObjectWithOwnerType>::value);
ASSERT_FALSE(is_span<DummyObjectWithPackingContext>::value);
ASSERT_FALSE(is_span<DummyObjectWithAllocator>::value);
}

TEST(TraitsTest, isFieldConstructorEnabled)
{
assertTrue(has_field_ctor<int, DummyObjectInitialize, std::allocator<uint8_t>>::value);
Expand Down

0 comments on commit 812a208

Please sign in to comment.