Tests for new toArray() methods

This commit is contained in:
Sebastiano Vigna 2022-09-27 22:51:10 +02:00
parent c29b109fe3
commit 25d02f6ace
8 changed files with 107 additions and 5 deletions

View file

@ -122,7 +122,11 @@ public abstract class ABSTRACT_COLLECTION KEY_GENERIC extends AbstractCollection
@Override
public KEY_TYPE[] TO_KEY_ARRAY() {
return toArray((KEY_TYPE[]) null);
final int size = size();
if (size == 0) return ARRAYS.EMPTY_ARRAY;
final KEY_TYPE a[] = new KEY_TYPE[size];
ITERATORS.unwrap(iterator(), a);
return a;
}
/** {@inheritDoc}

View file

@ -600,4 +600,9 @@ public class IntArrayListTest {
public void testLegacyMainMethodTests() throws Exception {
MainRunner.callMainIfExists(IntArrayList.class, "test", /*num=*/"500", /*seed=*/"939384");
}
@Test
public void testZeroLengthToArray() {
assertSame(IntArrays.EMPTY_ARRAY, new IntArrayList().toIntArray());
}
}

View file

@ -19,6 +19,7 @@ package it.unimi.dsi.fastutil.ints;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
@ -65,8 +66,8 @@ public class IntArraySetTest {
assertTrue(s.contains(1));
assertTrue(s.contains(2));
assertTrue(s.contains(3));
int[] expectedArray = i == 0 ? new int[] { 1, 2, 3 } : new int[] { 0, 1, 2, 3 };
IntSet expected = new IntOpenHashSet(expectedArray);
final int[] expectedArray = i == 0 ? new int[] { 1, 2, 3 } : new int[] { 0, 1, 2, 3 };
final IntSet expected = new IntOpenHashSet(expectedArray);
assertEquals(expected, s);
assertEquals(s, expected);
assertEquals(expected, new IntOpenHashSet(s.iterator()));
@ -191,4 +192,9 @@ public class IntArraySetTest {
final IntArraySet s = IntArraySet.ofUnchecked(0, 0);
assertEquals(new IntArraySet(new int[] { 0 , 0 }), s);
}
@Test
public void testZeroLengthToArray() {
assertSame(IntArrays.EMPTY_ARRAY, new IntArraySet().toIntArray());
}
}

View file

@ -0,0 +1,30 @@
/*
* Copyright (C) 2017-2022 Sebastiano Vigna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package it.unimi.dsi.fastutil.ints;
import static org.junit.Assert.assertArrayEquals;
import org.junit.Test;
public class IntBigListsTest {
@Test
public void testSingletonToArray() {
assertArrayEquals(new int[] { 1 }, IntBigLists.singleton(1).toIntArray());
}
}

View file

@ -16,10 +16,11 @@
package it.unimi.dsi.fastutil.ints;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
@ -313,4 +314,9 @@ public class IntImmutableListTest {
assertTrue(baseList.compareTo(greaterBecauseItIsLarger) < 0);
assertTrue(baseList.compareTo(equalList) == 0);
}
@Test
public void testZeroLengthToArray() {
assertSame(IntArrays.EMPTY_ARRAY, IntImmutableList.of().toIntArray());
}
}

View file

@ -225,4 +225,22 @@ public class ObjectArrayListTest {
public void testLegacyMainMethodTests() throws Exception {
MainRunner.callMainIfExists(ObjectArrayList.class, "test", /*num=*/"500", /*seed=*/"939384");
}
@Test
public void testZeroLengthToArray() {
assertSame(ObjectArrays.EMPTY_ARRAY, new ObjectArrayList<Integer>().toArray());
assertSame(ObjectArrays.EMPTY_ARRAY, new AbstractObjectList<Integer>() {
@Override
public Integer get(final int index) {
throw new ArrayIndexOutOfBoundsException();
}
@Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
}.toArray());
}
}

View file

@ -234,4 +234,9 @@ public class ObjectArraySetTest {
final ObjectArraySet<String> l = ObjectArraySet.ofUnchecked("0", "0");
assertEquals(new ObjectArraySet<>(new String[] { "0" , "0" }), l);
}
@Test
public void testZeroLengthToArray() {
assertSame(ObjectArrays.EMPTY_ARRAY, new ObjectArraySet<Integer>().toArray());
}
}

View file

@ -0,0 +1,28 @@
/*
* Copyright (C) 2017-2022 Sebastiano Vigna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package it.unimi.dsi.fastutil.objects;
import static org.junit.Assert.assertSame;
import org.junit.Test;
public class ObjectImmutableListTest {
@Test
public void testZeroLengthToArray() {
assertSame(ObjectArrays.EMPTY_ARRAY, ObjectImmutableList.of().toArray());
}
}