8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.Comparator;
28 import java.util.Comparators;
29 import java.util.List;
30 import java.util.LinkedList;
31 import java.util.Stack;
32 import java.util.TreeMap;
33 import java.util.TreeSet;
34 import java.util.Vector;
35 import java.util.concurrent.CopyOnWriteArrayList;
36 import java.util.concurrent.atomic.AtomicBoolean;
37 import java.util.concurrent.atomic.AtomicInteger;
38
39 import org.testng.annotations.DataProvider;
40 import org.testng.annotations.Test;
41
42 import static org.testng.Assert.assertEquals;
43 import static org.testng.Assert.assertFalse;
44 import static org.testng.Assert.assertTrue;
45 import static org.testng.Assert.fail;
46
47 import java.lang.reflect.Constructor;
48 import java.util.ConcurrentModificationException;
320 });
321 }
322 }
323
324 @Test
325 public void testSort() throws Exception {
326 final CollectionSupplier supplier = new CollectionSupplier(LIST_CLASSES, SIZE);
327 for (final CollectionSupplier.TestCase test : supplier.get()) {
328 final List<Integer> original = ((List<Integer>) test.original);
329 final List<Integer> list = ((List<Integer>) test.collection);
330 CollectionSupplier.shuffle(list);
331 list.sort(Integer::compare);
332 CollectionAsserts.assertSorted(list, Integer::compare);
333 if (test.name.startsWith("reverse")) {
334 Collections.reverse(list);
335 }
336 CollectionAsserts.assertContents(list, original);
337
338 CollectionSupplier.shuffle(list);
339 list.sort(null);
340 CollectionAsserts.assertSorted(list, Comparators.<Integer>naturalOrder());
341 if (test.name.startsWith("reverse")) {
342 Collections.reverse(list);
343 }
344 CollectionAsserts.assertContents(list, original);
345
346 CollectionSupplier.shuffle(list);
347 list.sort(Comparators.<Integer>naturalOrder());
348 CollectionAsserts.assertSorted(list, Comparators.<Integer>naturalOrder());
349 if (test.name.startsWith("reverse")) {
350 Collections.reverse(list);
351 }
352 CollectionAsserts.assertContents(list, original);
353
354 CollectionSupplier.shuffle(list);
355 list.sort(Comparators.<Integer>reverseOrder());
356 CollectionAsserts.assertSorted(list, Comparators.<Integer>reverseOrder());
357 if (!test.name.startsWith("reverse")) {
358 Collections.reverse(list);
359 }
360 CollectionAsserts.assertContents(list, original);
361
362 CollectionSupplier.shuffle(list);
363 list.sort(BIT_COUNT_COMPARATOR);
364 CollectionAsserts.assertSorted(list, BIT_COUNT_COMPARATOR);
365 // check sort by verifying that bitCount increases and never drops
366 int minBitCount = 0;
367 int bitCount = 0;
368 for (final Integer i : list) {
369 bitCount = Integer.bitCount(i);
370 assertTrue(bitCount >= minBitCount);
371 minBitCount = bitCount;
372 }
373
374 @SuppressWarnings("unchecked")
375 final Class<? extends List<AtomicInteger>> type =
376 (Class<? extends List<AtomicInteger>>) Class.forName(test.className);
377 final Constructor<? extends List<AtomicInteger>> defaultConstructor = type.getConstructor();
378 final List<AtomicInteger> incomparables = (List<AtomicInteger>) defaultConstructor.newInstance();
379
380 for (int i=0; i < test.original.size(); i++) {
381 incomparables.add(new AtomicInteger(i));
382 }
383 CollectionSupplier.shuffle(incomparables);
384 incomparables.sort(ATOMIC_INTEGER_COMPARATOR);
385 for (int i=0; i < test.original.size(); i++) {
386 assertEquals(i, incomparables.get(i).intValue());
387 }
388
389 if (original.size() > SUBLIST_SIZE) {
390 final List<Integer> copy = new ArrayList<>(list);
391 final List<Integer> subList = list.subList(SUBLIST_FROM, SUBLIST_TO);
392 CollectionSupplier.shuffle(subList);
393 subList.sort(Comparators.<Integer>naturalOrder());
394 CollectionAsserts.assertSorted(subList, Comparators.<Integer>naturalOrder());
395 // verify that elements [0, from) remain unmodified
396 for (int i = 0; i < SUBLIST_FROM; i++) {
397 assertTrue(list.get(i) == copy.get(i),
398 "mismatch at index " + i);
399 }
400 // verify that elements [to, size) remain unmodified
401 for (int i = SUBLIST_TO; i < list.size(); i++) {
402 assertTrue(list.get(i) == copy.get(i),
403 "mismatch at index " + i);
404 }
405 }
406 }
407
408 for (final CollectionSupplier.TestCase test : supplier.get()) {
409 final List<Integer> list = ((List<Integer>) test.collection);
410 trimmedSubList(list, new Callback() {
411 @Override
412 public void call(final List<Integer> list) {
413 final List<Integer> copy = new ArrayList<>(list);
414 CollectionSupplier.shuffle(list);
415 list.sort(Comparators.<Integer>naturalOrder());
416 CollectionAsserts.assertSorted(list, Comparators.<Integer>naturalOrder());
417 }
418 });
419 }
420 }
421
422 @Test
423 public void testForEachThrowsCME() throws Exception {
424 final CollectionSupplier supplier = new CollectionSupplier(LIST_CME_CLASSES, SIZE);
425 for (final CollectionSupplier.TestCase test : supplier.get()) {
426 final List<Integer> list = ((List<Integer>) test.collection);
427 if (list.size() <= 1) {
428 continue;
429 }
430 boolean gotException = false;
431 try {
432 // bad predicate that modifies its list, should throw CME
433 list.forEach((x) -> {list.add(x);});
434 } catch (ConcurrentModificationException cme) {
435 gotException = true;
436 }
|
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.Comparator;
28 import java.util.List;
29 import java.util.LinkedList;
30 import java.util.Stack;
31 import java.util.TreeMap;
32 import java.util.TreeSet;
33 import java.util.Vector;
34 import java.util.concurrent.CopyOnWriteArrayList;
35 import java.util.concurrent.atomic.AtomicBoolean;
36 import java.util.concurrent.atomic.AtomicInteger;
37
38 import org.testng.annotations.DataProvider;
39 import org.testng.annotations.Test;
40
41 import static org.testng.Assert.assertEquals;
42 import static org.testng.Assert.assertFalse;
43 import static org.testng.Assert.assertTrue;
44 import static org.testng.Assert.fail;
45
46 import java.lang.reflect.Constructor;
47 import java.util.ConcurrentModificationException;
319 });
320 }
321 }
322
323 @Test
324 public void testSort() throws Exception {
325 final CollectionSupplier supplier = new CollectionSupplier(LIST_CLASSES, SIZE);
326 for (final CollectionSupplier.TestCase test : supplier.get()) {
327 final List<Integer> original = ((List<Integer>) test.original);
328 final List<Integer> list = ((List<Integer>) test.collection);
329 CollectionSupplier.shuffle(list);
330 list.sort(Integer::compare);
331 CollectionAsserts.assertSorted(list, Integer::compare);
332 if (test.name.startsWith("reverse")) {
333 Collections.reverse(list);
334 }
335 CollectionAsserts.assertContents(list, original);
336
337 CollectionSupplier.shuffle(list);
338 list.sort(null);
339 CollectionAsserts.assertSorted(list, Comparator.<Integer>naturalOrder());
340 if (test.name.startsWith("reverse")) {
341 Collections.reverse(list);
342 }
343 CollectionAsserts.assertContents(list, original);
344
345 CollectionSupplier.shuffle(list);
346 list.sort(Comparator.<Integer>naturalOrder());
347 CollectionAsserts.assertSorted(list, Comparator.<Integer>naturalOrder());
348 if (test.name.startsWith("reverse")) {
349 Collections.reverse(list);
350 }
351 CollectionAsserts.assertContents(list, original);
352
353 CollectionSupplier.shuffle(list);
354 list.sort(Comparator.<Integer>reverseOrder());
355 CollectionAsserts.assertSorted(list, Comparator.<Integer>reverseOrder());
356 if (!test.name.startsWith("reverse")) {
357 Collections.reverse(list);
358 }
359 CollectionAsserts.assertContents(list, original);
360
361 CollectionSupplier.shuffle(list);
362 list.sort(BIT_COUNT_COMPARATOR);
363 CollectionAsserts.assertSorted(list, BIT_COUNT_COMPARATOR);
364 // check sort by verifying that bitCount increases and never drops
365 int minBitCount = 0;
366 int bitCount = 0;
367 for (final Integer i : list) {
368 bitCount = Integer.bitCount(i);
369 assertTrue(bitCount >= minBitCount);
370 minBitCount = bitCount;
371 }
372
373 @SuppressWarnings("unchecked")
374 final Class<? extends List<AtomicInteger>> type =
375 (Class<? extends List<AtomicInteger>>) Class.forName(test.className);
376 final Constructor<? extends List<AtomicInteger>> defaultConstructor = type.getConstructor();
377 final List<AtomicInteger> incomparables = (List<AtomicInteger>) defaultConstructor.newInstance();
378
379 for (int i=0; i < test.original.size(); i++) {
380 incomparables.add(new AtomicInteger(i));
381 }
382 CollectionSupplier.shuffle(incomparables);
383 incomparables.sort(ATOMIC_INTEGER_COMPARATOR);
384 for (int i=0; i < test.original.size(); i++) {
385 assertEquals(i, incomparables.get(i).intValue());
386 }
387
388 if (original.size() > SUBLIST_SIZE) {
389 final List<Integer> copy = new ArrayList<>(list);
390 final List<Integer> subList = list.subList(SUBLIST_FROM, SUBLIST_TO);
391 CollectionSupplier.shuffle(subList);
392 subList.sort(Comparator.<Integer>naturalOrder());
393 CollectionAsserts.assertSorted(subList, Comparator.<Integer>naturalOrder());
394 // verify that elements [0, from) remain unmodified
395 for (int i = 0; i < SUBLIST_FROM; i++) {
396 assertTrue(list.get(i) == copy.get(i),
397 "mismatch at index " + i);
398 }
399 // verify that elements [to, size) remain unmodified
400 for (int i = SUBLIST_TO; i < list.size(); i++) {
401 assertTrue(list.get(i) == copy.get(i),
402 "mismatch at index " + i);
403 }
404 }
405 }
406
407 for (final CollectionSupplier.TestCase test : supplier.get()) {
408 final List<Integer> list = ((List<Integer>) test.collection);
409 trimmedSubList(list, new Callback() {
410 @Override
411 public void call(final List<Integer> list) {
412 final List<Integer> copy = new ArrayList<>(list);
413 CollectionSupplier.shuffle(list);
414 list.sort(Comparator.<Integer>naturalOrder());
415 CollectionAsserts.assertSorted(list, Comparator.<Integer>naturalOrder());
416 }
417 });
418 }
419 }
420
421 @Test
422 public void testForEachThrowsCME() throws Exception {
423 final CollectionSupplier supplier = new CollectionSupplier(LIST_CME_CLASSES, SIZE);
424 for (final CollectionSupplier.TestCase test : supplier.get()) {
425 final List<Integer> list = ((List<Integer>) test.collection);
426 if (list.size() <= 1) {
427 continue;
428 }
429 boolean gotException = false;
430 try {
431 // bad predicate that modifies its list, should throw CME
432 list.forEach((x) -> {list.add(x);});
433 } catch (ConcurrentModificationException cme) {
434 gotException = true;
435 }
|