Skip to content

Commit

Permalink
test: Add with_bit_position test
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes-wolf committed Jul 19, 2024
1 parent 636cd8e commit 70001dc
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
25 changes: 25 additions & 0 deletions test/arguments/with_bit_position/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}
)
Empty file.
79 changes: 79 additions & 0 deletions test/arguments/with_bit_position/cpp/WithBitPositionTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <vector>

#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<uint8_t> 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<uint8_t> buffer;
buffer.resize(32);

zserio::BitStreamWriter writer(buffer.data(), buffer.size());

std::vector<Item> items;
items.emplace_back();
items.emplace_back();

ItemHolder tmp;
tmp.setSize(static_cast<decltype(tmp.getSize())>(items.size()));
tmp.setItems(items);
tmp.write(writer);

zserio::BitStreamReader reader(buffer.data(), buffer.size());

return ItemHolder(reader);
}
};

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
13 changes: 13 additions & 0 deletions test/arguments/with_bit_position/zs/with_bit_position.zs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package with_bit_position;

struct Item
{
uint32 value;
optional int8 opt;
};

struct ItemHolder
{
uint16 size;
Item items[size];
};

0 comments on commit 70001dc

Please sign in to comment.