From 2978667042e6344ec9d7ab0e894de9506be1a879 Mon Sep 17 00:00:00 2001 From: Michael Valdron Date: Tue, 19 Jul 2022 18:31:55 -0400 Subject: [PATCH] arraylist util functions and testing added. Signed-off-by: Michael Valdron --- index/server/go.mod | 2 +- index/server/go.sum | 2 - index/server/pkg/util/arraylist.go | 80 ++++++++++ index/server/pkg/util/arraylist_test.go | 204 ++++++++++++++++++++++++ 4 files changed, 285 insertions(+), 3 deletions(-) create mode 100644 index/server/pkg/util/arraylist.go create mode 100644 index/server/pkg/util/arraylist_test.go diff --git a/index/server/go.mod b/index/server/go.mod index 8b463e5b..cf7bcf3d 100644 --- a/index/server/go.mod +++ b/index/server/go.mod @@ -8,6 +8,7 @@ require ( github.com/devfile/api/v2 v2.0.0-20220117162434-6e6e6a8bc14c github.com/devfile/library v1.2.1-0.20220308191614-f0f7e11b17de github.com/devfile/registry-support/index/generator v0.0.0-20220624203950-e7282a4695b6 + github.com/emirpasic/gods v1.12.0 github.com/gin-gonic/gin v1.7.7 github.com/hashicorp/go-version v1.4.0 github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348 @@ -27,7 +28,6 @@ require ( github.com/cespare/xxhash/v2 v2.1.1 // indirect github.com/containerd/cgroups v0.0.0-20190919134610-bf292b21730f // indirect github.com/davecgh/go-spew v1.1.1 // indirect - github.com/emirpasic/gods v1.12.0 // indirect github.com/evanphx/json-patch v4.11.0+incompatible // indirect github.com/fatih/color v1.7.0 // indirect github.com/fsnotify/fsnotify v1.4.9 // indirect diff --git a/index/server/go.sum b/index/server/go.sum index c68f78aa..0e44b1a6 100644 --- a/index/server/go.sum +++ b/index/server/go.sum @@ -140,8 +140,6 @@ github.com/devfile/api/v2 v2.0.0-20220117162434-6e6e6a8bc14c h1:sjghKUov/WT71dBr github.com/devfile/api/v2 v2.0.0-20220117162434-6e6e6a8bc14c/go.mod h1:d99eTN6QxgzihOOFyOZA+VpUyD4Q1pYRYHZ/ci9J96Q= github.com/devfile/library v1.2.1-0.20220308191614-f0f7e11b17de h1:jImHtiAxjyul1UkPmf6C3EMS5wqNz+k84LKkCXkeqws= github.com/devfile/library v1.2.1-0.20220308191614-f0f7e11b17de/go.mod h1:GSPfJaBg0+bBjBHbwBE5aerJLH6tWGQu2q2rHYd9czM= -github.com/devfile/registry-support/index/generator v0.0.0-20220316161530-f06d84c42b54 h1:k7F4Hc4svkA+qHerBTZzcU1iVrQAJHOh8KurPnL4uYk= -github.com/devfile/registry-support/index/generator v0.0.0-20220316161530-f06d84c42b54/go.mod h1:1fyDJL+fPHtcrYA6yjSVWeLmXmjCNth0d5Rq1rvtryc= github.com/devfile/registry-support/index/generator v0.0.0-20220624203950-e7282a4695b6 h1:bTbZxKSjF9xfiUuOKpoyX7P/ZcnIRy993+JBvkQ91hw= github.com/devfile/registry-support/index/generator v0.0.0-20220624203950-e7282a4695b6/go.mod h1:1fyDJL+fPHtcrYA6yjSVWeLmXmjCNth0d5Rq1rvtryc= github.com/dgrijalva/jwt-go v0.0.0-20170104182250-a601269ab70c/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= diff --git a/index/server/pkg/util/arraylist.go b/index/server/pkg/util/arraylist.go new file mode 100644 index 00000000..4e45347d --- /dev/null +++ b/index/server/pkg/util/arraylist.go @@ -0,0 +1,80 @@ +package util + +import "github.com/emirpasic/gods/lists/arraylist" + +// ConvertStringArrayToArrayList converts string array to interface arraylist +func ConvertStringArrayToArrayList(sArray []string) *arraylist.List { + arrayList := arraylist.New() + + for _, s := range sArray { + arrayList.Add(s) + } + + return arrayList +} + +// ConvertBoolArrayToArrayList converts bool array to interface arraylist +func ConvertBoolArrayToArrayList(bArray []bool) *arraylist.List { + arrayList := arraylist.New() + + for _, b := range bArray { + arrayList.Add(b) + } + + return arrayList +} + +// ConvertIntArrayToArrayList converts int array to interface arraylist +func ConvertIntArrayToArrayList(iArray []int) *arraylist.List { + arrayList := arraylist.New() + + for _, i := range iArray { + arrayList.Add(i) + } + + return arrayList +} + +// ConvertShortArrayToArrayList converts int16 array to interface arraylist +func ConvertShortArrayToArrayList(iArray []int16) *arraylist.List { + arrayList := arraylist.New() + + for _, i := range iArray { + arrayList.Add(i) + } + + return arrayList +} + +// ConvertLongArrayToArrayList converts int64 array to interface arraylist +func ConvertLongArrayToArrayList(iArray []int64) *arraylist.List { + arrayList := arraylist.New() + + for _, i := range iArray { + arrayList.Add(i) + } + + return arrayList +} + +// ConvertFloatArrayToArrayList converts float32 array to interface arraylist +func ConvertFloatArrayToArrayList(fArray []float32) *arraylist.List { + arrayList := arraylist.New() + + for _, f := range fArray { + arrayList.Add(f) + } + + return arrayList +} + +// ConvertDoubleArrayToArrayList converts float64 array to interface arraylist +func ConvertDoubleArrayToArrayList(fArray []float64) *arraylist.List { + arrayList := arraylist.New() + + for _, f := range fArray { + arrayList.Add(f) + } + + return arrayList +} diff --git a/index/server/pkg/util/arraylist_test.go b/index/server/pkg/util/arraylist_test.go new file mode 100644 index 00000000..2254ad12 --- /dev/null +++ b/index/server/pkg/util/arraylist_test.go @@ -0,0 +1,204 @@ +package util + +import ( + "reflect" + "testing" + + "github.com/emirpasic/gods/lists/arraylist" +) + +func TestConvertStringArrayToArrayList(t *testing.T) { + tests := []struct { + name string + array []string + wantResult *arraylist.List + }{ + { + name: "Test singleton array", + array: []string{"abc"}, + wantResult: arraylist.New("abc"), + }, + { + name: "Test Array with multiple values", + array: []string{"abc", "ab", "test"}, + wantResult: arraylist.New("abc", "ab", "test"), + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + gotResult := ConvertStringArrayToArrayList(test.array) + if !reflect.DeepEqual(gotResult, test.wantResult) { + t.Fatalf("Expected: %v, Got: %v\n", *test.wantResult, *gotResult) + } + }) + } +} + +func TestConvertBoolArrayToArrayList(t *testing.T) { + tests := []struct { + name string + array []bool + wantResult *arraylist.List + }{ + { + name: "Test singleton array", + array: []bool{true}, + wantResult: arraylist.New(true), + }, + { + name: "Test Array with multiple values", + array: []bool{true, false, true}, + wantResult: arraylist.New(true, false, true), + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + gotResult := ConvertBoolArrayToArrayList(test.array) + if !reflect.DeepEqual(gotResult, test.wantResult) { + t.Fatalf("Expected: %v, Got: %v\n", *test.wantResult, *gotResult) + } + }) + } +} + +func TestConvertIntArrayToArrayList(t *testing.T) { + tests := []struct { + name string + array []int + wantResult *arraylist.List + }{ + { + name: "Test singleton array", + array: []int{0}, + wantResult: arraylist.New(0), + }, + { + name: "Test Array with multiple values", + array: []int{0, 1, 2}, + wantResult: arraylist.New(0, 1, 2), + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + gotResult := ConvertIntArrayToArrayList(test.array) + if !reflect.DeepEqual(gotResult, test.wantResult) { + t.Fatalf("Expected: %v, Got: %v\n", *test.wantResult, *gotResult) + } + }) + } +} + +func TestConvertShortArrayToArrayList(t *testing.T) { + tests := []struct { + name string + array []int16 + wantResult *arraylist.List + }{ + { + name: "Test singleton array", + array: []int16{0}, + wantResult: arraylist.New(int16(0)), + }, + { + name: "Test Array with multiple values", + array: []int16{0, 1, 2}, + wantResult: arraylist.New(int16(0), int16(1), int16(2)), + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + gotResult := ConvertShortArrayToArrayList(test.array) + if !reflect.DeepEqual(gotResult, test.wantResult) { + t.Fatalf("Expected: %v, Got: %v\n", *test.wantResult, *gotResult) + } + }) + } +} + +func TestConvertLongArrayToArrayList(t *testing.T) { + tests := []struct { + name string + array []int64 + wantResult *arraylist.List + }{ + { + name: "Test singleton array", + array: []int64{0}, + wantResult: arraylist.New(int64(0)), + }, + { + name: "Test Array with multiple values", + array: []int64{0, 1, 2}, + wantResult: arraylist.New(int64(0), int64(1), int64(2)), + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + gotResult := ConvertLongArrayToArrayList(test.array) + if !reflect.DeepEqual(gotResult, test.wantResult) { + t.Fatalf("Expected: %v, Got: %v\n", *test.wantResult, *gotResult) + } + }) + } +} + +func TestConvertFloatArrayToArrayList(t *testing.T) { + tests := []struct { + name string + array []float32 + wantResult *arraylist.List + }{ + { + name: "Test singleton array", + array: []float32{0.23}, + wantResult: arraylist.New(float32(0.23)), + }, + { + name: "Test Array with multiple values", + array: []float32{0.23, 1.1, 2.2}, + wantResult: arraylist.New(float32(0.23), float32(1.1), float32(2.2)), + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + gotResult := ConvertFloatArrayToArrayList(test.array) + if !reflect.DeepEqual(gotResult, test.wantResult) { + t.Fatalf("Expected: %v, Got: %v\n", *test.wantResult, *gotResult) + } + }) + } +} + +func TestConvertDoubleArrayToArrayList(t *testing.T) { + tests := []struct { + name string + array []float64 + wantResult *arraylist.List + }{ + { + name: "Test singleton array", + array: []float64{0.23}, + wantResult: arraylist.New(0.23), + }, + { + name: "Test Array with multiple values", + array: []float64{0.23, 1.1, 2.2}, + wantResult: arraylist.New(0.23, 1.1, 2.2), + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + gotResult := ConvertDoubleArrayToArrayList(test.array) + if !reflect.DeepEqual(gotResult, test.wantResult) { + t.Fatalf("Expected: %v, Got: %v\n", *test.wantResult, *gotResult) + } + }) + } +}