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

Add Test Cases for slices package #1084

Merged
merged 3 commits into from
May 17, 2024
Merged
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
336 changes: 328 additions & 8 deletions pkg/utils/slices/slices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ limitations under the License.
package slices

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

func TestContains(t *testing.T) {
type args[S interface{ ~[]T }, T comparable] struct {
type args[S ~[]T, T comparable] struct {
s S
t T
}
type testCase[S interface{ ~[]T }, T comparable] struct {
type testCase[S ~[]T, T comparable] struct {
name string
args args[S, T]
want bool
Expand Down Expand Up @@ -59,11 +61,11 @@ func TestContains(t *testing.T) {
}

func TestFilter(t *testing.T) {
type args[S interface{ ~[]T }, T any] struct {
type args[S ~[]T, T any] struct {
s S
f func(T) bool
}
type testCase[S interface{ ~[]T }, T any] struct {
type testCase[S ~[]T, T any] struct {
name string
args args[S, T]
want []T
Expand All @@ -90,11 +92,11 @@ func TestFilter(t *testing.T) {
}

func TestFind(t *testing.T) {
type args[S interface{ ~[]T }, T any] struct {
type args[S ~[]T, T any] struct {
s S
f func(T) bool
}
type testCase[S interface{ ~[]T }, T any] struct {
type testCase[S ~[]T, T any] struct {
name string
args args[S, T]
wantT T
Expand Down Expand Up @@ -127,11 +129,11 @@ func TestFind(t *testing.T) {
}

func TestMap(t *testing.T) {
type args[S interface{ ~[]T }, T any, O any] struct {
type args[S ~[]T, T any, O any] struct {
s S
f func(T) O
}
type testCase[S interface{ ~[]T }, T any, O any] struct {
type testCase[S ~[]T, T any, O any] struct {
name string
args args[S, T, O]
want []O
Expand All @@ -156,3 +158,321 @@ func TestMap(t *testing.T) {
})
}
}

func TestMapWithError(t *testing.T) {
type args[S ~[]T, T any, O any] struct {
s S
f func(T) (O, error)
}
type testCase[S ~[]T, T any, O any] struct {
name string
args args[S, T, O]
want []O
wantErr bool
}
tests := []testCase[[]string, string, int]{
{
name: "test map with error no error",
args: args[[]string, string, int]{
s: []string{"a", "b", "c"},
f: func(s string) (int, error) {
return len(s), nil
},
},
want: []int{1, 1, 1},
wantErr: false,
},
{
name: "test map with error with error",
args: args[[]string, string, int]{
s: []string{"a", "b", "c"},
f: func(s string) (int, error) {
if s == "b" {
return 0, errors.New("error")
}
return len(s), nil
},
},
want: nil,
wantErr: true,
},
{
name: "test map with error empty slice",
args: args[[]string, string, int]{
s: []string{},
f: func(s string) (int, error) {
return len(s), nil
},
},
want: []int{},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := MapWithError(tt.args.s, tt.args.f)
if (err != nil) != tt.wantErr {
t.Errorf("MapWithError() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("MapWithError() = %v, want %v", got, tt.want)
}
})
}
}

func TestFilterAndMap(t *testing.T) {
type args[S ~[]T, T any, O any] struct {
s S
f func(T) (O, bool)
}
type testCase[S ~[]T, T any, O any] struct {
name string
args args[S, T, O]
want []O
}
tests := []testCase[[]string, string, int]{
{
name: "test filter and map",
args: args[[]string, string, int]{
s: []string{"a", "b", "c"},
f: func(s string) (int, bool) {
if s == "b" {
return 2, true
}
return 0, false
},
},
want: []int{2},
},
{
name: "test filter and map no match",
args: args[[]string, string, int]{
s: []string{"a", "b", "c"},
f: func(s string) (int, bool) {
return 0, false
},
},
want: []int{},
},
{
name: "test filter and map empty slice",
args: args[[]string, string, int]{
s: []string{},
f: func(s string) (int, bool) {
return 0, false
},
},
want: []int{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := FilterAndMap(tt.args.s, tt.args.f); !reflect.DeepEqual(got, tt.want) {
t.Errorf("FilterAndMap() = %v, want %v", got, tt.want)
}
})
}
}

func TestUnique(t *testing.T) {
type args[S ~[]T, T comparable] struct {
s S
}
type testCase[S ~[]T, T comparable] struct {
name string
args args[S, T]
want []T
}
tests := []testCase[[]int, int]{
{
name: "test unique with duplicates",
args: args[[]int, int]{
s: []int{1, 2, 2, 3, 4, 4, 4, 5},
},
want: []int{1, 2, 3, 4, 5},
},
{
name: "test unique without duplicates",
args: args[[]int, int]{
s: []int{1, 2, 3, 4, 5},
},
want: []int{1, 2, 3, 4, 5},
},
{
name: "test unique empty slice",
args: args[[]int, int]{
s: []int{},
},
want: []int{},
},
{
name: "test unique single element",
args: args[[]int, int]{
s: []int{1},
},
want: []int{1},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Unique(tt.args.s); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Unique() = %v, want %v", got, tt.want)
}
})
}
}

func TestEqual(t *testing.T) {
type args[S ~[]T, T comparable] struct {
s1 S
s2 S
}
type testCase[S ~[]T, T comparable] struct {
name string
args args[S, T]
want bool
}
tests := []testCase[[]int, int]{
{
name: "test equal slices",
args: args[[]int, int]{
s1: []int{1, 2, 3, 4, 5},
s2: []int{1, 2, 3, 4, 5},
},
want: true,
},
{
name: "test not equal slices different lengths",
args: args[[]int, int]{
s1: []int{1, 2, 3, 4, 5},
s2: []int{1, 2, 3, 4},
},
want: false,
},
{
name: "test not equal slices different elements",
args: args[[]int, int]{
s1: []int{1, 2, 3, 4, 5},
s2: []int{1, 2, 3, 4, 6},
},
want: false,
},
{
name: "test equal empty slices",
args: args[[]int, int]{
s1: []int{},
s2: []int{},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Equal(tt.args.s1, tt.args.s2); got != tt.want {
t.Errorf("Equal() = %v, want %v", got, tt.want)
}
})
}
}

func TestReverse(t *testing.T) {
type args[S ~[]T, T any] struct {
s S
}
type testCase[S ~[]T, T any] struct {
name string
args args[S, T]
want []T
}
tests := []testCase[[]int, int]{
{
name: "test reverse",
args: args[[]int, int]{
s: []int{1, 2, 3, 4, 5},
},
want: []int{5, 4, 3, 2, 1},
},
{
name: "test reverse empty slice",
args: args[[]int, int]{
s: []int{},
},
want: []int{},
},
{
name: "test reverse single element",
args: args[[]int, int]{
s: []int{1},
},
want: []int{1},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Reverse(tt.args.s); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Reverse() = %v, want %v", got, tt.want)
}
})
}
}

func TestGroupBy(t *testing.T) {
type args[S ~[]T, T any, K comparable] struct {
s S
f func(T) K
}
type testCase[S ~[]T, T any, K comparable] struct {
name string
args args[S, T, K]
want map[K][]T
}
tests := []testCase[[]string, string, string]{
{
name: "test group by first letter",
args: args[[]string, string, string]{
s: []string{"apple", "apricot", "banana", "cherry"},
f: func(s string) string {
return string(s[0])
},
},
want: map[string][]string{
"a": {"apple", "apricot"},
"b": {"banana"},
"c": {"cherry"},
},
},
{
name: "test group by length",
args: args[[]string, string, string]{
s: []string{"apple", "banana", "cherry", "date"},
f: func(s string) string {
return fmt.Sprintf("%d", len(s))
},
},
want: map[string][]string{
"5": {"apple"},
"6": {"banana", "cherry"},
"4": {"date"},
},
},
{
name: "test group by empty slice",
args: args[[]string, string, string]{
s: []string{},
f: func(s string) string {
return fmt.Sprintf("%d", len(s))
},
},
want: map[string][]string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GroupBy(tt.args.s, tt.args.f); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GroupBy() = %v, want %v", got, tt.want)
}
})
}
}
Loading