diff --git a/compiler/extensions/cpp/src/zserio/extension/cpp/CppExpressionFormattingPolicy.java b/compiler/extensions/cpp/src/zserio/extension/cpp/CppExpressionFormattingPolicy.java index f7d1eaa00..a713b86bc 100644 --- a/compiler/extensions/cpp/src/zserio/extension/cpp/CppExpressionFormattingPolicy.java +++ b/compiler/extensions/cpp/src/zserio/extension/cpp/CppExpressionFormattingPolicy.java @@ -242,6 +242,20 @@ public BinaryExpressionFormatting getIsSet(Expression expr) return new BinaryExpressionFormatting("::zserio::builtin::isSet(", ", ", ")"); } + @Override + public BinaryExpressionFormatting getLeftShift(Expression expr) + { + if (expr.op1().getIntegerValue() != null && + expr.op1().getIntegerValue().compareTo(BigInteger.ZERO) >= 0) + { + return new BinaryExpressionFormatting("(", ") << (", ")"); + } + else + { + return new BinaryExpressionFormatting("(", ") * (1 << (", "))"); + } + } + protected String getAccessPrefixForCompoundType() { return ""; diff --git a/test/data b/test/data index 8b31f84da..fc851cef4 160000 --- a/test/data +++ b/test/data @@ -1 +1 @@ -Subproject commit 8b31f84daacc0ca12f6833f57075937a1128c0ac +Subproject commit fc851cef493687c3e653d442a73e22ea53691b55 diff --git a/test/extensions/language/expressions/CMakeLists.txt b/test/extensions/language/expressions/CMakeLists.txt index 3d264fbfc..179c0081b 100644 --- a/test/extensions/language/expressions/CMakeLists.txt +++ b/test/extensions/language/expressions/CMakeLists.txt @@ -32,6 +32,7 @@ add_custom_test(expressions ${CMAKE_CURRENT_SOURCE_DIR}/cpp/FunctionTypeTest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/cpp/IndexOperatorTest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/cpp/IsSetOperatorTest.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/cpp/LeftShiftOperatorTest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/cpp/LengthOfOperatorTest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/cpp/ModuloOperatorTest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/cpp/NegationOperatorTest.cpp diff --git a/test/extensions/language/expressions/ClangTidySuppressions.txt b/test/extensions/language/expressions/ClangTidySuppressions.txt index 381210716..3d21ceaeb 100644 --- a/test/extensions/language/expressions/ClangTidySuppressions.txt +++ b/test/extensions/language/expressions/ClangTidySuppressions.txt @@ -15,6 +15,8 @@ google-build-using-namespace:cpp/StringTypeTest.cpp google-explicit-constructor:gen/expressions/bitmask_type/Colors.h google-explicit-constructor:gen/expressions/isset_operator/TestBitmask.h +hicpp-signed-bitwise:gen/expressions/left_shift_operator/LeftShiftOperator.cpp + performance-move-const-arg:gen/expressions/field_type/FieldTypeExpression.cpp performance-move-const-arg:gen/expressions/field_type_with_clash/FieldTypeExpression.cpp performance-move-const-arg:gen/expressions/index_operator/ElementList.cpp diff --git a/test/extensions/language/expressions/cpp/LeftShiftOperatorTest.cpp b/test/extensions/language/expressions/cpp/LeftShiftOperatorTest.cpp new file mode 100644 index 000000000..454b8d8f6 --- /dev/null +++ b/test/extensions/language/expressions/cpp/LeftShiftOperatorTest.cpp @@ -0,0 +1,61 @@ +#include "expressions/left_shift_operator/LeftShiftOperator.h" +#include "gtest/gtest.h" + +namespace expressions +{ +namespace left_shift_operator +{ + +TEST(LeftShiftOperatorTest, defaultValues) +{ + LeftShiftOperator leftShiftOperator; + EXPECT_EQ(40, leftShiftOperator.getU32()); + EXPECT_EQ(-40, leftShiftOperator.getI32()); + EXPECT_EQ(32, leftShiftOperator.getU32Complex()); + EXPECT_EQ(-32, leftShiftOperator.getI32Complex()); + EXPECT_EQ(24, leftShiftOperator.getU32Plus()); + EXPECT_EQ(-64, leftShiftOperator.getI32Minus()); + EXPECT_EQ(12, leftShiftOperator.getU32PlusRhsExpr()); + EXPECT_EQ(-24, leftShiftOperator.getI32MinusRhsExpr()); + EXPECT_EQ(11534336, leftShiftOperator.getU63Complex()); + EXPECT_EQ(-9216, leftShiftOperator.getI64Complex()); +} + +TEST(LeftShiftOperatorTest, getU63LShift3) +{ + LeftShiftOperator leftShiftOperator; + ASSERT_EQ(104, leftShiftOperator.funcGetU63LShift3()); +} + +TEST(LeftShiftOperatorTest, getI64LShift4) +{ + LeftShiftOperator leftShiftOperator; + ASSERT_EQ(-208, leftShiftOperator.funcGetI64LShift4()); +} + +TEST(LeftShiftOperatorTest, getU63LShift) +{ + LeftShiftOperator leftShiftOperator; + ASSERT_EQ(13312, leftShiftOperator.funcGetU63LShift()); +} + +TEST(LeftShiftOperatorTest, getI64LShift) +{ + LeftShiftOperator leftShiftOperator; + ASSERT_EQ(-13312, leftShiftOperator.funcGetI64LShift()); +} + +TEST(LeftShiftOperatorTest, getPositiveI32LShift) +{ + LeftShiftOperator leftShiftOperator; + ASSERT_EQ(13312, leftShiftOperator.funcGetPositiveI32LShift()); +} + +TEST(LeftShiftOperatorTest, getI64ComplexLShift) +{ + LeftShiftOperator leftShiftOperator; + ASSERT_EQ(-3072, leftShiftOperator.funcGetI64ComplexLShift()); +} + +} // namespace left_shift_operator +} // namespace expressions diff --git a/test/extensions/language/expressions/java/expressions/LeftShiftOperatorTest.java b/test/extensions/language/expressions/java/expressions/LeftShiftOperatorTest.java new file mode 100644 index 000000000..9d7a507f8 --- /dev/null +++ b/test/extensions/language/expressions/java/expressions/LeftShiftOperatorTest.java @@ -0,0 +1,68 @@ +package expressions; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +import expressions.left_shift_operator.LeftShiftOperator; + +public class LeftShiftOperatorTest +{ + @Test + public void defaultValues() + { + final LeftShiftOperator leftShiftOperator = new LeftShiftOperator(); + assertEquals(40, leftShiftOperator.getU32()); + assertEquals(-40, leftShiftOperator.getI32()); + assertEquals(32, leftShiftOperator.getU32Complex()); + assertEquals(-32, leftShiftOperator.getI32Complex()); + assertEquals(24, leftShiftOperator.getU32Plus()); + assertEquals(-64, leftShiftOperator.getI32Minus()); + assertEquals(12, leftShiftOperator.getU32PlusRhsExpr()); + assertEquals(-24, leftShiftOperator.getI32MinusRhsExpr()); + assertEquals(11534336, leftShiftOperator.getU63Complex()); + assertEquals(-9216, leftShiftOperator.getI64Complex()); + } + + @Test + public void getU63LShift3() + { + final LeftShiftOperator leftShiftOperator = new LeftShiftOperator(); + assertEquals(104, leftShiftOperator.funcGetU63LShift3()); + } + + @Test + public void getI64LShift4() + { + final LeftShiftOperator leftShiftOperator = new LeftShiftOperator(); + assertEquals(-208, leftShiftOperator.funcGetI64LShift4()); + } + + @Test + public void getU63LShift() + { + final LeftShiftOperator leftShiftOperator = new LeftShiftOperator(); + assertEquals(13312, leftShiftOperator.funcGetU63LShift()); + } + + @Test + public void getI64LShift() + { + final LeftShiftOperator leftShiftOperator = new LeftShiftOperator(); + assertEquals(-13312, leftShiftOperator.funcGetI64LShift()); + } + + @Test + public void getPositiveI32LShift() + { + final LeftShiftOperator leftShiftOperator = new LeftShiftOperator(); + assertEquals(13312, leftShiftOperator.funcGetPositiveI32LShift()); + } + + @Test + public void getI64ComplexLShift() + { + final LeftShiftOperator leftShiftOperator = new LeftShiftOperator(); + assertEquals(-3072, leftShiftOperator.funcGetI64ComplexLShift()); + } +} diff --git a/test/extensions/language/expressions/python/LeftShiftOperatorTest.py b/test/extensions/language/expressions/python/LeftShiftOperatorTest.py new file mode 100644 index 000000000..7a91be4ae --- /dev/null +++ b/test/extensions/language/expressions/python/LeftShiftOperatorTest.py @@ -0,0 +1,40 @@ +import Expressions + + +class LeftShiftOperatorTest(Expressions.TestCase): + def testDefaultValues(self): + leftShiftOperator = self.api.LeftShiftOperator() + self.assertEqual(40, leftShiftOperator.u32) + self.assertEqual(-40, leftShiftOperator.i32) + self.assertEqual(32, leftShiftOperator.u32_complex) + self.assertEqual(-32, leftShiftOperator.i32_complex) + self.assertEqual(24, leftShiftOperator.u32_plus) + self.assertEqual(-64, leftShiftOperator.i32_minus) + self.assertEqual(12, leftShiftOperator.u32_plus_rhs_expr) + self.assertEqual(-24, leftShiftOperator.i32_minus_rhs_expr) + self.assertEqual(11534336, leftShiftOperator.u63_complex) + self.assertEqual(-9216, leftShiftOperator.i64_complex) + + def testGetU63LShift3(self): + leftShiftOperator = self.api.LeftShiftOperator() + self.assertEqual(104, leftShiftOperator.get_u63l_shift3()) + + def testGetI64LShift4(self): + leftShiftOperator = self.api.LeftShiftOperator() + self.assertEqual(-208, leftShiftOperator.get_i64l_shift4()) + + def testGetU63LShift(self): + leftShiftOperator = self.api.LeftShiftOperator() + self.assertEqual(13312, leftShiftOperator.get_u63l_shift()) + + def testGetI64LShift(self): + leftShiftOperator = self.api.LeftShiftOperator() + self.assertEqual(-13312, leftShiftOperator.get_i64l_shift()) + + def testGetPositiveI32LShift(self): + leftShiftOperator = self.api.LeftShiftOperator() + self.assertEqual(13312, leftShiftOperator.get_positive_i32l_shift()) + + def testGetI64ComplexLShift(self): + leftShiftOperator = self.api.LeftShiftOperator() + self.assertEqual(-3072, leftShiftOperator.get_i64_complex_l_shift())