1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jexl3;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20
21 import java.math.BigInteger;
22
23 import org.apache.commons.jexl3.junit.Asserter;
24 import org.junit.jupiter.api.Test;
25
26
27
28
29 @SuppressWarnings({"UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes"})
30 class ShiftOperatorsTest extends JexlTestCase {
31 public static class ShiftArithmetic extends JexlArithmetic {
32 ShiftArithmetic(final boolean flag) {
33 super(flag);
34 }
35
36 public Object shiftLeft(final StringBuilder c, final String value) {
37 c.append(value);
38 return c;
39 }
40
41 public Object shiftRight(final String value, final StringBuilder c) {
42 c.append(value);
43 return c;
44 }
45
46 public Object shiftRightUnsigned(final String value, final StringBuilder c) {
47 c.append(value.toLowerCase());
48 return c;
49 }
50 }
51
52 static BigInteger shiftRightUnsigned(final BigInteger bl, final int r) {
53 return bl.signum() < 0 ? bl.negate().shiftRight(r) : bl.shiftRight(r);
54 }
55
56 static BigInteger shiftRightUnsigned(final String bl, final int r) {
57 return shiftRightUnsigned(new BigInteger(bl), r);
58 }
59
60 private final Asserter asserter;
61
62 private final Asserter a360;
63
64 public ShiftOperatorsTest() {
65 super("ShiftOperatorsTest");
66 asserter = new Asserter(JEXL);
67 asserter.setStrict(false, false);
68
69 final JexlEngine j360 = new JexlBuilder().arithmetic(new Arithmetic360(true)).strict(true).create();
70 a360 = new Asserter(j360);
71 a360.setStrict(false, false);
72 }
73
74 @Test
75 void testLeftShiftIntValue() throws Exception {
76 final String expr = "(x, y)-> x << y";
77 asserter.assertExpression(expr, 1L << 2, 1L, 2);
78 asserter.assertExpression(expr, 1L << -2, 1L, -2);
79 asserter.assertExpression(expr, -1L << 2, -1L, 2);
80 asserter.assertExpression(expr, -1L << -2, -1L, -2);
81
82 a360.assertExpression(expr, 1L << 2, 1L, 2);
83 a360.assertExpression(expr, 1L << -2, 1L, -2);
84 a360.assertExpression(expr, -1L << 2, -1L, 2);
85 a360.assertExpression(expr, -1L << -2, -1L, -2);
86
87 a360.assertExpression(expr, 1 << 2, 1, 2);
88 a360.assertExpression(expr, 1 << -2, 1, -2);
89 a360.assertExpression(expr, -1 << 2, -1, 2);
90 a360.assertExpression(expr, -1 << -2, -1, -2);
91 }
92
93 @Test
94 void testLeftShiftLongValue() throws Exception {
95 a360.assertExpression("2147483648 << 2", 2147483648L << 2);
96 a360.assertExpression("2147483648 << -2", 2147483648L << -2);
97 a360.assertExpression("-2147483649 << 2", -2147483649L << 2);
98 a360.assertExpression("-2147483649 << -2", -2147483649L << -2);
99 }
100
101 @Test
102 void testOverloadedShift() throws Exception {
103 final JexlEngine jexl = new JexlBuilder().arithmetic(new ShiftArithmetic(true)).create();
104 StringBuilder x;
105 JexlScript e;
106 Object o;
107
108 x = new StringBuilder("1");
109 e = jexl.createScript("x << 'Left'", "x");
110 o = e.execute(null, x);
111 assertEquals("1Left", o.toString(), e::getSourceText);
112
113 e = jexl.createScript("'Right' >> x", "x");
114 x = new StringBuilder("1");
115 o = e.execute(null, x);
116 assertEquals("1Right", x.toString(), e::getSourceText);
117
118 e = jexl.createScript("'Right' >>> x", "x");
119 x = new StringBuilder("1");
120 o = e.execute(null, x);
121 assertEquals("1right", x.toString(), e::getSourceText);
122 }
123
124 @Test
125 void testPrecedence() throws Exception {
126 a360.assertExpression("40 + 2 << 1 + 1", 40 + 2 << 1 + 1);
127 a360.assertExpression("40 + (2 << 1) + 1", 40 + (2 << 1) + 1);
128 a360.assertExpression("(40 + 2) << (1 + 1)", 40 + 2 << 1 + 1);
129
130 a360.assertExpression("40 + 2L << 1 + 1", 40 + 2L << 1 + 1);
131 a360.assertExpression("40 + (2L << 1) + 1", 40 + (2L << 1) + 1);
132 a360.assertExpression("(40 + 2L) << (1 + 1)", 40 + 2L << 1 + 1);
133
134 a360.assertExpression("40L + 2 << 1 + 1", 40L + 2L << 1 + 1);
135 a360.assertExpression("40L + (2 << 1) + 1", 40L + (2L << 1) + 1);
136 a360.assertExpression("(40L + 2) << (1 + 1)", 40L + 2L << 1 + 1);
137 }
138
139 @Test
140 void testRightShiftBigValue() throws Exception {
141 a360.assertExpression("9223372036854775808 >> 2", new BigInteger("9223372036854775808").shiftRight(2));
142 a360.assertExpression("9223372036854775808 >> -2", new BigInteger("9223372036854775808").shiftRight(-2));
143 a360.assertExpression("-9223372036854775809 >> 2", new BigInteger("-9223372036854775809").shiftRight(2));
144 a360.assertExpression("-9223372036854775809 >> -2", new BigInteger("-9223372036854775809").shiftRight(-2));
145 }
146
147 @Test
148 void testRightShiftIntValue() throws Exception {
149 final String expr = "(x, y)-> x >> y";
150 asserter.assertExpression(expr, 42L >> 2, 42L, 2);
151 asserter.assertExpression(expr, 42L >> -2, 42L, -2);
152 asserter.assertExpression(expr, -42L >> 2, -42L, 2);
153 asserter.assertExpression(expr, -42L >> -2, -42L, -2);
154
155 a360.assertExpression(expr, 42L >> 2, 42L, 2);
156 a360.assertExpression(expr, 42L >> -2, 42L, -2);
157 a360.assertExpression(expr, -42L >> 2, -42L, 2);
158 a360.assertExpression(expr, -42L >> -2, -42L, -2);
159
160 a360.assertExpression(expr, 42 >> 2, 42, 2);
161 a360.assertExpression(expr, 42 >> -2, 42, -2);
162 a360.assertExpression(expr, -42 >> 2, -42, 2);
163 a360.assertExpression(expr, -42 >> -2, -42, -2);
164 }
165
166 @Test
167 void testRightShiftLongValue() throws Exception {
168 a360.assertExpression("8589934592 >> 2", 8589934592L >> 2);
169 a360.assertExpression("8589934592 >> -2", 8589934592L >> -2);
170 a360.assertExpression("-8589934592 >> 2", -8589934592L >> 2);
171 a360.assertExpression("-8589934592 >> -2", -8589934592L >> -2);
172 }
173
174 @Test
175 void testRightShiftUnsignedBigValue() throws Exception {
176 a360.assertExpression("9223372036854775808 >>> 2", shiftRightUnsigned("9223372036854775808", 2));
177 a360.assertExpression("9223372036854775808 >>> -2", shiftRightUnsigned("9223372036854775808", -2));
178 a360.assertExpression("-9223372036854775809 >>> 2", shiftRightUnsigned("-9223372036854775809", 2));
179 a360.assertExpression("-9223372036854775809 >>> -2", shiftRightUnsigned("-9223372036854775809", -2));
180 }
181
182 @Test
183 void testRightShiftUnsignedIntValue() throws Exception {
184 final String expr = "(x, y)-> x >>> y";
185 asserter.assertExpression(expr, 42L >>> 2, 42L, 2);
186 asserter.assertExpression(expr, 42L >>> -2, 42L, -2);
187 asserter.assertExpression(expr, -42L >>> 2, -42L, 2);
188 asserter.assertExpression(expr, -42L >>> -2, -42L, -2);
189 }
190
191 }