1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
/*
* 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 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;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import org.junit.Test;
import it.unimi.dsi.fastutil.io.BinIO;
public class IntArraySetTest {
@Test
public void testCopyConstructor() {
final IntOpenHashSet s = new IntOpenHashSet(new int[] { 1, 2 });
assertEquals(new IntArraySet(s), s);
assertEquals(new HashSet<>(s), s);
}
@SuppressWarnings("boxing")
@Test
public void testNullInEquals() {
assertFalse(new IntArraySet(Arrays.asList(42)).equals(Collections.singleton(null)));
}
@Test
public void testSet() {
for (int i = 0; i <= 1; i++) {
final IntArraySet s = i == 0 ? new IntArraySet() : new IntArraySet(new int[i]);
assertTrue(s.add(1));
assertEquals(1 + i, s.size());
assertTrue(s.contains(1));
assertTrue(s.add(2));
assertTrue(s.contains(2));
assertEquals(2 + i, s.size());
assertFalse(s.add(1));
assertFalse(s.remove(3));
assertTrue(s.add(3));
assertEquals(3 + i, s.size());
assertTrue(s.contains(1));
assertTrue(s.contains(2));
assertTrue(s.contains(3));
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()));
assertEquals(expected, new IntArraySet(s.intStream().toArray()));
// Test iterator and spliterator (through stream) preserve order.
assertArrayEquals(expectedArray, s.toIntArray());
assertArrayEquals(expectedArray, new IntArrayList(s.iterator()).toIntArray());
assertArrayEquals(expectedArray, s.intStream().toArray());
assertTrue(s.remove(3));
assertEquals(2 + i, s.size());
assertTrue(s.remove(1));
assertEquals(1 + i, s.size());
assertFalse(s.contains(1));
assertTrue(s.remove(2));
assertEquals(0 + i, s.size());
assertFalse(s.contains(1));
}
}
@Test
public void testClone() {
final IntArraySet s = new IntArraySet();
assertEquals(s, s.clone());
s.add(0);
assertEquals(s, s.clone());
s.add(0);
assertEquals(s, s.clone());
s.add(1);
assertEquals(s, s.clone());
s.add(2);
assertEquals(s, s.clone());
s.remove(0);
assertEquals(s, s.clone());
}
@Test
public void testSerialisation() throws IOException, ClassNotFoundException {
final IntArraySet s = new IntArraySet();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(s);
oos.close();
assertEquals(s, BinIO.loadObject(new ByteArrayInputStream(baos.toByteArray())));
s.add(0);
s.add(1);
baos.reset();
oos = new ObjectOutputStream(baos);
oos.writeObject(s);
oos.close();
assertEquals(s, BinIO.loadObject(new ByteArrayInputStream(baos.toByteArray())));
}
@Test
public void testRemove() {
IntSet set = new IntArraySet(new int[] { 42 });
IntIterator iterator = set.iterator();
assertTrue(iterator.hasNext());
iterator.nextInt();
iterator.remove();
assertFalse(iterator.hasNext());
assertEquals(0, set.size());
set = new IntArraySet(new int[] { 42, 43, 44 });
iterator = set.iterator();
assertTrue(iterator.hasNext());
iterator.nextInt();
iterator.nextInt();
iterator.remove();
assertEquals(44, iterator.nextInt());
assertFalse(iterator.hasNext());
assertEquals(new IntArraySet(new int[] { 42, 44 }), set);
}
@Test
public void testOf() {
final IntArraySet s = IntArraySet.of(0, 1, 2);
assertEquals(new IntArraySet(new int[] { 0, 1, 2 }), s);
}
@Test
public void testOfEmpty() {
final IntArraySet s = IntArraySet.of();
assertTrue(s.isEmpty());
}
@Test
public void testOfSingleton() {
final IntArraySet s = IntArraySet.of(0);
assertEquals(new IntArraySet(new int[] { 0 }), s);
}
@Test(expected = IllegalArgumentException.class)
public void testOfDuplicateThrows() {
IntArraySet.of(0, 0);
}
@Test
public void testOfUnchecked() {
final IntArraySet s = IntArraySet.ofUnchecked(0, 1, 2);
assertEquals(new IntArraySet(new int[] { 0, 1, 2 }), s);
}
@Test
public void testOfUncheckedEmpty() {
final IntArraySet s = IntArraySet.ofUnchecked();
assertTrue(s.isEmpty());
}
@Test
public void testOfUnchekedSingleton() {
final IntArraySet s = IntArraySet.ofUnchecked(0);
assertEquals(new IntArraySet(new int[] { 0 }), s);
}
@Test
public void testOfUnchekedDuplicatesNotDetected() {
// A IntArraySet in an invalid state that by spec we aren't checking for in this method.
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());
}
@Test(expected = IllegalStateException.class)
public void testRemovalAtStart() {
final IntArraySet s = IntArraySet.ofUnchecked(0, 1);
final IntIterator i = s.intIterator();
i.remove();
}
@Test
public void testForEachRemaining() {
final IntArraySet s = IntArraySet.ofUnchecked(0, 1, 2, 3, 4);
for (int i = 0; i <= s.size(); i++) {
final IntIterator iterator = s.intIterator();
final int[] j = new int[1];
for(j[0] = 0; j[0] < i; j[0]++) iterator.nextInt();
iterator.forEachRemaining(x -> {
if (x != j[0]++) throw new AssertionError();
});
}
}
@Test(expected = IllegalStateException.class)
public void testSkipZeroAtStart() {
final IntArraySet s = IntArraySet.ofUnchecked(0, 1);
final IntIterator i = s.intIterator();
i.skip(0);
i.remove();
}
@Test
public void testSkipOneAtStart() {
final IntArraySet s = IntArraySet.ofUnchecked(0, 1);
final IntIterator i = s.intIterator();
i.skip(1);
i.remove();
assertEquals(IntArraySet.ofUnchecked(1), s);
}
@Test
public void testSkipBeyondEnd() {
final IntArraySet s = IntArraySet.ofUnchecked(0, 1);
final IntIterator i = s.intIterator();
i.skip(4);
i.remove();
assertEquals(IntArraySet.ofUnchecked(0), s);
}
}
|