|
33 | 33 | import java.util.stream.Collectors; |
34 | 34 |
|
35 | 35 | import org.graalvm.compiler.jtt.JTTTest; |
36 | | -import org.graalvm.compiler.lir.hashing.HashFunction; |
37 | | -import org.graalvm.compiler.lir.hashing.Hasher; |
38 | 36 | import org.junit.Test; |
39 | 37 |
|
40 | 38 | import jdk.vm.ci.meta.JavaConstant; |
|
44 | 42 | * Code generated by `SwitchHashTableTest.TestGenerator.main` |
45 | 43 | */ |
46 | 44 | public class SwitchHashTableTest extends JTTTest { |
47 | | - @Test |
48 | | - public void checkHashFunctionInstances() { |
49 | | - List<String> coveredByTestCases = Arrays.asList("val >> min", "val", "val >> (val & min)", "(val >> min) ^ val", "val - min", "rotateRight(val, prime)", "rotateRight(val, prime) ^ val", |
50 | | - "rotateRight(val, prime) + val", "(val >> min) * val", "(val * prime) >> min"); |
51 | | - Set<String> functions = HashFunction.instances().stream().map(Object::toString).collect(Collectors.toSet()); |
52 | | - functions.removeAll(coveredByTestCases); |
53 | | - assertTrue("The following hash functions are not covered by the `Switch03` test: " + functions + |
54 | | - ". Re-run the `Switch03.TestGenerator.main` and update the test class.", functions.isEmpty()); |
55 | | - } |
56 | 45 |
|
57 | | - // Hasher[function=rotateRight(val, prime), effort=4, cardinality=16] |
58 | 46 | public static int test1(int arg) { |
59 | 47 | switch (arg) { |
60 | 48 | case 3080012: |
@@ -103,7 +91,6 @@ public void run1() throws Throwable { |
103 | 91 | runTest("test1", 3080013); // miss |
104 | 92 | } |
105 | 93 |
|
106 | | - // Hasher[function=rotateRight(val, prime) ^ val, effort=5, cardinality=28] |
107 | 94 | public static int test2(int arg) { |
108 | 95 | switch (arg) { |
109 | 96 | case 718707335: |
@@ -152,7 +139,6 @@ public void run2() throws Throwable { |
152 | 139 | runTest("test2", 718707337); // miss |
153 | 140 | } |
154 | 141 |
|
155 | | - // Hasher[function=(val * prime) >> min, effort=4, cardinality=16] |
156 | 142 | public static int test3(int arg) { |
157 | 143 | switch (arg) { |
158 | 144 | case 880488712: |
@@ -201,7 +187,6 @@ public void run3() throws Throwable { |
201 | 187 | runTest("test3", 880488713); // miss |
202 | 188 | } |
203 | 189 |
|
204 | | - // Hasher[function=rotateRight(val, prime) + val, effort=5, cardinality=28] |
205 | 190 | public static int test4(int arg) { |
206 | 191 | switch (arg) { |
207 | 192 | case 189404658: |
@@ -250,7 +235,6 @@ public void run4() throws Throwable { |
250 | 235 | runTest("test4", 189404659); // miss |
251 | 236 | } |
252 | 237 |
|
253 | | - // Hasher[function=val - min, effort=2, cardinality=24] |
254 | 238 | public static int test5(int arg) { |
255 | 239 | switch (arg) { |
256 | 240 | case 527674226: |
@@ -299,7 +283,6 @@ public void run5() throws Throwable { |
299 | 283 | runTest("test5", 527674227); // miss |
300 | 284 | } |
301 | 285 |
|
302 | | - // Hasher[function=val, effort=1, cardinality=24] |
303 | 286 | public static int test6(int arg) { |
304 | 287 | switch (arg) { |
305 | 288 | case 676979121: |
@@ -348,7 +331,6 @@ public void run6() throws Throwable { |
348 | 331 | runTest("test6", 676979122); // miss |
349 | 332 | } |
350 | 333 |
|
351 | | - // Hasher[function=(val >> min) ^ val, effort=3, cardinality=16] |
352 | 334 | public static int test7(int arg) { |
353 | 335 | switch (arg) { |
354 | 336 | case 634218696: |
@@ -397,7 +379,6 @@ public void run7() throws Throwable { |
397 | 379 | runTest("test7", 634218697); // miss |
398 | 380 | } |
399 | 381 |
|
400 | | - // Hasher[function=val >> min, effort=2, cardinality=16] |
401 | 382 | public static int test8(int arg) { |
402 | 383 | switch (arg) { |
403 | 384 | case 473982403: |
@@ -446,7 +427,6 @@ public void run8() throws Throwable { |
446 | 427 | runTest("test8", 473982404); // miss |
447 | 428 | } |
448 | 429 |
|
449 | | - // Hasher[function=val >> (val & min), effort=3, cardinality=16] |
450 | 430 | public static int test9(int arg) { |
451 | 431 | switch (arg) { |
452 | 432 | case 15745090: |
@@ -495,7 +475,6 @@ public void run9() throws Throwable { |
495 | 475 | runTest("test9", 15745091); // miss |
496 | 476 | } |
497 | 477 |
|
498 | | - // Hasher[function=(val >> min) * val, effort=4, cardinality=28] |
499 | 478 | public static int test10(int arg) { |
500 | 479 | switch (arg) { |
501 | 480 | case 989358996: |
@@ -543,93 +522,4 @@ public void run10() throws Throwable { |
543 | 522 | runTest("test10", 989359109); // above |
544 | 523 | runTest("test10", 989358997); // miss |
545 | 524 | } |
546 | | - |
547 | | - public static class TestGenerator { |
548 | | - |
549 | | - private static int nextId = 0; |
550 | | - private static final int size = 15; |
551 | | - private static double minDensity = 0.5; |
552 | | - |
553 | | - // test code generator |
554 | | - public static void main(String[] args) { |
555 | | - |
556 | | - Random r = new Random(0); |
557 | | - Set<String> seen = new HashSet<>(); |
558 | | - Set<String> all = HashFunction.instances().stream().map(Object::toString).collect(Collectors.toSet()); |
559 | | - |
560 | | - println("@Test"); |
561 | | - println("public void checkHashFunctionInstances() {"); |
562 | | - println(" List<String> coveredByTestCases = Arrays.asList(" + String.join(", ", all.stream().map(s -> "\"" + s + "\"").collect(Collectors.toSet())) + ");"); |
563 | | - println(" Set<String> functions = HashFunction.instances().stream().map(Object::toString).collect(Collectors.toSet());"); |
564 | | - println(" functions.removeAll(coveredByTestCases);"); |
565 | | - println(" assertTrue(\"The following hash functions are not covered by the `Switch03` test: \" + functions +"); |
566 | | - println(" \". Re-run the `Switch03.TestGenerator.main` and update the test class.\", functions.isEmpty());"); |
567 | | - println("}"); |
568 | | - |
569 | | - while (seen.size() < all.size()) { |
570 | | - int v = r.nextInt(Integer.MAX_VALUE / 2); |
571 | | - List<Integer> keys = new ArrayList<>(); |
572 | | - while (keys.size() < 15) { |
573 | | - keys.add(v); |
574 | | - v += r.nextInt(15); |
575 | | - } |
576 | | - keys.sort(Integer::compare); |
577 | | - double density = ((double) keys.size() + 1) / (keys.get(keys.size() - 1) - keys.get(0)); |
578 | | - if (density < minDensity) { |
579 | | - Hasher.forKeys(toConstants(keys), minDensity).ifPresent(h -> { |
580 | | - String f = h.function().toString(); |
581 | | - if (!seen.contains(f)) { |
582 | | - gen(keys, h); |
583 | | - seen.add(f); |
584 | | - } |
585 | | - }); |
586 | | - } |
587 | | - } |
588 | | - } |
589 | | - |
590 | | - private static void gen(List<Integer> keys, Hasher hasher) { |
591 | | - int id = ++nextId; |
592 | | - |
593 | | - println("// " + hasher + ""); |
594 | | - println("public static int test" + id + "(int arg) {"); |
595 | | - println(" switch (arg) {"); |
596 | | - |
597 | | - for (Integer key : keys) { |
598 | | - println(" case " + key + ": return " + key + ";"); |
599 | | - } |
600 | | - |
601 | | - println(" default: return -1;"); |
602 | | - println(" }"); |
603 | | - println("}"); |
604 | | - |
605 | | - int miss = keys.get(0) + 1; |
606 | | - while (keys.contains(miss)) { |
607 | | - miss++; |
608 | | - } |
609 | | - |
610 | | - println("@Test"); |
611 | | - println("public void run" + id + "() throws Throwable {"); |
612 | | - println(" runTest(\"test" + id + "\", 0); // zero "); |
613 | | - println(" runTest(\"test" + id + "\", " + (keys.get(0) - 1) + "); // bellow "); |
614 | | - println(" runTest(\"test" + id + "\", " + keys.get(0) + "); // first "); |
615 | | - println(" runTest(\"test" + id + "\", " + keys.get(size / 2) + "); // middle "); |
616 | | - println(" runTest(\"test" + id + "\", " + keys.get(size - 1) + "); // last "); |
617 | | - println(" runTest(\"test" + id + "\", " + (keys.get(size - 1) + 1) + "); // above "); |
618 | | - println(" runTest(\"test" + id + "\", " + miss + "); // miss "); |
619 | | - println("}"); |
620 | | - } |
621 | | - |
622 | | - private static void println(String s) { |
623 | | - System.out.println(s); |
624 | | - } |
625 | | - |
626 | | - private static JavaConstant[] toConstants(List<Integer> keys) { |
627 | | - JavaConstant[] ckeys = new JavaConstant[keys.size()]; |
628 | | - |
629 | | - for (int i = 0; i < keys.size(); i++) { |
630 | | - ckeys[i] = JavaConstant.forInt(keys.get(i)); |
631 | | - } |
632 | | - return ckeys; |
633 | | - } |
634 | | - } |
635 | 525 | } |
0 commit comments