-
Notifications
You must be signed in to change notification settings - Fork 176
8384611: [lworld] Add value class test coverage to java.util HashMap and HashSet #2501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: lworld
Are you sure you want to change the base?
Changes from 9 commits
a2c8542
f81ee61
9d395b1
fc57bfc
ddb2d77
56e6fe2
26d3307
62ef28e
0cc66e6
b704f53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /* | ||
| * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. | ||
| * Copyright (c) 2014, 2026, Oracle and/or its affiliates. All rights reserved. | ||
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| * | ||
| * This code is free software; you can redistribute it and/or modify it | ||
|
|
@@ -26,8 +26,10 @@ | |
| * @bug 8046085 | ||
| * @summary Ensure that when trees are being used for collisions that null key | ||
| * insertion still works. | ||
| * @library /test/lib | ||
| */ | ||
|
|
||
| import jdk.test.lib.valueclass.AsValueClass; | ||
| import java.util.*; | ||
| import java.util.stream.IntStream; | ||
|
|
||
|
|
@@ -45,6 +47,7 @@ public class PutNullKey { | |
| // A value 1.0 will ensure that a new threshold == capacity | ||
| static final float LOAD_FACTOR = 1.0f; | ||
|
|
||
| @AsValueClass | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please add the second testcase and new class You fix is going to work perfectly now, but later the plan is to rewrite @AsValueClass with 'value class' and don't run the test with and without preview. So 2 testcases would be reuiqred.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes |
||
| public static class CollidingHash implements Comparable<CollidingHash> { | ||
|
|
||
| private final int value; | ||
|
|
@@ -79,6 +82,10 @@ public int compareTo(CollidingHash o) { | |
| } | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| testCollidingHash(); | ||
| } | ||
|
|
||
| private static void testCollidingHash() { | ||
| Map<Object,Object> m = new HashMap<>(INITIAL_CAPACITY, LOAD_FACTOR); | ||
| IntStream.range(0, SIZE) | ||
| .mapToObj(CollidingHash::new) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /* | ||
| * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved. | ||
| * Copyright (c) 2002, 2026, Oracle and/or its affiliates. All rights reserved. | ||
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| * | ||
| * This code is free software; you can redistribute it and/or modify it | ||
|
|
@@ -26,8 +26,10 @@ | |
| * @bug 4627516 | ||
| * @summary HashMap.Entry.setValue() returns new value (as opposed to old) | ||
| * @author jbloch | ||
| * @library /test/lib | ||
| */ | ||
|
|
||
| import jdk.test.lib.valueclass.VClass; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
|
|
@@ -37,11 +39,27 @@ public class SetValue { | |
| static final String newValue = "new"; | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| testStringValue(); | ||
| testTupleValue(); | ||
| } | ||
|
|
||
| private static void testStringValue() { | ||
| Map m = new HashMap(); | ||
| m.put(key, oldValue); | ||
| Map.Entry e = (Map.Entry) m.entrySet().iterator().next(); | ||
| Object returnVal = e.setValue(newValue); | ||
| if (!returnVal.equals(oldValue)) | ||
| throw new RuntimeException("Return value: " + returnVal); | ||
| } | ||
|
|
||
| private static void testTupleValue() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if tuple is good name here, might be testVClassValue? |
||
| Map<String, VClass> m = new HashMap<>(); | ||
| VClass oldValue = new VClass(1, new int[] { 1 }); | ||
| VClass newValue = new VClass(2, new int[] { 2 }); | ||
| m.put(key, oldValue); | ||
| Map.Entry<String, VClass> e = m.entrySet().iterator().next(); | ||
| Object returnVal = e.setValue(newValue); | ||
| if (!returnVal.equals(oldValue)) | ||
| throw new RuntimeException("Return value: " + returnVal); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /* | ||
| * Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved. | ||
| * Copyright (c) 2019, 2026, Oracle and/or its affiliates. All rights reserved. | ||
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| * | ||
| * This code is free software; you can redistribute it and/or modify it | ||
|
|
@@ -31,27 +31,26 @@ | |
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.LongStream; | ||
| import jdk.test.lib.valueclass.VClass; | ||
|
|
||
| /* | ||
| * @test | ||
| * @bug 8336669 | ||
| * @summary HashMap.toArray() behavior tests | ||
| * @author tvaleev | ||
| * @library /test/lib | ||
| */ | ||
| public class ToArray { | ||
| // An interned identity class holding an int (like non-Preview Integer) | ||
| record Int(int intValue) implements Comparable<Int> { | ||
| @Override | ||
| public int compareTo(Int o) { | ||
| return Integer.compare(intValue, o.intValue); | ||
| } | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| checkMap(false); | ||
| checkMap(true); | ||
| checkSet(false); | ||
| checkSet(true); | ||
| checkTupleMap(false); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same about tuple in name. |
||
| checkTupleMap(true); | ||
| checkTupleSet(false); | ||
| checkTupleSet(true); | ||
| } | ||
|
|
||
| private static <T extends Comparable<T>> void checkToArray(String message, T[] expected, Collection<T> collection, | ||
|
|
@@ -161,4 +160,46 @@ private static void checkSet(boolean ordered) { | |
| checkToArray("Collisions", LongStream.range(0, 100).mapToObj(x -> x | (x << 32)) | ||
| .toArray(Long[]::new), longSet, !ordered); | ||
| } | ||
|
|
||
| private static void checkTupleMap(boolean ordered) { | ||
| Map<VClass, VClass> map = ordered ? new LinkedHashMap<>() : new HashMap<>(); | ||
| checkToArray("Empty-tuple-keys", new VClass[0], map.keySet(), !ordered); | ||
| checkToArray("Empty-tuple-values", new VClass[0], map.values(), !ordered); | ||
|
|
||
| List<VClass> keys = new ArrayList<>(); | ||
| List<VClass> values = new ArrayList<>(); | ||
| for (int i = 0; i < 100; i++) { | ||
| keys.add(new VClass(i, new int[] { i })); | ||
| values.add(new VClass(i * 2, new int[] { i * 2 })); | ||
| map.put(new VClass(i, new int[] { i }), new VClass(i * 2, new int[] { i * 2 })); | ||
| checkToArray(i + "-tuple-keys", keys.toArray(new VClass[0]), map.keySet(), !ordered); | ||
| checkToArray(i + "-tuple-values", values.toArray(new VClass[0]), map.values(), !ordered); | ||
| } | ||
| map.clear(); | ||
| checkToArray("Empty-tuple-keys", new VClass[0], map.keySet(), !ordered); | ||
| checkToArray("Empty-tuple-values", new VClass[0], map.values(), !ordered); | ||
| } | ||
|
|
||
| private static void checkTupleSet(boolean ordered) { | ||
| Collection<VClass> set = ordered ? new LinkedHashSet<>() : new HashSet<>(); | ||
| checkToArray("Empty-tuple", new VClass[0], set, !ordered); | ||
| set.add(new VClass(1, new int[] { 1 })); | ||
| checkToArray("One-tuple", new VClass[]{new VClass(1, new int[] { 1 })}, set, !ordered); | ||
| set.add(new VClass(2, new int[] { 2 })); | ||
| checkToArray("Two-tuple", new VClass[]{new VClass(1, new int[] { 1 }), new VClass(2, new int[] { 2 })}, set, !ordered); | ||
|
|
||
| Collection<VClass> tupleSet = ordered ? new LinkedHashSet<>() : new HashSet<>(); | ||
| for (int x = 0; x < 100; x++) { | ||
| tupleSet.add(new VClass(x, new int[] { x })); | ||
| } | ||
| checkToArray("100-tuple", LongStream.range(0, 100).mapToObj(x -> new VClass((int) x, new int[] { (int) x })) | ||
| .toArray(VClass[]::new), tupleSet, !ordered); | ||
| tupleSet.clear(); | ||
| checkToArray("After-clear-tuple", new VClass[0], tupleSet, !ordered); | ||
| for (int x = 0; x < 100; x++) { | ||
| tupleSet.add(new VClass(x, new int[] { -31 * x })); | ||
| } | ||
| checkToArray("Collisions-tuple", LongStream.range(0, 100).mapToObj(x -> new VClass((int) x, new int[] { -31 * (int) x })) | ||
| .toArray(VClass[]::new), tupleSet, !ordered); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /* | ||
| * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. | ||
| * Copyright (c) 2018, 2026, Oracle and/or its affiliates. All rights reserved. | ||
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| * | ||
| * This code is free software; you can redistribute it and/or modify it | ||
|
|
@@ -25,9 +25,11 @@ | |
| * @test | ||
| * @bug 8205399 | ||
| * @summary Check for AssertionError from HashMap TreeBin after Iterator.remove | ||
| * @library /test/lib | ||
| * @run testng/othervm -esa TreeBinAssert | ||
| */ | ||
|
|
||
| import jdk.test.lib.valueclass.AsValueClass; | ||
| import org.testng.annotations.DataProvider; | ||
| import org.testng.annotations.Test; | ||
| import org.testng.annotations.BeforeTest; | ||
|
|
@@ -154,6 +156,7 @@ private void doTest(Object collection, int[] hashes, | |
| /** | ||
| * Class that will have specified hash code in a HashMap. | ||
| */ | ||
| @AsValueClass | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same comment about having 2 testcases. |
||
| static class Key implements Comparable<Key> { | ||
| final int hash; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please update indentation (spaces)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to confirm, are you referring to the spacing inside the for loop parentheses, e.g. changing for (int i=0; i<valueMaps.length; i++) to for (int i = 0; i < valueMaps.length; i++)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, exactly.