package edd.util; import static org.junit.Assert.*; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; import org.junit.Test; public class ListsAndIteratorsTest { @Test public void testDuplicate() { List doubleList = new ArrayList<>(); assertEquals("[]", ListsAndIterators.duplicate(doubleList).toString()); List integerList = Arrays.asList(4, 8, 15, 16, 23, 42); assertEquals("[4, 4, 8, 8, 15, 15, 16, 16, 23, 23, 42, 42]", ListsAndIterators.duplicate(integerList).toString()); List characterList = Arrays.asList('a', 'b', 'c', 'd', 'e'); assertEquals("[a, a, b, b, c, c, d, d, e, e]", ListsAndIterators .duplicate(characterList).toString()); } @Test public void testConsecute() { List doubleList = new ArrayList<>(); assertEquals("[]", ListsAndIterators.consecute(doubleList).toString()); List integerList = Arrays.asList(1, 2, 3, 4, 5); assertEquals("[1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]", ListsAndIterators.consecute(integerList).toString()); List characterList = Arrays.asList('a', 'b', 'c', 'd'); assertEquals("[a, b, b, c, c, c, d, d, d, d]", ListsAndIterators .consecute(characterList).toString()); } @Test public void testRemoveDuplicates() { List integerList = new ArrayList<>(); assertEquals("[]", ListsAndIterators.removeDuplicates(integerList) .toString()); integerList = Arrays.asList(4, 1, 4, 2, 4, 3, 4, 1, 2, 3); assertEquals("[4, 1, 2, 3]", ListsAndIterators.removeDuplicates(integerList).toString()); integerList = Arrays.asList(4, 1, 2, 3); assertEquals("[4, 1, 2, 3]", ListsAndIterators.removeDuplicates(integerList).toString()); List stringList = Arrays.asList("a", "b", "c", "a", "b", "c", "d", "a", "b", "c"); assertEquals("[a, b, c, d]", ListsAndIterators.removeDuplicates(stringList).toString()); } private void testIterators(Iterator it, int[] expected) { try { it.remove(); fail(); } catch (UnsupportedOperationException e) { } for (int i = 0; i < expected.length; i++) { assertTrue(it.hasNext()); assertEquals(expected[i], (int) it.next()); } try { it.next(); fail(); } catch (NoSuchElementException e) { } } @Test public void testPow2() { testIterators(ListsAndIterators.pow2(4), new int[] { 1, 2, 4, 8 }); testIterators(ListsAndIterators.pow2(10), new int[] { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 }); testIterators(ListsAndIterators.pow2(30), new int[] { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912 }); } @Test public void testPrimes() { testIterators(ListsAndIterators.primes(1), new int[] {}); testIterators(ListsAndIterators.primes(2), new int[] { 2 }); testIterators(ListsAndIterators.primes(5), new int[] { 2, 3, 5 }); testIterators(ListsAndIterators.primes(10), new int[] { 2, 3, 5, 7 }); testIterators(ListsAndIterators.primes(50), new int[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 }); testIterators(ListsAndIterators.primes(1000), new int[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997 }); } @Test public void testSquares() { testIterators(ListsAndIterators.squares(new int[] {}), new int[] {}); testIterators( ListsAndIterators.squares(new int[] { 10, -1, 0, 4, -5 }), new int[] { 100, 1, 0, 16, 25 }); testIterators( ListsAndIterators.squares(new int[] { -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 }), new int[] { 25, 16, 9, 4, 1, 0, 1, 4, 9, 16, 25 }); } }