From 2877f751dce4dfdb7a325e5cc55811200ccaceb4 Mon Sep 17 00:00:00 2001 From: fancl Date: Tue, 22 Aug 2023 16:11:34 +0800 Subject: [PATCH] fix bugs --- util/arrays/array.go | 43 +++++++++++++++++++++++ util/arrays/array_test.go | 73 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 util/arrays/array.go create mode 100644 util/arrays/array_test.go diff --git a/util/arrays/array.go b/util/arrays/array.go new file mode 100644 index 0000000..c93313f --- /dev/null +++ b/util/arrays/array.go @@ -0,0 +1,43 @@ +package arrays + +func IndexOf[T comparable](a T, vs []T) int { + for i, v := range vs { + if v == a { + return i + } + } + return -1 +} + +func Exists[T comparable](a T, vs []T) bool { + return IndexOf(a, vs) > -1 +} + +func Fill[T any](startIndex int, num uint, value T) map[int]T { + m := make(map[int]T) + var i uint + for i = 0; i < num; i++ { + m[startIndex] = value + startIndex++ + } + return m +} + +func Reverse[T comparable](s []T) []T { + for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { + s[i], s[j] = s[j], s[i] + } + return s +} + +func Merge[T comparable](ss ...[]T) []T { + n := 0 + for _, v := range ss { + n += len(v) + } + s := make([]T, 0, n) + for _, v := range ss { + s = append(s, v...) + } + return s +} diff --git a/util/arrays/array_test.go b/util/arrays/array_test.go new file mode 100644 index 0000000..ac2cc27 --- /dev/null +++ b/util/arrays/array_test.go @@ -0,0 +1,73 @@ +package arrays + +import ( + "reflect" + "testing" +) + +func TestIndexOf(t *testing.T) { + type args[T comparable] struct { + a T + vs []T + } + type testCase[T comparable] struct { + name string + args args[T] + want int + } + tests := []testCase[string]{ + {"exists", args[string]{a: "a", vs: []string{"a", "b"}}, 0}, + {"not exists", args[string]{a: "a", vs: []string{"c", "b"}}, -1}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := IndexOf(tt.args.a, tt.args.vs); got != tt.want { + t.Errorf("IndexOf() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestExists(t *testing.T) { + type args[T comparable] struct { + a T + vs []T + } + type testCase[T comparable] struct { + name string + args args[T] + want bool + } + tests := []testCase[int]{ + {"exists", args[int]{a: 1, vs: []int{1, 2}}, true}, + {"not exists", args[int]{a: 2, vs: []int{3, 4}}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := Exists(tt.args.a, tt.args.vs); got != tt.want { + t.Errorf("Exists() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestReverse(t *testing.T) { + type args[T comparable] struct { + s []T + } + type testCase[T comparable] struct { + name string + args args[T] + want []T + } + tests := []testCase[int]{ + {"one", args[int]{s: []int{1, 2, 3}}, []int{3, 2, 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) + } + }) + } +}