|
31 | 31 | import java.util.Set; |
32 | 32 | import java.util.function.BiFunction; |
33 | 33 | import java.util.function.IntFunction; |
| 34 | +import java.util.function.IntUnaryOperator; |
| 35 | +import java.util.stream.Stream; |
| 36 | +import java.util.stream.Collectors; |
| 37 | + |
| 38 | +import org.testng.Assert; |
34 | 39 |
|
35 | 40 | public class AbstractVectorTest { |
36 | 41 |
|
@@ -149,12 +154,53 @@ static int[] fillInts(int[] a, IntOp f) { |
149 | 154 | withToString("mask[false]", boolean[]::new) |
150 | 155 | ); |
151 | 156 |
|
| 157 | + static final List<List<IntFunction<boolean[]>>> |
| 158 | + BOOLEAN_MASK_COMPARE_GENERATOR_PAIRS = |
| 159 | + Stream.of(BOOLEAN_MASK_GENERATORS.get(0)). |
| 160 | + flatMap(fa -> BOOLEAN_MASK_GENERATORS.stream().skip(1).map( |
| 161 | + fb -> List.of(fa, fb))).collect(Collectors.toList()); |
| 162 | + |
152 | 163 | static final List<BiFunction<Integer,Integer,int[]>> INT_SHUFFLE_GENERATORS = List.of( |
153 | 164 | withToStringBi("shuffle[random]", (Integer l, Integer m) -> { |
154 | 165 | return RAND.ints(l, 0, m).toArray(); |
155 | 166 | }) |
156 | 167 | ); |
157 | 168 |
|
| 169 | + interface RangeIntOp { |
| 170 | + int apply(int i, int min, int max); |
| 171 | + } |
| 172 | + |
| 173 | + static int[] fillRangeInts(int s, int min, int max, RangeIntOp f) { |
| 174 | + return fillRangeInts(new int[s], min, max, f); |
| 175 | + } |
| 176 | + |
| 177 | + static int[] fillRangeInts(int[] a, int min, int max, RangeIntOp f) { |
| 178 | + for (int i = 0; i < a.length; i++) { |
| 179 | + a[i] = f.apply(i, min, max); |
| 180 | + } |
| 181 | + return a; |
| 182 | + } |
| 183 | + |
| 184 | + static final List<List<BiFunction<Integer, Integer, int[]>>> |
| 185 | + INT_SHUFFLE_COMPARE_GENERATOR_PAIRS = List.of( |
| 186 | + List.of( |
| 187 | + withToStringBi("shuffle[i]", (Integer l, Integer m) -> { |
| 188 | + return fillRangeInts(l, 0, m, (i, _min, _max) -> (i % _max)); |
| 189 | + }), |
| 190 | + withToStringBi("shuffle[random]", (Integer l, Integer m) -> { |
| 191 | + return RAND.ints(l, 0, m).toArray(); |
| 192 | + }) |
| 193 | + ), |
| 194 | + List.of( |
| 195 | + withToStringBi("shuffle[i]", (Integer l, Integer m) -> { |
| 196 | + return fillRangeInts(l, 0, m, (i, _min, _max) -> (i % _max)); |
| 197 | + }), |
| 198 | + withToStringBi("shuffle[random]", (Integer l, Integer m) -> { |
| 199 | + return RAND.ints(l, 0, m).toArray(); |
| 200 | + }) |
| 201 | + ) |
| 202 | + ); |
| 203 | + |
158 | 204 | static final List<BiFunction<Integer,Integer,int[]>> INT_INDEX_GENERATORS = List.of( |
159 | 205 | withToStringBi("index[random]", (Integer l, Integer m) -> { |
160 | 206 | return RAND.ints(l, 0, m).toArray(); |
@@ -187,4 +233,32 @@ static boolean isIndexOutOfBounds(int size, int offset, int length) { |
187 | 233 | int upperBound = offset + size; |
188 | 234 | return upperBound < size || upperBound > length; |
189 | 235 | } |
| 236 | + |
| 237 | + public static int[] expectedShuffle(int length, IntUnaryOperator fn) { |
| 238 | + int [] a = new int[length]; |
| 239 | + for (int i = 0; i < length; i++) { |
| 240 | + int elem = fn.applyAsInt(i); |
| 241 | + int wrapElem = Math.floorMod(elem, length); |
| 242 | + if (elem != wrapElem) { |
| 243 | + elem = wrapElem - length; |
| 244 | + } |
| 245 | + a[i] = elem; |
| 246 | + } |
| 247 | + return a; |
| 248 | + } |
| 249 | + |
| 250 | + interface FBooleanBinOp { |
| 251 | + boolean apply(boolean a, boolean b); |
| 252 | + } |
| 253 | + |
| 254 | + static void assertArraysEquals(boolean[] a, boolean[] b, boolean[] r, FBooleanBinOp f) { |
| 255 | + int i = 0; |
| 256 | + try { |
| 257 | + for (; i < a.length; i++) { |
| 258 | + Assert.assertEquals(r[i], f.apply(a[i], b[i])); |
| 259 | + } |
| 260 | + } catch (AssertionError e) { |
| 261 | + Assert.assertEquals(r[i], f.apply(a[i], b[i]), "(" + a[i] + ", " + b[i] + ") at index #" + i); |
| 262 | + } |
| 263 | + } |
190 | 264 | } |
0 commit comments