classlib: fix issue in TreeMap iterator remove method

This commit is contained in:
Alexey Andreev 2023-10-09 20:35:54 +02:00
parent dabe0d5d74
commit 05454380d9
2 changed files with 31 additions and 4 deletions

View File

@ -732,7 +732,13 @@ public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TCloneable, TS
if (last == null) { if (last == null) {
throw new TNoSuchElementException(); throw new TNoSuchElementException();
} }
owner.root = owner.deleteNode(owner.root, last.getKey()); var newRoot = owner.deleteNode(owner.root, last.getKey());
if (owner.root != newRoot) {
owner.root = newRoot;
var newPath = owner.pathToNext(last.getKey(), reverse);
System.arraycopy(newPath, 0, path, 0, newPath.length);
depth = newPath.length;
}
modCount = ++owner.modCount; modCount = ++owner.modCount;
last = null; last = null;
} }

View File

@ -68,7 +68,7 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.teavm.junit.TeaVMTestRunner; import org.teavm.junit.TeaVMTestRunner;
@SuppressWarnings({ "UnnecessaryTemporaryOnConversionToString", "SuspiciousMethodCalls" }) @SuppressWarnings("SuspiciousMethodCalls")
@RunWith(TeaVMTestRunner.class) @RunWith(TeaVMTestRunner.class)
public class TreeMapTest { public class TreeMapTest {
@ -148,9 +148,9 @@ public class TreeMapTest {
public void test_ConstructorLjava_util_Map() { public void test_ConstructorLjava_util_Map() {
// Test for method java.util.TreeMap(java.util.Map) // Test for method java.util.TreeMap(java.util.Map)
TreeMap<Object, Object> myTreeMap = new TreeMap<>(new HashMap<>(tm)); TreeMap<Object, Object> myTreeMap = new TreeMap<>(new HashMap<>(tm));
assertTrue("Map is incorrect size", myTreeMap.size() == objArray.length); assertEquals("Map is incorrect size", objArray.length, myTreeMap.size());
for (Object element : objArray) { for (Object element : objArray) {
assertTrue("Map has incorrect mappings", myTreeMap.get(element.toString()).equals(element)); assertEquals("Map has incorrect mappings", myTreeMap.get(element.toString()), element);
} }
} }
@ -707,4 +707,25 @@ public class TreeMapTest {
assertEquals("{10=119}", map.subMap(10, 29).toString()); assertEquals("{10=119}", map.subMap(10, 29).toString());
assertEquals("{}", map.subMap(29, 100).toString()); assertEquals("{}", map.subMap(29, 100).toString());
} }
@Test
public void iteratorRemove() {
var keys = new String[] { "a", "b", "c", "d", "e", "f", "g" };
for (var i = 1; i < keys.length; ++i) {
for (var j = 0; j < i; ++j) {
var map = new TreeMap<String, Integer>();
for (var k = 0; k < i; ++k) {
map.put(keys[k], k);
}
var iter = map.keySet().iterator();
for (var k = 0; k < i; ++k) {
assertEquals(keys[k], iter.next());
if (k == j) {
iter.remove();
}
}
assertFalse(iter.hasNext());
}
}
}
} }