|
| 1 | +/** |
| 2 | + * Algorithms-in-Java |
| 3 | + * HoppingIterator_Test.java |
| 4 | + */ |
| 5 | +package com.deepak.algorithms.Iterators; |
| 6 | + |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import org.junit.Assert; |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +/** |
| 14 | + * Test cases for Hopping Iterator |
| 15 | + * |
| 16 | + * @author Deepak |
| 17 | + */ |
| 18 | +public class HoppingIterator_Test { |
| 19 | + |
| 20 | + /** |
| 21 | + * Test case for collection of integers |
| 22 | + */ |
| 23 | + @Test |
| 24 | + public void testHoppingIteratorForIntegers() { |
| 25 | + List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5, 6); |
| 26 | + |
| 27 | + /* Iterator with 0 hops, behaves the same way */ |
| 28 | + HoppingIterator<Integer> iteratorWith0Hops = new HoppingIterator<>(integerList.iterator(), 0); |
| 29 | + Assert.assertTrue(iteratorWith0Hops.hasNext()); |
| 30 | + Assert.assertTrue(iteratorWith0Hops.next() == 1); |
| 31 | + Assert.assertTrue(iteratorWith0Hops.next() == 2); |
| 32 | + Assert.assertTrue(iteratorWith0Hops.next() == 3); |
| 33 | + Assert.assertTrue(iteratorWith0Hops.hasNext()); |
| 34 | + Assert.assertTrue(iteratorWith0Hops.next() == 4); |
| 35 | + Assert.assertTrue(iteratorWith0Hops.hasNext()); |
| 36 | + Assert.assertTrue(iteratorWith0Hops.next() == 5); |
| 37 | + Assert.assertTrue(iteratorWith0Hops.next() == 6); |
| 38 | + |
| 39 | + /* Iterator with 1 hop, jumps 1 */ |
| 40 | + HoppingIterator<Integer> iteratorWith1Hops = new HoppingIterator<>(integerList.iterator(), 1); |
| 41 | + Assert.assertTrue(iteratorWith1Hops.hasNext()); |
| 42 | + Assert.assertTrue(iteratorWith1Hops.next() == 1); |
| 43 | + Assert.assertTrue(iteratorWith1Hops.next() == 3); |
| 44 | + Assert.assertTrue(iteratorWith1Hops.hasNext()); |
| 45 | + Assert.assertTrue(iteratorWith1Hops.next() == 5); |
| 46 | + |
| 47 | + /* Iterator with 2 hops, jumps 2 */ |
| 48 | + HoppingIterator<Integer> iteratorWith2Hops = new HoppingIterator<>(integerList.iterator(), 2); |
| 49 | + Assert.assertTrue(iteratorWith2Hops.hasNext()); |
| 50 | + Assert.assertTrue(iteratorWith2Hops.next() == 1); |
| 51 | + Assert.assertTrue(iteratorWith2Hops.next() == 4); |
| 52 | + Assert.assertFalse(iteratorWith2Hops.hasNext()); |
| 53 | + |
| 54 | + /* Iterator with 3 hops, jumps 3 */ |
| 55 | + HoppingIterator<Integer> iteratorWith3Hops = new HoppingIterator<>(integerList.iterator(), 3); |
| 56 | + Assert.assertTrue(iteratorWith3Hops.hasNext()); |
| 57 | + Assert.assertTrue(iteratorWith3Hops.next() == 1); |
| 58 | + Assert.assertTrue(iteratorWith3Hops.next() == 5); |
| 59 | + Assert.assertFalse(iteratorWith3Hops.hasNext()); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Test case for collection of strings |
| 64 | + */ |
| 65 | + @Test |
| 66 | + public void testHoppingIteratorForStrings() { |
| 67 | + List<String> stringList = Arrays.asList("A", "B", "C", "D", "E", "F"); |
| 68 | + |
| 69 | + /* Iterator with 0 hops, behaves the same way */ |
| 70 | + HoppingIterator<String> iteratorWith0Hops = new HoppingIterator<>(stringList.iterator(), 0); |
| 71 | + Assert.assertTrue(iteratorWith0Hops.hasNext()); |
| 72 | + Assert.assertTrue(iteratorWith0Hops.next() == "A"); |
| 73 | + Assert.assertTrue(iteratorWith0Hops.next() == "B"); |
| 74 | + Assert.assertTrue(iteratorWith0Hops.next() == "C"); |
| 75 | + Assert.assertTrue(iteratorWith0Hops.hasNext()); |
| 76 | + Assert.assertTrue(iteratorWith0Hops.next() == "D"); |
| 77 | + Assert.assertTrue(iteratorWith0Hops.hasNext()); |
| 78 | + Assert.assertTrue(iteratorWith0Hops.next() == "E"); |
| 79 | + Assert.assertTrue(iteratorWith0Hops.next() == "F"); |
| 80 | + |
| 81 | + /* Iterator with 1 hop, jumps 1 */ |
| 82 | + HoppingIterator<String> iteratorWith1Hops = new HoppingIterator<>(stringList.iterator(), 1); |
| 83 | + Assert.assertTrue(iteratorWith1Hops.hasNext()); |
| 84 | + Assert.assertTrue(iteratorWith1Hops.next() == "A"); |
| 85 | + Assert.assertTrue(iteratorWith1Hops.next() == "C"); |
| 86 | + Assert.assertTrue(iteratorWith1Hops.hasNext()); |
| 87 | + Assert.assertTrue(iteratorWith1Hops.next() == "E"); |
| 88 | + |
| 89 | + /* Iterator with 2 hops, jumps 2 */ |
| 90 | + HoppingIterator<String> iteratorWith2Hops = new HoppingIterator<>(stringList.iterator(), 2); |
| 91 | + Assert.assertTrue(iteratorWith2Hops.hasNext()); |
| 92 | + Assert.assertTrue(iteratorWith2Hops.next() == "A"); |
| 93 | + Assert.assertTrue(iteratorWith2Hops.next() == "D"); |
| 94 | + Assert.assertFalse(iteratorWith2Hops.hasNext()); |
| 95 | + |
| 96 | + /* Iterator with 3 hops, jumps 3 */ |
| 97 | + HoppingIterator<String> iteratorWith3Hops = new HoppingIterator<>(stringList.iterator(), 3); |
| 98 | + Assert.assertTrue(iteratorWith3Hops.hasNext()); |
| 99 | + Assert.assertTrue(iteratorWith3Hops.next() == "A"); |
| 100 | + Assert.assertTrue(iteratorWith3Hops.next() == "E"); |
| 101 | + Assert.assertFalse(iteratorWith3Hops.hasNext()); |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments