Skip to content

Commit

Permalink
[#675] Add new type info test with strings in expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mi-La committed Dec 4, 2024
1 parent df12750 commit 92ef5a1
Show file tree
Hide file tree
Showing 7 changed files with 325 additions and 1 deletion.
2 changes: 1 addition & 1 deletion test/data
20 changes: 20 additions & 0 deletions test/extensions/arguments/with_type_info_code/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,34 @@ if (ZSERIO_LOG)
check_zserio_warnings("${ZSERIO_LOG}" 0)
endif ()

add_library(type_info_string_expressions_zs STATIC ${TEST_ZS_ROOT}/type_info_string_expressions.zs)
zserio_generate_cpp(
TARGET type_info_string_expressions_zs
SRC_DIR ${TEST_ZS_ROOT}
GEN_DIR ${CMAKE_CURRENT_BINARY_DIR}/gen_string_expressions
EXTRA_ARGS -withoutSourcesAmalgamation -withTypeInfoCode -withReflectionCode ${ZSERIO_EXTRA_ARGS}
GENERATED_SOURCES_VAR GENERATED_SOURCES_STRING_EXPRESSIONS
OUTPUT_VAR ZSERIO_LOG_STRING_EXPRESSIONS
ERROR_VAR ZSERIO_LOG_STRING_EXPRESSIONS
)
target_link_libraries(type_info_string_expressions_zs PUBLIC ZserioCppRuntime)
target_include_directories(type_info_string_expressions_zs SYSTEM PRIVATE ${SQLITE_INCDIR})
if (ZSERIO_LOG_STRING_EXPRESSIONS)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/zserio_log_string_expressions.txt "${ZSERIO_LOG_STRING_EXPRESSIONS}")
check_zserio_warnings("${ZSERIO_LOG_STRING_EXPRESSIONS}" 0)
endif ()

add_custom_test(with_type_info_code
DEPENDS
type_info_string_expressions_zs
with_type_info_code_zs
SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/cpp/DebugStringTest.cpp
${CMAKE_CURRENT_SOURCE_DIR}/cpp/TypeInfoStringExpressionsTest.cpp
${CMAKE_CURRENT_SOURCE_DIR}/cpp/WithTypeInfoCodeCreator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/cpp/WithTypeInfoCodeCreator.h
${CMAKE_CURRENT_SOURCE_DIR}/cpp/WithTypeInfoCodeTest.cpp
GENERATED_SOURCES
${GENERATED_SOURCES}
${GENERATED_SOURCES_STRING_EXPRESSIONS}
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
bugprone-exception-escape:gen_string_expressions/type_info_string_expressions/TestChoice.cpp
bugprone-exception-escape:gen_string_expressions/type_info_string_expressions/TestChoice.h
bugprone-exception-escape:gen/with_type_info_code/RecursiveChoice.cpp
bugprone-exception-escape:gen/with_type_info_code/RecursiveChoice.h

bugprone-forwarding-reference-overload:gen_string_expressions/type_info_string_expressions/TypeInfoStringExpressions.h
bugprone-forwarding-reference-overload:gen/with_type_info_code/ParameterizedStruct.h
bugprone-forwarding-reference-overload:gen/with_type_info_code/TemplatedParameterizedStruct_TS32.h

Expand All @@ -13,6 +16,8 @@ bugprone-unhandled-self-assignment:gen/with_type_info_code/TemplatedParameterize
cert-err58-cpp:cpp/DebugStringTest.cpp
cert-err58-cpp:cpp/WithTypeInfoCodeTest.cpp

cppcoreguidelines-pro-type-member-init:gen_string_expressions/type_info_string_expressions/TestChoice.cpp
cppcoreguidelines-pro-type-member-init:gen_string_expressions/type_info_string_expressions/TestStruct.cpp
cppcoreguidelines-pro-type-member-init:gen/with_type_info_code/Fts4Table.cpp
cppcoreguidelines-pro-type-member-init:gen/with_type_info_code/ParameterizedStruct.cpp
cppcoreguidelines-pro-type-member-init:gen/with_type_info_code/RecursiveChoice.cpp
Expand All @@ -32,18 +37,24 @@ cppcoreguidelines-pro-type-reinterpret-cast:gen/with_type_info_code/WithoutRowId
fuchsia-multiple-inheritance:gen/with_type_info_code/SqlDatabase.h

google-build-using-namespace:cpp/DebugStringTest.cpp
google-build-using-namespace:cpp/TypeInfoStringExpressionsTest.cpp
google-build-using-namespace:cpp/WithTypeInfoCodeCreator.cpp
google-build-using-namespace:cpp/WithTypeInfoCodeTest.cpp

google-explicit-constructor:gen_string_expressions/type_info_string_expressions/TestBitmask.h
google-explicit-constructor:gen/with_type_info_code/TestBitmask.h

hicpp-signed-bitwise:gen_string_expressions/type_info_string_expressions/TestBitmask.h
hicpp-signed-bitwise:gen/with_type_info_code/TestBitmask.h

misc-no-recursion # reports also tested_release and some system headers

performance-move-const-arg:gen_string_expressions/type_info_string_expressions/TestStruct.cpp
performance-move-const-arg:gen/with_type_info_code/ComplexStruct.cpp
performance-move-const-arg:gen/with_type_info_code/WithTypeInfoCode.cpp

readability-make-member-function-const:gen_string_expressions/type_info_string_expressions/TestChoice.cpp
readability-make-member-function-const:gen_string_expressions/type_info_string_expressions/TestStruct.cpp
readability-make-member-function-const:gen/with_type_info_code/ComplexStruct.cpp
readability-make-member-function-const:gen/with_type_info_code/SimpleChoice.cpp
readability-make-member-function-const:gen/with_type_info_code/SimpleStruct.cpp
Expand Down
3 changes: 3 additions & 0 deletions test/extensions/arguments/with_type_info_code/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<arg name="withTypeInfoCode"/>
<arg name="allowImplicitArrays"/>
</testGen>
<testGen testName="with_type_info_code" zsFile="type_info_string_expressions.zs">
<arg name="withTypeInfoCode"/>
</testGen>
</target>

<target name="gen.checkWarnings" depends="gen">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "gtest/gtest.h"
#include "type_info_string_expressions/TypeInfoStringExpressions.h"
#include "zserio/SerializeUtil.h"

using namespace zserio::literals;

namespace type_info_string_expressions
{

class TypeInfoStringExpressionsTest : public ::testing::Test
{
protected:
static void fillData(TypeInfoStringExpressions& typeInfoStringExpressions)
{
TestChoice& choiceField = typeInfoStringExpressions.getChoiceField();
choiceField.setStructField(TestStruct());
TestStruct& structField = choiceField.getStructField();
structField.getArrayField().resize(LENGTHOF_LITERAL * 2);
structField.setDynBitField(0);
structField.setBitField(0);
structField.setEnumField(TestEnum::ONE);
structField.setBitmaskField(TestBitmask::Values::READ);
}

private:
static constexpr size_t LENGTHOF_LITERAL = 7;
};

constexpr size_t TypeInfoStringExpressionsTest::LENGTHOF_LITERAL;

TEST_F(TypeInfoStringExpressionsTest, writeRead)
{
TypeInfoStringExpressions typeInfoStringExpressions;
fillData(typeInfoStringExpressions);

auto bitBuffer = zserio::serialize(typeInfoStringExpressions);
TypeInfoStringExpressions readTypeInfoStringExpressions =
zserio::deserialize<TypeInfoStringExpressions>(bitBuffer);

ASSERT_EQ(typeInfoStringExpressions, readTypeInfoStringExpressions);
}

TEST_F(TypeInfoStringExpressionsTest, typeInfo)
{
const zserio::ITypeInfo& typeInfo = TypeInfoStringExpressions::typeInfo();

// choiceField
const auto& choiceFieldInfo = typeInfo.getFields()[0];
ASSERT_EQ("::zserio::makeStringView(\"literal\").size()"_sv, choiceFieldInfo.typeArguments[0]);

// TestChoice
const auto& choiceTypeInfo = choiceFieldInfo.typeInfo;
ASSERT_EQ("getSelector() + ::zserio::makeStringView(\"literal\").size()"_sv, choiceTypeInfo.getSelector());
ASSERT_EQ("0 + ::zserio::makeStringView(\"literal\").size()"_sv,
choiceTypeInfo.getCases()[0].caseExpressions[0]);

// structField
const auto& structFieldInfo = choiceTypeInfo.getFields()[1];
ASSERT_EQ("::zserio::makeStringView(\"literal\").size()"_sv, structFieldInfo.typeArguments[0]);

// TestStruct
const auto& structTypeInfo = structFieldInfo.typeInfo;

// arrayField
const auto& arrayFieldInfo = structTypeInfo.getFields()[0];
ASSERT_EQ("getParam() + ::zserio::makeStringView(\"literal\").size()"_sv, arrayFieldInfo.arrayLength);
ASSERT_EQ("getArrayField().size() > ::zserio::makeStringView(\"literal\").size()"_sv,
arrayFieldInfo.constraint);

// dynBitField
const auto& dynBitFieldInfo = structTypeInfo.getFields()[1];
ASSERT_EQ("::zserio::makeStringView(\"literal\").size()"_sv, dynBitFieldInfo.typeArguments[0]);
ASSERT_EQ("getParam() + ::zserio::makeStringView(\"literal\").size() != 0"_sv,
dynBitFieldInfo.optionalCondition);

// bitField
const auto& bitFieldInfo = structTypeInfo.getFields()[2];
ASSERT_EQ("::zserio::makeStringView(\"literal\").size() + 1"_sv, bitFieldInfo.alignment);

// enumField
const auto& enumFieldInfo = structTypeInfo.getFields()[3];

// TestEnum
const auto& enumTypeInfo = enumFieldInfo.typeInfo;
ASSERT_EQ("::zserio::makeStringView(\"literal\").size()"_sv, enumTypeInfo.getUnderlyingTypeArguments()[0]);

// bitmaskField
const auto& bitmaskFieldInfo = structTypeInfo.getFields()[4];

// TestBitmask
const auto& bitmaskTypeInfo = bitmaskFieldInfo.typeInfo;
ASSERT_EQ(
"::zserio::makeStringView(\"literal\").size()"_sv, bitmaskTypeInfo.getUnderlyingTypeArguments()[0]);
}

} // namespace type_info_string_expressions
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package type_info_string_expressions;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

import zserio.runtime.io.BitBuffer;
import zserio.runtime.io.SerializeUtil;
import zserio.runtime.typeinfo.CaseInfo;
import zserio.runtime.typeinfo.FieldInfo;
import zserio.runtime.typeinfo.TypeInfo;

public class TypeInfoStringExpressionsTest
{
@Test
public void typeInfo()
{
final TypeInfoStringExpressions typeInfoStringExpressions = createData();
final TypeInfo typeInfo = TypeInfoStringExpressions.typeInfo();

// choiceField
final TestChoice choiceField = typeInfoStringExpressions.getChoiceField();
final FieldInfo choiceFieldInfo = typeInfo.getFields().get(0);
assertEquals((int)LENGTHOF_LITERAL,
choiceFieldInfo.getTypeArguments().get(0).apply(typeInfoStringExpressions, 0));

// ChoiceType
final TypeInfo choiceTypeInfo = choiceFieldInfo.getTypeInfo();
final String choiceSelector = choiceTypeInfo.getSelector();
assertEquals("getSelector() + zserio.runtime.BuiltInOperators.lengthOf(\"literal\")", choiceSelector);

final CaseInfo caseInfo0 = choiceTypeInfo.getCases().get(0);
assertEquals((int)LENGTHOF_LITERAL, caseInfo0.getCaseExpressions().get(0).get());

// structField
final TestStruct structField = choiceField.getStructField();
final FieldInfo structFieldInfo = choiceTypeInfo.getFields().get(1);
assertEquals((int)LENGTHOF_LITERAL, structFieldInfo.getTypeArguments().get(0).apply(choiceField, 0));

// TestStruct
final TypeInfo structTypeInfo = structFieldInfo.getTypeInfo();

// arrayField
final FieldInfo arrayFieldInfo = structTypeInfo.getFields().get(0);
assertEquals(2 * LENGTHOF_LITERAL, arrayFieldInfo.getArrayLength().applyAsInt(structField));
assertTrue(arrayFieldInfo.getConstraint().test(structField));

// dynBitField
final FieldInfo dynBitFieldInfo = structTypeInfo.getFields().get(1);
assertEquals((int)LENGTHOF_LITERAL, dynBitFieldInfo.getTypeArguments().get(0).apply(structField, 0));
assertTrue(dynBitFieldInfo.getOptionalCondition().test(structField));

// bitField
final FieldInfo bitFieldInfo = structTypeInfo.getFields().get(2);
assertEquals((int)LENGTHOF_LITERAL + 1, bitFieldInfo.getAlignment().getAsInt());

// enumField
final FieldInfo enumFieldInfo = structTypeInfo.getFields().get(3);

// TestEnum
final TypeInfo enumTypeInfo = enumFieldInfo.getTypeInfo();
assertEquals((int)LENGTHOF_LITERAL, enumTypeInfo.getUnderlyingTypeArguments().get(0).get());

// bitmaskField
final FieldInfo bitmaskFieldInfo = structTypeInfo.getFields().get(4);

// TestBitmask
final TypeInfo bitmaskTypeInfo = bitmaskFieldInfo.getTypeInfo();
assertEquals((int)LENGTHOF_LITERAL, bitmaskTypeInfo.getUnderlyingTypeArguments().get(0).get());
}

@Test
public void writeRead()
{
final TypeInfoStringExpressions typeInfoStringExpressions = createData();
final BitBuffer bitBuffer = SerializeUtil.serialize(typeInfoStringExpressions);
final TypeInfoStringExpressions readTypeInfoStringExpressions =
SerializeUtil.deserialize(TypeInfoStringExpressions.class, bitBuffer);
assertEquals(typeInfoStringExpressions, readTypeInfoStringExpressions);
}

private static TypeInfoStringExpressions createData()
{
final TestChoice testChoice = new TestChoice(LENGTHOF_LITERAL);
testChoice.setStructField(new TestStruct(LENGTHOF_LITERAL,
new long[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, (byte)0, (byte)0, TestEnum.ONE,
TestBitmask.Values.READ));

return new TypeInfoStringExpressions(testChoice);
}

private static final long LENGTHOF_LITERAL = 7;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import unittest
import zserio

from zserio.typeinfo import MemberAttribute, TypeAttribute

from testutils import getZserioApi


class TypeInfoStringExpressionsTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.api = getZserioApi(__file__, "type_info_string_expressions.zs", extraArgs=["-withTypeInfoCode"])

def testTypeInfo(self):
typeInfoStringExpressions = self._createData()
typeInfo = self.api.TypeInfoStringExpressions.type_info()

# choiceField
choiceField = typeInfoStringExpressions.choice_field
choiceFieldInfo = typeInfo.attributes[TypeAttribute.FIELDS][0]
self.assertEqual(
self.LENGTHOF_LITERAL,
choiceFieldInfo.attributes[MemberAttribute.TYPE_ARGUMENTS][0](typeInfoStringExpressions, 0),
)

# TestChoice
choiceTypeInfo = choiceFieldInfo.type_info
choiceSelector = choiceTypeInfo.attributes[TypeAttribute.SELECTOR]
self.assertEqual(2 * self.LENGTHOF_LITERAL, choiceSelector(choiceField))

caseInfo0 = choiceTypeInfo.attributes[TypeAttribute.CASES][0]
self.assertEqual(self.LENGTHOF_LITERAL, caseInfo0.case_expressions[0]())

# structField
structField = choiceField.struct_field
structFieldInfo = choiceTypeInfo.attributes[TypeAttribute.FIELDS][1]
self.assertEqual(
self.LENGTHOF_LITERAL, structFieldInfo.attributes[MemberAttribute.TYPE_ARGUMENTS][0](choiceField, 0)
)

# TestStruct
structTypeInfo = structFieldInfo.type_info

# arrayField
arrayFieldInfo = structTypeInfo.attributes[TypeAttribute.FIELDS][0]
self.assertEqual(
2 * self.LENGTHOF_LITERAL, arrayFieldInfo.attributes[MemberAttribute.ARRAY_LENGTH](structField)
)
self.assertTrue(arrayFieldInfo.attributes[MemberAttribute.CONSTRAINT](structField))

# dynBitField
dynBitFieldInfo = structTypeInfo.attributes[TypeAttribute.FIELDS][1]
self.assertEqual(
self.LENGTHOF_LITERAL, dynBitFieldInfo.attributes[MemberAttribute.TYPE_ARGUMENTS][0](structField, 0)
)
self.assertTrue(dynBitFieldInfo.attributes[MemberAttribute.OPTIONAL](structField))

# bitField
bitFieldInfo = structTypeInfo.attributes[TypeAttribute.FIELDS][2]
self.assertEqual(self.LENGTHOF_LITERAL + 1, bitFieldInfo.attributes[MemberAttribute.ALIGN]())

# enumField
enumFieldInfo = structTypeInfo.attributes[TypeAttribute.FIELDS][3]

# TestEnum
enumTypeInfo = enumFieldInfo.type_info
self.assertEqual(
self.LENGTHOF_LITERAL, enumTypeInfo.attributes[TypeAttribute.UNDERLYING_TYPE_ARGUMENTS][0]()
)

# bitmaskField
bitmaskFieldInfo = structTypeInfo.attributes[TypeAttribute.FIELDS][4]

# TestBitmask
bitmaskTypeInfo = bitmaskFieldInfo.type_info
self.assertEqual(
self.LENGTHOF_LITERAL, bitmaskTypeInfo.attributes[TypeAttribute.UNDERLYING_TYPE_ARGUMENTS][0]()
)

def testWriteRead(self):
typeInfoStringExpressions = self._createData()
bitBuffer = zserio.serialize(typeInfoStringExpressions)
readTypeInfoStringExpressions = zserio.deserialize(self.api.TypeInfoStringExpressions, bitBuffer)
self.assertEqual(typeInfoStringExpressions, readTypeInfoStringExpressions)

def _createData(self):
return self.api.TypeInfoStringExpressions(
self.api.TestChoice(
self.LENGTHOF_LITERAL,
struct_field_=self.api.TestStruct(
self.LENGTHOF_LITERAL, # param
[0] * self.LENGTHOF_LITERAL * 2, # array
0, # dynBitField
0, # bitField
self.api.TestEnum.ONE,
self.api.TestBitmask.Values.READ,
),
)
)

LENGTHOF_LITERAL = 7

0 comments on commit 92ef5a1

Please sign in to comment.