Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test slices functions #496

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions utils/slices_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package utils

import (
"fmt"
"reflect"
"testing"
)

func TestFlatten(t *testing.T) {
t.Parallel()

tests := []struct {
name string
input [][]int
expected []int
}{
{
name: "nil slice",
input: nil,
expected: nil,
},
{
name: "empty slices",
input: [][]int{{}},
expected: nil,
},
{
name: "single slice",
input: [][]int{{1, 2, 3}},
expected: []int{1, 2, 3},
},
{
name: "multiple slices",
input: [][]int{{1, 2}, {3, 4}, {5}},
expected: []int{1, 2, 3, 4, 5},
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
result := Flatten(tc.input...)
if !reflect.DeepEqual(result, tc.expected) {
t.Errorf("Flatten(%v) = %v, want %v", tc.input, result, tc.expected)
}
})
}
}

func TestMap(t *testing.T) {
t.Parallel()

tests := []struct {
name string
input []int
expected []string
}{
{
name: "empty slice",
input: []int{},
expected: []string{},
},
{
name: "non-empty slice",
input: []int{1, 2, 3},
expected: []string{"1", "2", "3"},
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
result := Map(tc.input, func(i int) string { return fmt.Sprintf("%d", i) })
if !reflect.DeepEqual(result, tc.expected) {
t.Errorf("Map(%v) = %v, want %v", tc.input, result, tc.expected)
}
})
}
}

func TestFilter(t *testing.T) {
t.Parallel()

tests := []struct {
name string
input []int
expected []int
}{
{
name: "empty slice",
input: []int{},
expected: []int{},
},
{
name: "filter even numbers",
input: []int{1, 2, 3, 4, 5},
expected: []int{2, 4},
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
result := Filter(tc.input, func(i int) bool { return i%2 == 0 })
if !reflect.DeepEqual(result, tc.expected) {
t.Errorf("Filter(%v) = %v, want %v", tc.input, result, tc.expected)
}
})
}
}