-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#675] Add new type info test with strings in expressions
- Loading branch information
Showing
7 changed files
with
325 additions
and
1 deletion.
There are no files selected for viewing
Submodule data
updated
1 files
+36 −0 | arguments/with_type_info_code/zs/type_info_string_expressions.zs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
test/extensions/arguments/with_type_info_code/cpp/TypeInfoStringExpressionsTest.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
93 changes: 93 additions & 0 deletions
93
.../with_type_info_code/java/type_info_string_expressions/TypeInfoStringExpressionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
101 changes: 101 additions & 0 deletions
101
test/extensions/arguments/with_type_info_code/python/TypeInfoStringExpressionsTest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |