initial commit
This commit is contained in:
362
pkg/array/map_test.go
Normal file
362
pkg/array/map_test.go
Normal file
@@ -0,0 +1,362 @@
|
||||
package array
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMapWithError(t *testing.T) {
|
||||
t.Run("success case", func(t *testing.T) {
|
||||
// Arrange
|
||||
input := []int{1, 2, 3}
|
||||
expected := []string{"1", "2", "3"}
|
||||
|
||||
// Act
|
||||
result, err := MapWithError(input, func(val int, index int) (*string, error) {
|
||||
str := string(rune(val + '0'))
|
||||
return &str, nil
|
||||
})
|
||||
|
||||
// Assert
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("Expected %v, got %v", expected, result)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("error case", func(t *testing.T) {
|
||||
// Arrange
|
||||
input := []int{1, 2, 3}
|
||||
testErr := errors.New("test error")
|
||||
|
||||
// Act
|
||||
result, err := MapWithError(input, func(val int, index int) (*string, error) {
|
||||
if val == 2 {
|
||||
return nil, testErr
|
||||
}
|
||||
str := string(rune(val + '0'))
|
||||
return &str, nil
|
||||
})
|
||||
|
||||
// Assert
|
||||
if err != testErr {
|
||||
t.Errorf("Expected error %v, got %v", testErr, err)
|
||||
}
|
||||
if result != nil {
|
||||
t.Errorf("Expected nil result, got %v", result)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("empty array", func(t *testing.T) {
|
||||
// Arrange
|
||||
var input []int
|
||||
|
||||
// Act
|
||||
result, err := MapWithError(input, func(val int, index int) (*string, error) {
|
||||
str := string(rune(val + '0'))
|
||||
return &str, nil
|
||||
})
|
||||
|
||||
// Assert
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
if len(result) != 0 {
|
||||
t.Errorf("Expected empty result, got %v", result)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestMap(t *testing.T) {
|
||||
t.Run("basic transformation", func(t *testing.T) {
|
||||
// Arrange
|
||||
input := []int{1, 2, 3}
|
||||
expected := []string{"1", "2", "3"}
|
||||
|
||||
// Act
|
||||
result := Map(input, func(val int, index int) string {
|
||||
return string(rune(val + '0'))
|
||||
})
|
||||
|
||||
// Assert
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("Expected %v, got %v", expected, result)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("use index in transformation", func(t *testing.T) {
|
||||
// Arrange
|
||||
input := []string{"a", "b", "c"}
|
||||
expected := []string{"a0", "b1", "c2"}
|
||||
|
||||
// Act
|
||||
result := Map(input, func(val string, index int) string {
|
||||
return val + string(rune(index+'0'))
|
||||
})
|
||||
|
||||
// Assert
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("Expected %v, got %v", expected, result)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("empty array", func(t *testing.T) {
|
||||
// Arrange
|
||||
var input []int
|
||||
|
||||
// Act
|
||||
result := Map(input, func(val int, index int) string {
|
||||
return string(rune(val + '0'))
|
||||
})
|
||||
|
||||
// Assert
|
||||
if len(result) != 0 {
|
||||
t.Errorf("Expected empty result, got %v", result)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestMapD(t *testing.T) {
|
||||
t.Run("map dictionary to array", func(t *testing.T) {
|
||||
// Arrange
|
||||
input := map[string]int{
|
||||
"a": 1,
|
||||
"b": 2,
|
||||
"c": 3,
|
||||
}
|
||||
|
||||
// Act
|
||||
result := MapD(input, func(val int, key string) string {
|
||||
return key + string(rune(val+'0'))
|
||||
})
|
||||
|
||||
// Assert
|
||||
// Since map iteration order is not guaranteed, we check that all expected elements are in the result
|
||||
expectedElements := []string{"a1", "b2", "c3"}
|
||||
if len(result) != len(expectedElements) {
|
||||
t.Errorf("Expected result length %d, got %d", len(expectedElements), len(result))
|
||||
}
|
||||
|
||||
resultMap := make(map[string]bool)
|
||||
for _, v := range result {
|
||||
resultMap[v] = true
|
||||
}
|
||||
|
||||
for _, expected := range expectedElements {
|
||||
if !resultMap[expected] {
|
||||
t.Errorf("Expected result to contain %s, but it doesn't", expected)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("empty map", func(t *testing.T) {
|
||||
// Arrange
|
||||
input := map[string]int{}
|
||||
|
||||
// Act
|
||||
result := MapD(input, func(val int, key string) string {
|
||||
return key + string(rune(val+'0'))
|
||||
})
|
||||
|
||||
// Assert
|
||||
if len(result) != 0 {
|
||||
t.Errorf("Expected empty result, got %v", result)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestForEach(t *testing.T) {
|
||||
t.Run("modify array in place", func(t *testing.T) {
|
||||
// Arrange
|
||||
input := []int{1, 2, 3}
|
||||
expected := []int{2, 3, 4}
|
||||
|
||||
// Act
|
||||
ForEach(input, func(val *int, index int) {
|
||||
*val += 1
|
||||
})
|
||||
|
||||
// Assert
|
||||
if !reflect.DeepEqual(input, expected) {
|
||||
t.Errorf("Expected %v, got %v", expected, input)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("use index in modification", func(t *testing.T) {
|
||||
// Arrange
|
||||
input := []int{1, 2, 3}
|
||||
expected := []int{1, 3, 5}
|
||||
|
||||
// Act
|
||||
ForEach(input, func(val *int, index int) {
|
||||
*val = *val + index
|
||||
})
|
||||
|
||||
// Assert
|
||||
if !reflect.DeepEqual(input, expected) {
|
||||
t.Errorf("Expected %v, got %v", expected, input)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("empty array", func(t *testing.T) {
|
||||
// Arrange
|
||||
var input []int
|
||||
callCount := 0
|
||||
|
||||
// Act
|
||||
ForEach(input, func(val *int, index int) {
|
||||
callCount++
|
||||
})
|
||||
|
||||
// Assert
|
||||
if callCount != 0 {
|
||||
t.Errorf("Expected callback not to be called, but it was called %d times", callCount)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestMapMany(t *testing.T) {
|
||||
t.Run("basic flat mapping", func(t *testing.T) {
|
||||
// Arrange
|
||||
input := []int{1, 2}
|
||||
expected := []string{"1a", "1b", "2a", "2b"}
|
||||
|
||||
// Act
|
||||
result := MapMany(input,
|
||||
func(i int) []string {
|
||||
return []string{"a", "b"}
|
||||
},
|
||||
func(i int, s string) *string {
|
||||
res := string(rune(i+'0')) + s
|
||||
return &res
|
||||
})
|
||||
|
||||
// Assert
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("Expected %v, got %v", expected, result)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("with nil results", func(t *testing.T) {
|
||||
// Arrange
|
||||
input := []int{1, 2, 3}
|
||||
expected := []string{"1a", "2a", "3a"}
|
||||
|
||||
// Act
|
||||
result := MapMany(input,
|
||||
func(i int) []string {
|
||||
return []string{"a", "b"}
|
||||
},
|
||||
func(i int, s string) *string {
|
||||
if s == "b" {
|
||||
return nil
|
||||
}
|
||||
res := string(rune(i+'0')) + s
|
||||
return &res
|
||||
})
|
||||
|
||||
// Assert
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("Expected %v, got %v", expected, result)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("empty input array", func(t *testing.T) {
|
||||
// Arrange
|
||||
var input []int
|
||||
|
||||
// Act
|
||||
result := MapMany(input,
|
||||
func(i int) []string {
|
||||
return []string{"a", "b"}
|
||||
},
|
||||
func(i int, s string) *string {
|
||||
res := string(rune(i+'0')) + s
|
||||
return &res
|
||||
})
|
||||
|
||||
// Assert
|
||||
if len(result) != 0 {
|
||||
t.Errorf("Expected empty result, got %v", result)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestMapManyD(t *testing.T) {
|
||||
t.Run("map dictionary to flattened array", func(t *testing.T) {
|
||||
// Arrange
|
||||
input := map[string]int{
|
||||
"a": 1,
|
||||
"b": 2,
|
||||
}
|
||||
|
||||
// Act
|
||||
result := MapManyD(input,
|
||||
func(val int) []string {
|
||||
return []string{"x", "y"}
|
||||
},
|
||||
func(s string) string {
|
||||
return s + "z"
|
||||
})
|
||||
|
||||
// Assert
|
||||
// Since map iteration order is not guaranteed, we check that all expected elements are in the result
|
||||
expectedElements := []string{"xz", "yz", "xz", "yz"}
|
||||
if len(result) != len(expectedElements) {
|
||||
t.Errorf("Expected result length %d, got %d", len(expectedElements), len(result))
|
||||
}
|
||||
|
||||
resultMap := make(map[string]int)
|
||||
for _, v := range result {
|
||||
resultMap[v]++
|
||||
}
|
||||
|
||||
if resultMap["xz"] != 2 || resultMap["yz"] != 2 {
|
||||
t.Errorf("Expected result to contain 2 of each 'xz' and 'yz', got %v", resultMap)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("empty inner collection", func(t *testing.T) {
|
||||
// Arrange
|
||||
input := map[string]int{
|
||||
"a": 1,
|
||||
"b": 2,
|
||||
}
|
||||
|
||||
// Act
|
||||
result := MapManyD(input,
|
||||
func(val int) []string {
|
||||
return []string{}
|
||||
},
|
||||
func(s string) string {
|
||||
return s + "z"
|
||||
})
|
||||
|
||||
// Assert
|
||||
if len(result) != 0 {
|
||||
t.Errorf("Expected empty result, got %v", result)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("empty input map", func(t *testing.T) {
|
||||
// Arrange
|
||||
input := map[string]int{}
|
||||
|
||||
// Act
|
||||
result := MapManyD(input,
|
||||
func(val int) []string {
|
||||
return []string{"x", "y"}
|
||||
},
|
||||
func(s string) string {
|
||||
return s + "z"
|
||||
})
|
||||
|
||||
// Assert
|
||||
if len(result) != 0 {
|
||||
t.Errorf("Expected empty result, got %v", result)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user