|
| 1 | +package fj.data; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import static org.hamcrest.core.Is.is; |
| 6 | +import static org.junit.Assert.assertThat; |
| 7 | + |
| 8 | +public class ZipperTest { |
| 9 | + @Test |
| 10 | + public void testZipper() { |
| 11 | + Zipper<Integer> z = Zipper.zipper(Stream.nil(), 0, Stream.range(1, 9)); |
| 12 | + assertThat(z.map(i -> i + 13).toStream(), is(Stream.range(13, 22))); |
| 13 | + } |
| 14 | + |
| 15 | + @Test |
| 16 | + public void testNext() { |
| 17 | + Zipper<Integer> z = Zipper.zipper(Stream.single(1), 2, Stream.single(3)); |
| 18 | + z = z.next().some(); |
| 19 | + assertThat(z.lefts(), is(Stream.arrayStream(new Integer[]{2, 1}))); |
| 20 | + assertThat(z.focus(), is(3)); |
| 21 | + assertThat(z.rights(), is(Stream.nil())); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void testNextNone() { |
| 26 | + Zipper<Integer> z = Zipper.zipper(Stream.single(1), 2, Stream.nil()); |
| 27 | + assertThat(z.next().isNone(), is(true)); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void testCycleNext() { |
| 32 | + Zipper<Integer> z = Zipper.zipper(Stream.single(1), 2, Stream.single(3)); |
| 33 | + assertThat(z.cycleNext(), is(z.next().some())); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testCycleNextLast() { |
| 38 | + Zipper<Integer> z = Zipper.zipper(Stream.single(1), 2, Stream.nil()); |
| 39 | + z = z.cycleNext(); |
| 40 | + assertThat(z.lefts(), is(Stream.nil())); |
| 41 | + assertThat(z.focus(), is(1)); |
| 42 | + assertThat(z.rights(), is(Stream.single(2))); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testPrevious() { |
| 47 | + Zipper<Integer> z = Zipper.zipper(Stream.single(1), 2, Stream.single(3)); |
| 48 | + z = z.previous().some(); |
| 49 | + assertThat(z.lefts(), is(Stream.nil())); |
| 50 | + assertThat(z.focus(), is(1)); |
| 51 | + assertThat(z.rights(), is(Stream.arrayStream(new Integer[]{2, 3}))); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testPreviousNone() { |
| 56 | + Zipper<Integer> z = Zipper.zipper(Stream.nil(), 2, Stream.single(3)); |
| 57 | + assertThat(z.previous().isNone(), is(true)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testCyclePrevious() { |
| 62 | + Zipper<Integer> z = Zipper.zipper(Stream.single(1), 2, Stream.single(3)); |
| 63 | + assertThat(z.cyclePrevious(), is(z.previous().some())); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testCyclePreviousFirst() { |
| 68 | + Zipper<Integer> z = Zipper.zipper(Stream.nil(), 1, Stream.single(2)); |
| 69 | + z = z.cyclePrevious(); |
| 70 | + assertThat(z.lefts(), is(Stream.single(1))); |
| 71 | + assertThat(z.focus(), is(2)); |
| 72 | + assertThat(z.rights(), is(Stream.nil())); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testInsertLeft() { |
| 77 | + Zipper<Integer> z = Zipper.single(2); |
| 78 | + z = z.insertLeft(1); |
| 79 | + assertThat(z.lefts(), is(Stream.nil())); |
| 80 | + assertThat(z.focus(), is(1)); |
| 81 | + assertThat(z.rights(), is(Stream.single(2))); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testInsertRight() { |
| 86 | + Zipper<Integer> z = Zipper.single(2); |
| 87 | + z = z.insertRight(3); |
| 88 | + assertThat(z.lefts(), is(Stream.single(2))); |
| 89 | + assertThat(z.focus(), is(3)); |
| 90 | + assertThat(z.rights(), is(Stream.nil())); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testDeleteOthers() { |
| 95 | + Zipper<Integer> z = Zipper.zipper(Stream.single(1), 2, Stream.single(3)); |
| 96 | + z = z.deleteOthers(); |
| 97 | + assertThat(z.lefts(), is(Stream.nil())); |
| 98 | + assertThat(z.focus(), is(2)); |
| 99 | + assertThat(z.rights(), is(Stream.nil())); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testFind() { |
| 104 | + Zipper<Integer> z = Zipper.zipper(Stream.nil(), 0, Stream.range(1)); |
| 105 | + z = z.find(i -> i == 4).some(); |
| 106 | + assertThat(z.lefts(), is(Stream.arrayStream(new Integer[]{3, 2, 1, 0}))); |
| 107 | + assertThat(z.focus(), is(4)); |
| 108 | + assertThat(z.rights().take(3), is(Stream.range(5, 8))); |
| 109 | + } |
| 110 | +} |
0 commit comments