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);
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 }
|
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 * @library /test/lib
28 * @run main/othervm --add-exports java.base/jdk.internal.util=ALL-UNNAMED
29 * --add-opens java.base/jdk.internal.util=ALL-UNNAMED -Xcomp -Xbatch HashCode
30 */
31
32 import java.lang.reflect.Method;
33 import java.util.Arrays;
34 import jdk.test.lib.valueclass.AsValueClass;
35
36 public class HashCode {
37
38 @AsValueClass
39 static class Point {
40 int x, y;
41 Point(int x, int y) {this.x = x; this.y = y;}
42 }
43
44 private static String[] tests = { "", " ", "a", "abcdefg",
45 "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",
46 "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)",
47 "禅道修行を志した雲水は、一般に参禅のしきたりを踏んだうえで一人の師につき、各地にある専門道場と呼ばれる養成寺院に入門し、与えられた公案に取り組むことになる。公案は、師家(老師)から雲水が悟りの境地へと進んで行くために手助けとして課す問題であり、悟りの境地に達していない人には容易に理解し難い難問だが、屁理屈や詭弁が述べられているわけではなく、頓知や謎かけとも異なる。"
48 };
49
50 byte[][] zeroes = new byte[64][];
51 private static byte[][] testBytes = new byte[tests.length][];
52 private static short[][] testShorts = new short[tests.length][];
53 private static char[][] testChars = new char[tests.length][];
54 private static int[][] testInts = new int[tests.length][];
55
56 private static int[] expected = { 1, 63, 128, 536518979, -1174896354, -1357593156, 428276276};
57 private static int[] expectedUnsigned = { 1, 63, 128, 536518979, -1174896354, 584369596, -2025326028};
58
59 public static void main(String[] args) throws Exception {
60
61 // Deep introspection into range-based hash functions
62 Class<?> arraysSupport = Class.forName("jdk.internal.util.ArraysSupport");
63 Method vectorizedHashCode = arraysSupport.getDeclaredMethod("vectorizedHashCode", Object.class, int.class, int.class, int.class, int.class);
159
160 try {
161 for (int i = 0; i < tests.length; i++) {
162 for (int j = 0; j < 64; j++) {
163 int e = expectedUnsigned[i];
164 int hashCode = Arrays.hashCode(testChars[i]);
165 if (hashCode != e) {
166 throw new RuntimeException("char[] \"" + Arrays.toString(testChars[i]) + "\": "
167 + " e = " + e
168 + ", hashCode = " + hashCode
169 + ", repetition = " + j);
170 }
171 }
172 }
173 System.out.println("char[] tests passed");
174 } catch (RuntimeException e) {
175 System.out.println(e.getMessage());
176 failed = true;
177 }
178
179 try {
180 testValueClassArrayHashCode();
181 System.out.println("value class array hashCode tests passed");
182 } catch (RuntimeException e) {
183 System.out.println(e.getMessage());
184 failed = true;
185 }
186
187 if (failed) {
188 throw new RuntimeException("Some tests failed");
189 }
190 }
191
192 private static void testValueClassArrayHashCode() {
193 Point[] a = {new Point(1, 2), new Point(3, 4)};
194 Point[] b = {new Point(1, 2), new Point(3, 4)}; // distinct instances, same fields
195 int hashA = Arrays.hashCode(a);
196 int hashB = Arrays.hashCode(b);
197 if (Point.class.isValue()) {
198 if (hashA != hashB) {
199 throw new RuntimeException("testValueClassArrayHashCode: expected equal hash codes " +
200 "for equal value class arrays, got " + hashA + " and " + hashB);
201 }
202 // Repeated calls should produce the same array hash code
203 if (hashA != Arrays.hashCode(a)) {
204 throw new RuntimeException("testValueClassArrayHashCode: " +
205 "hash code not consistent across repeated calls");
206 }
207 }
208 }
209 }
|