mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 07:14:30 +02:00
8259622: TreeMap.computeIfAbsent deviates from spec
Reviewed-by: smarks
This commit is contained in:
parent
d701babb06
commit
2c8e337dff
2 changed files with 26 additions and 4 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -575,8 +575,12 @@ public class TreeMap<K,V>
|
||||||
t = t.left;
|
t = t.left;
|
||||||
else if (cmp > 0)
|
else if (cmp > 0)
|
||||||
t = t.right;
|
t = t.right;
|
||||||
else
|
else {
|
||||||
|
if (t.value == null) {
|
||||||
|
t.value = callMappingFunctionWithCheck(key, mappingFunction);
|
||||||
|
}
|
||||||
return t.value;
|
return t.value;
|
||||||
|
}
|
||||||
} while (t != null);
|
} while (t != null);
|
||||||
} else {
|
} else {
|
||||||
Objects.requireNonNull(key);
|
Objects.requireNonNull(key);
|
||||||
|
@ -589,8 +593,12 @@ public class TreeMap<K,V>
|
||||||
t = t.left;
|
t = t.left;
|
||||||
else if (cmp > 0)
|
else if (cmp > 0)
|
||||||
t = t.right;
|
t = t.right;
|
||||||
else
|
else {
|
||||||
|
if (t.value == null) {
|
||||||
|
t.value = callMappingFunctionWithCheck(key, mappingFunction);
|
||||||
|
}
|
||||||
return t.value;
|
return t.value;
|
||||||
|
}
|
||||||
} while (t != null);
|
} while (t != null);
|
||||||
}
|
}
|
||||||
newValue = callMappingFunctionWithCheck(key, mappingFunction);
|
newValue = callMappingFunctionWithCheck(key, mappingFunction);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -257,6 +257,20 @@ public class InPlaceOpsCollisions extends MapWithCollisionsProviders {
|
||||||
testComputeIfAbsent(map, desc, keys, (k) -> null);
|
testComputeIfAbsent(map, desc, keys, (k) -> null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(dataProvider = "nullValueFriendlyMaps")
|
||||||
|
void testComputeIfAbsentOverwriteNull(String desc, Supplier<Map<Object, Object>> ms) {
|
||||||
|
Map<Object, Object> map = ms.get();
|
||||||
|
map.put("key", null);
|
||||||
|
assertEquals(map.size(), 1, desc + ": size != 1");
|
||||||
|
assertTrue(map.containsKey("key"), desc + ": does not have key");
|
||||||
|
assertNull(map.get("key"), desc + ": value is not null");
|
||||||
|
Object result = map.computeIfAbsent("key", k -> "value"); // must rewrite
|
||||||
|
assertEquals(result, "value", desc + ": computeIfAbsent result is not 'value'");
|
||||||
|
assertEquals(map.size(), 1, desc + ": size != 1");
|
||||||
|
assertTrue(map.containsKey("key"), desc + ": does not have key");
|
||||||
|
assertEquals(map.get("key"), "value", desc + ": value is not 'value'");
|
||||||
|
}
|
||||||
|
|
||||||
private static <T> void testComputeIfPresent(Map<T, T> map, String desc, T[] keys,
|
private static <T> void testComputeIfPresent(Map<T, T> map, String desc, T[] keys,
|
||||||
BiFunction<T, T, T> mappingFunction) {
|
BiFunction<T, T, T> mappingFunction) {
|
||||||
// remove a third of the keys
|
// remove a third of the keys
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue