Remove broken .toArray from IntObjectHashMap entirely (#13876)

This commit is contained in:
Dawid Weiss 2024-10-09 21:48:08 +02:00
parent d888f6d5a7
commit af3200ae34
2 changed files with 15 additions and 15 deletions

View File

@ -562,15 +562,6 @@ public class IntObjectHashMap<VType>
public int size() { public int size() {
return IntObjectHashMap.this.size(); return IntObjectHashMap.this.size();
} }
public VType[] toArray() {
VType[] array = (VType[]) new Object[size()];
int i = 0;
for (ObjectCursor<VType> cursor : this) {
array[i++] = cursor.value;
}
return array;
}
} }
/** An iterator over the set of assigned values. */ /** An iterator over the set of assigned values. */

View File

@ -18,12 +18,15 @@
package org.apache.lucene.internal.hppc; package org.apache.lucene.internal.hppc;
import com.carrotsearch.randomizedtesting.RandomizedTest; import com.carrotsearch.randomizedtesting.RandomizedTest;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import org.apache.lucene.tests.util.LuceneTestCase; import org.apache.lucene.tests.util.LuceneTestCase;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
/** /**
@ -66,10 +69,8 @@ public class TestIntObjectHashMap extends LuceneTestCase {
} }
/** Check if the array's content is identical to a given sequence of elements. */ /** Check if the array's content is identical to a given sequence of elements. */
private static void assertSortedListEquals(Object[] array, Object... elements) { private static void assertSortedListEquals(List<Object> array, Object... elements) {
assertEquals(elements.length, array.length); Assert.assertEquals(Arrays.asList(elements), array.stream().sorted().toList());
Arrays.sort(array);
assertArrayEquals(elements, array);
} }
private final int value0 = vcast(0); private final int value0 = vcast(0);
@ -584,13 +585,21 @@ public class TestIntObjectHashMap extends LuceneTestCase {
map.put(key1, value3); map.put(key1, value3);
map.put(key2, value2); map.put(key2, value2);
map.put(key3, value1); map.put(key3, value1);
assertSortedListEquals(map.values().toArray(), value1, value2, value3); assertSortedListEquals(toList(map.values()), value1, value2, value3);
map.clear(); map.clear();
map.put(key1, value1); map.put(key1, value1);
map.put(key2, value2); map.put(key2, value2);
map.put(key3, value2); map.put(key3, value2);
assertSortedListEquals(map.values().toArray(), value1, value2, value2); assertSortedListEquals(toList(map.values()), value1, value2, value2);
}
private static <T> List<T> toList(Iterable<ObjectCursor<T>> values) {
ArrayList<T> list = new ArrayList<>();
for (var c : values) {
list.add(c.value);
}
return list;
} }
/* */ /* */