Skip to content

Commit

Permalink
[#671] Implement left shift operator by multiplication in C++
Browse files Browse the repository at this point in the history
* extend language tests
  • Loading branch information
Mi-La committed Dec 3, 2024
1 parent 32d6a7c commit d9de529
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
Expand Down
2 changes: 1 addition & 1 deletion test/data
1 change: 1 addition & 0 deletions test/extensions/language/expressions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 61 additions & 0 deletions test/extensions/language/expressions/cpp/LeftShiftOperatorTest.cpp
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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())

0 comments on commit d9de529

Please sign in to comment.