/src/test/java/lambda/enumerable/primitives/EnumerableDoublesTest.java

https://github.com/danielbodart/enumerable · Java · 294 lines · 238 code · 56 blank · 0 comment · 4 complexity · dbc1c9ce6f36a67a8344bedcf6177700 MD5 · raw file

  1. package lambda.enumerable.primitives;
  2. import static lambda.Parameters.*;
  3. import static lambda.enumerable.primitives.EnumerableDoubles.*;
  4. import static lambda.primitives.LambdaPrimitives.*;
  5. import static org.junit.Assert.*;
  6. import java.util.HashSet;
  7. import java.util.List;
  8. import java.util.Set;
  9. import lambda.TestBase;
  10. import lambda.annotation.LambdaParameter;
  11. import lambda.enumerable.EnumerableArrays;
  12. import org.junit.Test;
  13. public class EnumerableDoublesTest extends TestBase {
  14. double[] doublesOneToFive = new double[] { 1, 2, 3, 4, 5 };
  15. @Test
  16. public void callsBlockOnceForEachElement() throws Exception {
  17. List<Double> actual = list();
  18. each(doublesOneToFive, λ(d, actual.add(d)));
  19. assertEquals(toList(doublesOneToFive), actual);
  20. }
  21. @Test
  22. public void canUsePrimitiveLambdaWithMatchingObjectVersion() throws Exception {
  23. List<Double> actual = list();
  24. EnumerableArrays.each(new Double[] { 1.0, 2.0, 3.0, 4.0, 5.0 }, λ(d, actual.add(d)));
  25. assertEquals(toList(doublesOneToFive), actual);
  26. }
  27. @Test
  28. public void callsBlockOnceForEachElementWithDoubleReturn() throws Exception {
  29. double result = 1;
  30. each(doublesOneToFive, λ(d, result *= 3.14 * d));
  31. assertEquals(36629.373141888005, result, 0);
  32. }
  33. @Test
  34. public void eachReturnsArray() throws Exception {
  35. assertArrayEquals(doublesOneToFive, each(doublesOneToFive, λ(d, d)), 0.0);
  36. }
  37. @LambdaParameter
  38. public static int[] ints;
  39. @Test
  40. public void collectElementsToDifferentType() throws Exception {
  41. String[] expected = new String[] { "#1.0", "#2.0", "#3.0", "#4.0", "#5.0" };
  42. Object[] actual = collect(doublesOneToFive, λ(d, "#" + d));
  43. assertArrayEquals(expected, actual);
  44. assertFalse(expected.getClass().equals(actual.getClass()));
  45. String[] actualStrings = collect(doublesOneToFive, λ(d, "#" + d), String.class);
  46. assertArrayEquals(expected, actualStrings);
  47. }
  48. @Test
  49. public void collectElementsToDifferentTypeWithEmptyArray() throws Exception {
  50. String[] expected = new String[] {};
  51. Object[] actuals = collect(new double[0], λ(d, "#" + d));
  52. assertArrayEquals(expected, actuals);
  53. }
  54. @Test
  55. public void detectFirstMatchingElement() throws Exception {
  56. assertEquals(2.0, detect(doublesOneToFive, ifNone(-1), λ(d, d % 2 == 0)), 0.0);
  57. }
  58. @Test
  59. public void detectReturnsIfNoneValueIfNoMatch() throws Exception {
  60. assertEquals(-1.0, detect(doublesOneToFive, ifNone(-1), λ(d, d > 5)), 0.0);
  61. }
  62. @Test
  63. public void eachWithIndexUsingMixedPrimitivesDoubleAndInt() throws Exception {
  64. int totalIndex = 0;
  65. eachWithIndex(doublesOneToFive, λ(d, idx, totalIndex += idx));
  66. assertEquals(10, totalIndex);
  67. }
  68. @Test
  69. public void eachWithIndexUsingMixedPrimitivesDoubleAndLong() throws Exception {
  70. int totalIndex = 0;
  71. eachWithIndex(doublesOneToFive, λ(d, l, totalIndex += l));
  72. assertEquals(10, totalIndex);
  73. }
  74. @LambdaParameter
  75. static float aFloat;
  76. @Test
  77. public void eachWithIndexUsingMixedPrimitivesDoubleAndFloat() throws Exception {
  78. float totalIndex = 0;
  79. eachWithIndex(doublesOneToFive, λ(d, aFloat, totalIndex += aFloat));
  80. assertEquals(10, totalIndex, 0.0);
  81. }
  82. @LambdaParameter
  83. static short aShort;
  84. @Test
  85. public void eachWithIndexUsingMixedPrimitivesDoubleAndShort() throws Exception {
  86. short totalIndex = 0;
  87. eachWithIndex(doublesOneToFive, λ(d, aShort, totalIndex += aShort));
  88. assertEquals(10, totalIndex);
  89. }
  90. @LambdaParameter
  91. static byte aByte;
  92. @Test
  93. public void eachWithIndexUsingMixedPrimitivesDoubleAndByte() throws Exception {
  94. byte totalIndex = 0;
  95. eachWithIndex(doublesOneToFive, λ(d, aByte, totalIndex += aByte));
  96. assertEquals(10, totalIndex);
  97. }
  98. @LambdaParameter
  99. static char aChar;
  100. @Test
  101. public void eachWithIndexUsingMixedPrimitivesDoubleAndChar() throws Exception {
  102. char totalIndex = 0;
  103. eachWithIndex(doublesOneToFive, λ(d, aChar, totalIndex += aChar));
  104. assertEquals(10, totalIndex);
  105. }
  106. @Test
  107. public void eachWithIndexUsingMixedPrimitivesIntAndDouble() throws Exception {
  108. int total = 0;
  109. eachWithIndex(doublesOneToFive, λ(i, d, total += i));
  110. assertEquals(15, total);
  111. }
  112. @Test
  113. public void eachWithIndexUsingMixedPrimitivesLongAndDouble() throws Exception {
  114. int total = 0;
  115. eachWithIndex(doublesOneToFive, λ(l, d, total += l));
  116. assertEquals(15, total);
  117. }
  118. @Test
  119. public void eachWithIndexIsZeroBased() throws Exception {
  120. List<Integer> actual = list();
  121. eachWithIndex(doublesOneToFive, λ(d, x, actual.add((int) x + 1)));
  122. assertEquals(list(1, 2, 3, 4, 5), actual);
  123. }
  124. @Test
  125. public void eachWithIndexToString() throws Exception {
  126. String indexes = "";
  127. eachWithIndex(doublesOneToFive, λ(d, x, indexes += x));
  128. assertEquals("0.01.02.03.04.0", indexes);
  129. }
  130. @Test
  131. public void selectMatchingElements() throws Exception {
  132. double[] selected = select(doublesOneToFive, λ(d, d % 2 == 0));
  133. assertArrayEquals(new double[] { 2, 4 }, selected, 0.0);
  134. }
  135. @Test
  136. public void rejectMatchingElements() throws Exception {
  137. double[] odd = { 1, 3, 5 };
  138. assertArrayEquals(odd, reject(doublesOneToFive, λ(d, d % 2 == 0)), 0.0);
  139. }
  140. @Test
  141. public void sortUsingNaturalOrder() throws Exception {
  142. assertArrayEquals(doublesOneToFive, sort(new double[] { 5, 4, 3, 2, 1 }), 0.0);
  143. }
  144. @Test
  145. public void injectUsingInitialValue() throws Exception {
  146. assertEquals(15, inject(doublesOneToFive, 0, λ(x, y, x + y)), 0.0);
  147. }
  148. @Test
  149. public void injectWithoutInitialValue() throws Exception {
  150. assertEquals(120, inject(doublesOneToFive, λ(x, y, x * y)), 0.0);
  151. }
  152. @Test
  153. public void injectWithoutInitialValueAndOnlyOneElementReturnsElement() throws Exception {
  154. assertEquals(1.0, inject(new double[] { 1.0 }, λ(x, y, x * y)), 0.0);
  155. }
  156. @Test(expected = ArrayIndexOutOfBoundsException.class)
  157. public void injectWithoutInitialValueAndEmptyArrayThrowsException() throws Exception {
  158. inject(new double[0], λ(x, y, x * y));
  159. }
  160. @Test
  161. public void anyOnEmptyArray() throws Exception {
  162. assertFalse(any(new double[0], λ(d, d > 0)));
  163. }
  164. @Test
  165. public void anyMarchingPredicate() throws Exception {
  166. assertTrue(any(doublesOneToFive, λ(d, d > 3)));
  167. }
  168. @Test
  169. public void anyNotMatchingPredicate() throws Exception {
  170. assertFalse(any(doublesOneToFive, λ(d, d > 5)));
  171. }
  172. @Test
  173. public void allMatchingPredicate() throws Exception {
  174. assertTrue(all(doublesOneToFive, λ(d, d > 0)));
  175. }
  176. @Test
  177. public void allNotMatchingPredicate() throws Exception {
  178. assertFalse(all(doublesOneToFive, λ(d, d > 1)));
  179. }
  180. @Test
  181. public void allOnEmptyList() throws Exception {
  182. assertTrue(all(new double[0], λ(d, d > 0)));
  183. }
  184. @Test(expected = ArrayIndexOutOfBoundsException.class)
  185. public void maxThrowsExceptionForEmptyArray() throws Exception {
  186. max(new double[0]);
  187. }
  188. @Test
  189. public void maxReturnsLastValueUsingNaturalOrder() throws Exception {
  190. assertEquals(5, max(doublesOneToFive), 0.0);
  191. }
  192. @Test
  193. public void maxReturnsFirstValueUsingReverseNaturalOrder() throws Exception {
  194. assertEquals(1, max(doublesOneToFive, λ(x, y, y - x)), 0.0);
  195. }
  196. @Test
  197. public void minReturnsFirstValueUsingNaturalOrder() throws Exception {
  198. assertEquals(1, min(doublesOneToFive), 0.0);
  199. }
  200. @Test
  201. public void minReturnsLastValueUsingReverseNaturalOrder() throws Exception {
  202. assertEquals(5, min(doublesOneToFive, λ(x, y, y - x)), 0.0);
  203. }
  204. @Test(expected = ArrayIndexOutOfBoundsException.class)
  205. public void minThrowsExceptionForEmptyArray() throws Exception {
  206. min(new double[0]);
  207. }
  208. @Test
  209. public void memberReturnsTrueForExistingElement() throws Exception {
  210. assertTrue(member(doublesOneToFive, 3));
  211. }
  212. @Test
  213. public void memberReturnsFalseForNonExistingElement() throws Exception {
  214. assertFalse(member(doublesOneToFive, 0));
  215. }
  216. @Test
  217. public void toListCreatesIntegerListFromDoubleArray() throws Exception {
  218. assertEquals(list(1.0, 2.0, 3.0, 4.0, 5.0), toList(doublesOneToFive));
  219. }
  220. @Test
  221. public void toSetCreatesIntegerSetFromDoubleArray() throws Exception {
  222. Set<Double> expected = new HashSet<Double>(list(1.0, 2.0, 3.0, 4.0));
  223. assertEquals(expected, toSet(new double[] { 1, 2, 2, 3, 4, 4 }));
  224. }
  225. @Test
  226. public void toSetCreatesIntegerSetFromIntArrayUsingBlock() throws Exception {
  227. Set<String> expected = new HashSet<String>(list("1.0", "2.0", "3.0", "4.0"));
  228. assertEquals(expected, toSet(new double[] { 1, 2, 2, 3, 4, 4 }, λ(d, d + "")));
  229. }
  230. @Test
  231. public void partitionArrayIntoTwoBasedOnPredicate() throws Exception {
  232. double[] even = { 2, 4 };
  233. double[] odd = { 1, 3, 5 };
  234. double[][] partition = partition(doublesOneToFive, λ(d, d % 2 == 0));
  235. assertArrayEquals(even, partition[0], 0.0);
  236. assertArrayEquals(odd, partition[1], 0.0);
  237. }
  238. }