|
| 1 | +package headwater |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestForEach(t *testing.T) { |
| 9 | + items := []int64{0, 1, 2} |
| 10 | + result := 0 |
| 11 | + |
| 12 | + ForEach(items, func(item int64) { |
| 13 | + result++ |
| 14 | + }) |
| 15 | + |
| 16 | + want := 3 |
| 17 | + |
| 18 | + if result != want { |
| 19 | + t.Errorf("Count is wrong. Expected: %#v, Received: %#v", want, result) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +// Map should create an array with a new value for each value of the original array |
| 24 | +func TestMapEach(t *testing.T) { |
| 25 | + items := []int64{0, 1, 2} |
| 26 | + |
| 27 | + result := Map(items, func(item int64) string { |
| 28 | + return "" + fmt.Sprint(item) |
| 29 | + }) |
| 30 | + |
| 31 | + want := []string{"0", "1", "2"} |
| 32 | + |
| 33 | + if !Equal(want, result) { |
| 34 | + t.Errorf("Count is wrong. Expected: %#v, Received: %#v", want, result) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func TestNestedMap(t *testing.T) { |
| 39 | + items := []int64{0, 1, 2} |
| 40 | + |
| 41 | + result := Map(Map(Map( |
| 42 | + items, |
| 43 | + func(item int64) float64 { |
| 44 | + return float64(item) / 2 |
| 45 | + }), |
| 46 | + func(item float64) string { |
| 47 | + return "" + fmt.Sprint(item) |
| 48 | + }), |
| 49 | + func(item string) string { |
| 50 | + return "a" + item |
| 51 | + }) |
| 52 | + |
| 53 | + want := []string{"a0", "a0.5", "a1"} |
| 54 | + |
| 55 | + if !Equal(want, result) { |
| 56 | + t.Errorf("Count is wrong. Expected: %#v, Received: %#v", want, result) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestReduce(t *testing.T) { |
| 61 | + items := []int{0, 1, 2} |
| 62 | + target := 0 |
| 63 | + |
| 64 | + result := Reduce(items, func(target int, item int) int { |
| 65 | + return target + item |
| 66 | + }, target) |
| 67 | + |
| 68 | + want := 3 |
| 69 | + |
| 70 | + if result != want { |
| 71 | + t.Errorf("Count is wrong. Expected: %#v, Received: %#v", want, result) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestFilter(t *testing.T) { |
| 76 | + items := []int{0, 3, 1, 4, 2, 5} |
| 77 | + |
| 78 | + result := Filter(items, func(item int) bool { |
| 79 | + if item >= 3 { |
| 80 | + return true |
| 81 | + } else { |
| 82 | + return false |
| 83 | + } |
| 84 | + }) |
| 85 | + |
| 86 | + out := fmt.Sprintf("%v, %v", items, result) |
| 87 | + fmt.Println(out) |
| 88 | + |
| 89 | + if !Equal(result, []int{3, 4, 5}) { |
| 90 | + t.Error("Count is wrong") |
| 91 | + } |
| 92 | +} |
0 commit comments