|
24 | 24 | */ |
25 | 25 | package org.graalvm.compiler.jtt.optimize; |
26 | 26 |
|
27 | | -import java.util.ArrayList; |
28 | | -import java.util.Arrays; |
29 | | -import java.util.HashSet; |
30 | | -import java.util.List; |
31 | | -import java.util.Random; |
32 | | -import java.util.Set; |
33 | | -import java.util.stream.Collectors; |
34 | | - |
35 | 27 | import org.graalvm.compiler.jtt.JTTTest; |
36 | | -import org.graalvm.compiler.lir.hashing.HashFunction; |
37 | | -import org.graalvm.compiler.lir.hashing.Hasher; |
38 | 28 | import org.junit.Test; |
39 | 29 |
|
40 | | -import jdk.vm.ci.meta.JavaConstant; |
41 | | - |
42 | 30 | /* |
43 | 31 | * Tests optimization of hash table switches. |
44 | 32 | * Code generated by `SwitchHashTableTest.TestGenerator.main` |
45 | 33 | */ |
46 | 34 | 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 | 35 |
|
57 | | - // Hasher[function=rotateRight(val, prime), effort=4, cardinality=16] |
58 | 36 | public static int test1(int arg) { |
59 | 37 | switch (arg) { |
60 | 38 | case 3080012: |
@@ -103,7 +81,6 @@ public void run1() throws Throwable { |
103 | 81 | runTest("test1", 3080013); // miss |
104 | 82 | } |
105 | 83 |
|
106 | | - // Hasher[function=rotateRight(val, prime) ^ val, effort=5, cardinality=28] |
107 | 84 | public static int test2(int arg) { |
108 | 85 | switch (arg) { |
109 | 86 | case 718707335: |
@@ -152,7 +129,6 @@ public void run2() throws Throwable { |
152 | 129 | runTest("test2", 718707337); // miss |
153 | 130 | } |
154 | 131 |
|
155 | | - // Hasher[function=(val * prime) >> min, effort=4, cardinality=16] |
156 | 132 | public static int test3(int arg) { |
157 | 133 | switch (arg) { |
158 | 134 | case 880488712: |
@@ -201,7 +177,6 @@ public void run3() throws Throwable { |
201 | 177 | runTest("test3", 880488713); // miss |
202 | 178 | } |
203 | 179 |
|
204 | | - // Hasher[function=rotateRight(val, prime) + val, effort=5, cardinality=28] |
205 | 180 | public static int test4(int arg) { |
206 | 181 | switch (arg) { |
207 | 182 | case 189404658: |
@@ -250,7 +225,6 @@ public void run4() throws Throwable { |
250 | 225 | runTest("test4", 189404659); // miss |
251 | 226 | } |
252 | 227 |
|
253 | | - // Hasher[function=val - min, effort=2, cardinality=24] |
254 | 228 | public static int test5(int arg) { |
255 | 229 | switch (arg) { |
256 | 230 | case 527674226: |
@@ -299,7 +273,6 @@ public void run5() throws Throwable { |
299 | 273 | runTest("test5", 527674227); // miss |
300 | 274 | } |
301 | 275 |
|
302 | | - // Hasher[function=val, effort=1, cardinality=24] |
303 | 276 | public static int test6(int arg) { |
304 | 277 | switch (arg) { |
305 | 278 | case 676979121: |
@@ -348,7 +321,6 @@ public void run6() throws Throwable { |
348 | 321 | runTest("test6", 676979122); // miss |
349 | 322 | } |
350 | 323 |
|
351 | | - // Hasher[function=(val >> min) ^ val, effort=3, cardinality=16] |
352 | 324 | public static int test7(int arg) { |
353 | 325 | switch (arg) { |
354 | 326 | case 634218696: |
@@ -397,7 +369,6 @@ public void run7() throws Throwable { |
397 | 369 | runTest("test7", 634218697); // miss |
398 | 370 | } |
399 | 371 |
|
400 | | - // Hasher[function=val >> min, effort=2, cardinality=16] |
401 | 372 | public static int test8(int arg) { |
402 | 373 | switch (arg) { |
403 | 374 | case 473982403: |
@@ -446,7 +417,6 @@ public void run8() throws Throwable { |
446 | 417 | runTest("test8", 473982404); // miss |
447 | 418 | } |
448 | 419 |
|
449 | | - // Hasher[function=val >> (val & min), effort=3, cardinality=16] |
450 | 420 | public static int test9(int arg) { |
451 | 421 | switch (arg) { |
452 | 422 | case 15745090: |
@@ -495,7 +465,6 @@ public void run9() throws Throwable { |
495 | 465 | runTest("test9", 15745091); // miss |
496 | 466 | } |
497 | 467 |
|
498 | | - // Hasher[function=(val >> min) * val, effort=4, cardinality=28] |
499 | 468 | public static int test10(int arg) { |
500 | 469 | switch (arg) { |
501 | 470 | case 989358996: |
@@ -543,93 +512,4 @@ public void run10() throws Throwable { |
543 | 512 | runTest("test10", 989359109); // above |
544 | 513 | runTest("test10", 989358997); // miss |
545 | 514 | } |
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 | 515 | } |
0 commit comments