65 lines
1.7 KiB
Go
65 lines
1.7 KiB
Go
package utils
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestMergeArrays(t *testing.T) {
|
|
type Person struct {
|
|
Name string
|
|
Age int
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
arrays interface{}
|
|
expected interface{}
|
|
}{
|
|
// Integer arrays
|
|
{"Empty arrays", [][]int{}, []int{}},
|
|
{"Single array", [][]int{{1, 2, 3}}, []int{1, 2, 3}},
|
|
{"Two arrays", [][]int{{1, 2}, {3, 4}}, []int{1, 2, 3, 4}},
|
|
{"Multiple arrays", [][]int{{1}, {2}, {3, 4}}, []int{1, 2, 3, 4}},
|
|
{"Array with duplicates", [][]int{{1, 2}, {2, 3}}, []int{1, 2, 2, 3}},
|
|
|
|
// String arrays
|
|
{"String arrays", [][]string{{"a", "b"}, {"c", "d"}}, []string{"a", "b", "c", "d"}},
|
|
{"String arrays with duplicates", [][]string{{"hello", "world"}, {"world", "Go"}}, []string{"hello", "world", "world", "Go"}},
|
|
|
|
// Struct arrays
|
|
{"Struct arrays", [][]Person{
|
|
{{Name: "Alice", Age: 30}, {Name: "Bob", Age: 25}},
|
|
{{Name: "Charlie", Age: 40}},
|
|
}, []Person{
|
|
{Name: "Alice", Age: 30},
|
|
{Name: "Bob", Age: 25},
|
|
{Name: "Charlie", Age: 40},
|
|
}},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
switch v := tt.arrays.(type) {
|
|
case [][]int:
|
|
result := MergeArrays(v...)
|
|
if !reflect.DeepEqual(result, tt.expected.([]int)) {
|
|
t.Errorf("MergeArrays(%v) = %v; want %v", v, result, tt.expected)
|
|
}
|
|
case [][]string:
|
|
result := MergeArrays(v...)
|
|
if !reflect.DeepEqual(result, tt.expected.([]string)) {
|
|
t.Errorf("MergeArrays(%v) = %v; want %v", v, result, tt.expected)
|
|
}
|
|
case [][]Person:
|
|
result := MergeArrays(v...)
|
|
if !reflect.DeepEqual(result, tt.expected.([]Person)) {
|
|
t.Errorf("MergeArrays(%v) = %v; want %v", v, result, tt.expected)
|
|
}
|
|
default:
|
|
t.Fatalf("Unsupported test case type: %T", v)
|
|
}
|
|
})
|
|
}
|
|
}
|