diff --git a/test/arguments/with_bit_position/CMakeLists.txt b/test/arguments/with_bit_position/CMakeLists.txt new file mode 100644 index 000000000..45eedb10f --- /dev/null +++ b/test/arguments/with_bit_position/CMakeLists.txt @@ -0,0 +1,25 @@ +add_library(with_bit_position_zs STATIC zs/with_bit_position.zs) +zserio_generate_cpp( + TARGET with_bit_position_zs + SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/zs + GEN_DIR ${CMAKE_CURRENT_BINARY_DIR}/gen + EXTRA_ARGS -withoutSourcesAmalgamation -withTypeInfoCode -withReflectionCode -withBitPosition ${ZSERIO_EXTRA_ARGS} + GENERATED_SOURCES_VAR GENERATED_SOURCES + OUTPUT_VAR ZSERIO_LOG + ERROR_VAR ZSERIO_LOG +) +target_link_libraries(with_bit_position_zs PUBLIC ZserioCppRuntime) +target_include_directories(with_bit_position_zs SYSTEM PRIVATE ${SQLITE_INCDIR}) +if (ZSERIO_LOG) + file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/zserio_log.txt "${ZSERIO_LOG}") + check_zserio_warnings("${ZSERIO_LOG}" 0) +endif () + +add_custom_test(with_bit_position + DEPENDS + with_bit_position_zs + SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/cpp/WithBitPositionTest.cpp + GENERATED_SOURCES + ${GENERATED_SOURCES} +) diff --git a/test/arguments/with_bit_position/ClangTidySuppressions.txt b/test/arguments/with_bit_position/ClangTidySuppressions.txt new file mode 100644 index 000000000..e69de29bb diff --git a/test/arguments/with_bit_position/cpp/WithBitPositionTest.cpp b/test/arguments/with_bit_position/cpp/WithBitPositionTest.cpp new file mode 100644 index 000000000..cbbad0699 --- /dev/null +++ b/test/arguments/with_bit_position/cpp/WithBitPositionTest.cpp @@ -0,0 +1,80 @@ +#include + +#include "gtest/gtest.h" +#include "test_utils/Assertions.h" +#include "with_bit_position/Item.h" +#include "with_bit_position/ItemHolder.h" +#include "zserio/BitStreamReader.h" +#include "zserio/BitStreamWriter.h" + +namespace with_bit_position +{ + +class WithBitPosition : public ::testing::Test +{ +protected: + Item readItem() + { + std::vector buffer; + buffer.resize(16); + + zserio::BitStreamWriter writer(buffer.data(), buffer.size()); + + Item tmp; + tmp.write(writer); + + zserio::BitStreamReader reader(buffer.data(), buffer.size()); + + return Item(reader); + } + + ItemHolder readItemHolder() + { + std::vector buffer; + buffer.resize(32); + + zserio::BitStreamWriter writer(buffer.data(), buffer.size()); + + std::vector items; + items.emplace_back(); + items.emplace_back(); + + ItemHolder tmp; + tmp.setSize(static_cast(items.size())); + tmp.setItems(items); + tmp.write(writer); + + zserio::BitStreamReader reader(buffer.data(), buffer.size()); + + return ItemHolder(reader); + } +}; + +static const char* const BLOB_NAME = "arguments/with_bit_position/with_bit_position.blob"; +static const char* const PATH = "arguments/with_bit_position/gen/with_bit_position/"; + +TEST_F(WithBitPosition, checkItemTypeMethods) +{ + ASSERT_METHOD_PRESENT(PATH, "Item", "size_t bitPosition(", "size_t bitPosition("); + ASSERT_METHOD_PRESENT(PATH, "ItemHolder", "size_t bitPosition(", "size_t bitPosition("); +} + +TEST_F(WithBitPosition, readRootPosition) +{ + auto item = readItem(); + + ASSERT_EQ(item.bitPosition(), 0); +} + +TEST_F(WithBitPosition, readChildPosition) +{ + auto holder = readItemHolder(); + + ASSERT_EQ(holder.bitPosition(), 0); + + size_t offset = 16; // The size field + ASSERT_EQ(holder.getItems().at(0).bitPosition(), offset + 0); + ASSERT_EQ(holder.getItems().at(1).bitPosition(), offset + holder.getItems().at(0).bitSizeOf()); +} + +} // namespace with_bit_position diff --git a/test/arguments/with_bit_position/zs/with_bit_position.zs b/test/arguments/with_bit_position/zs/with_bit_position.zs new file mode 100644 index 000000000..c18202710 --- /dev/null +++ b/test/arguments/with_bit_position/zs/with_bit_position.zs @@ -0,0 +1,13 @@ +package with_bit_position; + +struct Item +{ + uint32 value; + optional int8 opt; +}; + +struct ItemHolder +{ + uint16 size; + Item items[size]; +};