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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/*
* 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.shorts;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.util.Collections;
import java.util.function.Predicate;
import org.junit.Test;
import it.unimi.dsi.fastutil.MainRunner;
public class ShortArrayListTest {
@SuppressWarnings("unlikely-arg-type")
@Test
public void testEmptyListIsDifferentFromEmptySet() {
assertFalse(ShortLists.EMPTY_LIST.equals(ShortSets.EMPTY_SET));
assertFalse(ShortSets.EMPTY_SET.equals(ShortLists.EMPTY_LIST));
}
@SuppressWarnings("deprecation")
@Test
public void testNullInContains() {
assertFalse(new ShortArrayList().contains(null));
}
// Here because Java int -> short literal logic annoyingly only handles variable assignment, not method parameters.
private static short s(final int i) {
return it.unimi.dsi.fastutil.SafeMath.safeIntToShort(i);
}
@Test
public void testAddUsingIteratorToTheFirstPosition() {
final ShortArrayList list = new ShortArrayList();
list.add(s(24));
final ShortListIterator it = list.listIterator();
it.add(s(42));
assertTrue(it.hasNext());
assertEquals(ShortArrayList.wrap(new short[] { 42, 24 }), list);
}
@Test
public void testAddUsingIterator() {
final ShortArrayList list = new ShortArrayList();
list.add(s(24));
list.add(s(42));
final ShortListIterator it = list.listIterator(1);
it.add(s(86));
assertTrue(it.hasNext());
assertEquals(ShortArrayList.wrap(new short[] { 24, 86, 42}), list);
}
@Test
public void testAddUsingSublistIterator() {
final ShortArrayList list = new ShortArrayList();
list.add(s(24));
list.add(s(42));
final ShortList sublist = list.subList(1,2);
final ShortListIterator it = sublist.listIterator();
it.add(s(86));
assertTrue(it.hasNext());
assertEquals(ShortArrayList.wrap(new short[] { 86, 42 }), sublist);
assertEquals(ShortArrayList.wrap(new short[] { 24, 86, 42 }), list);
}
@Test
public void testAddUsingSubSublistIterator() {
final ShortArrayList list = new ShortArrayList();
list.add(s(24));
list.add(s(86));
list.add(s(42));
final ShortList sublist = list.subList(1,3);
assertEquals(ShortArrayList.wrap(new short[] { 86, 42 }), sublist);
final ShortList subsublist = sublist.subList(0,1);
assertEquals(ShortArrayList.wrap(new short[] { 86 }), subsublist);
final ShortListIterator it = subsublist.listIterator();
assertTrue(it.hasNext());
it.add(s(126));
assertTrue(it.hasNext());
assertEquals(ShortArrayList.wrap(new short[] { 126, 86 }), subsublist);
assertEquals(ShortArrayList.wrap(new short[] { 126, 86, 42 }), sublist);
assertEquals(ShortArrayList.wrap(new short[] { 24, 126, 86, 42 }), list);
}
@Test
public void testRemoveUsingIterator() {
final ShortArrayList list = new ShortArrayList();
list.add(s(24));
list.add(s(42));
final ShortListIterator it = list.listIterator(1);
assertEquals(42, it.nextShort());
it.remove();
assertFalse(it.hasNext());
assertEquals(ShortArrayList.wrap(new short[] { 24 }), list);
}
@Test
public void testRemoveUsingSublistIterator() {
final ShortArrayList list = new ShortArrayList();
list.add(s(24));
list.add(s(86));
list.add(s(42));
final ShortList sublist = list.subList(1,3);
final ShortListIterator it = sublist.listIterator();
assertEquals(86, it.nextShort());
it.remove();
assertTrue(it.hasNext());
assertEquals(ShortArrayList.wrap(new short[] { 42 }), sublist);
assertEquals(ShortArrayList.wrap(new short[] { 24, 42 }), list);
}
@Test
public void testRemoveUsingSubSublistIterator() {
final ShortArrayList list = new ShortArrayList();
list.add(s(24));
list.add(s(126));
list.add(s(86));
list.add(s(42));
final ShortList sublist = list.subList(1,3);
assertEquals(ShortArrayList.wrap(new short[] { 126, 86 }), sublist);
final ShortList subsublist = sublist.subList(0,1);
assertEquals(ShortArrayList.wrap(new short[] { 126 }), subsublist);
final ShortListIterator it = subsublist.listIterator();
assertTrue(it.hasNext());
assertEquals(126, it.nextShort());
it.remove();
assertFalse(it.hasNext());
assertEquals(ShortArrayList.wrap(new short[] { }), subsublist);
assertEquals(ShortArrayList.wrap(new short[] { 86 }), sublist);
assertEquals(ShortArrayList.wrap(new short[] { 24, 86, 42 }), list);
}
@Test
public void testAddAll() {
final ShortArrayList l = ShortArrayList.wrap(new short[] { 0, 1 });
l.addAll(ShortArrayList.wrap(new short[] { 2, 3 } ));
assertEquals(ShortArrayList.wrap(new short[] { 0, 1, 2, 3 }), l);
// Test object based lists still work too.
l.addAll(java.util.Arrays.asList(Short.valueOf(s(4)), Short.valueOf(s(5))));
assertEquals(ShortArrayList.wrap(new short[] { 0, 1, 2, 3, 4, 5 }), l);
}
@Test
public void testAddAllAtIndex() {
final ShortArrayList l = ShortArrayList.wrap(new short[] { 0, 3 });
l.addAll(1, ShortArrayList.wrap(new short[] { 1, 2 } ));
assertEquals(ShortArrayList.wrap(new short[] { 0, 1, 2, 3 }), l);
// Test object based lists still work too.
l.addAll(2, java.util.Arrays.asList(Short.valueOf(s(4)), Short.valueOf(s(5))));
assertEquals(ShortArrayList.wrap(new short[] { 0, 1, 4, 5, 2, 3 }), l);
}
@Test
public void testRemoveAll() {
ShortArrayList l = ShortArrayList.wrap(new short[] { 0, 1, 1, 2 });
l.removeAll(ShortSets.singleton((short)1));
assertEquals(ShortArrayList.wrap(new short[] { 0, 2 }), l);
l = ShortArrayList.wrap(new short[] { 0, 1, 1, 2 });
l.removeAll(Collections.singleton(Short.valueOf(s(1))));
assertEquals(ShortArrayList.wrap(new short[] { 0, 2 }), l);
}
@Test
public void testClearSublist() {
final ShortArrayList l = ShortArrayList.wrap(new short[] { 0, 1, 1, 2 });
final ShortList sublist = l.subList(1,3);
sublist.clear();
assertEquals(ShortArrayList.wrap(new short[] { }), sublist);
assertEquals(ShortArrayList.wrap(new short[] { 0, 2 }), l);
}
@Test
public void testSort() {
final ShortArrayList l = ShortArrayList.wrap(new short[] { 4, 2, 1, 3 });
l.sort(null);
assertEquals(ShortArrayList.wrap(new short[] { 1, 2, 3, 4 }), l);
}
@Test
public void testDefaultConstructors() {
ShortArrayList l;
l = new ShortArrayList();
for(int i = 0; i < ShortArrayList.DEFAULT_INITIAL_CAPACITY + 2; i++) l.add(s(0));
l = new ShortArrayList();
l.addElements(0, new short[ShortArrayList.DEFAULT_INITIAL_CAPACITY], 0, ShortArrayList.DEFAULT_INITIAL_CAPACITY);
l = new ShortArrayList();
l.addElements(0, new short[2 * ShortArrayList.DEFAULT_INITIAL_CAPACITY], 0, 2 * ShortArrayList.DEFAULT_INITIAL_CAPACITY);
l = new ShortArrayList(0);
for(int i = 0; i < ShortArrayList.DEFAULT_INITIAL_CAPACITY + 2; i++) l.add(s(0));
l = new ShortArrayList(0);
l.addElements(0, new short[ShortArrayList.DEFAULT_INITIAL_CAPACITY], 0, ShortArrayList.DEFAULT_INITIAL_CAPACITY);
l = new ShortArrayList(0);
l.addElements(0, new short[2 * ShortArrayList.DEFAULT_INITIAL_CAPACITY], 0, 2 * ShortArrayList.DEFAULT_INITIAL_CAPACITY);
l = new ShortArrayList(2 * ShortArrayList.DEFAULT_INITIAL_CAPACITY );
for(int i = 0; i < 3 * ShortArrayList.DEFAULT_INITIAL_CAPACITY; i++) l.add(s(0));
l = new ShortArrayList(2 * ShortArrayList.DEFAULT_INITIAL_CAPACITY );
l.addElements(0, new short[3 * ShortArrayList.DEFAULT_INITIAL_CAPACITY]);
l = new ShortArrayList(2 * ShortArrayList.DEFAULT_INITIAL_CAPACITY );
l.addElements(0, new short[3 * ShortArrayList.DEFAULT_INITIAL_CAPACITY]);
// Test lazy allocation
l = new ShortArrayList();
l.ensureCapacity(1);
assertSame(ShortArrays.DEFAULT_EMPTY_ARRAY, l.elements());
l.ensureCapacity(4);
assertSame(ShortArrays.DEFAULT_EMPTY_ARRAY, l.elements());
l = new ShortArrayList();
l.ensureCapacity(1000000);
assertNotSame(ShortArrays.DEFAULT_EMPTY_ARRAY, l.elements());
assertEquals(1000000, l.elements().length);
l = new ShortArrayList(0);
l.ensureCapacity(1);
assertNotSame(ShortArrays.DEFAULT_EMPTY_ARRAY, l.elements());
l = new ShortArrayList(0);
l.ensureCapacity(1);
assertNotSame(ShortArrays.DEFAULT_EMPTY_ARRAY, l.elements());
l.ensureCapacity(1);
}
@Test
public void testSizeOnDefaultInstance() {
final ShortArrayList l = new ShortArrayList();
l.size(100);
}
@Test
public void testForEach() {
final ShortArrayList l = new ShortArrayList(new short[] {1,2,3,4,5,6});
// A hack-ish loop testing forEach, in real code you would use intStream to compute the sum.
final int[] forEachSum = new int[1];
// i is a short here; we are getting ShortConsumer overload.
l.forEach(i -> forEachSum[0] += i);
final int realSum = l.intStream().sum();
assertEquals(realSum, forEachSum[0]);
}
@Test
public void testForEach_jdkConsumer() {
final ShortArrayList l = new ShortArrayList(new short[] {1,2,3,4,5,6});
// A hack-ish loop testing forEach, in real code you would use intStream to compute the sum.
final int[] forEachSum = new int[1];
l.forEach((java.util.function.IntConsumer) (i -> forEachSum[0] += i));
final int realSum = l.intStream().sum();
assertEquals(realSum, forEachSum[0]);
}
@SuppressWarnings("deprecation")
@Test
public void testForEach_objectConsumer() {
final ShortArrayList l = new ShortArrayList(new short[] {1,2,3,4,5,6});
// A hack-ish loop testing forEach, in real code you would use intStream to compute the sum.
final int[] forEachSum = new int[1];
l.forEach((java.util.function.Consumer<Short>)(i -> forEachSum[0] += i.shortValue()));
final int realSum = l.intStream().sum();
assertEquals(realSum, forEachSum[0]);
}
@Test
public void testIterator_forEachRemaining() {
final ShortArrayList l = new ShortArrayList(new short[] {1,2,3,4,5,6});
// A hack-ish loop testing forEach, in real code you would use intStream to compute the sum.
final int[] forEachSum = new int[1];
l.iterator().forEachRemaining(i -> forEachSum[0] += i);
final int realSum = l.intStream().sum();
assertEquals(realSum, forEachSum[0]);
}
@Test
public void testIterator_forEachRemaining_jdkConsumer() {
final ShortArrayList l = new ShortArrayList(new short[] {1,2,3,4,5,6});
// A hack-ish loop testing forEach, in real code you would use intStream to compute the sum.
final int[] forEachSum = new int[1];
l.iterator().forEachRemaining((java.util.function.IntConsumer) (i -> forEachSum[0] += i));
final int realSum = l.intStream().sum();
assertEquals(realSum, forEachSum[0]);
}
@SuppressWarnings("deprecation")
@Test
public void testIterator_forEachRemaining_objectConsumer() {
final ShortArrayList l = new ShortArrayList(new short[] {1,2,3,4,5,6});
// A hack-ish loop testing forEach, in real code you would use intStream to compute the sum.
final int[] forEachSum = new int[1];
l.iterator().forEachRemaining((java.util.function.Consumer<Short>)(i -> forEachSum[0] += i.shortValue()));
final int realSum = l.intStream().sum();
assertEquals(realSum, forEachSum[0]);
}
@Test
public void testRemoveIf() {
final ShortArrayList l = new ShortArrayList(new short[] {1,2,3,4,5,6});
l.removeIf(i -> i % 2 == 0);
assertEquals(ShortArrayList.of(s(1),s(3),s(5)), l);
}
@Test
public void testRemoveIf_jdkPredicate() {
final ShortArrayList l = new ShortArrayList(new short[] {1,2,3,4,5,6});
l.removeIf((java.util.function.IntPredicate) (i -> i % 2 == 0));
assertEquals(ShortArrayList.of(s(1),s(3),s(5)), l);
}
@SuppressWarnings("deprecation")
@Test
public void testRemoveIf_objectPredicate() {
final ShortArrayList l = new ShortArrayList(new short[] {1,2,3,4,5,6});
l.removeIf((java.util.function.Predicate<Short>)(i -> i.shortValue() % 2 == 0));
assertEquals(ShortArrayList.of(s(1),s(3),s(5)), l);
}
@Test
public void testOf() {
final ShortArrayList l = ShortArrayList.of(s(0), s(1), s(2));
assertEquals(ShortArrayList.wrap(new short[] { 0, 1, 2 }), l);
}
public void testOfEmpty() {
final ShortArrayList l = ShortArrayList.of();
assertTrue(l.isEmpty());
}
@Test
public void testOfSingleton() {
final ShortArrayList l = ShortArrayList.of(s(0));
assertEquals(ShortArrayList.wrap(new short[]{0}), l);
}
@Test
public void testLegacyMainMethodTests() throws Exception {
MainRunner.callMainIfExists(ShortArrayList.class, "test", /*num=*/"500", /*seed=*/"939384");
}
@SuppressWarnings("deprecation")
@Test
public void testRemoveIfOverloads() {
final ShortArrayList l = ShortArrayList.of((short)1, (short)2, (short)3);
l.removeIf(x -> x == 1);
final ShortPredicate p = x -> x == 1;
l.removeIf(p);
final it.unimi.dsi.fastutil.ints.IntPredicate q = x -> x == 1;
l.removeIf(q);
@SuppressWarnings("boxing")
final Predicate<Short> r = x -> x == 1;
l.removeIf(r);
}
}
|