151 lines
2.7 KiB
Go
151 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func Map(data []int, action func(int) int) []int {
|
|
// need to implement
|
|
return nil
|
|
}
|
|
|
|
func Filter(data []int, action func(int) bool) []int {
|
|
// need to implement
|
|
return nil
|
|
}
|
|
|
|
func Reduce(data []int, initial int, action func(int, int) int) int {
|
|
// need to implement
|
|
return 0
|
|
}
|
|
|
|
func TestMap(t *testing.T) {
|
|
tests := map[string]struct {
|
|
data []int
|
|
action func(int) int
|
|
result []int
|
|
}{
|
|
"nil numbers": {
|
|
action: func(number int) int {
|
|
return -number
|
|
},
|
|
},
|
|
"empty numbers": {
|
|
data: []int{},
|
|
action: func(number int) int {
|
|
return -number
|
|
},
|
|
result: []int{},
|
|
},
|
|
"inc numbers": {
|
|
data: []int{1, 2, 3, 4, 5},
|
|
action: func(number int) int {
|
|
return number + 1
|
|
},
|
|
result: []int{2, 3, 4, 5, 6},
|
|
},
|
|
"double numbers": {
|
|
data: []int{1, 2, 3, 4, 5},
|
|
action: func(number int) int {
|
|
return number * number
|
|
},
|
|
result: []int{1, 4, 9, 16, 25},
|
|
},
|
|
}
|
|
|
|
for name, test := range tests {
|
|
t.Run(name, func(t *testing.T) {
|
|
result := Map(test.data, test.action)
|
|
assert.True(t, reflect.DeepEqual(test.result, result))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFilter(t *testing.T) {
|
|
tests := map[string]struct {
|
|
data []int
|
|
action func(int) bool
|
|
result []int
|
|
}{
|
|
"nil numbers": {
|
|
action: func(number int) bool {
|
|
return number == 0
|
|
},
|
|
},
|
|
"empty numbers": {
|
|
data: []int{},
|
|
action: func(number int) bool {
|
|
return number == 1
|
|
},
|
|
result: []int{},
|
|
},
|
|
"even numbers": {
|
|
data: []int{1, 2, 3, 4, 5},
|
|
action: func(number int) bool {
|
|
return number%2 == 0
|
|
},
|
|
result: []int{2, 4},
|
|
},
|
|
"positive numbers": {
|
|
data: []int{-1, -2, 1, 2},
|
|
action: func(number int) bool {
|
|
return number > 0
|
|
},
|
|
result: []int{1, 2},
|
|
},
|
|
}
|
|
|
|
for name, test := range tests {
|
|
t.Run(name, func(t *testing.T) {
|
|
result := Filter(test.data, test.action)
|
|
assert.True(t, reflect.DeepEqual(test.result, result))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestReduce(t *testing.T) {
|
|
tests := map[string]struct {
|
|
initial int
|
|
data []int
|
|
action func(int, int) int
|
|
result int
|
|
}{
|
|
"nil numbers": {
|
|
action: func(lhs, rhs int) int {
|
|
return 0
|
|
},
|
|
},
|
|
"empty numbers": {
|
|
data: []int{},
|
|
action: func(lhs, rhs int) int {
|
|
return 0
|
|
},
|
|
},
|
|
"sum of numbers": {
|
|
data: []int{1, 2, 3, 4, 5},
|
|
action: func(lhs, rhs int) int {
|
|
return lhs + rhs
|
|
},
|
|
result: 15,
|
|
},
|
|
"sum of numbers with initial value": {
|
|
initial: 10,
|
|
data: []int{1, 2, 3, 4, 5},
|
|
action: func(lhs, rhs int) int {
|
|
return lhs + rhs
|
|
},
|
|
result: 25,
|
|
},
|
|
}
|
|
|
|
for name, test := range tests {
|
|
t.Run(name, func(t *testing.T) {
|
|
result := Reduce(test.data, test.initial, test.action)
|
|
assert.Equal(t, test.result, result)
|
|
})
|
|
}
|
|
}
|