PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/test/com/googlecode/totallylazy/SequencesTest.java

https://code.google.com/p/totallylazy/
Java | 186 lines | 164 code | 22 blank | 0 comment | 1 complexity | 776fd760bd1c24d6fd84b975754a5eac MD5 | raw file
  1. package com.googlecode.totallylazy;
  2. import org.hamcrest.Matchers;
  3. import org.junit.Test;
  4. import java.util.Enumeration;
  5. import java.util.Vector;
  6. import static com.googlecode.totallylazy.Callables.returns;
  7. import static com.googlecode.totallylazy.Option.none;
  8. import static com.googlecode.totallylazy.Option.some;
  9. import static com.googlecode.totallylazy.Pair.pair;
  10. import static com.googlecode.totallylazy.Quadruple.quadruple;
  11. import static com.googlecode.totallylazy.Quintuple.quintuple;
  12. import static com.googlecode.totallylazy.Sequences.characters;
  13. import static com.googlecode.totallylazy.Sequences.empty;
  14. import static com.googlecode.totallylazy.Sequences.iterate;
  15. import static com.googlecode.totallylazy.Sequences.join;
  16. import static com.googlecode.totallylazy.Sequences.repeat;
  17. import static com.googlecode.totallylazy.Sequences.sequence;
  18. import static com.googlecode.totallylazy.Sequences.transpose;
  19. import static com.googlecode.totallylazy.Triple.triple;
  20. import static com.googlecode.totallylazy.matchers.IterableMatcher.hasExactly;
  21. import static com.googlecode.totallylazy.matchers.IterableMatcher.startsWith;
  22. import static com.googlecode.totallylazy.numbers.Numbers.decrement;
  23. import static com.googlecode.totallylazy.numbers.Numbers.even;
  24. import static com.googlecode.totallylazy.numbers.Numbers.increment;
  25. import static com.googlecode.totallylazy.numbers.Numbers.numbers;
  26. import static com.googlecode.totallylazy.numbers.Numbers.odd;
  27. import static com.googlecode.totallylazy.numbers.Numbers.range;
  28. import static java.util.Arrays.asList;
  29. import static org.hamcrest.MatcherAssert.assertThat;
  30. import static org.hamcrest.core.Is.is;
  31. public class SequencesTest {
  32. @Test
  33. public void shouldCreateEmptySequenceWhenIterableIsNull() throws Exception {
  34. assertThat(sequence((Iterable<Object>) null), is(empty()));
  35. assertThat(sequence((Object[]) null), is(empty(Object.class)));
  36. }
  37. @Test
  38. @SuppressWarnings("unchecked")
  39. public void supportsTranspose() {
  40. Sequence<Sequence<Integer>> transposed = transpose(sequence(1, 2), sequence(3, 4), sequence(5, 6));
  41. assertThat(transposed, hasExactly(sequence(1, 3, 5), sequence(2, 4, 6)));
  42. assertThat(transpose(transposed), hasExactly(sequence(1, 2), sequence(3, 4), sequence(5, 6)));
  43. assertThat(sequence(1, 2).transpose(sequence(3, 4)), hasExactly(sequence(1, 3), sequence(2, 4)));
  44. }
  45. @Test
  46. public void supportsUnzip() {
  47. Pair<Sequence<Integer>, Sequence<Integer>> pair = Sequences.unzip(sequence(pair(1, 2), pair(3, 4), pair(5, 6)));
  48. assertThat(pair.first(), hasExactly(1, 3, 5));
  49. assertThat(pair.second(), hasExactly(2, 4, 6));
  50. }
  51. @Test
  52. public void supportsUnzippingTriples() {
  53. Triple<Sequence<Integer>, Sequence<Integer>, Sequence<String>> triple = Sequences.unzip3(sequence(triple(1, 2, "car"), triple(3, 4, "cat")));
  54. assertThat(triple.first(), hasExactly(1, 3));
  55. assertThat(triple.second(), hasExactly(2, 4));
  56. assertThat(triple.third(), hasExactly("car", "cat"));
  57. }
  58. @Test
  59. public void supportsUnzippingQuadruples() {
  60. Quadruple<Sequence<Integer>, Sequence<Integer>, Sequence<String>, Sequence<Character>> quadruple = Sequences.unzip4(sequence(quadruple(1, 2, "car", 'C'), quadruple(3, 4, "cat", 'D')));
  61. assertThat(quadruple.first(), hasExactly(1, 3));
  62. assertThat(quadruple.second(), hasExactly(2, 4));
  63. assertThat(quadruple.third(), hasExactly("car", "cat"));
  64. assertThat(quadruple.fourth(), hasExactly('C', 'D'));
  65. }
  66. @Test
  67. public void supportsUnzippingQuintuples() {
  68. Quintuple<Sequence<Integer>, Sequence<Integer>, Sequence<String>, Sequence<Character>, Sequence<Integer>> quintuple =
  69. Sequences.unzip5(sequence(quintuple(1, 2, "car", 'C', 1), quintuple(3, 4, "cat", 'D', 2)));
  70. assertThat(quintuple.first(), hasExactly(1, 3));
  71. assertThat(quintuple.second(), hasExactly(2, 4));
  72. assertThat(quintuple.third(), hasExactly("car", "cat"));
  73. assertThat(quintuple.fourth(), hasExactly('C', 'D'));
  74. assertThat(quintuple.fifth(), hasExactly(1, 2));
  75. }
  76. @Test
  77. public void supportsCycle() throws Exception {
  78. assertThat(range(1, 3).cycle(), startsWith((Number) 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3));
  79. }
  80. @Test
  81. public void supportsAddingToAnEmptyList() throws Exception {
  82. assertThat(sequence().append(1).append(2).append(3), hasExactly((Object) 1, 2, 3));
  83. }
  84. @Test
  85. public void joinWorksEvenWhenFirstIterableIsEmpty() throws Exception {
  86. final Sequence<Integer> empty = Sequences.<Integer>empty();
  87. assertThat(empty.append(1).join(sequence(2, 3)).append(4), hasExactly(1, 2, 3, 4));
  88. assertThat(join(empty, sequence(1, 2, 3), empty, asList(4, 5, 6)), hasExactly(1, 2, 3, 4, 5, 6));
  89. }
  90. @Test
  91. public void supportsJoiningSubTypes() throws Exception {
  92. final Sequence<Number> numbers = numbers(2, 3.0D);
  93. Sequence<Integer> integers = sequence(2, 3);
  94. Sequence<Long> longs = sequence(2L, 3L);
  95. assertThat(numbers.join(integers).join(longs), hasExactly((Number) 2, 3.0D, 2, 3, 2L, 3L));
  96. assertThat(join(sequence(1L, 2.0D, 3), numbers, asList(4, 5, 6), integers), hasExactly((Number) 1L, 2.0D, 3, 2, 3.0D, 4, 5, 6, 2, 3));
  97. }
  98. @Test
  99. public void supportsEnumeration() throws Exception {
  100. Vector<String> vector = new Vector<String>();
  101. vector.add("foo");
  102. Enumeration enumeration = vector.elements();
  103. Sequence<String> forwardOnly = Sequences.forwardOnly(enumeration, String.class);
  104. assertThat(forwardOnly.headOption().isEmpty(), is(false));
  105. assertThat(forwardOnly.headOption().isEmpty(), is(true));
  106. Sequence<String> forwardOnlyWithType = Sequences.forwardOnly(vector.elements());
  107. assertThat(forwardOnlyWithType.headOption().isEmpty(), is(false));
  108. assertThat(forwardOnlyWithType.headOption().isEmpty(), is(true));
  109. }
  110. @Test
  111. public void supportsMemorisingAnEnumeration() throws Exception {
  112. Vector<String> vector = new Vector<String>();
  113. vector.add("foo");
  114. Enumeration enumeration = vector.elements();
  115. Sequence<String> memorise = Sequences.memorise(enumeration, String.class);
  116. assertThat(memorise.headOption().isEmpty(), is(false));
  117. assertThat(memorise.headOption().isEmpty(), is(false));
  118. Sequence<String> memorisedWithType = Sequences.memorise(vector.elements());
  119. assertThat(memorisedWithType.headOption().isEmpty(), is(false));
  120. assertThat(memorisedWithType.headOption().isEmpty(), is(false));
  121. }
  122. @Test
  123. public void supportRepeat() throws Exception {
  124. assertThat(repeat(10), startsWith(10, 10, 10, 10, 10));
  125. assertThat(repeat(returns(10)), startsWith(10, 10, 10, 10, 10));
  126. }
  127. @Test
  128. public void supportsCharacters() throws Exception {
  129. assertThat(characters("text"), hasExactly('t', 'e', 'x', 't'));
  130. assertThat(characters("text".toCharArray()), hasExactly('t', 'e', 'x', 't'));
  131. assertThat(characters("text").drop(2).toString(""), is("xt"));
  132. }
  133. @Test
  134. public void supportsIterate() throws Exception {
  135. assertThat(iterate(increment, 1), startsWith((Number) 1, 2, 3, 4, 5));
  136. }
  137. @Test
  138. public void supportsIteratingEvenWhenCallableReturnNull() throws Exception {
  139. final Sequence<Integer> sequence = iterate(new Callable1<Integer, Integer>() {
  140. public Integer call(Integer integer) throws Exception {
  141. assertThat("Should never see a null value", integer, is(Matchers.notNullValue()));
  142. return null;
  143. }
  144. }, 1).takeWhile(Predicates.notNullValue());
  145. assertThat(sequence, hasExactly(1));
  146. }
  147. @Test
  148. public void canCombineIterateWithOtherOperations() throws Exception {
  149. final Sequence<Number> numbers = iterate(increment, 1);
  150. assertThat(numbers.filter(even()), startsWith((Number) 2, 4, 6));
  151. assertThat(numbers.filter(odd()), startsWith((Number) 1, 3, 5, 7, 9));
  152. }
  153. @Test
  154. public void supportsUnfoldRight() throws Exception {
  155. Sequence<Number> result = Sequences.unfoldRight(new Function1<Number, Option<Pair<Number, Number>>>() {
  156. @Override
  157. public Option<Pair<Number, Number>> call(Number number) throws Exception {
  158. if (number.equals(0)) return none();
  159. return some(pair(number, decrement(number)));
  160. }
  161. }, 10);
  162. assertThat(result, is(range(10, 1)));
  163. }
  164. }