1 /*
  2  * Copyright (c) 2022, 2024, 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
 28  *     --add-opens java.base/jdk.internal.util=ALL-UNNAMED -Xcomp -Xbatch HashCode
 29  */
 30 
 31 import java.lang.reflect.Method;
 32 import java.util.Arrays;
 33 
 34 public class HashCode {
 35     private static String[] tests = { "", " ", "a", "abcdefg",
 36             "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",
 37             "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)",
 38             "禅道修行を志した雲水は、一般に参禅のしきたりを踏んだうえで一人の師につき、各地にある専門道場と呼ばれる養成寺院に入門し、与えられた公案に取り組むことになる。公案は、師家(老師)から雲水が悟りの境地へと進んで行くために手助けとして課す問題であり、悟りの境地に達していない人には容易に理解し難い難問だが、屁理屈や詭弁が述べられているわけではなく、頓知や謎かけとも異なる。"
 39     };
 40 
 41     byte[][] zeroes = new byte[64][];
 42     private static byte[][] testBytes = new byte[tests.length][];
 43     private static short[][] testShorts = new short[tests.length][];
 44     private static char[][] testChars = new char[tests.length][];
 45     private static int[][] testInts = new int[tests.length][];
 46 
 47     private static int[] expected = { 1, 63, 128, 536518979, -1174896354, -1357593156, 428276276};
 48     private static int[] expectedUnsigned = { 1, 63, 128, 536518979, -1174896354, 584369596, -2025326028};
 49 
 50     public static void main(String[] args) throws Exception {
 51 
 52         // Deep introspection into range-based hash functions
 53         Class<?> arraysSupport = Class.forName("jdk.internal.util.ArraysSupport");
 54         Method vectorizedHashCode = arraysSupport.getDeclaredMethod("vectorizedHashCode", Object.class, int.class, int.class, int.class, int.class);
 55         vectorizedHashCode.setAccessible(true);
 56 
 57         for (int i = 0; i < tests.length; i++) {
 58             testBytes[i] = tests[i].getBytes("UTF-8");
 59             int len = testBytes[i].length;
 60             testChars[i] = new char[len];
 61             testShorts[i] = new short[len];
 62             testInts[i] = new int[len];
 63             for (int j = 0; j < len; j++) {
 64                 testChars[i][j] = (char) testBytes[i][j];
 65                 testShorts[i][j] = testBytes[i][j];
 66                 testInts[i][j] = testBytes[i][j];
 67             }
 68         }
 69 
 70         boolean failed = false;
 71         try {
 72             int zeroResult = 1;
 73             for (int i = 0; i < 64; i++) {
 74                 byte[] zeroes = new byte[i];
 75                 byte[] extraZeroes = new byte[i + 47];
 76                 for (int j = 0; j < 10_000; j++) {
 77                     int hashCode = Arrays.hashCode(zeroes);
 78                     if (hashCode != zeroResult) {
 79                         throw new RuntimeException("byte[] \"" + Arrays.toString(zeroes) + "\": "
 80                                 + " e = " + zeroResult
 81                                 + ", hashCode = " + hashCode
 82                                 + ", repetition = " + j);
 83                     }
 84                     hashCode = (int) vectorizedHashCode.invoke(null, extraZeroes, 17, i, 1, /* ArraysSupport.T_BYTE */ 8);
 85                     if (hashCode != zeroResult) {
 86                         throw new RuntimeException("byte[] subrange \"" + Arrays.toString(extraZeroes)
 87                                 + "\" at offset 17, limit " + i + ": "
 88                                 + " e = " + zeroResult
 89                                 + ", hashCode = " + hashCode
 90                                 + ", repetition = " + j);
 91                     }
 92                 }
 93                 zeroResult *= 31;
 94             }
 95             for (int i = 0; i < tests.length; i++) {
 96                 for (int j = 0; j < 64; j++) {
 97                     int e = expected[i];
 98                     int hashCode = Arrays.hashCode(testBytes[i]);
 99                     if (hashCode != e) {
100                         throw new RuntimeException("byte[] \"" + Arrays.toString(testBytes[i]) + "\": "
101                                 + " e = " + e
102                                 + ", hashCode = " + hashCode
103                                 + ", repetition = " + j);
104                     }
105                 }
106             }
107             System.out.println("byte[] tests passed");
108         } catch (RuntimeException e) {
109             System.out.println(e.getMessage());
110             failed = true;
111         }
112 
113         try {
114             for (int i = 0; i < tests.length; i++) {
115                 for (int j = 0; j < 64; j++) {
116                     int e = expected[i];
117                     int hashCode = Arrays.hashCode(testShorts[i]);
118                     if (hashCode != e) {
119                         throw new RuntimeException("short[] \"" + Arrays.toString(testShorts[i]) + "\": "
120                                 + " e = " + e
121                                 + ", hashCode = " + hashCode
122                                 + ", repetition = " + j);
123                     }
124                 }
125             }
126             System.out.println("short[] tests passed");
127         } catch (RuntimeException e) {
128             System.out.println(e.getMessage());
129             failed = true;
130         }
131 
132         try {
133             for (int i = 0; i < tests.length; i++) {
134                 for (int j = 0; j < 64; j++) {
135                     int e = expected[i];
136                     int hashCode = Arrays.hashCode(testInts[i]);
137                     if (hashCode != e) {
138                         throw new RuntimeException("int[] \"" + Arrays.toString(testInts[i]) + "\": "
139                                 + " e = " + e
140                                 + ", hashCode = " + hashCode
141                                 + ", repetition = " + j);
142                     }
143                 }
144             }
145             System.out.println("int[] tests passed");
146         } catch (RuntimeException e) {
147             System.out.println(e.getMessage());
148             failed = true;
149         }
150 
151         try {
152             for (int i = 0; i < tests.length; i++) {
153                 for (int j = 0; j < 64; j++) {
154                     int e = expectedUnsigned[i];
155                     int hashCode = Arrays.hashCode(testChars[i]);
156                     if (hashCode != e) {
157                         throw new RuntimeException("char[] \"" + Arrays.toString(testChars[i]) + "\": "
158                                 + " e = " + e
159                                 + ", hashCode = " + hashCode
160                                 + ", repetition = " + j);
161                     }
162                 }
163             }
164             System.out.println("char[] tests passed");
165         } catch (RuntimeException e) {
166             System.out.println(e.getMessage());
167             failed = true;
168         }
169 
170         if (failed) {
171             throw new RuntimeException("Some tests failed");
172         }
173     }
174 }