|
| 1 | +package fn |
| 2 | + |
| 3 | +import ( |
| 4 | + "slices" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | +) |
| 9 | + |
| 10 | +func even(a int) bool { return a%2 == 0 } |
| 11 | +func odd(a int) bool { return a%2 != 0 } |
| 12 | + |
| 13 | +func TestAll(t *testing.T) { |
| 14 | + x := []int{0, 2, 4, 6, 8} |
| 15 | + require.True(t, All(even, x)) |
| 16 | + require.False(t, All(odd, x)) |
| 17 | + |
| 18 | + y := []int{1, 3, 5, 7, 9} |
| 19 | + require.False(t, All(even, y)) |
| 20 | + require.True(t, All(odd, y)) |
| 21 | + |
| 22 | + z := []int{0, 2, 4, 6, 9} |
| 23 | + require.False(t, All(even, z)) |
| 24 | + require.False(t, All(odd, z)) |
| 25 | +} |
| 26 | + |
| 27 | +func TestAny(t *testing.T) { |
| 28 | + x := []int{1, 3, 5, 7, 9} |
| 29 | + require.False(t, Any(even, x)) |
| 30 | + require.True(t, Any(odd, x)) |
| 31 | + |
| 32 | + y := []int{0, 3, 5, 7, 9} |
| 33 | + require.True(t, Any(even, y)) |
| 34 | + require.True(t, Any(odd, y)) |
| 35 | + |
| 36 | + z := []int{0, 2, 4, 6, 8} |
| 37 | + require.True(t, Any(even, z)) |
| 38 | + require.False(t, Any(odd, z)) |
| 39 | +} |
| 40 | + |
| 41 | +func TestMap(t *testing.T) { |
| 42 | + inc := func(i int) int { return i + 1 } |
| 43 | + |
| 44 | + x := []int{0, 2, 4, 6, 8} |
| 45 | + |
| 46 | + y := Map(inc, x) |
| 47 | + |
| 48 | + z := []int{1, 3, 5, 7, 9} |
| 49 | + |
| 50 | + require.True(t, slices.Equal(y, z)) |
| 51 | +} |
| 52 | + |
| 53 | +func TestFilter(t *testing.T) { |
| 54 | + x := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} |
| 55 | + |
| 56 | + y := Filter(even, x) |
| 57 | + |
| 58 | + require.True(t, All(even, y)) |
| 59 | + |
| 60 | + z := Filter(odd, y) |
| 61 | + |
| 62 | + require.Zero(t, len(z)) |
| 63 | +} |
| 64 | + |
| 65 | +func TestFoldl(t *testing.T) { |
| 66 | + seed := []int{} |
| 67 | + stupid := func(s []int, a int) []int { return append(s, a) } |
| 68 | + |
| 69 | + x := []int{0, 1, 2, 3, 4} |
| 70 | + |
| 71 | + r := Foldl(stupid, seed, x) |
| 72 | + |
| 73 | + require.True(t, slices.Equal(x, r)) |
| 74 | +} |
| 75 | + |
| 76 | +func TestFoldr(t *testing.T) { |
| 77 | + seed := []int{} |
| 78 | + stupid := func(a int, s []int) []int { return append(s, a) } |
| 79 | + |
| 80 | + x := []int{0, 1, 2, 3, 4} |
| 81 | + |
| 82 | + z := Foldr(stupid, seed, x) |
| 83 | + |
| 84 | + slices.Reverse[[]int](x) |
| 85 | + |
| 86 | + require.True(t, slices.Equal(x, z)) |
| 87 | +} |
| 88 | + |
| 89 | +func TestFind(t *testing.T) { |
| 90 | + x := []int{10, 11, 12, 13, 14, 15} |
| 91 | + |
| 92 | + div3 := func(a int) bool { return a%3 == 0 } |
| 93 | + div8 := func(a int) bool { return a%8 == 0 } |
| 94 | + |
| 95 | + require.Equal(t, Find(div3, x), Some(12)) |
| 96 | + |
| 97 | + require.Equal(t, Find(div8, x), None[int]()) |
| 98 | +} |
| 99 | + |
| 100 | +func TestFlatten(t *testing.T) { |
| 101 | + x := [][]int{{0}, {1}, {2}} |
| 102 | + |
| 103 | + y := Flatten(x) |
| 104 | + |
| 105 | + require.True(t, slices.Equal(y, []int{0, 1, 2})) |
| 106 | +} |
| 107 | + |
| 108 | +func TestReplicate(t *testing.T) { |
| 109 | + require.True(t, slices.Equal([]int{1, 1, 1, 1, 1}, Replicate(5, 1))) |
| 110 | +} |
| 111 | + |
| 112 | +func TestSpan(t *testing.T) { |
| 113 | + x := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} |
| 114 | + lt5 := func(a int) bool { return a < 5 } |
| 115 | + |
| 116 | + low, high := Span(lt5, x) |
| 117 | + |
| 118 | + require.True(t, slices.Equal(low, []int{0, 1, 2, 3, 4})) |
| 119 | + require.True(t, slices.Equal(high, []int{5, 6, 7, 8, 9})) |
| 120 | +} |
| 121 | + |
| 122 | +func TestSplitAt(t *testing.T) { |
| 123 | + x := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} |
| 124 | + fst, snd := SplitAt(5, x) |
| 125 | + |
| 126 | + require.True(t, slices.Equal(fst, []int{0, 1, 2, 3, 4})) |
| 127 | + require.True(t, slices.Equal(snd, []int{5, 6, 7, 8, 9})) |
| 128 | +} |
| 129 | + |
| 130 | +func TestZipWith(t *testing.T) { |
| 131 | + eq := func(a, b int) bool { return a == b } |
| 132 | + x := []int{0, 1, 2, 3, 4} |
| 133 | + y := Replicate(5, 1) |
| 134 | + z := ZipWith(eq, x, y) |
| 135 | + require.True(t, slices.Equal( |
| 136 | + z, []bool{false, true, false, false, false}, |
| 137 | + )) |
| 138 | +} |
0 commit comments