|
3 | 3 | import org.highj.data.List; |
4 | 4 | import org.highj.data.Maybe; |
5 | 5 | import org.highj.data.num.Integers; |
| 6 | +import org.highj.function.Strings; |
6 | 7 | import org.junit.Test; |
7 | 8 |
|
| 9 | +import java.util.function.BiFunction; |
8 | 10 | import java.util.function.Function; |
9 | 11 |
|
10 | | -import static org.junit.Assert.assertEquals; |
11 | | -import static org.junit.Assert.assertTrue; |
| 12 | +import static org.assertj.core.api.Assertions.assertThat; |
| 13 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
12 | 14 |
|
13 | 15 |
|
14 | 16 | public class FoldableTest { |
15 | 17 |
|
16 | 18 | private final Function<String, Function<String, String>> wrapFn = a -> b -> "(" + a + "," + b + ")"; |
| 19 | + private final BiFunction<String, String, String> wrapBiFn = (a, b) -> "(" + a + "," + b + ")"; |
17 | 20 |
|
18 | 21 | @Test |
19 | | - public void fold() { |
| 22 | + public void fold() { |
20 | 23 | List<Integer> numbers = List.of(1, 2, 3, 4, 5); |
21 | | - int result = List.traversable.fold(Integers.multiplicativeMonoid, numbers); |
22 | | - assertEquals(120, result); |
| 24 | + assertThat(List.traversable.fold(Integers.multiplicativeMonoid, numbers)).isEqualTo(120); |
| 25 | + |
| 26 | + assertThat(List.traversable.fold(Integers.multiplicativeMonoid, List.of())).isEqualTo(1); |
23 | 27 | } |
24 | 28 |
|
25 | 29 | @Test |
26 | | - public void foldMap() { |
| 30 | + public void foldMap() { |
27 | 31 | List<String> strings = List.of("a", "bb", "ccc", "dddd", "eeeee"); |
28 | 32 | int result = List.traversable.foldMap(Integers.multiplicativeMonoid, String::length, strings); |
29 | | - assertEquals(120, result); |
| 33 | + assertThat(result).isEqualTo(120); |
| 34 | + |
| 35 | + assertThat(List.traversable.foldMap(Integers.multiplicativeMonoid, String::length, List.of())) |
| 36 | + .isEqualTo(1); |
30 | 37 | } |
31 | 38 |
|
32 | 39 | @Test |
33 | | - public void foldr() { |
| 40 | + public void foldr() { |
34 | 41 | List<String> strings = List.of("a", "e", "i", "o"); |
35 | 42 | String result = List.traversable.foldr(wrapFn, "u", strings); |
36 | | - assertEquals("(a,(e,(i,(o,u))))", result); |
| 43 | + assertThat(result).isEqualTo("(a,(e,(i,(o,u))))"); |
| 44 | + |
| 45 | + result = List.traversable.foldr(wrapBiFn, "u", strings); |
| 46 | + assertThat(result).isEqualTo("(a,(e,(i,(o,u))))"); |
37 | 47 | } |
38 | 48 |
|
39 | 49 | @Test |
40 | | - public void foldl() { |
| 50 | + public void foldl() { |
41 | 51 | List<String> strings = List.of("e", "i", "o", "u"); |
42 | 52 | String result = List.traversable.foldl(wrapFn, "a", strings); |
43 | | - assertEquals("((((a,e),i),o),u)", result); |
| 53 | + assertThat(result).isEqualTo("((((a,e),i),o),u)"); |
| 54 | + |
| 55 | + result = List.traversable.foldl(wrapBiFn, "a", strings); |
| 56 | + assertThat(result).isEqualTo("((((a,e),i),o),u)"); |
44 | 57 | } |
45 | 58 |
|
46 | 59 | @Test |
47 | | - public void foldr1() { |
| 60 | + public void foldr1() { |
48 | 61 | List<String> strings = List.of("a", "e", "i", "o", "u"); |
49 | 62 | Maybe<String> result = List.traversable.foldr1(wrapFn, strings); |
50 | | - assertEquals("(a,(e,(i,(o,u))))", result.get()); |
51 | | - List<String> noStrings = List.of(); |
52 | | - assertTrue(List.traversable.foldr1(wrapFn, noStrings).isNothing()); |
| 63 | + assertThat(result.get()).isEqualTo("(a,(e,(i,(o,u))))"); |
| 64 | + assertThat(List.traversable.foldr1(wrapFn, List.of())).isEmpty(); |
| 65 | + |
| 66 | + result = List.traversable.foldr1(wrapBiFn, strings); |
| 67 | + assertThat(result.get()).isEqualTo("(a,(e,(i,(o,u))))"); |
| 68 | + assertThat(List.traversable.foldr1(wrapBiFn, List.of())).isEmpty(); |
53 | 69 | } |
54 | 70 |
|
55 | 71 | @Test |
56 | | - public void foldl1() { |
| 72 | + public void foldl1() { |
57 | 73 | List<String> strings = List.of("a", "e", "i", "o", "u"); |
58 | 74 | Maybe<String> result = List.traversable.foldl1(wrapFn, strings); |
59 | | - assertEquals("((((a,e),i),o),u)", result.get()); |
60 | | - List<String> noStrings = List.of(); |
61 | | - assertTrue(List.traversable.foldl1(wrapFn, noStrings).isNothing()); |
| 75 | + assertThat(result.get()).isEqualTo("((((a,e),i),o),u)"); |
| 76 | + assertThat(List.traversable.foldl1(wrapFn, List.of())).isEmpty(); |
| 77 | + |
| 78 | + result = List.traversable.foldl1(wrapBiFn, strings); |
| 79 | + assertThat(result.get()).isEqualTo("((((a,e),i),o),u)"); |
| 80 | + assertThat(List.traversable.foldl1(wrapBiFn, List.of())).isEmpty(); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void fold1() { |
| 85 | + List<String> strings = List.of("a", "e", "i", "o", "u"); |
| 86 | + assertThat(List.traversable.fold1(Strings.group, strings)).isEqualTo("aeiou"); |
| 87 | + |
| 88 | + assertThatThrownBy(() -> List.traversable.fold1(Strings.group, List.of())) |
| 89 | + .isInstanceOf(RuntimeException.class); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void toList() { |
| 94 | + List<String> strings = List.of("a", "e", "i", "o", "u"); |
| 95 | + List<String> result = List.traversable.toList(strings); |
| 96 | + assertThat(result).containsExactly("a", "e", "i", "o", "u"); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void foldMap1() { |
| 101 | + List<String> strings = List.of("a", "bb", "ccc", "dddd", "eeeee"); |
| 102 | + Integer result = List.traversable.foldMap1(Integers.multiplicativeMonoid, String::length, strings); |
| 103 | + assertThat(result).isEqualTo(120); |
| 104 | + |
| 105 | + assertThatThrownBy(() -> List.traversable.foldMap1(Integers.multiplicativeMonoid, String::length, List.of())) |
| 106 | + .isInstanceOf(RuntimeException.class); |
62 | 107 | } |
63 | 108 | } |
0 commit comments