|
| 1 | +package com.fasterxml.sort; |
| 2 | + |
| 3 | +import com.fasterxml.sort.util.CollectionReader; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.Iterator; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +public class TestIterableSorter extends SortTestBase |
| 11 | +{ |
| 12 | + private static final List<Integer> INPUT = Arrays.asList(5, 2, 8, 1, 4, 6, 9, 3, 7); |
| 13 | + private static final List<Integer> EXPECTED = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9); |
| 14 | + |
| 15 | + public void testBasic() throws IOException { |
| 16 | + IteratingSorter<Integer> sorter = new IteratingSorter<Integer>(); |
| 17 | + sortAndCheck("", sorter); |
| 18 | + sorter.close(); |
| 19 | + } |
| 20 | + |
| 21 | + public void testReused() throws IOException { |
| 22 | + IteratingSorter<Integer> sorter = new IteratingSorter<Integer>(); |
| 23 | + sortAndCheck("pass 1 ", sorter); |
| 24 | + sortAndCheck("pass 2 ", sorter); |
| 25 | + sortAndCheck("pass 3 ", sorter); |
| 26 | + sorter.close(); |
| 27 | + } |
| 28 | + |
| 29 | + private void sortAndCheck(String msg, IteratingSorter<Integer> sorter) throws IOException { |
| 30 | + Iterator<Integer> sorterIt = sorter.sort(new CollectionReader<Integer>(INPUT)); |
| 31 | + assertTrue("sort success", sorterIt != null); |
| 32 | + Iterator<Integer> expectedIt = EXPECTED.iterator(); |
| 33 | + int i = 0; |
| 34 | + while(expectedIt.hasNext()) { |
| 35 | + assertTrue(msg + "hasNext on " + i, sorterIt.hasNext()); |
| 36 | + assertEquals(msg + "value on " + i, expectedIt.next(), sorterIt.next()); |
| 37 | + } |
| 38 | + assertFalse(msg + "extra items", sorterIt.hasNext()); |
| 39 | + } |
| 40 | +} |
0 commit comments