Path: blob/master/test/jdk/java/util/Map/InPlaceOpsCollisions.java
41149 views
/*1* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 800569826* @run testng/othervm -Dtest.map.collisions.shortrun=true InPlaceOpsCollisions27* @summary Ensure overrides of in-place operations in Maps behave well with lots of collisions.28*/2930import java.util.Arrays;31import java.util.Comparator;32import java.util.HashMap;33import java.util.Iterator;34import java.util.LinkedHashMap;35import java.util.Map;36import java.util.TreeMap;37import java.util.function.BiFunction;38import java.util.function.Function;39import java.util.function.Supplier;4041import org.testng.annotations.DataProvider;42import org.testng.annotations.Test;43import static org.testng.Assert.assertTrue;44import static org.testng.Assert.assertFalse;45import static org.testng.Assert.assertEquals;46import static org.testng.Assert.assertNull;4748public class InPlaceOpsCollisions extends MapWithCollisionsProviders {4950@Test(dataProvider = "mapsWithObjectsAndStrings")51void testPutIfAbsent(String desc, Supplier<Map<Object, Object>> ms, Object val) {52Map<Object, Object> map = ms.get();53Object[] keys = map.keySet().toArray();54Object retVal;55removeOddKeys(map, keys);56for (int i = 0; i < keys.length; i++) {57retVal = map.putIfAbsent(keys[i], val);58if (i % 2 == 0) { // even: not absent, not put5960assertEquals(retVal, keys[i],61String.format("putIfAbsent: (%s[%d]) retVal", desc, i));62assertEquals(keys[i], map.get(keys[i]),63String.format("putIfAbsent: get(%s[%d])", desc, i));64assertTrue(map.containsValue(keys[i]),65String.format("putIfAbsent: containsValue(%s[%d])", desc, i));66} else { // odd: absent, was put67assertNull(retVal,68String.format("putIfAbsent: (%s[%d]) retVal", desc, i));69assertEquals(val, map.get(keys[i]),70String.format("putIfAbsent: get(%s[%d])", desc, i));71assertFalse(map.containsValue(keys[i]),72String.format("putIfAbsent: !containsValue(%s[%d])", desc, i));73}74assertTrue(map.containsKey(keys[i]),75String.format("insertion: containsKey(%s[%d])", desc, i));76}77assertEquals(map.size(), keys.length,78String.format("map expected size m%d != k%d", map.size(), keys.length));79}8081@Test(dataProvider = "nullValueFriendlyMaps")82void testPutIfAbsentOverwriteNull(String desc, Supplier<Map<Object, Object>> ms) {83Map<Object, Object> map = ms.get();84map.put("key", null);85assertEquals(map.size(), 1, desc + ": size != 1");86assertTrue(map.containsKey("key"), desc + ": does not have key");87assertNull(map.get("key"), desc + ": value is not null");88map.putIfAbsent("key", "value"); // must rewrite89assertEquals(map.size(), 1, desc + ": size != 1");90assertTrue(map.containsKey("key"), desc + ": does not have key");91assertEquals(map.get("key"), "value", desc + ": value is not 'value'");92}9394@Test(dataProvider = "mapsWithObjectsAndStrings")95void testRemoveMapping(String desc, Supplier<Map<Object, Object>> ms, Object val) {96Map<Object, Object> map = ms.get();97Object[] keys = map.keySet().toArray();98boolean removed;99int removes = 0;100remapOddKeys(map, keys, val);101for (int i = 0; i < keys.length; i++) {102removed = map.remove(keys[i], keys[i]);103if (i % 2 == 0) { // even: original mapping, should be removed104assertTrue(removed,105String.format("removeMapping: retVal(%s[%d])", desc, i));106assertNull(map.get(keys[i]),107String.format("removeMapping: get(%s[%d])", desc, i));108assertFalse(map.containsKey(keys[i]),109String.format("removeMapping: !containsKey(%s[%d])", desc, i));110assertFalse(map.containsValue(keys[i]),111String.format("removeMapping: !containsValue(%s[%d])", desc, i));112removes++;113} else { // odd: new mapping, not removed114assertFalse(removed,115String.format("removeMapping: retVal(%s[%d])", desc, i));116assertEquals(val, map.get(keys[i]),117String.format("removeMapping: get(%s[%d])", desc, i));118assertTrue(map.containsKey(keys[i]),119String.format("removeMapping: containsKey(%s[%d])", desc, i));120assertTrue(map.containsValue(val),121String.format("removeMapping: containsValue(%s[%d])", desc, i));122}123}124assertEquals(map.size(), keys.length - removes,125String.format("map expected size m%d != k%d", map.size(), keys.length - removes));126}127128@Test(dataProvider = "mapsWithObjectsAndStrings")129void testReplaceOldValue(String desc, Supplier<Map<Object, Object>> ms, Object val) {130// remap odds to val131// call replace to replace for val, for all keys132// check that all keys map to value from keys array133Map<Object, Object> map = ms.get();134Object[] keys = map.keySet().toArray();135boolean replaced;136remapOddKeys(map, keys, val);137138for (int i = 0; i < keys.length; i++) {139replaced = map.replace(keys[i], val, keys[i]);140if (i % 2 == 0) { // even: original mapping, should not be replaced141assertFalse(replaced,142String.format("replaceOldValue: retVal(%s[%d])", desc, i));143} else { // odd: new mapping, should be replaced144assertTrue(replaced,145String.format("replaceOldValue: get(%s[%d])", desc, i));146}147assertEquals(keys[i], map.get(keys[i]),148String.format("replaceOldValue: get(%s[%d])", desc, i));149assertTrue(map.containsKey(keys[i]),150String.format("replaceOldValue: containsKey(%s[%d])", desc, i));151assertTrue(map.containsValue(keys[i]),152String.format("replaceOldValue: containsValue(%s[%d])", desc, i));153}154assertFalse(map.containsValue(val),155String.format("replaceOldValue: !containsValue(%s[%s])", desc, val));156assertEquals(map.size(), keys.length,157String.format("map expected size m%d != k%d", map.size(), keys.length));158}159160@Test(dataProvider = "mapsWithObjectsAndStrings")161void testReplaceIfMapped(String desc, Supplier<Map<Object, Object>> ms, Object val) {162// remove odd keys163// call replace for all keys[]164// odd keys should remain absent, even keys should be mapped to EXTRA, no value from keys[] should be in map165Map<Object, Object> map = ms.get();166Object[] keys = map.keySet().toArray();167int expectedSize1 = 0;168removeOddKeys(map, keys);169int expectedSize2 = map.size();170171for (int i = 0; i < keys.length; i++) {172Object retVal = map.replace(keys[i], val);173if (i % 2 == 0) { // even: still in map, should be replaced174assertEquals(retVal, keys[i],175String.format("replaceIfMapped: retVal(%s[%d])", desc, i));176assertEquals(val, map.get(keys[i]),177String.format("replaceIfMapped: get(%s[%d])", desc, i));178assertTrue(map.containsKey(keys[i]),179String.format("replaceIfMapped: containsKey(%s[%d])", desc, i));180expectedSize1++;181} else { // odd: was removed, should not be replaced182assertNull(retVal,183String.format("replaceIfMapped: retVal(%s[%d])", desc, i));184assertNull(map.get(keys[i]),185String.format("replaceIfMapped: get(%s[%d])", desc, i));186assertFalse(map.containsKey(keys[i]),187String.format("replaceIfMapped: containsKey(%s[%d])", desc, i));188}189assertFalse(map.containsValue(keys[i]),190String.format("replaceIfMapped: !containsValue(%s[%d])", desc, i));191}192assertTrue(map.containsValue(val),193String.format("replaceIfMapped: containsValue(%s[%s])", desc, val));194assertEquals(map.size(), expectedSize1,195String.format("map expected size#1 m%d != k%d", map.size(), expectedSize1));196assertEquals(map.size(), expectedSize2,197String.format("map expected size#2 m%d != k%d", map.size(), expectedSize2));198199}200201private static <T> void testComputeIfAbsent(Map<T, T> map, String desc, T[] keys,202Function<T, T> mappingFunction) {203// remove a third of the keys204// call computeIfAbsent for all keys, func returns EXTRA205// check that removed keys now -> EXTRA, other keys -> original val206T expectedVal = mappingFunction.apply(keys[0]);207T retVal;208int expectedSize = 0;209removeThirdKeys(map, keys);210for (int i = 0; i < keys.length; i++) {211retVal = map.computeIfAbsent(keys[i], mappingFunction);212if (i % 3 != 2) { // key present, not computed213assertEquals(retVal, keys[i],214String.format("computeIfAbsent: (%s[%d]) retVal", desc, i));215assertEquals(keys[i], map.get(keys[i]),216String.format("computeIfAbsent: get(%s[%d])", desc, i));217assertTrue(map.containsValue(keys[i]),218String.format("computeIfAbsent: containsValue(%s[%d])", desc, i));219assertTrue(map.containsKey(keys[i]),220String.format("insertion: containsKey(%s[%d])", desc, i));221expectedSize++;222} else { // key absent, computed unless function return null223assertEquals(retVal, expectedVal,224String.format("computeIfAbsent: (%s[%d]) retVal", desc, i));225assertEquals(expectedVal, map.get(keys[i]),226String.format("computeIfAbsent: get(%s[%d])", desc, i));227assertFalse(map.containsValue(keys[i]),228String.format("computeIfAbsent: !containsValue(%s[%d])", desc, i));229// mapping should not be added if function returns null230assertTrue(map.containsKey(keys[i]) != (expectedVal == null),231String.format("insertion: containsKey(%s[%d])", desc, i));232if (expectedVal != null) {233expectedSize++;234}235}236}237if (expectedVal != null) {238assertTrue(map.containsValue(expectedVal),239String.format("computeIfAbsent: containsValue(%s[%s])", desc, expectedVal));240}241assertEquals(map.size(), expectedSize,242String.format("map expected size m%d != k%d", map.size(), expectedSize));243}244245@Test(dataProvider = "mapsWithObjectsAndStrings")246void testComputeIfAbsentNonNull(String desc, Supplier<Map<Object, Object>> ms, Object val) {247Map<Object, Object> map = ms.get();248Object[] keys = map.keySet().toArray();249testComputeIfAbsent(map, desc, keys, (k) -> val);250}251252@Test(dataProvider = "mapsWithObjectsAndStrings")253void testComputeIfAbsentNull(String desc, Supplier<Map<Object, Object>> ms, Object val) {254Map<Object, Object> map = ms.get();255Object[] keys = map.keySet().toArray();256testComputeIfAbsent(map, desc, keys, (k) -> null);257}258259@Test(dataProvider = "nullValueFriendlyMaps")260void testComputeIfAbsentOverwriteNull(String desc, Supplier<Map<Object, Object>> ms) {261Map<Object, Object> map = ms.get();262map.put("key", null);263assertEquals(map.size(), 1, desc + ": size != 1");264assertTrue(map.containsKey("key"), desc + ": does not have key");265assertNull(map.get("key"), desc + ": value is not null");266Object result = map.computeIfAbsent("key", k -> "value"); // must rewrite267assertEquals(result, "value", desc + ": computeIfAbsent result is not 'value'");268assertEquals(map.size(), 1, desc + ": size != 1");269assertTrue(map.containsKey("key"), desc + ": does not have key");270assertEquals(map.get("key"), "value", desc + ": value is not 'value'");271}272273private static <T> void testComputeIfPresent(Map<T, T> map, String desc, T[] keys,274BiFunction<T, T, T> mappingFunction) {275// remove a third of the keys276// call testComputeIfPresent for all keys[]277// removed keys should remain absent, even keys should be mapped to $RESULT278// no value from keys[] should be in map279T funcResult = mappingFunction.apply(keys[0], keys[0]);280int expectedSize1 = 0;281removeThirdKeys(map, keys);282283for (int i = 0; i < keys.length; i++) {284T retVal = map.computeIfPresent(keys[i], mappingFunction);285if (i % 3 != 2) { // key present286if (funcResult == null) { // was removed287assertFalse(map.containsKey(keys[i]),288String.format("replaceIfMapped: containsKey(%s[%d])", desc, i));289} else { // value was replaced290assertTrue(map.containsKey(keys[i]),291String.format("replaceIfMapped: containsKey(%s[%d])", desc, i));292expectedSize1++;293}294assertEquals(retVal, funcResult,295String.format("computeIfPresent: retVal(%s[%s])", desc, i));296assertEquals(funcResult, map.get(keys[i]),297String.format("replaceIfMapped: get(%s[%d])", desc, i));298299} else { // odd: was removed, should not be replaced300assertNull(retVal,301String.format("replaceIfMapped: retVal(%s[%d])", desc, i));302assertNull(map.get(keys[i]),303String.format("replaceIfMapped: get(%s[%d])", desc, i));304assertFalse(map.containsKey(keys[i]),305String.format("replaceIfMapped: containsKey(%s[%d])", desc, i));306}307assertFalse(map.containsValue(keys[i]),308String.format("replaceIfMapped: !containsValue(%s[%d])", desc, i));309}310assertEquals(map.size(), expectedSize1,311String.format("map expected size#1 m%d != k%d", map.size(), expectedSize1));312}313314@Test(dataProvider = "mapsWithObjectsAndStrings")315void testComputeIfPresentNonNull(String desc, Supplier<Map<Object, Object>> ms, Object val) {316Map<Object, Object> map = ms.get();317Object[] keys = map.keySet().toArray();318testComputeIfPresent(map, desc, keys, (k, v) -> val);319}320321@Test(dataProvider = "mapsWithObjectsAndStrings")322void testComputeIfPresentNull(String desc, Supplier<Map<Object, Object>> ms, Object val) {323Map<Object, Object> map = ms.get();324Object[] keys = map.keySet().toArray();325testComputeIfPresent(map, desc, keys, (k, v) -> null);326}327328@Test(dataProvider = "hashMapsWithObjects")329void testComputeNonNull(String desc, Supplier<Map<IntKey, IntKey>> ms, IntKey val) {330// remove a third of the keys331// call compute() for all keys[]332// all keys should be present: removed keys -> EXTRA, others to k-1333Map<IntKey, IntKey> map = ms.get();334IntKey[] keys = map.keySet().stream().sorted().toArray(IntKey[]::new);335BiFunction<IntKey, IntKey, IntKey> mappingFunction = (k, v) -> {336if (v == null) {337return val;338} else {339return keys[k.getValue() - 1];340}341};342removeThirdKeys(map, keys);343for (int i = 1; i < keys.length; i++) {344IntKey retVal = map.compute(keys[i], mappingFunction);345if (i % 3 != 2) { // key present, should be mapped to k-1346assertEquals(retVal, keys[i - 1],347String.format("compute: retVal(%s[%d])", desc, i));348assertEquals(keys[i - 1], map.get(keys[i]),349String.format("compute: get(%s[%d])", desc, i));350} else { // odd: was removed, should be replaced with EXTRA351assertEquals(retVal, val,352String.format("compute: retVal(%s[%d])", desc, i));353assertEquals(val, map.get(keys[i]),354String.format("compute: get(%s[%d])", desc, i));355}356assertTrue(map.containsKey(keys[i]),357String.format("compute: containsKey(%s[%d])", desc, i));358}359assertEquals(map.size(), keys.length,360String.format("map expected size#1 m%d != k%d", map.size(), keys.length));361assertTrue(map.containsValue(val),362String.format("compute: containsValue(%s[%s])", desc, val));363assertFalse(map.containsValue(null),364String.format("compute: !containsValue(%s,[null])", desc));365}366367@Test(dataProvider = "mapsWithObjectsAndStrings")368void testComputeNull(String desc, Supplier<Map<Object, Object>> ms, Object val) {369// remove a third of the keys370// call compute() for all keys[]371// removed keys should -> EXTRA372// for other keys: func returns null, should have no mapping373Map<Object, Object> map = ms.get();374Object[] keys = map.keySet().toArray();375BiFunction<Object, Object, Object> mappingFunction = (k, v) -> {376// if absent/null -> EXTRA377// if present -> null378if (v == null) {379return val;380} else {381return null;382}383};384int expectedSize = 0;385removeThirdKeys(map, keys);386for (int i = 0; i < keys.length; i++) {387Object retVal = map.compute(keys[i], mappingFunction);388if (i % 3 != 2) { // key present, func returned null, should be absent from map389assertNull(retVal,390String.format("compute: retVal(%s[%d])", desc, i));391assertNull(map.get(keys[i]),392String.format("compute: get(%s[%d])", desc, i));393assertFalse(map.containsKey(keys[i]),394String.format("compute: containsKey(%s[%d])", desc, i));395assertFalse(map.containsValue(keys[i]),396String.format("compute: containsValue(%s[%s])", desc, i));397} else { // odd: was removed, should now be mapped to EXTRA398assertEquals(retVal, val,399String.format("compute: retVal(%s[%d])", desc, i));400assertEquals(val, map.get(keys[i]),401String.format("compute: get(%s[%d])", desc, i));402assertTrue(map.containsKey(keys[i]),403String.format("compute: containsKey(%s[%d])", desc, i));404expectedSize++;405}406}407assertTrue(map.containsValue(val),408String.format("compute: containsValue(%s[%s])", desc, val));409assertEquals(map.size(), expectedSize,410String.format("map expected size#1 m%d != k%d", map.size(), expectedSize));411}412413@Test(dataProvider = "hashMapsWithObjects")414void testMergeNonNull(String desc, Supplier<Map<IntKey, IntKey>> ms, IntKey val) {415// remove a third of the keys416// call merge() for all keys[]417// all keys should be present: removed keys now -> EXTRA, other keys -> k-1418Map<IntKey, IntKey> map = ms.get();419IntKey[] keys = map.keySet().stream().sorted().toArray(IntKey[]::new);420421// Map to preceding key422BiFunction<IntKey, IntKey, IntKey> mappingFunction423= (k, v) -> keys[k.getValue() - 1];424removeThirdKeys(map, keys);425for (int i = 1; i < keys.length; i++) {426IntKey retVal = map.merge(keys[i], val, mappingFunction);427if (i % 3 != 2) { // key present, should be mapped to k-1428assertEquals(retVal, keys[i - 1],429String.format("compute: retVal(%s[%d])", desc, i));430assertEquals(keys[i - 1], map.get(keys[i]),431String.format("compute: get(%s[%d])", desc, i));432} else { // odd: was removed, should be replaced with EXTRA433assertEquals(retVal, val,434String.format("compute: retVal(%s[%d])", desc, i));435assertEquals(val, map.get(keys[i]),436String.format("compute: get(%s[%d])", desc, i));437}438assertTrue(map.containsKey(keys[i]),439String.format("compute: containsKey(%s[%d])", desc, i));440}441442assertEquals(map.size(), keys.length,443String.format("map expected size#1 m%d != k%d", map.size(), keys.length));444assertTrue(map.containsValue(val),445String.format("compute: containsValue(%s[%s])", desc, val));446assertFalse(map.containsValue(null),447String.format("compute: !containsValue(%s,[null])", desc));448}449450@Test(dataProvider = "mapsWithObjectsAndStrings")451void testMergeNull(String desc, Supplier<Map<Object, Object>> ms, Object val) {452// remove a third of the keys453// call merge() for all keys[]454// result: removed keys -> EXTRA, other keys absent455456Map<Object, Object> map = ms.get();457Object[] keys = map.keySet().toArray();458BiFunction<Object, Object, Object> mappingFunction = (k, v) -> null;459int expectedSize = 0;460removeThirdKeys(map, keys);461for (int i = 0; i < keys.length; i++) {462Object retVal = map.merge(keys[i], val, mappingFunction);463if (i % 3 != 2) { // key present, func returned null, should be absent from map464assertNull(retVal,465String.format("compute: retVal(%s[%d])", desc, i));466assertNull(map.get(keys[i]),467String.format("compute: get(%s[%d])", desc, i));468assertFalse(map.containsKey(keys[i]),469String.format("compute: containsKey(%s[%d])", desc, i));470} else { // odd: was removed, should now be mapped to EXTRA471assertEquals(retVal, val,472String.format("compute: retVal(%s[%d])", desc, i));473assertEquals(val, map.get(keys[i]),474String.format("compute: get(%s[%d])", desc, i));475assertTrue(map.containsKey(keys[i]),476String.format("compute: containsKey(%s[%d])", desc, i));477expectedSize++;478}479assertFalse(map.containsValue(keys[i]),480String.format("compute: containsValue(%s[%s])", desc, i));481}482assertTrue(map.containsValue(val),483String.format("compute: containsValue(%s[%s])", desc, val));484assertEquals(map.size(), expectedSize,485String.format("map expected size#1 m%d != k%d", map.size(), expectedSize));486}487488/*489* Remove half of the keys490*/491private static <T> void removeOddKeys(Map<T, T> map, /*String keys_desc, */ T[] keys) {492int removes = 0;493for (int i = 0; i < keys.length; i++) {494if (i % 2 != 0) {495map.remove(keys[i]);496removes++;497}498}499assertEquals(map.size(), keys.length - removes,500String.format("map expected size m%d != k%d", map.size(), keys.length - removes));501}502503/*504* Remove every third key505* This will hopefully leave some removed keys in TreeBins for, e.g., computeIfAbsent506* w/ a func that returns null.507*508* TODO: consider using this in other tests (and maybe adding a remapThirdKeys)509*/510private static <T> void removeThirdKeys(Map<T, T> map, /*String keys_desc, */ T[] keys) {511int removes = 0;512for (int i = 0; i < keys.length; i++) {513if (i % 3 == 2) {514map.remove(keys[i]);515removes++;516}517}518assertEquals(map.size(), keys.length - removes,519String.format("map expected size m%d != k%d", map.size(), keys.length - removes));520}521522/*523* Re-map the odd-numbered keys to map to the EXTRA value524*/525private static <T> void remapOddKeys(Map<T, T> map, T[] keys, T val) {526for (int i = 0; i < keys.length; i++) {527if (i % 2 != 0) {528map.put(keys[i], val);529}530}531}532533@DataProvider534public Iterator<Object[]> nullValueFriendlyMaps() {535return Arrays.asList(536new Object[]{"HashMap", (Supplier<Map<?, ?>>) HashMap::new},537new Object[]{"LinkedHashMap", (Supplier<Map<?, ?>>) LinkedHashMap::new},538new Object[]{"TreeMap", (Supplier<Map<?, ?>>) TreeMap::new},539new Object[]{"TreeMap(cmp)", (Supplier<Map<?, ?>>) () -> new TreeMap<>(Comparator.reverseOrder())},540new Object[]{"TreeMap.descendingMap", (Supplier<Map<?, ?>>) () -> new TreeMap<>().descendingMap()}541).iterator();542}543}544545546