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 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
22 import static org.junit.jupiter.api.Assertions.assertNotSame;
23
24 import java.util.Collection;
25
26 import org.junit.jupiter.api.Test;
27
28
29
30
31 @SuppressWarnings({"UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes"})
32 class RangeTest extends JexlTestCase {
33
34 public RangeTest() {
35 super("RangeTest");
36 }
37
38 @Test
39 void testIntegerContains() throws Exception {
40 final JexlScript e = JEXL.createScript("(x)->{ x =~ (1..10) }");
41 final JexlContext jc = new MapContext();
42
43 Object o = e.execute(jc, 5);
44 assertEquals(Boolean.TRUE, o);
45 o = e.execute(jc, 0);
46 assertEquals(Boolean.FALSE, o);
47 o = e.execute(jc, 100);
48 assertEquals(Boolean.FALSE, o);
49 }
50
51 @Test
52 void testIntegerRange() throws Exception {
53 final JexlExpression e = JEXL.createExpression("(1..32)");
54 final JexlContext jc = new MapContext();
55
56 final Object o0 = e.evaluate(jc);
57 final Object o = e.evaluate(jc);
58 assertInstanceOf(Collection.class, o);
59 final Collection<?> c = (Collection<?>) o;
60 assertEquals(32, c.size());
61
62 assertNotSame(o0, o);
63 assertEquals(o0.hashCode(), o.hashCode());
64 assertEquals(o0, o);
65
66 int i = 0;
67 for (final Object v : c) {
68 i += 1;
69 assertEquals(i, ((Number) v).intValue());
70 }
71 assertEquals(32, i);
72
73 Integer[] aa = c.<Integer>toArray(new Integer[32]);
74 assertEquals(32, aa.length);
75 for (int l = 0; l < 32; ++l) {
76 assertEquals((int) aa[l], l + 1);
77 }
78
79 aa = c.<Integer>toArray(new Integer[2]);
80 assertEquals(32, aa.length);
81 for (int l = 0; l < 32; ++l) {
82 assertEquals((int) aa[l], l + 1);
83 }
84
85 aa = c.<Integer>toArray(new Integer[34]);
86 assertEquals(34, aa.length);
87 for (int l = 0; l < 32; ++l) {
88 assertEquals((int) aa[l], l + 1);
89 }
90
91 final Object[] oaa = c.toArray();
92 assertEquals(32, oaa.length);
93 for (int l = 0; l < 32; ++l) {
94 assertEquals(oaa[l], l + 1);
95 }
96 }
97
98 @Test
99 void testIntegerRangeOne() throws Exception {
100 final JexlExpression e = JEXL.createExpression("(1..1)");
101 final JexlContext jc = new MapContext();
102
103 final Object o = e.evaluate(jc);
104 assertInstanceOf(Collection.class, o);
105 final Collection<?> c = (Collection<?>) o;
106 assertEquals(1, c.size());
107 final Object[] a = c.toArray();
108 assertEquals(1, a.length);
109 assertEquals(1, ((Number) a[0]).intValue());
110 assertFalse((Boolean) JEXL.createScript("empty x", "x").execute(null, e));
111 }
112
113 @Test
114 void testIntegerSum() throws Exception {
115 final JexlScript e = JEXL.createScript("var s = 0; for(var i : (1..5)) { s = s + i; }; s");
116 final JexlContext jc = new MapContext();
117
118 final Object o = e.execute(jc);
119 assertEquals(15, ((Number) o).intValue());
120 }
121
122 @Test
123 void testLongContains() throws Exception {
124 final JexlScript e = JEXL.createScript("(x)->{ x =~ (90000000001L..90000000010L) }");
125 final JexlContext jc = new MapContext();
126
127 Object o = e.execute(jc, 90000000005L);
128 assertEquals(Boolean.TRUE, o);
129 o = e.execute(jc, 0);
130 assertEquals(Boolean.FALSE, o);
131 o = e.execute(jc, 90000000011L);
132 assertEquals(Boolean.FALSE, o);
133 }
134
135 @Test
136 void testLongRange() throws Exception {
137 final JexlExpression e = JEXL.createExpression("(6789000001L..6789000032L)");
138 final JexlContext jc = new MapContext();
139
140 final Object o0 = e.evaluate(jc);
141 final Object o = e.evaluate(jc);
142 assertInstanceOf(Collection.class, o);
143 final Collection<?> c = (Collection<?>) o;
144 assertEquals(32, c.size());
145 assertFalse((Boolean) JEXL.createScript("empty x", "x").execute(null, e));
146
147 assertNotSame(o0, o);
148 assertEquals(o0.hashCode(), o.hashCode());
149 assertEquals(o0, o);
150
151 long i = 6789000000L;
152 for (final Object v : c) {
153 i += 1;
154 assertEquals(i, ((Number) v).longValue());
155 }
156 assertEquals(6789000032L, i);
157
158 Long[] aa = c.<Long>toArray(new Long[32]);
159 assertEquals(32, aa.length);
160 for (int l = 0; l < 32; ++l) {
161 assertEquals((long) aa[l], 6789000001L + l);
162 }
163
164 aa = c.<Long>toArray(new Long[2]);
165 assertEquals(32, aa.length);
166 for (int l = 0; l < 32; ++l) {
167 assertEquals((long) aa[l], 6789000001L + l);
168 }
169
170 aa = c.<Long>toArray(new Long[34]);
171 assertEquals(34, aa.length);
172 for (int l = 0; l < 32; ++l) {
173 assertEquals((long) aa[l], 6789000001L + l);
174 }
175
176 final Object[] oaa = c.toArray();
177 assertEquals(32, oaa.length);
178 for (int l = 0; l < 32; ++l) {
179 assertEquals(oaa[l], 6789000001L + l);
180 }
181 }
182
183 @Test
184 void testLongSum() throws Exception {
185 final JexlScript e = JEXL.createScript("var s = 0; for(var i : (6789000001L..6789000001L)) { s = s + i; }; s");
186 final JexlContext jc = new MapContext();
187
188 final Object o = e.execute(jc);
189 assertEquals(6789000001L, ((Number) o).longValue());
190 }
191 }