From 8096ba79ead1198a610f56e4d6dc6d52f7a36789 Mon Sep 17 00:00:00 2001 From: Milan Kriz Date: Fri, 16 Aug 2024 16:29:02 +0200 Subject: [PATCH] [#641] Disallow most of the expressions in offsets --- compiler/core/src/zserio/ast/Field.java | 6 - .../zserio/ast/ZserioParseTreeChecker.java | 310 +++++++++++++++++- test/data | 2 +- .../ArrayTypesErrorTest.java | 2 +- ...plicit_array_with_indexed_offsets_error.zs | 8 - test/errors/indexed_offsets_error/build.xml | 46 +++ .../IndexedOffsetsErrorTest.java | 232 ++++++++++++- ..._add_operator_in_array_expression_error.zs | 8 + ...mult_operator_in_array_expression_error.zs | 8 + .../zs/array_in_array_expression_error.zs | 9 + ..._and_operator_in_array_expression_error.zs | 8 + ...e_or_operator_in_array_expression_error.zs | 8 + ..._xor_operator_in_array_expression_error.zs | 8 + ...ot_expression_in_array_expression_error.zs | 14 + ...lity_operator_in_array_expression_error.zs | 8 + ...function_call_in_array_expression_error.zs | 13 + .../zs/id_in_array_expression_error.zs | 9 + .../zs/isset_in_array_expression_error.zs | 15 + .../zs/lengthof_in_array_expression_error.zs | 8 + .../zs/literal_in_array_expression_error.zs} | 2 +- ..._and_operator_in_array_expression_error.zs | 8 + ...l_or_operator_in_array_expression_error.zs | 8 + .../zs/multiple_index_operators_error.zs | 13 + .../zs/numbits_in_array_expression_error.zs | 9 + .../zs/packed_complex_offset_array_error.zs | 11 +- .../zs/packed_param_offset_array_error.zs | 12 +- .../parenthesis_in_array_expression_error.zs | 8 + ...onal_operator_in_array_expression_error.zs | 8 + ...hift_operator_in_array_expression_error.zs | 8 + ...nary_operator_in_array_expression_error.zs | 8 + ...nary_operator_in_array_expression_error.zs | 8 + .../zs/valueof_in_array_expression_error.zs | 14 + test/errors/offsets_error/build.xml | 19 +- .../java/offsets_error/OffsetsErrorTest.java | 143 +++++++- .../zs/arithmetic_add_operator_error.zs | 8 + .../zs/arithmetic_mult_operator_error.zs | 8 + .../zs/bitwise_and_operator_error.zs | 8 + .../zs/bitwise_or_operator_error.zs | 8 + .../zs/bitwise_xor_operator_error.zs | 8 + .../zs/equality_operator_error.zs | 8 + test/errors/offsets_error/zs/index_error.zs | 7 + test/errors/offsets_error/zs/isset_error.zs | 14 + .../errors/offsets_error/zs/lengthof_error.zs | 8 + test/errors/offsets_error/zs/literal_error.zs | 7 + .../zs/logical_and_operator_error.zs | 8 + .../zs/logical_or_operator_error.zs | 8 + test/errors/offsets_error/zs/numbits_error.zs | 9 + .../offsets_error/zs/parenthesis_error.zs | 8 + .../zs/relational_operator_error.zs | 8 + .../offsets_error/zs/shift_operator_error.zs | 8 + .../offsets_error/zs/unary_operator_error.zs | 8 + test/errors/offsets_error/zs/valueof_error.zs | 13 + .../indexed_offsets/ClangTidySuppressions.txt | 1 + .../PackedIndexedOffsetArrayHolderTest.cpp | 14 +- .../PackedIndexedOffsetArrayHolderTest.java | 16 +- .../PackedIndexedOffsetArrayHolderTest.py | 12 +- .../packed_indexed_offset_array_holder.zs | 8 +- test/language/offsets/CMakeLists.txt | 1 - .../offsets/cpp/TernaryOperatorOffsetTest.cpp | 146 --------- .../offsets/TernaryOperatorOffsetTest.java | 146 --------- .../python/TernaryOperatorOffsetTest.py | 92 ------ test/language/offsets/zs/offsets.zs | 1 - .../zs/offsets/ternary_operator_offset.zs | 19 -- 63 files changed, 1135 insertions(+), 476 deletions(-) create mode 100644 test/errors/indexed_offsets_error/zs/arithmetic_add_operator_in_array_expression_error.zs create mode 100644 test/errors/indexed_offsets_error/zs/arithmetic_mult_operator_in_array_expression_error.zs create mode 100644 test/errors/indexed_offsets_error/zs/array_in_array_expression_error.zs create mode 100644 test/errors/indexed_offsets_error/zs/bitwise_and_operator_in_array_expression_error.zs create mode 100644 test/errors/indexed_offsets_error/zs/bitwise_or_operator_in_array_expression_error.zs create mode 100644 test/errors/indexed_offsets_error/zs/bitwise_xor_operator_in_array_expression_error.zs create mode 100644 test/errors/indexed_offsets_error/zs/dot_expression_in_array_expression_error.zs create mode 100644 test/errors/indexed_offsets_error/zs/equality_operator_in_array_expression_error.zs create mode 100644 test/errors/indexed_offsets_error/zs/function_call_in_array_expression_error.zs create mode 100644 test/errors/indexed_offsets_error/zs/id_in_array_expression_error.zs create mode 100644 test/errors/indexed_offsets_error/zs/isset_in_array_expression_error.zs create mode 100644 test/errors/indexed_offsets_error/zs/lengthof_in_array_expression_error.zs rename test/errors/{offsets_error/zs/packed_offset_array_error.zs => indexed_offsets_error/zs/literal_in_array_expression_error.zs} (84%) create mode 100644 test/errors/indexed_offsets_error/zs/logical_and_operator_in_array_expression_error.zs create mode 100644 test/errors/indexed_offsets_error/zs/logical_or_operator_in_array_expression_error.zs create mode 100644 test/errors/indexed_offsets_error/zs/multiple_index_operators_error.zs create mode 100644 test/errors/indexed_offsets_error/zs/numbits_in_array_expression_error.zs create mode 100644 test/errors/indexed_offsets_error/zs/parenthesis_in_array_expression_error.zs create mode 100644 test/errors/indexed_offsets_error/zs/relational_operator_in_array_expression_error.zs create mode 100644 test/errors/indexed_offsets_error/zs/shift_operator_in_array_expression_error.zs create mode 100644 test/errors/indexed_offsets_error/zs/ternary_operator_in_array_expression_error.zs create mode 100644 test/errors/indexed_offsets_error/zs/unary_operator_in_array_expression_error.zs create mode 100644 test/errors/indexed_offsets_error/zs/valueof_in_array_expression_error.zs create mode 100644 test/errors/offsets_error/zs/arithmetic_add_operator_error.zs create mode 100644 test/errors/offsets_error/zs/arithmetic_mult_operator_error.zs create mode 100644 test/errors/offsets_error/zs/bitwise_and_operator_error.zs create mode 100644 test/errors/offsets_error/zs/bitwise_or_operator_error.zs create mode 100644 test/errors/offsets_error/zs/bitwise_xor_operator_error.zs create mode 100644 test/errors/offsets_error/zs/equality_operator_error.zs create mode 100644 test/errors/offsets_error/zs/index_error.zs create mode 100644 test/errors/offsets_error/zs/isset_error.zs create mode 100644 test/errors/offsets_error/zs/lengthof_error.zs create mode 100644 test/errors/offsets_error/zs/literal_error.zs create mode 100644 test/errors/offsets_error/zs/logical_and_operator_error.zs create mode 100644 test/errors/offsets_error/zs/logical_or_operator_error.zs create mode 100644 test/errors/offsets_error/zs/numbits_error.zs create mode 100644 test/errors/offsets_error/zs/parenthesis_error.zs create mode 100644 test/errors/offsets_error/zs/relational_operator_error.zs create mode 100644 test/errors/offsets_error/zs/shift_operator_error.zs create mode 100644 test/errors/offsets_error/zs/unary_operator_error.zs create mode 100644 test/errors/offsets_error/zs/valueof_error.zs delete mode 100644 test/language/offsets/cpp/TernaryOperatorOffsetTest.cpp delete mode 100644 test/language/offsets/java/offsets/TernaryOperatorOffsetTest.java delete mode 100644 test/language/offsets/python/TernaryOperatorOffsetTest.py delete mode 100644 test/language/offsets/zs/offsets/ternary_operator_offset.zs diff --git a/compiler/core/src/zserio/ast/Field.java b/compiler/core/src/zserio/ast/Field.java index 37e5a543d..fbba34486 100644 --- a/compiler/core/src/zserio/ast/Field.java +++ b/compiler/core/src/zserio/ast/Field.java @@ -373,12 +373,6 @@ private void checkOffsetExpression() "' is not an unsigned fixed sized integer type!"); } - if (offsetExpr.containsFunctionCallOutOfArray()) - throw new ParserException(offsetExpr, "Function call cannot be used for offset setting!"); - - if (offsetExpr.containsTernaryOperatorOutOfArray()) - throw new ParserException(offsetExpr, "Ternary operator cannot be used for offset setting!"); - if (offsetExpr.op2() == null) { final AstNode symbolObject = offsetExpr.getExprSymbolObject(); diff --git a/compiler/core/src/zserio/ast/ZserioParseTreeChecker.java b/compiler/core/src/zserio/ast/ZserioParseTreeChecker.java index 5d1d62d95..84cb35c2f 100644 --- a/compiler/core/src/zserio/ast/ZserioParseTreeChecker.java +++ b/compiler/core/src/zserio/ast/ZserioParseTreeChecker.java @@ -9,6 +9,9 @@ import java.nio.charset.CharsetDecoder; import java.nio.charset.StandardCharsets; +import org.antlr.v4.runtime.ParserRuleContext; +import org.antlr.v4.runtime.Token; + import zserio.antlr.ZserioParser; import zserio.antlr.ZserioParserBaseVisitor; import zserio.tools.WarningsConfig; @@ -61,10 +64,10 @@ public Void visitStructureFieldDefinition(ZserioParser.StructureFieldDefinitionC public Void visitFieldOffset(ZserioParser.FieldOffsetContext ctx) { // index expression in offsets is allowed if we are in array which is not implicit - if (isInArrayField && !isInImplicitArrayField) - isIndexAllowed = true; + isInOffsetExpression = true; visitChildren(ctx); - isIndexAllowed = false; + isInOffsetExpression = false; + wasIndexUsedInOffsetExpression = false; return null; } @@ -143,6 +146,273 @@ public Void visitSqlTableFieldDefinition(ZserioParser.SqlTableFieldDefinitionCon return null; } + @Override + public Void visitParenthesizedExpression(ZserioParser.ParenthesizedExpressionContext ctx) + { + if (isInOffsetExpression) + { + throwForbiddenInOffsets(ctx, ctx.getStart(), "Parenthesis are not allowed in offset expressions!"); + } + + return visitChildren(ctx); + } + + @Override + public Void visitFunctionCallExpression(ZserioParser.FunctionCallExpressionContext ctx) + { + if (isInOffsetExpression) + { + throwForbiddenInOffsets( + ctx, ctx.LPAREN().getSymbol(), "Function call is not allowed in offset expressions!"); + } + + return visitChildren(ctx); + } + + @Override + public Void visitArrayExpression(ZserioParser.ArrayExpressionContext ctx) + { + if (isInOffsetArrayExpression) + { + throwForbiddenInOffsets( + ctx, ctx.operator, "Array expression is not allowed in offset array expressions!"); + } + + visit(ctx.expression(0)); + + if (isInOffsetExpression) + { + if (isInArrayField && !isInImplicitArrayField) + isIndexAllowed = true; + isInOffsetArrayExpression = true; + } + + visit(ctx.expression(1)); + + if (isInOffsetExpression) + { + isIndexAllowed = false; + isInOffsetArrayExpression = false; + } + + return null; + } + + @Override + public Void visitDotExpression(ZserioParser.DotExpressionContext ctx) + { + if (isInOffsetArrayExpression) + { + throwForbiddenInOffsets( + ctx, ctx.operator, "Dot expression is not allowed in offset array expressions!"); + } + + return visitChildren(ctx); + } + + @Override + public Void visitIsSetExpression(ZserioParser.IsSetExpressionContext ctx) + { + if (isInOffsetExpression) + { + throwForbiddenInOffsets( + ctx, ctx.getStart(), "Operator isset is not allowed in offset expressions!"); + } + + return visitChildren(ctx); + } + + @Override + public Void visitLengthofExpression(ZserioParser.LengthofExpressionContext ctx) + { + if (isInOffsetExpression) + { + throwForbiddenInOffsets( + ctx, ctx.getStart(), "Operator lengthof is not allowed in offset expressions!"); + } + + return visitChildren(ctx); + } + + @Override + public Void visitValueofExpression(ZserioParser.ValueofExpressionContext ctx) + { + if (isInOffsetExpression) + { + throwForbiddenInOffsets( + ctx, ctx.getStart(), "Operator valueof is not allowed in offset expressions!"); + } + + return visitChildren(ctx); + } + + @Override + public Void visitNumbitsExpression(ZserioParser.NumbitsExpressionContext ctx) + { + if (isInOffsetExpression) + { + throwForbiddenInOffsets( + ctx, ctx.getStart(), "Operator numbits is not allowed in offset expressions!"); + } + + return visitChildren(ctx); + } + + @Override + public Void visitUnaryExpression(ZserioParser.UnaryExpressionContext ctx) + { + if (isInOffsetExpression) + { + throwForbiddenInOffsets( + ctx, ctx.getStart(), "Unary operators are not allowed in offset expressions!"); + } + + return visitChildren(ctx); + } + + @Override + public Void visitMultiplicativeExpression(ZserioParser.MultiplicativeExpressionContext ctx) + { + if (isInOffsetExpression) + { + throwForbiddenInOffsets( + ctx, ctx.operator, "Arithmetic operators are not allowed in offset expressions!"); + } + + return visitChildren(ctx); + } + + @Override + public Void visitAdditiveExpression(ZserioParser.AdditiveExpressionContext ctx) + { + if (isInOffsetExpression) + { + throwForbiddenInOffsets( + ctx, ctx.operator, "Arithmetic operators are not allowed in offset expressions!"); + } + + return visitChildren(ctx); + } + + @Override + public Void visitShiftExpression(ZserioParser.ShiftExpressionContext ctx) + { + if (isInOffsetExpression) + { + throwForbiddenInOffsets( + ctx, ctx.operator, "Shift operators are not allowed in offset expressions!"); + } + + return visitChildren(ctx); + } + + @Override + public Void visitRelationalExpression(ZserioParser.RelationalExpressionContext ctx) + { + if (isInOffsetExpression) + { + throwForbiddenInOffsets( + ctx, ctx.operator, "Relational operators are not allowed in offset expressions!"); + } + + return visitChildren(ctx); + } + + @Override + public Void visitEqualityExpression(ZserioParser.EqualityExpressionContext ctx) + { + if (isInOffsetExpression) + { + throwForbiddenInOffsets( + ctx, ctx.operator, "Relational operators are not allowed in offset expressions!"); + } + + return visitChildren(ctx); + } + + @Override + public Void visitBitwiseAndExpression(ZserioParser.BitwiseAndExpressionContext ctx) + { + if (isInOffsetExpression) + { + throwForbiddenInOffsets( + ctx, ctx.operator, "Bitwise operators are not allowed in offset expressions!"); + } + + return visitChildren(ctx); + } + + @Override + public Void visitBitwiseXorExpression(ZserioParser.BitwiseXorExpressionContext ctx) + { + if (isInOffsetExpression) + { + throwForbiddenInOffsets( + ctx, ctx.operator, "Bitwise operators are not allowed in offset expressions!"); + } + + return visitChildren(ctx); + } + + @Override + public Void visitBitwiseOrExpression(ZserioParser.BitwiseOrExpressionContext ctx) + { + if (isInOffsetExpression) + { + throwForbiddenInOffsets( + ctx, ctx.operator, "Bitwise operators are not allowed in offset expressions!"); + } + + return visitChildren(ctx); + } + + @Override + public Void visitLogicalAndExpression(ZserioParser.LogicalAndExpressionContext ctx) + { + if (isInOffsetExpression) + { + throwForbiddenInOffsets( + ctx, ctx.operator, "Logical operators are not allowed in offset expressions!"); + } + + return visitChildren(ctx); + } + + @Override + public Void visitLogicalOrExpression(ZserioParser.LogicalOrExpressionContext ctx) + { + if (isInOffsetExpression) + { + throwForbiddenInOffsets( + ctx, ctx.operator, "Logical operators are not allowed in offset expressions!"); + } + + return visitChildren(ctx); + } + + @Override + public Void visitTernaryExpression(ZserioParser.TernaryExpressionContext ctx) + { + if (isInOffsetExpression) + { + throwForbiddenInOffsets( + ctx, ctx.operator, "Ternary operator is not allowed in offset expressions!"); + } + + return visitChildren(ctx); + } + + @Override + public Void visitLiteralExpression(ZserioParser.LiteralExpressionContext ctx) + { + if (isInOffsetExpression) + { + throwForbiddenInOffsets(ctx, ctx.getStart(), "Literals are not allowed in offset expressions!"); + } + + return visitChildren(ctx); + } + @Override public Void visitIndexExpression(ZserioParser.IndexExpressionContext ctx) { @@ -161,12 +431,28 @@ public Void visitIndexExpression(ZserioParser.IndexExpressionContext ctx) } } + if (isInOffsetExpression) + { + if (wasIndexUsedInOffsetExpression) + { + throw new ParserException( + ctx.getStart(), "Index operator can be used only once within an offset expression!"); + } + wasIndexUsedInOffsetExpression = true; + } + return visitChildren(ctx); } @Override public Void visitId(ZserioParser.IdContext ctx) { + if (isInOffsetArrayExpression) + { + throwForbiddenInOffsets( + ctx, ctx.getStart(), "Identifiers are not allowed in offset array expressions!"); + } + try { IdentifierValidator.validate(ctx.getText()); @@ -196,6 +482,21 @@ public Void visitTypeArgument(ZserioParser.TypeArgumentContext ctx) return null; } + private void throwForbiddenInOffsets(ParserRuleContext ctx, Token locationToken, String message) + { + if (isInOffsetArrayExpression) + { + final ParserStackedException exception = new ParserStackedException( + new AstLocation(ctx.getStart()), "Only @index is allowed in offset array expressions!"); + exception.pushMessage(new AstLocation(locationToken), message); + throw exception; + } + else + { + throw new ParserException(locationToken, message); + } + } + private void checkUtf8Encoding(AstLocation location) { final byte[] fileContent = readFile(location); @@ -262,6 +563,9 @@ private byte[] readFile(AstLocation location) private boolean isInArrayField = false; private boolean isInImplicitArrayField = false; private boolean isIndexAllowed = false; + private boolean isInOffsetExpression = false; + private boolean isInOffsetArrayExpression = false; + private boolean wasIndexUsedInOffsetExpression = false; /** Flag used to allow explicit keyword only for SQL tables. */ private boolean isInSqlTableField = false; diff --git a/test/data b/test/data index 7fc855306..76fb1153d 160000 --- a/test/data +++ b/test/data @@ -1 +1 @@ -Subproject commit 7fc8553065ff1bca58b848d861c71a299895a220 +Subproject commit 76fb1153d283f54701c5fe78649bfd16bdd936ae diff --git a/test/errors/array_types_error/java/array_types_error/ArrayTypesErrorTest.java b/test/errors/array_types_error/java/array_types_error/ArrayTypesErrorTest.java index 07fe21c85..08e5a79cc 100644 --- a/test/errors/array_types_error/java/array_types_error/ArrayTypesErrorTest.java +++ b/test/errors/array_types_error/java/array_types_error/ArrayTypesErrorTest.java @@ -146,7 +146,7 @@ public void implicitArrayWithLength() @Test public void implicitArrayWithIndexedOffsets() { - final String error = "implicit_array_with_indexed_offsets_error.zs:22:9: " + final String error = "implicit_array_with_indexed_offsets_error.zs:14:9: " + "Implicit arrays cannot have indexed offsets!"; assertTrue(zserioErrors.isPresent(error)); } diff --git a/test/errors/array_types_error/zs/implicit_array_with_indexed_offsets_error.zs b/test/errors/array_types_error/zs/implicit_array_with_indexed_offsets_error.zs index 62c5e0a41..0959f0546 100644 --- a/test/errors/array_types_error/zs/implicit_array_with_indexed_offsets_error.zs +++ b/test/errors/array_types_error/zs/implicit_array_with_indexed_offsets_error.zs @@ -8,14 +8,6 @@ offset: implicit uint32 array[]; }; -struct OffsetArrayWithoutIndexIsOk -{ - string description; - uint32 offset[]; -offset[0]: - implicit uint32 array[]; -}; - struct ImplicitArrayWithOffsetsError { uint32 offsets[]; diff --git a/test/errors/indexed_offsets_error/build.xml b/test/errors/indexed_offsets_error/build.xml index 8ee4a28c0..fbf94ddea 100644 --- a/test/errors/indexed_offsets_error/build.xml +++ b/test/errors/indexed_offsets_error/build.xml @@ -12,12 +12,46 @@ + + + + + + + + + + + + + + + + + @@ -26,6 +60,18 @@ + + + + + + diff --git a/test/errors/indexed_offsets_error/java/indexed_offsets_error/IndexedOffsetsErrorTest.java b/test/errors/indexed_offsets_error/java/indexed_offsets_error/IndexedOffsetsErrorTest.java index 3a98d5a1c..93d07ca40 100644 --- a/test/errors/indexed_offsets_error/java/indexed_offsets_error/IndexedOffsetsErrorTest.java +++ b/test/errors/indexed_offsets_error/java/indexed_offsets_error/IndexedOffsetsErrorTest.java @@ -3,6 +3,7 @@ import static org.junit.jupiter.api.Assertions.*; import java.io.IOException; +import java.util.function.Function; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -25,6 +26,66 @@ public void alignmentExpression() assertTrue(zserioErrors.isPresent(error)); } + @Test + public void arithmeticAddOperatorInArrayExpression() + { + final String[] errors = {"arithmetic_add_operator_in_array_expression_error.zs:6:16: " + + "Arithmetic operators are not allowed in offset expressions!", + "arithmetic_add_operator_in_array_expression_error.zs:6:9: " + + "Only @index is allowed in offset array expressions!"}; + assertTrue(zserioErrors.isPresent(errors)); + } + + @Test + public void arithmeticMultOperatorInArrayExpression() + { + final String[] errors = {"arithmetic_mult_operator_in_array_expression_error.zs:6:16: " + + "Arithmetic operators are not allowed in offset expressions!", + "arithmetic_mult_operator_in_array_expression_error.zs:6:9: " + + "Only @index is allowed in offset array expressions!"}; + assertTrue(zserioErrors.isPresent(errors)); + } + + @Test + public void arrayInArrayExpression() + { + final String[] errors = {"array_in_array_expression_error.zs:7:14: " + + "Array expression is not allowed in offset array expressions!", + "array_in_array_expression_error.zs:7:9: " + + "Only @index is allowed in offset array expressions!"}; + assertTrue(zserioErrors.isPresent(errors)); + } + + @Test + public void bitwiseAndOperatorInArrayExpression() + { + final String[] errors = {"bitwise_and_operator_in_array_expression_error.zs:6:16: " + + "Bitwise operators are not allowed in offset expressions!", + "bitwise_and_operator_in_array_expression_error.zs:6:9: " + + "Only @index is allowed in offset array expressions!"}; + assertTrue(zserioErrors.isPresent(errors)); + } + + @Test + public void bitwiseOrOperatorInArrayExpression() + { + final String[] errors = {"bitwise_or_operator_in_array_expression_error.zs:6:16: " + + "Bitwise operators are not allowed in offset expressions!", + "bitwise_or_operator_in_array_expression_error.zs:6:9: " + + "Only @index is allowed in offset array expressions!"}; + assertTrue(zserioErrors.isPresent(errors)); + } + + @Test + public void bitwiseXorOperatorInArrayExpression() + { + final String[] errors = {"bitwise_xor_operator_in_array_expression_error.zs:6:16: " + + "Bitwise operators are not allowed in offset expressions!", + "bitwise_xor_operator_in_array_expression_error.zs:6:9: " + + "Only @index is allowed in offset array expressions!"}; + assertTrue(zserioErrors.isPresent(errors)); + } + @Test public void constantExpression() { @@ -49,6 +110,16 @@ public void defaultValueExpression() assertTrue(zserioErrors.isPresent(error)); } + @Test + public void dotExpressionInArrayExpression() + { + final String[] errors = {"dot_expression_in_array_expression_error.zs:12:17: " + + "Dot expression is not allowed in offset array expressions!", + "dot_expression_in_array_expression_error.zs:12:9: " + + "Only @index is allowed in offset array expressions!"}; + assertTrue(zserioErrors.isPresent(errors)); + } + @Test public void enumValueExpression() { @@ -57,6 +128,93 @@ public void enumValueExpression() assertTrue(zserioErrors.isPresent(error)); } + @Test + public void equalityOperatorInArrayExpression() + { + final String[] errors = {"equality_operator_in_array_expression_error.zs:6:16: " + + "Relational operators are not allowed in offset expressions!", + "equality_operator_in_array_expression_error.zs:6:9: " + + "Only @index is allowed in offset array expressions!"}; + assertTrue(zserioErrors.isPresent(errors)); + } + + @Test + public void functionCallInArrayExpression() + { + final String[] errors = {"function_call_in_array_expression_error.zs:6:12: " + + "Function call is not allowed in offset expressions!", + "function_call_in_array_expression_error.zs:6:9: " + + "Only @index is allowed in offset array expressions!"}; + assertTrue(zserioErrors.isPresent(errors)); + } + + @Test + public void id_in_array_expression() + { + final String[] errors = {"id_in_array_expression_error.zs:7:9: " + + "Identifiers are not allowed in offset array expressions!", + "id_in_array_expression_error.zs:7:9: Only @index is allowed in offset array expressions!"}; + assertTrue(zserioErrors.isPresent(errors)); + } + + @Test + public void isset_operator_in_array_expression() + { + final String[] errors = {"isset_in_array_expression_error.zs:13:9: " + + "Operator isset is not allowed in offset expressions!", + "isset_in_array_expression_error.zs:13:9: " + + "Only @index is allowed in offset array expressions!"}; + assertTrue(zserioErrors.isPresent(errors)); + } + + @Test + public void lengthof_operator_in_array_expression() + { + final String[] errors = {"lengthof_in_array_expression_error.zs:6:9: " + + "Operator lengthof is not allowed in offset expressions!", + "lengthof_in_array_expression_error.zs:6:9: " + + "Only @index is allowed in offset array expressions!"}; + assertTrue(zserioErrors.isPresent(errors)); + } + + @Test + public void literal_in_array_expression() + { + final String[] errors = {"literal_in_array_expression_error.zs:17:35: " + + "Literals are not allowed in offset expressions!", + "literal_in_array_expression_error.zs:17:35: " + + "Only @index is allowed in offset array expressions!"}; + assertTrue(zserioErrors.isPresent(errors)); + } + + @Test + public void logic_and_operator_in_array_expression() + { + final String[] errors = {"logical_and_operator_in_array_expression_error.zs:6:16: " + + "Logical operators are not allowed in offset expressions!", + "logical_and_operator_in_array_expression_error.zs:6:9: " + + "Only @index is allowed in offset array expressions!"}; + assertTrue(zserioErrors.isPresent(errors)); + } + + @Test + public void logic_or_operator_in_array_expression() + { + final String[] errors = {"logical_or_operator_in_array_expression_error.zs:6:16: " + + "Logical operators are not allowed in offset expressions!", + "logical_or_operator_in_array_expression_error.zs:6:9: " + + "Only @index is allowed in offset array expressions!"}; + assertTrue(zserioErrors.isPresent(errors)); + } + + @Test + public void multipleIndexOperators() + { + final String error = "multiple_index_operators_error.zs:11:25: " + + "Index operator can be used only once within an offset expression!"; + assertTrue(zserioErrors.isPresent(error)); + } + @Test public void noArray() { @@ -64,6 +222,16 @@ public void noArray() assertTrue(zserioErrors.isPresent(error)); } + @Test + public void numbits_in_array_expression() + { + final String[] errors = {"numbits_in_array_expression_error.zs:7:9: " + + "Operator numbits is not allowed in offset expressions!", + "numbits_in_array_expression_error.zs:7:9: " + + "Only @index is allowed in offset array expressions!"}; + assertTrue(zserioErrors.isPresent(errors)); + } + @Test public void optionalExpression() { @@ -75,7 +243,7 @@ public void optionalExpression() @Test public void packedComplexOffsetArray() { - final String error = "packed_complex_offset_array_error.zs:19:1: " + final String error = "packed_complex_offset_array_error.zs:12:1: " + "Packed array cannot be used as offset array!"; assertTrue(zserioErrors.isPresent(error)); } @@ -83,7 +251,7 @@ public void packedComplexOffsetArray() @Test public void packedParamOffsetArray() { - final String error = "packed_param_offset_array_error.zs:18:1: " + final String error = "packed_param_offset_array_error.zs:10:1: " + "Packed array cannot be used as offset array!"; assertTrue(zserioErrors.isPresent(error)); } @@ -104,6 +272,66 @@ public void parameterNoArray() assertTrue(zserioErrors.isPresent(error)); } + @Test + public void parenthesis_in_array_expression() + { + final String[] errors = {"parenthesis_in_array_expression_error.zs:6:9: " + + "Parenthesis are not allowed in offset expressions!", + "parenthesis_in_array_expression_error.zs:6:9: " + + "Only @index is allowed in offset array expressions!"}; + assertTrue(zserioErrors.isPresent(errors)); + } + + @Test + public void relational_operator_in_array_expression() + { + final String[] errors = {"relational_operator_in_array_expression_error.zs:6:16: " + + "Relational operators are not allowed in offset expressions!", + "relational_operator_in_array_expression_error.zs:6:9: " + + "Only @index is allowed in offset array expressions!"}; + assertTrue(zserioErrors.isPresent(errors)); + } + + @Test + public void shift_operator_in_array_expression() + { + final String[] errors = {"shift_operator_in_array_expression_error.zs:6:16: " + + "Shift operators are not allowed in offset expressions!", + "shift_operator_in_array_expression_error.zs:6:9: " + + "Only @index is allowed in offset array expressions!"}; + assertTrue(zserioErrors.isPresent(errors)); + } + + @Test + public void ternary_operator_in_array_expression() + { + final String[] errors = {"ternary_operator_in_array_expression_error.zs:6:20: " + + "Ternary operator is not allowed in offset expressions!", + "ternary_operator_in_array_expression_error.zs:6:9: " + + "Only @index is allowed in offset array expressions!"}; + assertTrue(zserioErrors.isPresent(errors)); + } + + @Test + public void unary_operator_in_array_expression() + { + final String[] errors = {"unary_operator_in_array_expression_error.zs:6:9: " + + "Unary operators are not allowed in offset expressions!", + "unary_operator_in_array_expression_error.zs:6:9: " + + "Only @index is allowed in offset array expressions!"}; + assertTrue(zserioErrors.isPresent(errors)); + } + + @Test + public void valueof_in_array_expression() + { + final String[] errors = {"valueof_in_array_expression_error.zs:12:9: " + + "Operator valueof is not allowed in offset expressions!", + "valueof_in_array_expression_error.zs:12:9: " + + "Only @index is allowed in offset array expressions!"}; + assertTrue(zserioErrors.isPresent(errors)); + } + @Test public void varuintOffsetArray() { diff --git a/test/errors/indexed_offsets_error/zs/arithmetic_add_operator_in_array_expression_error.zs b/test/errors/indexed_offsets_error/zs/arithmetic_add_operator_in_array_expression_error.zs new file mode 100644 index 000000000..969e81020 --- /dev/null +++ b/test/errors/indexed_offsets_error/zs/arithmetic_add_operator_in_array_expression_error.zs @@ -0,0 +1,8 @@ +package arithmetic_add_operator_in_array_expression_error; + +struct ArithmeticAddOperatorInArrayExpressionError +{ + uint32 offsets[]; +offsets[@index + 0]: + string fields[]; +}; diff --git a/test/errors/indexed_offsets_error/zs/arithmetic_mult_operator_in_array_expression_error.zs b/test/errors/indexed_offsets_error/zs/arithmetic_mult_operator_in_array_expression_error.zs new file mode 100644 index 000000000..cca33d708 --- /dev/null +++ b/test/errors/indexed_offsets_error/zs/arithmetic_mult_operator_in_array_expression_error.zs @@ -0,0 +1,8 @@ +package arithmetic_mult_operator_in_array_expression_error; + +struct ArithmeticMultOperatorInArrayExpressionError +{ + uint32 offsets[]; +offsets[@index * 1]: + string fields[]; +}; diff --git a/test/errors/indexed_offsets_error/zs/array_in_array_expression_error.zs b/test/errors/indexed_offsets_error/zs/array_in_array_expression_error.zs new file mode 100644 index 000000000..26ea29ece --- /dev/null +++ b/test/errors/indexed_offsets_error/zs/array_in_array_expression_error.zs @@ -0,0 +1,9 @@ +package array_in_array_expression_error; + +struct ArrayInArrayExpressionError +{ + uint32 array[]; + uint32 offsets[]; +offsets[array[@index]]: + string fields[]; +}; diff --git a/test/errors/indexed_offsets_error/zs/bitwise_and_operator_in_array_expression_error.zs b/test/errors/indexed_offsets_error/zs/bitwise_and_operator_in_array_expression_error.zs new file mode 100644 index 000000000..457793b23 --- /dev/null +++ b/test/errors/indexed_offsets_error/zs/bitwise_and_operator_in_array_expression_error.zs @@ -0,0 +1,8 @@ +package bitwise_and_operator_in_array_expression_error; + +struct BitwiseAndOperatorInArrayExpressionError +{ + uint32 offsets[]; +offsets[@index & 0xff]: + string fields[]; +}; diff --git a/test/errors/indexed_offsets_error/zs/bitwise_or_operator_in_array_expression_error.zs b/test/errors/indexed_offsets_error/zs/bitwise_or_operator_in_array_expression_error.zs new file mode 100644 index 000000000..f8a120b24 --- /dev/null +++ b/test/errors/indexed_offsets_error/zs/bitwise_or_operator_in_array_expression_error.zs @@ -0,0 +1,8 @@ +package bitwise_or_operator_in_array_expression_error; + +struct BitwiseOrOperatorInArrayExpressionError +{ + uint32 offsets[]; +offsets[@index | 0xff]: + string fields[]; +}; diff --git a/test/errors/indexed_offsets_error/zs/bitwise_xor_operator_in_array_expression_error.zs b/test/errors/indexed_offsets_error/zs/bitwise_xor_operator_in_array_expression_error.zs new file mode 100644 index 000000000..cc90d5c9e --- /dev/null +++ b/test/errors/indexed_offsets_error/zs/bitwise_xor_operator_in_array_expression_error.zs @@ -0,0 +1,8 @@ +package bitwise_xor_operator_in_array_expression_error; + +struct BitwiseXorOperatorInArrayExpressionError +{ + uint32 offsets[]; +offsets[@index ^ 0xff]: + string fields[]; +}; diff --git a/test/errors/indexed_offsets_error/zs/dot_expression_in_array_expression_error.zs b/test/errors/indexed_offsets_error/zs/dot_expression_in_array_expression_error.zs new file mode 100644 index 000000000..41084c315 --- /dev/null +++ b/test/errors/indexed_offsets_error/zs/dot_expression_in_array_expression_error.zs @@ -0,0 +1,14 @@ +package dot_expression_in_array_expression_error; + +struct Compound +{ + uint32 id; +}; + +struct DotExpressionInArrayExpressionError +{ + Compound compound; + uint32 offsets[]; +offsets[compound.id]: + string fields[]; +}; diff --git a/test/errors/indexed_offsets_error/zs/equality_operator_in_array_expression_error.zs b/test/errors/indexed_offsets_error/zs/equality_operator_in_array_expression_error.zs new file mode 100644 index 000000000..7667e8179 --- /dev/null +++ b/test/errors/indexed_offsets_error/zs/equality_operator_in_array_expression_error.zs @@ -0,0 +1,8 @@ +package equality_operator_in_array_expression_error; + +struct EqualityOperatorInArrayExpressionError +{ + uint32 offsets[]; +offsets[@index == 0]: + string fields[]; +}; diff --git a/test/errors/indexed_offsets_error/zs/function_call_in_array_expression_error.zs b/test/errors/indexed_offsets_error/zs/function_call_in_array_expression_error.zs new file mode 100644 index 000000000..749085b54 --- /dev/null +++ b/test/errors/indexed_offsets_error/zs/function_call_in_array_expression_error.zs @@ -0,0 +1,13 @@ +package function_call_in_array_expression_error; + +struct FunctionCallInArrayExpressionError +{ + uint32 offsets[]; +offsets[get()]: + string fields[]; + + function uint32 get() + { + return 0; + } +}; diff --git a/test/errors/indexed_offsets_error/zs/id_in_array_expression_error.zs b/test/errors/indexed_offsets_error/zs/id_in_array_expression_error.zs new file mode 100644 index 000000000..0989b107d --- /dev/null +++ b/test/errors/indexed_offsets_error/zs/id_in_array_expression_error.zs @@ -0,0 +1,9 @@ +package id_in_array_expression_error; + +struct IdInArrayExpressionError +{ + uint32 value; + uint32 offsets[]; +offsets[value]: + string fields[]; +}; diff --git a/test/errors/indexed_offsets_error/zs/isset_in_array_expression_error.zs b/test/errors/indexed_offsets_error/zs/isset_in_array_expression_error.zs new file mode 100644 index 000000000..6e53e720b --- /dev/null +++ b/test/errors/indexed_offsets_error/zs/isset_in_array_expression_error.zs @@ -0,0 +1,15 @@ +package isset_in_array_expression_error; + +bitmask uint8 Bitmask +{ + FIRST, + SECOND +}; + +struct IsSetInArrayExpressionError +{ + Bitmask bm; + uint32 offsets[]; +offsets[isset(bm, Bitmask.SECOND)]: + string fields[]; +}; diff --git a/test/errors/indexed_offsets_error/zs/lengthof_in_array_expression_error.zs b/test/errors/indexed_offsets_error/zs/lengthof_in_array_expression_error.zs new file mode 100644 index 000000000..9cce7de32 --- /dev/null +++ b/test/errors/indexed_offsets_error/zs/lengthof_in_array_expression_error.zs @@ -0,0 +1,8 @@ +package lengthof_in_array_expression_error; + +struct LengthOfInArrayExpressionError +{ + uint32 offsets[]; +offsets[lengthof(offsets)]: + string fields[]; +}; diff --git a/test/errors/offsets_error/zs/packed_offset_array_error.zs b/test/errors/indexed_offsets_error/zs/literal_in_array_expression_error.zs similarity index 84% rename from test/errors/offsets_error/zs/packed_offset_array_error.zs rename to test/errors/indexed_offsets_error/zs/literal_in_array_expression_error.zs index 6d8ed2a51..7f1d97d4d 100644 --- a/test/errors/offsets_error/zs/packed_offset_array_error.zs +++ b/test/errors/indexed_offsets_error/zs/literal_in_array_expression_error.zs @@ -1,4 +1,4 @@ -package packed_offset_array_error; +package literal_in_array_expression_error; struct OffsetArray { diff --git a/test/errors/indexed_offsets_error/zs/logical_and_operator_in_array_expression_error.zs b/test/errors/indexed_offsets_error/zs/logical_and_operator_in_array_expression_error.zs new file mode 100644 index 000000000..38af3448a --- /dev/null +++ b/test/errors/indexed_offsets_error/zs/logical_and_operator_in_array_expression_error.zs @@ -0,0 +1,8 @@ +package logical_and_operator_in_array_expression_error; + +struct LogicalAndOperatorInArrayExpressionError +{ + uint32 offsets[]; +offsets[@index && true]: + string fields[]; +}; diff --git a/test/errors/indexed_offsets_error/zs/logical_or_operator_in_array_expression_error.zs b/test/errors/indexed_offsets_error/zs/logical_or_operator_in_array_expression_error.zs new file mode 100644 index 000000000..5ad29e779 --- /dev/null +++ b/test/errors/indexed_offsets_error/zs/logical_or_operator_in_array_expression_error.zs @@ -0,0 +1,8 @@ +package logical_or_operator_in_array_expression_error; + +struct LogicalOrOperatorInArrayExpressionError +{ + uint32 offsets[]; +offsets[@index || true]: + string fields[]; +}; diff --git a/test/errors/indexed_offsets_error/zs/multiple_index_operators_error.zs b/test/errors/indexed_offsets_error/zs/multiple_index_operators_error.zs new file mode 100644 index 000000000..4d0674aaa --- /dev/null +++ b/test/errors/indexed_offsets_error/zs/multiple_index_operators_error.zs @@ -0,0 +1,13 @@ +package multiple_index_operators_error; + +struct Holder +{ + uint32 offsets[]; +}; + +struct MultipleIndexOperatorsError +{ + Holder holders[]; +holders[@index].offsets[@index]: + string fields[]; +}; diff --git a/test/errors/indexed_offsets_error/zs/numbits_in_array_expression_error.zs b/test/errors/indexed_offsets_error/zs/numbits_in_array_expression_error.zs new file mode 100644 index 000000000..f3377c638 --- /dev/null +++ b/test/errors/indexed_offsets_error/zs/numbits_in_array_expression_error.zs @@ -0,0 +1,9 @@ +package numbits_in_array_expression_error; + +struct NumBitsInArrayExpressionError +{ + uint32 value; + uint32 offsets[]; +offsets[numbits(value)]: + string fields[]; +}; diff --git a/test/errors/indexed_offsets_error/zs/packed_complex_offset_array_error.zs b/test/errors/indexed_offsets_error/zs/packed_complex_offset_array_error.zs index 4bfb0208f..3aa485fc8 100644 --- a/test/errors/indexed_offsets_error/zs/packed_complex_offset_array_error.zs +++ b/test/errors/indexed_offsets_error/zs/packed_complex_offset_array_error.zs @@ -1,10 +1,5 @@ package packed_complex_offset_array_error; -struct OffsetArray -{ - PackedOffsets packedOffsets; -}; - struct PackedOffsets { packed uint32 offsets[20]; @@ -12,10 +7,8 @@ struct PackedOffsets struct PackedOffsetArrayError { - OffsetArray offsetArrays[2]; - uint32 offsetIndexShift1; - uint32 offsetIndexShift2[2]; + PackedOffsets packedOffsets; -offsetArrays[0].packedOffsets.offsets[offsetIndexShift1 + @index + offsetIndexShift2[0]]: +packedOffsets.offsets[@index]: int32 fields[10]; }; diff --git a/test/errors/indexed_offsets_error/zs/packed_param_offset_array_error.zs b/test/errors/indexed_offsets_error/zs/packed_param_offset_array_error.zs index 4a4f7ea3a..f9d965cfd 100644 --- a/test/errors/indexed_offsets_error/zs/packed_param_offset_array_error.zs +++ b/test/errors/indexed_offsets_error/zs/packed_param_offset_array_error.zs @@ -1,20 +1,12 @@ package packed_param_offset_array_error; -struct OffsetArray -{ - PackedOffsets packedOffsets[]; -}; - struct PackedOffsets { packed uint32 offsets[20]; }; -struct PackedOffsetArrayError(OffsetArray offsetArray) +struct PackedOffsetArrayError(PackedOffsets packedOffsets) { - uint32 offsetIndexShift1; - uint32 offsetIndexShift2[2]; - -offsetArray.packedOffsets[0].offsets[offsetIndexShift1 + @index + offsetIndexShift2[0]]: +packedOffsets.offsets[@index]: int32 fields[10]; }; diff --git a/test/errors/indexed_offsets_error/zs/parenthesis_in_array_expression_error.zs b/test/errors/indexed_offsets_error/zs/parenthesis_in_array_expression_error.zs new file mode 100644 index 000000000..ced5aaf71 --- /dev/null +++ b/test/errors/indexed_offsets_error/zs/parenthesis_in_array_expression_error.zs @@ -0,0 +1,8 @@ +package parenthesis_in_array_expression_error; + +struct ParenthesisInArrayExpressionError +{ + uint32 offsets[]; +offsets[(@index)]: + string fields[]; +}; diff --git a/test/errors/indexed_offsets_error/zs/relational_operator_in_array_expression_error.zs b/test/errors/indexed_offsets_error/zs/relational_operator_in_array_expression_error.zs new file mode 100644 index 000000000..7daee7ad3 --- /dev/null +++ b/test/errors/indexed_offsets_error/zs/relational_operator_in_array_expression_error.zs @@ -0,0 +1,8 @@ +package relational_operator_in_array_expression_error; + +struct RelationalOperatorInArrayExpressionError +{ + uint32 offsets[]; +offsets[@index > 0]: + string fields[]; +}; diff --git a/test/errors/indexed_offsets_error/zs/shift_operator_in_array_expression_error.zs b/test/errors/indexed_offsets_error/zs/shift_operator_in_array_expression_error.zs new file mode 100644 index 000000000..4aa21a71b --- /dev/null +++ b/test/errors/indexed_offsets_error/zs/shift_operator_in_array_expression_error.zs @@ -0,0 +1,8 @@ +package shift_operator_in_array_expression_error; + +struct ShiftOperatorInArrayExpressionError +{ + uint32 offsets[]; +offsets[@index << 1]: + string fields[]; +}; diff --git a/test/errors/indexed_offsets_error/zs/ternary_operator_in_array_expression_error.zs b/test/errors/indexed_offsets_error/zs/ternary_operator_in_array_expression_error.zs new file mode 100644 index 000000000..83f1fe505 --- /dev/null +++ b/test/errors/indexed_offsets_error/zs/ternary_operator_in_array_expression_error.zs @@ -0,0 +1,8 @@ +package ternary_operator_in_array_expression_error; + +struct TernaryOperatorInArrayExpressionError +{ + uint32 offsets[]; +offsets[@index > 0 ? 1 : 0]: + string fields[]; +}; diff --git a/test/errors/indexed_offsets_error/zs/unary_operator_in_array_expression_error.zs b/test/errors/indexed_offsets_error/zs/unary_operator_in_array_expression_error.zs new file mode 100644 index 000000000..60a4f32b4 --- /dev/null +++ b/test/errors/indexed_offsets_error/zs/unary_operator_in_array_expression_error.zs @@ -0,0 +1,8 @@ +package unary_operator_in_array_expression_error; + +struct UnaryOperatorInArrayExpressionError +{ + uint32 offsets[]; +offsets[~@index]: + string fields[]; +}; diff --git a/test/errors/indexed_offsets_error/zs/valueof_in_array_expression_error.zs b/test/errors/indexed_offsets_error/zs/valueof_in_array_expression_error.zs new file mode 100644 index 000000000..dd8a5bf18 --- /dev/null +++ b/test/errors/indexed_offsets_error/zs/valueof_in_array_expression_error.zs @@ -0,0 +1,14 @@ +package valueof_in_array_expression_error; + +enum uint8 Enum +{ + ONE, + TWO +}; + +struct ValueOfInArrayExpressionError +{ + uint32 offsets[]; +offsets[valueof(Enum.TWO)]: + string fields[]; +}; diff --git a/test/errors/offsets_error/build.xml b/test/errors/offsets_error/build.xml index 40d638474..d01c85b05 100644 --- a/test/errors/offsets_error/build.xml +++ b/test/errors/offsets_error/build.xml @@ -11,16 +11,33 @@ + + + + + + - + + + + + + + + + + + + diff --git a/test/errors/offsets_error/java/offsets_error/OffsetsErrorTest.java b/test/errors/offsets_error/java/offsets_error/OffsetsErrorTest.java index 5b4153bd9..f9b4a613e 100644 --- a/test/errors/offsets_error/java/offsets_error/OffsetsErrorTest.java +++ b/test/errors/offsets_error/java/offsets_error/OffsetsErrorTest.java @@ -17,6 +17,22 @@ public static void readZserioErrors() throws IOException zserioErrors = new ZserioErrorOutput(); } + @Test + public void arithmeticAddOperatorError() + { + final String error = "arithmetic_add_operator_error.zs:6:8: " + + "Arithmetic operators are not allowed in offset expressions!"; + assertTrue(zserioErrors.isPresent(error)); + } + + @Test + public void arithmeticMultOperatorError() + { + final String error = "arithmetic_mult_operator_error.zs:6:8: " + + "Arithmetic operators are not allowed in offset expressions!"; + assertTrue(zserioErrors.isPresent(error)); + } + @Test public void arrayNoIndex() { @@ -25,6 +41,30 @@ public void arrayNoIndex() assertTrue(zserioErrors.isPresent(error)); } + @Test + public void bitwiseAndOperatorError() + { + final String error = "bitwise_and_operator_error.zs:6:8: " + + "Bitwise operators are not allowed in offset expressions!"; + assertTrue(zserioErrors.isPresent(error)); + } + + @Test + public void bitwiseOrOperatorError() + { + final String error = "bitwise_or_operator_error.zs:6:8: " + + "Bitwise operators are not allowed in offset expressions!"; + assertTrue(zserioErrors.isPresent(error)); + } + + @Test + public void bitwiseXorOperatorError() + { + final String error = "bitwise_xor_operator_error.zs:6:8: " + + "Bitwise operators are not allowed in offset expressions!"; + assertTrue(zserioErrors.isPresent(error)); + } + @Test public void builtinTypeParameter() { @@ -40,6 +80,14 @@ public void constant() assertTrue(zserioErrors.isPresent(error)); } + @Test + public void equalityOperator() + { + final String error = "equality_operator_error.zs:6:8: " + + "Relational operators are not allowed in offset expressions!"; + assertTrue(zserioErrors.isPresent(error)); + } + @Test public void floatError() { @@ -51,14 +99,84 @@ public void floatError() @Test public void functionError() { - final String error = "function_error.zs:12:1: Function call cannot be used for offset setting!"; + final String error = "function_error.zs:12:15: Function call is not allowed in offset expressions!"; + assertTrue(zserioErrors.isPresent(error)); + } + + @Test + public void indexError() + { + final String error = "index_error.zs:5:1: Index operator is not allowed in this context!"; + assertTrue(zserioErrors.isPresent(error)); + } + + @Test + public void issetError() + { + final String error = "isset_error.zs:12:1: Operator isset is not allowed in offset expressions!"; + assertTrue(zserioErrors.isPresent(error)); + } + + @Test + public void lengthofError() + { + final String error = "lengthof_error.zs:6:1: Operator lengthof is not allowed in offset expressions!"; + assertTrue(zserioErrors.isPresent(error)); + } + + @Test + public void literalError() + { + final String error = "literal_error.zs:5:1: Literals are not allowed in offset expressions!"; + assertTrue(zserioErrors.isPresent(error)); + } + + @Test + public void logicalAndOperatorError() + { + final String error = "logical_and_operator_error.zs:6:8: " + + "Logical operators are not allowed in offset expressions!"; + assertTrue(zserioErrors.isPresent(error)); + } + + @Test + public void logicalOrOperatorError() + { + final String error = "logical_and_operator_error.zs:6:8: " + + "Logical operators are not allowed in offset expressions!"; + assertTrue(zserioErrors.isPresent(error)); + } + + @Test + public void numbitsError() + { + final String error = "numbits_error.zs:7:1: Operator numbits is not allowed in offset expressions!"; + assertTrue(zserioErrors.isPresent(error)); + } + + @Test + public void parenthesisError() + { + + final String error = "parenthesis_error.zs:6:1: Parenthesis are not allowed in offset expressions!"; + assertTrue(zserioErrors.isPresent(error)); + } + + @Test + public void relationalOperatorError() + { + + final String error = "relational_operator_error.zs:6:8: " + + "Relational operators are not allowed in offset expressions!"; assertTrue(zserioErrors.isPresent(error)); } @Test - public void packed_offset_array() + public void shiftOperatorError() { - final String error = "packed_offset_array_error.zs:17:1: Packed array cannot be used as offset array!"; + + final String error = "shift_operator_error.zs:6:8: " + + "Shift operators are not allowed in offset expressions!"; assertTrue(zserioErrors.isPresent(error)); } @@ -89,8 +207,23 @@ public void string() @Test public void ternaryOperatorError() { - final String error = "ternary_operator_error.zs:8:1: " - + "Ternary operator cannot be used for offset setting!"; + final String error = "ternary_operator_error.zs:8:19: " + + "Ternary operator is not allowed in offset expressions!"; + assertTrue(zserioErrors.isPresent(error)); + } + + @Test + public void unaryOperatorError() + { + final String error = "unary_operator_error.zs:6:1: " + + "Unary operators are not allowed in offset expressions!"; + assertTrue(zserioErrors.isPresent(error)); + } + + @Test + public void valueofError() + { + final String error = "valueof_error.zs:11:1: Operator valueof is not allowed in offset expressions!"; assertTrue(zserioErrors.isPresent(error)); } diff --git a/test/errors/offsets_error/zs/arithmetic_add_operator_error.zs b/test/errors/offsets_error/zs/arithmetic_add_operator_error.zs new file mode 100644 index 000000000..4d9c5633f --- /dev/null +++ b/test/errors/offsets_error/zs/arithmetic_add_operator_error.zs @@ -0,0 +1,8 @@ +package arithmetic_add_operator_error; + +struct ArithmeticAddOperatorError +{ + uint32 offset; +offset + offset: + string field; +}; diff --git a/test/errors/offsets_error/zs/arithmetic_mult_operator_error.zs b/test/errors/offsets_error/zs/arithmetic_mult_operator_error.zs new file mode 100644 index 000000000..c0b7d9241 --- /dev/null +++ b/test/errors/offsets_error/zs/arithmetic_mult_operator_error.zs @@ -0,0 +1,8 @@ +package arithmetic_mult_operator_error; + +struct ArithmeticMultOperatorError +{ + uint32 offset; +offset * offset: + string field; +}; diff --git a/test/errors/offsets_error/zs/bitwise_and_operator_error.zs b/test/errors/offsets_error/zs/bitwise_and_operator_error.zs new file mode 100644 index 000000000..1879f3869 --- /dev/null +++ b/test/errors/offsets_error/zs/bitwise_and_operator_error.zs @@ -0,0 +1,8 @@ +package bitwise_and_operator_error; + +struct BitwiseAndOperatorError +{ + uint32 offset; +offset & 0xf: + string field; +}; diff --git a/test/errors/offsets_error/zs/bitwise_or_operator_error.zs b/test/errors/offsets_error/zs/bitwise_or_operator_error.zs new file mode 100644 index 000000000..a995510a0 --- /dev/null +++ b/test/errors/offsets_error/zs/bitwise_or_operator_error.zs @@ -0,0 +1,8 @@ +package bitwise_or_operator_error; + +struct BitwiseOrOperatorError +{ + uint32 offset; +offset | 0xf: + string field; +}; diff --git a/test/errors/offsets_error/zs/bitwise_xor_operator_error.zs b/test/errors/offsets_error/zs/bitwise_xor_operator_error.zs new file mode 100644 index 000000000..c7b3aeb56 --- /dev/null +++ b/test/errors/offsets_error/zs/bitwise_xor_operator_error.zs @@ -0,0 +1,8 @@ +package bitwise_xor_operator_error; + +struct BitwiseXorOperatorError +{ + uint32 offset; +offset ^ 0xf: + string field; +}; diff --git a/test/errors/offsets_error/zs/equality_operator_error.zs b/test/errors/offsets_error/zs/equality_operator_error.zs new file mode 100644 index 000000000..8715ee22e --- /dev/null +++ b/test/errors/offsets_error/zs/equality_operator_error.zs @@ -0,0 +1,8 @@ +package equality_operator_error; + +struct EqualityOperatorError +{ + uint32 offset; +offset == 10: + string field; +}; diff --git a/test/errors/offsets_error/zs/index_error.zs b/test/errors/offsets_error/zs/index_error.zs new file mode 100644 index 000000000..5f0cdd0ce --- /dev/null +++ b/test/errors/offsets_error/zs/index_error.zs @@ -0,0 +1,7 @@ +package index_error; + +struct IndexError +{ +@index: + string field[]; +}; diff --git a/test/errors/offsets_error/zs/isset_error.zs b/test/errors/offsets_error/zs/isset_error.zs new file mode 100644 index 000000000..083843356 --- /dev/null +++ b/test/errors/offsets_error/zs/isset_error.zs @@ -0,0 +1,14 @@ +package isset_error; + +bitmask uint32 Bitmask +{ + FIRST, + SECOND +}; + +struct IsSetError +{ + Bitmask bm; +isset(bm, Bitmask.FIRST): + string field; +}; diff --git a/test/errors/offsets_error/zs/lengthof_error.zs b/test/errors/offsets_error/zs/lengthof_error.zs new file mode 100644 index 000000000..353af1faf --- /dev/null +++ b/test/errors/offsets_error/zs/lengthof_error.zs @@ -0,0 +1,8 @@ +package lengthof_error; + +struct LengthOfError +{ + uint32 offsetArray[]; +lengthof(offsetArray): + string field; +}; diff --git a/test/errors/offsets_error/zs/literal_error.zs b/test/errors/offsets_error/zs/literal_error.zs new file mode 100644 index 000000000..cc7c06eeb --- /dev/null +++ b/test/errors/offsets_error/zs/literal_error.zs @@ -0,0 +1,7 @@ +package literal_error; + +struct LiteralError +{ +13: + string field; +}; diff --git a/test/errors/offsets_error/zs/logical_and_operator_error.zs b/test/errors/offsets_error/zs/logical_and_operator_error.zs new file mode 100644 index 000000000..f8b3a35e8 --- /dev/null +++ b/test/errors/offsets_error/zs/logical_and_operator_error.zs @@ -0,0 +1,8 @@ +package logical_and_operator_error; + +struct LogicalAndOperatorError +{ + bool offset; +offset && true: + string field; +}; diff --git a/test/errors/offsets_error/zs/logical_or_operator_error.zs b/test/errors/offsets_error/zs/logical_or_operator_error.zs new file mode 100644 index 000000000..62065e133 --- /dev/null +++ b/test/errors/offsets_error/zs/logical_or_operator_error.zs @@ -0,0 +1,8 @@ +package logical_or_operator_error; + +struct LogicalOrOperatorError +{ + bool offset; +offset || true: + string field; +}; diff --git a/test/errors/offsets_error/zs/numbits_error.zs b/test/errors/offsets_error/zs/numbits_error.zs new file mode 100644 index 000000000..63ed78186 --- /dev/null +++ b/test/errors/offsets_error/zs/numbits_error.zs @@ -0,0 +1,9 @@ +package numbits_error; + + +struct NumBitsError +{ + uint8 value; +numbits(value): + string field; +}; diff --git a/test/errors/offsets_error/zs/parenthesis_error.zs b/test/errors/offsets_error/zs/parenthesis_error.zs new file mode 100644 index 000000000..f3ac23ccb --- /dev/null +++ b/test/errors/offsets_error/zs/parenthesis_error.zs @@ -0,0 +1,8 @@ +package parenthesis_error; + +struct ParenthesisError +{ + uint32 offset; +(offset): + string field; +}; diff --git a/test/errors/offsets_error/zs/relational_operator_error.zs b/test/errors/offsets_error/zs/relational_operator_error.zs new file mode 100644 index 000000000..071800a8d --- /dev/null +++ b/test/errors/offsets_error/zs/relational_operator_error.zs @@ -0,0 +1,8 @@ +package relational_operator_error; + +struct RelationalOperatorError +{ + uint32 offset; +offset > 10: + string field; +}; diff --git a/test/errors/offsets_error/zs/shift_operator_error.zs b/test/errors/offsets_error/zs/shift_operator_error.zs new file mode 100644 index 000000000..4002c84de --- /dev/null +++ b/test/errors/offsets_error/zs/shift_operator_error.zs @@ -0,0 +1,8 @@ +package shift_operator_error; + +struct ShiftOperatorError +{ + uint32 offset; +offset << 2: + string field; +}; diff --git a/test/errors/offsets_error/zs/unary_operator_error.zs b/test/errors/offsets_error/zs/unary_operator_error.zs new file mode 100644 index 000000000..b379bf37b --- /dev/null +++ b/test/errors/offsets_error/zs/unary_operator_error.zs @@ -0,0 +1,8 @@ +package unary_operator_error; + +struct UnaryOperatorError +{ + uint32 offset; ++offset: + string field; +}; diff --git a/test/errors/offsets_error/zs/valueof_error.zs b/test/errors/offsets_error/zs/valueof_error.zs new file mode 100644 index 000000000..be0464296 --- /dev/null +++ b/test/errors/offsets_error/zs/valueof_error.zs @@ -0,0 +1,13 @@ +package valueof_error; + +enum uint32 Enum +{ + ONE, + TWO +}; + +struct ValueOfError +{ +valueof(Enum.ONE): + string field; +}; diff --git a/test/language/indexed_offsets/ClangTidySuppressions.txt b/test/language/indexed_offsets/ClangTidySuppressions.txt index 0f1f71c62..f36a562c3 100644 --- a/test/language/indexed_offsets/ClangTidySuppressions.txt +++ b/test/language/indexed_offsets/ClangTidySuppressions.txt @@ -7,5 +7,6 @@ performance-move-const-arg:gen/indexed_offsets/int14_indexed_offset_array/Int14I readability-make-member-function-const:gen/indexed_offsets/compound_indexed_offset_array/Compound.cpp readability-make-member-function-const:gen/indexed_offsets/optional_nested_indexed_offset_array/Header.cpp +readability-make-member-function-const:gen/indexed_offsets/packed_indexed_offset_array_holder/OffsetHolder.cpp readability-simplify-boolean-expr:gen/indexed_offsets/optional_indexed_offset_array/OptionalIndexedOffsetArray.cpp diff --git a/test/language/indexed_offsets/cpp/PackedIndexedOffsetArrayHolderTest.cpp b/test/language/indexed_offsets/cpp/PackedIndexedOffsetArrayHolderTest.cpp index 76f9454b4..caccac59b 100644 --- a/test/language/indexed_offsets/cpp/PackedIndexedOffsetArrayHolderTest.cpp +++ b/test/language/indexed_offsets/cpp/PackedIndexedOffsetArrayHolderTest.cpp @@ -18,10 +18,10 @@ class PackedIndexedOffsetArrayHolderTest : public ::testing::Test { auto& offsetArray = autoIndexedOffsetArray.getOffsetArray(); auto& offsetHolders = offsetArray.getOffsetHolders(); - offsetHolders.reserve(numElements + 1); - for (size_t i = 0; i < numElements + 1; ++i) + offsetHolders.reserve(numElements); + for (size_t i = 0; i < numElements; ++i) { - offsetHolders.emplace_back(UINT32_C(0), vector_type{0}, static_cast(i)); + offsetHolders.emplace_back(UINT32_C(0), UINT32_C(0), static_cast(i)); } auto& data1 = autoIndexedOffsetArray.getData1(); @@ -44,10 +44,10 @@ class PackedIndexedOffsetArrayHolderTest : public ::testing::Test size_t getAutoIndexedOffsetArrayBitSize(size_t numElements) { size_t bitSize = 0; - for (size_t i = 0; i < numElements + 1; ++i) + for (size_t i = 0; i < numElements; ++i) { - bitSize += 32; // offset[i] - bitSize += 32; // offsets[1] + bitSize += 32; // offset1[i] + bitSize += 32; // offset2[i] bitSize += 32; // value[i] } @@ -71,7 +71,7 @@ class PackedIndexedOffsetArrayHolderTest : public ::testing::Test const double unpackedBitSize = static_cast(getAutoIndexedOffsetArrayBitSize(numElements)); const double packedBitSize = static_cast(autoIndexedOffsetArray.bitSizeOf()); - const double minCompressionRation = 0.82; + const double minCompressionRation = 0.83; ASSERT_GT(unpackedBitSize * minCompressionRation, packedBitSize) << "Unpacked array has " << std::to_string(unpackedBitSize) << " bits, " << "packed array has " << std::to_string(packedBitSize) << " bits, " diff --git a/test/language/indexed_offsets/java/indexed_offsets/PackedIndexedOffsetArrayHolderTest.java b/test/language/indexed_offsets/java/indexed_offsets/PackedIndexedOffsetArrayHolderTest.java index 0a97805e8..83208417d 100644 --- a/test/language/indexed_offsets/java/indexed_offsets/PackedIndexedOffsetArrayHolderTest.java +++ b/test/language/indexed_offsets/java/indexed_offsets/PackedIndexedOffsetArrayHolderTest.java @@ -78,7 +78,7 @@ private void checkBitSizeOf(int numElements) throws IOException, ZserioError final AutoIndexedOffsetArray autoIndexedOffsetArray = createAutoIndexedOffsetArray(numElements); final int unpackedBitsizeOf = calcAutoIndexedOffsetArrayBitSize(numElements); final int packedBitsizeOf = autoIndexedOffsetArray.bitSizeOf(); - final double minCompressionRatio = 0.82; + final double minCompressionRatio = 0.83; assertTrue(unpackedBitsizeOf * minCompressionRatio > packedBitsizeOf, () @@ -116,9 +116,11 @@ private void checkWriteReadFile(int numElements) throws IOException, ZserioError private AutoIndexedOffsetArray createAutoIndexedOffsetArray(int numElements) { - final OffsetHolder[] offsetHolders = new OffsetHolder[numElements + 1]; - for (int i = 0; i < numElements + 1; ++i) - offsetHolders[i] = new OffsetHolder(0, new long[] {0}, i); + final OffsetHolder[] offsetHolders = new OffsetHolder[numElements]; + for (int i = 0; i < numElements; ++i) + { + offsetHolders[i] = new OffsetHolder(0, 0, i); + } final int[] data1 = new int[numElements]; for (int i = 0; i < numElements; ++i) @@ -138,10 +140,10 @@ private AutoIndexedOffsetArray createAutoIndexedOffsetArray(int numElements) private int calcAutoIndexedOffsetArrayBitSize(int numElements) { int bitSize = 0; - for (int i = 0; i < numElements + 1; ++i) + for (int i = 0; i < numElements; ++i) { - bitSize += 32; // offset[i] - bitSize += 32; // offsets[1] + bitSize += 32; // offset1[i] + bitSize += 32; // offset2[i] bitSize += 32; // value[i] } diff --git a/test/language/indexed_offsets/python/PackedIndexedOffsetArrayHolderTest.py b/test/language/indexed_offsets/python/PackedIndexedOffsetArrayHolderTest.py index bc0b4801e..34cae4d30 100644 --- a/test/language/indexed_offsets/python/PackedIndexedOffsetArrayHolderTest.py +++ b/test/language/indexed_offsets/python/PackedIndexedOffsetArrayHolderTest.py @@ -37,7 +37,7 @@ def _checkBitSizeOf(self, numElements): autoIndexedOffsetArray = self._createAutoIndexedOffsetArray(numElements) unpackedBitsizeOf = PackedIndexedOffsetArrayHolderTest._calcAutoIndexedOffsetArrayBitSize(numElements) packedBitsizeOf = autoIndexedOffsetArray.bitsizeof() - minCompressionRatio = 0.82 + minCompressionRatio = 0.83 self.assertTrue( unpackedBitsizeOf * minCompressionRatio > packedBitsizeOf, "Unpacked array has " @@ -72,8 +72,8 @@ def _checkWriteReadFile(self, numElements): def _createAutoIndexedOffsetArray(self, numElements): offsetHolders = [] - for i in range(numElements + 1): - offsetHolders.append(self.api.OffsetHolder(0, [0], i)) + for i in range(numElements): + offsetHolders.append(self.api.OffsetHolder(0, 0, i)) data1 = [] for i in range(numElements): @@ -88,9 +88,9 @@ def _createAutoIndexedOffsetArray(self, numElements): @staticmethod def _calcAutoIndexedOffsetArrayBitSize(numElements): bitSize = 0 - for _ in range(numElements + 1): - bitSize += 32 # offset[i] - bitSize += 32 # offsets[1] + for _ in range(numElements): + bitSize += 32 # offset1[i] + bitSize += 32 # offset2[1] bitSize += 32 # value[i] for _ in range(numElements): bitSize += 32 # data1[i] diff --git a/test/language/indexed_offsets/zs/indexed_offsets/packed_indexed_offset_array_holder.zs b/test/language/indexed_offsets/zs/indexed_offsets/packed_indexed_offset_array_holder.zs index 6412738c0..f34e70a96 100644 --- a/test/language/indexed_offsets/zs/indexed_offsets/packed_indexed_offset_array_holder.zs +++ b/test/language/indexed_offsets/zs/indexed_offsets/packed_indexed_offset_array_holder.zs @@ -7,8 +7,8 @@ struct OffsetArray struct OffsetHolder { - uint32 offset; // this is unpackable because it stores offset - uint32 offsets[1]; // this is unpackable because it stores offset + uint32 offset1; // this is unpackable because it stores offset + uint32 offset2; // this is unpackable because it stores offset uint32 value; // this is packable integer }; @@ -16,9 +16,9 @@ struct AutoIndexedOffsetArray { OffsetArray offsetArray; -offsetArray.offsetHolders[@index + 1].offset: +offsetArray.offsetHolders[@index].offset1: int32 data1[]; -offsetArray.offsetHolders[@index + 1].offsets[0]: +offsetArray.offsetHolders[@index].offset2: int32 data2[]; }; diff --git a/test/language/offsets/CMakeLists.txt b/test/language/offsets/CMakeLists.txt index 507e30634..15b58e7c2 100644 --- a/test/language/offsets/CMakeLists.txt +++ b/test/language/offsets/CMakeLists.txt @@ -24,7 +24,6 @@ add_custom_test(offsets ${CMAKE_CURRENT_SOURCE_DIR}/cpp/OptionalMemberOffsetTest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/cpp/PackedAutoArrayOffsetTest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/cpp/ParameterOffsetTest.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/cpp/TernaryOperatorOffsetTest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/cpp/UInt64OffsetTest.cpp GENERATED_SOURCES ${GENERATED_SOURCES} diff --git a/test/language/offsets/cpp/TernaryOperatorOffsetTest.cpp b/test/language/offsets/cpp/TernaryOperatorOffsetTest.cpp deleted file mode 100644 index 164e07787..000000000 --- a/test/language/offsets/cpp/TernaryOperatorOffsetTest.cpp +++ /dev/null @@ -1,146 +0,0 @@ -#include "gtest/gtest.h" -#include "offsets/ternary_operator_offset/TernaryOffset.h" -#include "zserio/BitStreamReader.h" -#include "zserio/BitStreamWriter.h" -#include "zserio/CppRuntimeException.h" - -namespace offsets -{ -namespace ternary_operator_offset -{ - -using allocator_type = TernaryOffset::allocator_type; -template -using vector_type = zserio::vector; - -class TernaryOperatorOffsetTest : public ::testing::Test -{ -protected: - void writeTernaryOffsetToByteArray( - zserio::BitStreamWriter& writer, bool isFirstOffsetUsed, bool writeWrongOffset) - { - writer.writeBool(isFirstOffsetUsed); - if (isFirstOffsetUsed) - { - writer.writeBits((writeWrongOffset) ? WRONG_FIELD_OFFSET : FIELD_OFFSET, 32); - writer.writeBits(WRONG_FIELD_OFFSET, 32); - } - else - { - writer.writeBits(WRONG_FIELD_OFFSET, 32); - writer.writeBits((writeWrongOffset) ? WRONG_FIELD_OFFSET : FIELD_OFFSET, 32); - } - writer.writeSignedBits(FIELD_VALUE, 32); - } - - void checkTernaryOffset(const TernaryOffset& ternaryOffset, bool isFirstOffsetUsed) - { - ASSERT_EQ(isFirstOffsetUsed, ternaryOffset.getIsFirstOffsetUsed()); - if (isFirstOffsetUsed) - { - ASSERT_EQ(FIELD_OFFSET, ternaryOffset.getOffsets()[0]); - ASSERT_EQ(WRONG_FIELD_OFFSET, ternaryOffset.getOffsets()[1]); - } - else - { - ASSERT_EQ(WRONG_FIELD_OFFSET, ternaryOffset.getOffsets()[0]); - ASSERT_EQ(FIELD_OFFSET, ternaryOffset.getOffsets()[1]); - } - ASSERT_EQ(FIELD_VALUE, ternaryOffset.getValue()); - } - - void fillTernaryOffset(TernaryOffset& ternaryOffset, bool isFirstOffsetUsed, bool createWrongOffset) - { - ternaryOffset.setIsFirstOffsetUsed(isFirstOffsetUsed); - const vector_type offsets = {WRONG_FIELD_OFFSET, WRONG_FIELD_OFFSET}; - ternaryOffset.setOffsets(offsets); - ternaryOffset.setValue(FIELD_VALUE); - if (!createWrongOffset) - { - ternaryOffset.initializeOffsets(); - } - } - - void testOffset(bool isFirstOffsetUsed) - { - TernaryOffset ternaryOffset; - const bool writeWrongOffset = false; - fillTernaryOffset(ternaryOffset, isFirstOffsetUsed, writeWrongOffset); - - zserio::BitStreamWriter writer(bitBuffer); - ternaryOffset.write(writer); - - zserio::BitStreamReader reader(writer.getWriteBuffer(), writer.getBitPosition(), zserio::BitsTag()); - const TernaryOffset readTernaryOffset(reader); - checkTernaryOffset(readTernaryOffset, isFirstOffsetUsed); - } - - void testOffsetWriteWrong(bool isFirstOffsetUsed) - { - TernaryOffset ternaryOffset; - const bool writeWrongOffset = true; - fillTernaryOffset(ternaryOffset, isFirstOffsetUsed, writeWrongOffset); - - zserio::BitStreamWriter writer(bitBuffer); - EXPECT_THROW(ternaryOffset.write(writer), zserio::CppRuntimeException); - } - - void testOffsetReadWrong(bool isFirstOffsetUsed) - { - zserio::BitStreamWriter writer(bitBuffer); - const bool writeWrongOffset = true; - writeTernaryOffsetToByteArray(writer, isFirstOffsetUsed, writeWrongOffset); - - zserio::BitStreamReader reader(writer.getWriteBuffer(), writer.getBitPosition(), zserio::BitsTag()); - EXPECT_THROW(TernaryOffset ternaryOffset(reader), zserio::CppRuntimeException); - } - - static const uint32_t WRONG_FIELD_OFFSET; - static const uint32_t FIELD_OFFSET; - static const int32_t FIELD_VALUE; - - zserio::BitBuffer bitBuffer = zserio::BitBuffer(1024 * 8); -}; - -const uint32_t TernaryOperatorOffsetTest::WRONG_FIELD_OFFSET = 0; -const uint32_t TernaryOperatorOffsetTest::FIELD_OFFSET = (1 + 32 + 32 + /* align */ +7) / 8; -const int32_t TernaryOperatorOffsetTest::FIELD_VALUE = 0xABCD; - -TEST_F(TernaryOperatorOffsetTest, firstOffset) -{ - const bool isFirstOffsetUsed = true; - testOffset(isFirstOffsetUsed); -} - -TEST_F(TernaryOperatorOffsetTest, firstOffsetWriteWrong) -{ - const bool isFirstOffsetUsed = true; - testOffsetWriteWrong(isFirstOffsetUsed); -} - -TEST_F(TernaryOperatorOffsetTest, firstOffsetReadWrong) -{ - const bool isFirstOffsetUsed = true; - testOffsetReadWrong(isFirstOffsetUsed); -} - -TEST_F(TernaryOperatorOffsetTest, secondOffset) -{ - const bool isFirstOffsetUsed = false; - testOffset(isFirstOffsetUsed); -} - -TEST_F(TernaryOperatorOffsetTest, secondOffsetWriteWrong) -{ - const bool isFirstOffsetUsed = false; - testOffsetWriteWrong(isFirstOffsetUsed); -} - -TEST_F(TernaryOperatorOffsetTest, secondOffsetReadWrong) -{ - const bool isFirstOffsetUsed = false; - testOffsetReadWrong(isFirstOffsetUsed); -} - -} // namespace ternary_operator_offset -} // namespace offsets diff --git a/test/language/offsets/java/offsets/TernaryOperatorOffsetTest.java b/test/language/offsets/java/offsets/TernaryOperatorOffsetTest.java deleted file mode 100644 index a95f529b6..000000000 --- a/test/language/offsets/java/offsets/TernaryOperatorOffsetTest.java +++ /dev/null @@ -1,146 +0,0 @@ -package offsets; - -import static org.junit.jupiter.api.Assertions.*; - -import java.io.IOException; - -import org.junit.jupiter.api.Test; - -import zserio.runtime.ZserioError; -import zserio.runtime.io.BitBuffer; -import zserio.runtime.io.BitStreamReader; -import zserio.runtime.io.BitStreamWriter; -import zserio.runtime.io.ByteArrayBitStreamReader; -import zserio.runtime.io.ByteArrayBitStreamWriter; - -import offsets.ternary_operator_offset.TernaryOffset; - -public class TernaryOperatorOffsetTest -{ - @Test - public void firstOffset() throws IOException, ZserioError - { - final boolean isFirstOffsetUsed = true; - testOffset(isFirstOffsetUsed); - } - - @Test - public void firstOffsetWriteWrong() throws IOException, ZserioError - { - final boolean isFirstOffsetUsed = true; - testOffsetWriteWrong(isFirstOffsetUsed); - } - - @Test - public void firstOffsetReadWrong() throws IOException, ZserioError - { - final boolean isFirstOffsetUsed = true; - testOffsetReadWrong(isFirstOffsetUsed); - } - - @Test - public void secondOffset() throws IOException, ZserioError - { - final boolean isFirstOffsetUsed = false; - testOffset(isFirstOffsetUsed); - } - - @Test - public void secondOffsetWriteWrong() throws IOException, ZserioError - { - final boolean isFirstOffsetUsed = false; - testOffsetWriteWrong(isFirstOffsetUsed); - } - - @Test - public void secondOffsetReadWrong() throws IOException, ZserioError - { - final boolean isFirstOffsetUsed = false; - testOffsetReadWrong(isFirstOffsetUsed); - } - - private BitBuffer writeTernaryOffsetToBitBuffer(boolean isFirstOffsetUsed, boolean writeWrongOffset) - throws IOException - { - try (final ByteArrayBitStreamWriter writer = new ByteArrayBitStreamWriter()) - { - writer.writeBool(isFirstOffsetUsed); - if (isFirstOffsetUsed) - { - writer.writeBits((writeWrongOffset) ? WRONG_FIELD_OFFSET : FIELD_OFFSET, 32); - writer.writeBits(WRONG_FIELD_OFFSET, 32); - } - else - { - writer.writeBits(WRONG_FIELD_OFFSET, 32); - writer.writeBits((writeWrongOffset) ? WRONG_FIELD_OFFSET : FIELD_OFFSET, 32); - } - writer.writeSignedBits(FIELD_VALUE, 32); - - return new BitBuffer(writer.toByteArray(), writer.getBitPosition()); - } - } - - private void checkTernaryOffset(TernaryOffset ternaryOffset, boolean isFirstOffsetUsed) - { - assertEquals(isFirstOffsetUsed, ternaryOffset.getIsFirstOffsetUsed()); - if (isFirstOffsetUsed) - { - assertEquals(FIELD_OFFSET, ternaryOffset.getOffsets()[0]); - assertEquals(WRONG_FIELD_OFFSET, ternaryOffset.getOffsets()[1]); - } - else - { - assertEquals(WRONG_FIELD_OFFSET, ternaryOffset.getOffsets()[0]); - assertEquals(FIELD_OFFSET, ternaryOffset.getOffsets()[1]); - } - assertEquals(FIELD_VALUE, ternaryOffset.getValue()); - } - - private TernaryOffset createTernaryOffset(boolean isFirstOffsetUsed, boolean createWrongOffset) - { - final TernaryOffset ternaryOffset = new TernaryOffset( - isFirstOffsetUsed, new long[] {WRONG_FIELD_OFFSET, WRONG_FIELD_OFFSET}, FIELD_VALUE); - if (!createWrongOffset) - ternaryOffset.initializeOffsets(); - - return ternaryOffset; - } - - private void testOffset(boolean isFirstOffsetUsed) throws IOException, ZserioError - { - final boolean writeWrongOffset = false; - final TernaryOffset ternaryOffset = createTernaryOffset(isFirstOffsetUsed, writeWrongOffset); - - final ByteArrayBitStreamWriter writer = new ByteArrayBitStreamWriter(); - ternaryOffset.write(writer); - - final BitStreamReader reader = - new ByteArrayBitStreamReader(writer.toByteArray(), writer.getBitPosition()); - final TernaryOffset readTernaryOffset = new TernaryOffset(reader); - checkTernaryOffset(readTernaryOffset, isFirstOffsetUsed); - } - - private void testOffsetWriteWrong(boolean isFirstOffsetUsed) throws IOException, ZserioError - { - final boolean writeWrongOffset = true; - final TernaryOffset ternaryOffset = createTernaryOffset(isFirstOffsetUsed, writeWrongOffset); - - final BitStreamWriter writer = new ByteArrayBitStreamWriter(); - assertThrows(ZserioError.class, () -> ternaryOffset.write(writer)); - writer.close(); - } - - private void testOffsetReadWrong(boolean isFirstOffsetUsed) throws IOException, ZserioError - { - final boolean writeWrongOffset = true; - final BitBuffer bitBuffer = writeTernaryOffsetToBitBuffer(isFirstOffsetUsed, writeWrongOffset); - - final BitStreamReader reader = new ByteArrayBitStreamReader(bitBuffer); - assertThrows(ZserioError.class, () -> new TernaryOffset(reader)); - } - - private static final long WRONG_FIELD_OFFSET = 0; - private static final long FIELD_OFFSET = (1 + 32 + 32 + /* align */ +7) / 8; - private static final int FIELD_VALUE = 0xABCD; -} diff --git a/test/language/offsets/python/TernaryOperatorOffsetTest.py b/test/language/offsets/python/TernaryOperatorOffsetTest.py deleted file mode 100644 index 0f044b622..000000000 --- a/test/language/offsets/python/TernaryOperatorOffsetTest.py +++ /dev/null @@ -1,92 +0,0 @@ -import zserio - -import Offsets - - -class TernaryOperatorOffsetTest(Offsets.TestCase): - def testFirstOffset(self): - isFirstOffsetUsed = True - self._testOffset(isFirstOffsetUsed) - - def testFirstOffsetWriteWrong(self): - isFirstOffsetUsed = True - self._testOffsetWriteWrong(isFirstOffsetUsed) - - def testFirstOffsetReadWrong(self): - isFirstOffsetUsed = True - self._testOffsetReadWrong(isFirstOffsetUsed) - - def testSecondOffset(self): - isFirstOffsetUsed = False - self._testOffset(isFirstOffsetUsed) - - def testSecondOffsetWriteWrong(self): - isFirstOffsetUsed = False - self._testOffsetWriteWrong(isFirstOffsetUsed) - - def testSecondOffsetReadWrong(self): - isFirstOffsetUsed = False - self._testOffsetReadWrong(isFirstOffsetUsed) - - def _writeTernaryOffsetToStream(self, writer, isFirstOffsetUsed, writeWrongOffset): - writer.write_bool(isFirstOffsetUsed) - if isFirstOffsetUsed: - writer.write_bits(self.WRONG_FIELD_OFFSET if writeWrongOffset else self.FIELD_OFFSET, 32) - writer.write_bits(self.WRONG_FIELD_OFFSET, 32) - else: - writer.write_bits(self.WRONG_FIELD_OFFSET, 32) - writer.write_bits(self.WRONG_FIELD_OFFSET if writeWrongOffset else self.FIELD_OFFSET, 32) - writer.write_signed_bits(self.FIELD_VALUE, 32) - - def _checkTernaryOffset(self, ternaryOffset, isFirstOffsetUsed): - self.assertEqual(isFirstOffsetUsed, ternaryOffset.is_first_offset_used) - if isFirstOffsetUsed: - self.assertEqual(self.FIELD_OFFSET, ternaryOffset.offsets[0]) - self.assertEqual(self.WRONG_FIELD_OFFSET, ternaryOffset.offsets[1]) - else: - self.assertEqual(self.WRONG_FIELD_OFFSET, ternaryOffset.offsets[0]) - self.assertEqual(self.FIELD_OFFSET, ternaryOffset.offsets[1]) - - self.assertEqual(self.FIELD_VALUE, ternaryOffset.value) - - def _createTernaryOffset(self, isFirstOffsetUsed, createWrongOffset): - ternaryOffset = self.api.TernaryOffset( - isFirstOffsetUsed, [self.WRONG_FIELD_OFFSET, self.WRONG_FIELD_OFFSET], self.FIELD_VALUE - ) - if not createWrongOffset: - ternaryOffset.initialize_offsets() - - return ternaryOffset - - def _testOffset(self, isFirstOffsetUsed): - writeWrongOffset = False - ternaryOffset = self._createTernaryOffset(isFirstOffsetUsed, writeWrongOffset) - - writer = zserio.BitStreamWriter() - ternaryOffset.write(writer) - - reader = zserio.BitStreamReader(writer.byte_array, writer.bitposition) - readTernaryOffset = self.api.TernaryOffset.from_reader(reader) - - self._checkTernaryOffset(readTernaryOffset, isFirstOffsetUsed) - - def _testOffsetWriteWrong(self, isFirstOffsetUsed): - writeWrongOffset = True - ternaryOffset = self._createTernaryOffset(isFirstOffsetUsed, writeWrongOffset) - - writer = zserio.BitStreamWriter() - with self.assertRaises(zserio.PythonRuntimeException): - ternaryOffset.write(writer) - - def _testOffsetReadWrong(self, isFirstOffsetUsed): - writer = zserio.BitStreamWriter() - writeWrongOffset = True - self._writeTernaryOffsetToStream(writer, isFirstOffsetUsed, writeWrongOffset) - - reader = zserio.BitStreamReader(writer.byte_array, writer.bitposition) - with self.assertRaises(zserio.PythonRuntimeException): - self.api.TernaryOffset.from_reader(reader) - - WRONG_FIELD_OFFSET = 0 - FIELD_OFFSET = (1 + 32 + 32 + 7) / 8 - FIELD_VALUE = 0xABCD diff --git a/test/language/offsets/zs/offsets.zs b/test/language/offsets/zs/offsets.zs index 35bf9f9f4..c82a80782 100644 --- a/test/language/offsets/zs/offsets.zs +++ b/test/language/offsets/zs/offsets.zs @@ -6,5 +6,4 @@ import offsets.nested_offset.*; import offsets.optional_member_offset.*; import offsets.packed_auto_array_offset.*; import offsets.parameter_offset.*; -import offsets.ternary_operator_offset.*; import offsets.uint64_offset.*; diff --git a/test/language/offsets/zs/offsets/ternary_operator_offset.zs b/test/language/offsets/zs/offsets/ternary_operator_offset.zs deleted file mode 100644 index f45f6a2b1..000000000 --- a/test/language/offsets/zs/offsets/ternary_operator_offset.zs +++ /dev/null @@ -1,19 +0,0 @@ -package offsets.ternary_operator_offset; - -struct TernaryOffset -{ - bool isFirstOffsetUsed; - uint32 offsets[2]; -offsets[isFirstOffsetUsed ? firstIndex() : secondIndex()]: - int32 value; - - function varsize firstIndex() - { - return 0; - } - - function varsize secondIndex() - { - return 1; - } -};