Skip to content

Commit

Permalink
fixup c++ test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mi-La committed Jun 18, 2024
1 parent 8311b07 commit 62379fe
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
3 changes: 2 additions & 1 deletion test/language/builtin_types/ClangTidySuppressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ readability-make-member-function-const:gen/builtin_types/all_builtin_types/AllBu
readability-make-member-function-const:gen/builtin_types/all_builtin_types/ExternalStructure.cpp
readability-make-member-function-const:gen/builtin_types/bitfield_function_length/Container.cpp
readability-make-member-function-const:gen/builtin_types/bitfield_uint64_length/Container.cpp
readability-make-member-function-const:gen/builtin_types/dynamic_bitfield_length_bounds/Container.cpp

readability-simplify-boolean-expr:gen/builtin_types/all_builtin_types/AllBuiltInTypes.cpp
readability-simplify-boolean-expr:gen/builtin_types/all_builtin_types/AllBuiltInTypes.cpp
70 changes: 59 additions & 11 deletions test/language/builtin_types/cpp/DynamicBitFieldLengthBoundsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,38 @@ using BitBuffer = zserio::BasicBitBuffer<zserio::RebindAlloc<allocator_type, uin
class DynamicBitFieldLengthBoundsTest : public ::testing::Test
{
protected:
void writeContainer(const Container container)
{
zserio::BitStreamWriter writer(bitBuffer);
writer.writeBits(container.getUnsignedBitLength(), 4);
if (container.getUnsignedBitLength() == 0)
{
return;
}
writer.writeBits(container.getUnsignedValue(), container.getUnsignedBitLength());
writer.writeBits(container.getUnsignedBigBitLength(), 8);
if (container.getUnsignedBigBitLength() == 0 || container.getUnsignedBigBitLength() > 64)
{
return;
}
writer.writeBits64(container.getUnsignedBigValue(), container.getUnsignedBigBitLength());
writer.writeBits64(container.getSignedBitLength(), 64);
if (container.getSignedBitLength() == 0 || container.getSignedBitLength() > 64)
{
return;
}
writer.writeSignedBits64(
container.getSignedValue(), static_cast<uint8_t>(container.getSignedBitLength()));
}

static constexpr uint8_t UNSIGNED_BIT_LENGTH = 15;
static constexpr uint16_t UNSIGNED_VALUE = (1 << UNSIGNED_BIT_LENGTH) - 1;
static constexpr uint16_t UNSIGNED_VALUE = (1U << UNSIGNED_BIT_LENGTH) - 1;
static constexpr uint16_t UNSIGNED_BIG_BIT_LENGTH = 13;
static constexpr uint64_t UNSIGNED_BIG_VALUE = (1 << UNSIGNED_BIG_BIT_LENGTH) - 1;
static constexpr uint64_t UNSIGNED_BIG_VALUE = (1U << UNSIGNED_BIG_BIT_LENGTH) - 1;
static constexpr uint64_t SIGNED_BIT_LENGTH = 7;
static constexpr int64_t SIGNED_VALUE = -(1 << (SIGNED_BIT_LENGTH - 1));
static constexpr int64_t SIGNED_VALUE = -static_cast<int64_t>(1U << (SIGNED_BIT_LENGTH - 1));

BitBuffer bitBuffer = zserio::BitBuffer(1024);
BitBuffer bitBuffer = BitBuffer(1024);
};

TEST_F(DynamicBitFieldLengthBoundsTest, writeRead)
Expand All @@ -40,21 +64,21 @@ TEST_F(DynamicBitFieldLengthBoundsTest, unsignedBitLengthZero)
0, UNSIGNED_VALUE, UNSIGNED_BIG_BIT_LENGTH, UNSIGNED_BIG_VALUE, SIGNED_BIT_LENGTH, SIGNED_VALUE};

ASSERT_THROW(zserio::serialize(container), zserio::CppRuntimeException);

writeContainer(container);
zserio::BitStreamReader reader(bitBuffer);
ASSERT_THROW(Container readContainer(reader), zserio::CppRuntimeException);
}

TEST_F(DynamicBitFieldLengthBoundsTest, unsignedBitLengthZeroValueZero)
{
Container container = {0, 0, UNSIGNED_BIG_BIT_LENGTH, UNSIGNED_BIG_VALUE, SIGNED_BIT_LENGTH, SIGNED_VALUE};

ASSERT_THROW(zserio::serialize(container), zserio::CppRuntimeException);
}

TEST_F(DynamicBitFieldLengthBoundsTest, unsignedBitLengthOverMax)
{
Container container = {
65, UNSIGNED_VALUE, UNSIGNED_BIG_BIT_LENGTH, UNSIGNED_BIG_VALUE, SIGNED_BIT_LENGTH, SIGNED_VALUE};

ASSERT_THROW(zserio::serialize(container), zserio::CppRuntimeException);
writeContainer(container);
zserio::BitStreamReader reader(bitBuffer);
ASSERT_THROW(Container readContainer(reader), zserio::CppRuntimeException);
}

TEST_F(DynamicBitFieldLengthBoundsTest, unsignedBigBitLengthZero)
Expand All @@ -63,13 +87,21 @@ TEST_F(DynamicBitFieldLengthBoundsTest, unsignedBigBitLengthZero)
UNSIGNED_BIT_LENGTH, UNSIGNED_VALUE, 0, UNSIGNED_BIG_VALUE, SIGNED_BIT_LENGTH, SIGNED_VALUE};

ASSERT_THROW(zserio::serialize(container), zserio::CppRuntimeException);

writeContainer(container);
zserio::BitStreamReader reader(bitBuffer);
ASSERT_THROW(Container readContainer(reader), zserio::CppRuntimeException);
}

TEST_F(DynamicBitFieldLengthBoundsTest, unsignedBigBitLengthZeroValueZero)
{
Container container = {UNSIGNED_BIT_LENGTH, UNSIGNED_VALUE, 0, 0, SIGNED_BIT_LENGTH, SIGNED_VALUE};

ASSERT_THROW(zserio::serialize(container), zserio::CppRuntimeException);

writeContainer(container);
zserio::BitStreamReader reader(bitBuffer);
ASSERT_THROW(Container readContainer(reader), zserio::CppRuntimeException);
}

TEST_F(DynamicBitFieldLengthBoundsTest, unsignedBigBitLengthOverMax)
Expand All @@ -78,6 +110,10 @@ TEST_F(DynamicBitFieldLengthBoundsTest, unsignedBigBitLengthOverMax)
UNSIGNED_BIT_LENGTH, UNSIGNED_VALUE, 65, UNSIGNED_BIG_VALUE, SIGNED_BIT_LENGTH, SIGNED_VALUE};

ASSERT_THROW(zserio::serialize(container), zserio::CppRuntimeException);

writeContainer(container);
zserio::BitStreamReader reader(bitBuffer);
ASSERT_THROW(Container readContainer(reader), zserio::CppRuntimeException);
}

TEST_F(DynamicBitFieldLengthBoundsTest, signedBitLengthZero)
Expand All @@ -86,6 +122,10 @@ TEST_F(DynamicBitFieldLengthBoundsTest, signedBitLengthZero)
UNSIGNED_BIT_LENGTH, UNSIGNED_VALUE, UNSIGNED_BIG_BIT_LENGTH, UNSIGNED_BIG_VALUE, 0, SIGNED_VALUE};

ASSERT_THROW(zserio::serialize(container), zserio::CppRuntimeException);

writeContainer(container);
zserio::BitStreamReader reader(bitBuffer);
ASSERT_THROW(Container readContainer(reader), zserio::CppRuntimeException);
}

TEST_F(DynamicBitFieldLengthBoundsTest, signedBitLengthZeroValueZero)
Expand All @@ -94,6 +134,10 @@ TEST_F(DynamicBitFieldLengthBoundsTest, signedBitLengthZeroValueZero)
UNSIGNED_BIT_LENGTH, UNSIGNED_VALUE, UNSIGNED_BIG_BIT_LENGTH, UNSIGNED_BIG_VALUE, 0, 0};

ASSERT_THROW(zserio::serialize(container), zserio::CppRuntimeException);

writeContainer(container);
zserio::BitStreamReader reader(bitBuffer);
ASSERT_THROW(Container readContainer(reader), zserio::CppRuntimeException);
}

TEST_F(DynamicBitFieldLengthBoundsTest, signedBitLengthOverMax)
Expand All @@ -102,6 +146,10 @@ TEST_F(DynamicBitFieldLengthBoundsTest, signedBitLengthOverMax)
UNSIGNED_BIT_LENGTH, UNSIGNED_VALUE, UNSIGNED_BIG_BIT_LENGTH, UNSIGNED_BIG_VALUE, 65, SIGNED_VALUE};

ASSERT_THROW(zserio::serialize(container), zserio::CppRuntimeException);

writeContainer(container);
zserio::BitStreamReader reader(bitBuffer);
ASSERT_THROW(Container readContainer(reader), zserio::CppRuntimeException);
}

} // namespace dynamic_bitfield_length_bounds
Expand Down

0 comments on commit 62379fe

Please sign in to comment.