1 /* 2 * Copyright (c) 2022, 2023, Oracle, Inc. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @summary Basic array hashCode functionality 27 * @run main/othervm --add-exports java.base/jdk.internal.util=ALL-UNNAMED -Xcomp -Xbatch HashCode 28 */ 29 30 package test.java.util.Arrays; 31 32 import java.lang.reflect.Method; 33 import java.util.Arrays; 34 35 public class HashCode { 36 private static String[] tests = { "", " ", "a", "abcdefg", 37 "It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way- in short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only. -- Charles Dickens, Tale of Two Cities", 38 "C'était le meilleur des temps, c'était le pire des temps, c'était l'âge de la sagesse, c'était l'âge de la folie, c'était l'époque de la croyance, c'était l'époque de l'incrédulité, c'était la saison de la Lumière, c'était C'était la saison des Ténèbres, c'était le printemps de l'espoir, c'était l'hiver du désespoir, nous avions tout devant nous, nous n'avions rien devant nous, nous allions tous directement au Ciel, nous allions tous directement dans l'autre sens bref, la période ressemblait tellement à la période actuelle, que certaines de ses autorités les plus bruyantes ont insisté pour qu'elle soit reçue, pour le bien ou pour le mal, au degré superlatif de la comparaison seulement. -- Charles Dickens, Tale of Two Cities (in French)", 39 "禅道修行を志した雲水は、一般に参禅のしきたりを踏んだうえで一人の師につき、各地にある専門道場と呼ばれる養成寺院に入門し、与えられた公案に取り組むことになる。公案は、師家(老師)から雲水が悟りの境地へと進んで行くために手助けとして課す問題であり、悟りの境地に達していない人には容易に理解し難い難問だが、屁理屈や詭弁が述べられているわけではなく、頓知や謎かけとも異なる。" 40 }; 41 42 byte[][] zeroes = new byte[64][]; 43 private static byte[][] testBytes = new byte[tests.length][]; 44 private static short[][] testShorts = new short[tests.length][]; 45 private static char[][] testChars = new char[tests.length][]; 46 private static int[][] testInts = new int[tests.length][]; 47 48 private static int[] expected = { 1, 63, 128, 536518979, -1174896354, -1357593156, 428276276}; 49 private static int[] expectedUnsigned = { 1, 63, 128, 536518979, -1174896354, 584369596, -2025326028}; 50 main(String[] args)51 public static void main(String[] args) throws Exception { 52 53 // Deep introspection into range-based hash functions 54 Class<?> arraysSupport = Class.forName("jdk.internal.util.ArraysSupport"); 55 Method vectorizedHashCode = arraysSupport.getDeclaredMethod("vectorizedHashCode", Object.class, int.class, int.class, int.class, int.class); 56 vectorizedHashCode.setAccessible(true); 57 58 for (int i = 0; i < tests.length; i++) { 59 testBytes[i] = tests[i].getBytes("UTF-8"); 60 int len = testBytes[i].length; 61 testChars[i] = new char[len]; 62 testShorts[i] = new short[len]; 63 testInts[i] = new int[len]; 64 for (int j = 0; j < len; j++) { 65 testChars[i][j] = (char) testBytes[i][j]; 66 testShorts[i][j] = testBytes[i][j]; 67 testInts[i][j] = testBytes[i][j]; 68 } 69 } 70 71 boolean failed = false; 72 try { 73 int zeroResult = 1; 74 for (int i = 0; i < 64; i++) { 75 byte[] zeroes = new byte[i]; 76 byte[] extraZeroes = new byte[i + 47]; 77 for (int j = 0; j < 10_000; j++) { 78 int hashCode = Arrays.hashCode(zeroes); 79 if (hashCode != zeroResult) { 80 throw new RuntimeException("byte[] \"" + Arrays.toString(zeroes) + "\": " 81 + " e = " + zeroResult 82 + ", hashCode = " + hashCode 83 + ", repetition = " + j); 84 } 85 hashCode = (int) vectorizedHashCode.invoke(null, extraZeroes, 17, i, 1, /* ArraysSupport.T_BYTE */ 8); 86 if (hashCode != zeroResult) { 87 throw new RuntimeException("byte[] subrange \"" + Arrays.toString(extraZeroes) 88 + "\" at offset 17, limit " + i + ": " 89 + " e = " + zeroResult 90 + ", hashCode = " + hashCode 91 + ", repetition = " + j); 92 } 93 } 94 zeroResult *= 31; 95 } 96 for (int i = 0; i < tests.length; i++) { 97 for (int j = 0; j < 64; j++) { 98 int e = expected[i]; 99 int hashCode = Arrays.hashCode(testBytes[i]); 100 if (hashCode != e) { 101 throw new RuntimeException("byte[] \"" + Arrays.toString(testBytes[i]) + "\": " 102 + " e = " + e 103 + ", hashCode = " + hashCode 104 + ", repetition = " + j); 105 } 106 } 107 } 108 System.out.println("byte[] tests passed"); 109 } catch (RuntimeException e) { 110 System.out.println(e.getMessage()); 111 failed = true; 112 } 113 114 try { 115 for (int i = 0; i < tests.length; i++) { 116 for (int j = 0; j < 64; j++) { 117 int e = expected[i]; 118 int hashCode = Arrays.hashCode(testShorts[i]); 119 if (hashCode != e) { 120 throw new RuntimeException("short[] \"" + Arrays.toString(testShorts[i]) + "\": " 121 + " e = " + e 122 + ", hashCode = " + hashCode 123 + ", repetition = " + j); 124 } 125 } 126 } 127 System.out.println("short[] tests passed"); 128 } catch (RuntimeException e) { 129 System.out.println(e.getMessage()); 130 failed = true; 131 } 132 133 try { 134 for (int i = 0; i < tests.length; i++) { 135 for (int j = 0; j < 64; j++) { 136 int e = expected[i]; 137 int hashCode = Arrays.hashCode(testInts[i]); 138 if (hashCode != e) { 139 throw new RuntimeException("int[] \"" + Arrays.toString(testInts[i]) + "\": " 140 + " e = " + e 141 + ", hashCode = " + hashCode 142 + ", repetition = " + j); 143 } 144 } 145 } 146 System.out.println("int[] tests passed"); 147 } catch (RuntimeException e) { 148 System.out.println(e.getMessage()); 149 failed = true; 150 } 151 152 try { 153 for (int i = 0; i < tests.length; i++) { 154 for (int j = 0; j < 64; j++) { 155 int e = expectedUnsigned[i]; 156 int hashCode = Arrays.hashCode(testChars[i]); 157 if (hashCode != e) { 158 throw new RuntimeException("char[] \"" + Arrays.toString(testChars[i]) + "\": " 159 + " e = " + e 160 + ", hashCode = " + hashCode 161 + ", repetition = " + j); 162 } 163 } 164 } 165 System.out.println("char[] tests passed"); 166 } catch (RuntimeException e) { 167 System.out.println(e.getMessage()); 168 failed = true; 169 } 170 171 if (failed) { 172 throw new RuntimeException("Some tests failed"); 173 } 174 } 175 } 176