goutils/maputils/mapUtils_test.go
2025-04-24 15:59:15 +02:00

274 lines
4.7 KiB
Go

package maputils_test
import (
"reflect"
"testing"
"git.mstar.dev/mstar/goutils/maputils"
"git.mstar.dev/mstar/goutils/sliceutils"
)
func TestMapSameKeys(t *testing.T) {
type args struct {
dic map[string]int
apply func(string, int) int
}
tests := []struct {
name string
args args
want map[string]int
}{
{
name: "map",
args: args{
dic: map[string]int{
"a": 1,
"b": 2,
"c": 3,
},
apply: func(s string, i int) int { return i + 1 },
},
want: map[string]int{
"a": 2,
"b": 3,
"c": 4,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := maputils.MapSameKeys(tt.args.dic, tt.args.apply); !reflect.DeepEqual(
got,
tt.want,
) {
t.Errorf("MapSameKeys() = %v, want %v", got, tt.want)
}
})
}
}
func TestMapNewKeys(t *testing.T) {
type args struct {
in map[string]int
apply func(string, int) (string, int)
}
tests := []struct {
name string
args args
want map[string]int
}{
{
name: "new keys",
args: args{
in: map[string]int{
"a": 1,
"b": 2,
"c": 3,
},
apply: func(s string, i int) (string, int) { return s + "-", i + 1 },
},
want: map[string]int{
"a-": 2,
"b-": 3,
"c-": 4,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := maputils.MapNewKeys(tt.args.in, tt.args.apply); !reflect.DeepEqual(
got,
tt.want,
) {
t.Errorf("MapNewKeys() = %v, want %v", got, tt.want)
}
})
}
}
func TestFilterMap(t *testing.T) {
type args struct {
dic map[string]int
filter func(string, int) bool
}
tests := []struct {
name string
args args
want map[string]int
}{
{
name: "filter",
args: args{
dic: map[string]int{
"a": 1,
"b": 2,
"c": 3,
},
filter: func(s string, i int) bool { return s == "a" },
},
want: map[string]int{"a": 1},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := maputils.FilterMap(tt.args.dic, tt.args.filter); !reflect.DeepEqual(
got,
tt.want,
) {
t.Errorf("FilterMap() = %v, want %v", got, tt.want)
}
})
}
}
func TestKeysFromMap(t *testing.T) {
type args struct {
m map[string]int
}
tests := []struct {
name string
args args
want []string
}{
{
name: "keys",
args: args{
m: map[string]int{
"a": 1,
"b": 2,
"c": 3,
},
},
want: []string{"a", "b", "c"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := maputils.KeysFromMap(tt.args.m); !sliceutils.CompareUnordered(got, tt.want) {
t.Errorf("KeysFromMap() = %v, want %v", got, tt.want)
}
})
}
}
func TestCompareMap(t *testing.T) {
type args struct {
a map[string]int
b map[string]int
}
tests := []struct {
name string
args args
want bool
}{
{
name: "equal",
args: args{
a: map[string]int{
"a": 1,
"b": 2,
"c": 3,
},
b: map[string]int{
"b": 2,
"a": 1,
"c": 3,
},
},
want: true,
},
{
name: "not equal, same len, same keys",
args: args{
a: map[string]int{
"a": 5,
"b": 6,
"c": 7,
},
b: map[string]int{
"a": 1,
"b": 2,
"c": 3,
},
},
want: false,
},
{
name: "not equal, diff len",
args: args{
a: map[string]int{
"a": 5,
"b": 6,
"c": 7,
},
b: map[string]int{
"a": 1,
"b": 2,
},
},
want: false,
},
{
name: "not equal, same len, diff keys",
args: args{
a: map[string]int{
"a": 5,
"b": 6,
"c": 7,
},
b: map[string]int{
"e": 1,
"f": 2,
"g": 3,
},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := maputils.CompareMap(tt.args.a, tt.args.b); got != tt.want {
t.Errorf("CompareMap() = %v, want %v", got, tt.want)
}
})
}
}
func TestCompact(t *testing.T) {
type args struct {
m map[int]int
compactor func(accK int, accV int, nextK int, nextV int) (int, int)
}
tests := []struct {
name string
args args
want int
want1 int
}{
// TODO: Add test cases.
{
name: "compact",
args: args{
m: map[int]int{
4: 1,
5: 2,
6: 3,
},
compactor: func(accK int, accV int, nextK int, nextV int) (int, int) { return accK + nextK, accV + nextV },
},
want: 4 + 5 + 6,
want1: 1 + 2 + 3,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1 := maputils.Compact(tt.args.m, tt.args.compactor)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Compact() got = %v, want %v", got, tt.want)
}
if !reflect.DeepEqual(got1, tt.want1) {
t.Errorf("Compact() got1 = %v, want %v", got1, tt.want1)
}
})
}
}