package narr_test import ( "git.noahlan.cn/noahlan/ntool/narr" "git.noahlan.cn/noahlan/ntool/ntest/assert" "testing" ) func TestIntsHas(t *testing.T) { ints := []int{2, 4, 5} assert.True(t, narr.IntsHas(ints, 2)) assert.True(t, narr.IntsHas(ints, 5)) assert.False(t, narr.IntsHas(ints, 3)) } func TestInt64sHas(t *testing.T) { ints := []int64{2, 4, 5} assert.True(t, narr.Int64sHas(ints, 2)) assert.True(t, narr.Int64sHas(ints, 5)) assert.False(t, narr.Int64sHas(ints, 3)) } func TestStringsHas(t *testing.T) { ss := []string{"a", "b"} assert.True(t, narr.StringsHas(ss, "a")) assert.True(t, narr.StringsHas(ss, "b")) assert.False(t, narr.StringsHas(ss, "c")) } func TestInAndNotIn(t *testing.T) { is := assert.New(t) arr := []int{1, 2, 3} is.True(narr.In(arr, 2)) is.False(narr.NotIn(arr, 2)) arr1 := []rune{'a', 'b'} is.True(narr.In(arr1, 'b')) is.False(narr.NotIn(arr1, 'b')) arr2 := []string{"a", "b", "c"} is.True(narr.In(arr2, "b")) is.False(narr.NotIn(arr2, "b")) } func TestContainsAll(t *testing.T) { is := assert.New(t) arr := []int{1, 2, 3} is.True(narr.ContainsAll(arr, []int{2})) is.False(narr.ContainsAll(arr, []int{2, 45})) is.True(narr.IsParent(arr, []int{2})) arr2 := []string{"a", "b", "c"} is.True(narr.ContainsAll(arr2, []string{"b"})) is.False(narr.ContainsAll(arr2, []string{"b", "e"})) is.True(narr.IsParent(arr2, []string{"b"})) } func TestContains(t *testing.T) { is := assert.New(t) tests := map[any]any{ 1: []int{1, 2, 3}, 2: []int8{1, 2, 3}, 3: []int16{1, 2, 3}, 4: []int32{4, 2, 3}, 5: []int64{5, 2, 3}, 6: []uint{6, 2, 3}, 7: []uint8{7, 2, 3}, 8: []uint16{8, 2, 3}, 9: []uint32{9, 2, 3}, 10: []uint64{10, 3}, 11: []string{"11", "3"}, 'a': []int64{97}, 'b': []rune{'a', 'b'}, 'c': []byte{'a', 'b', 'c'}, // byte -> uint8 "a": []string{"a", "b", "c"}, 12: [5]uint{12, 1, 2, 3, 4}, 'A': [3]rune{'A', 'B', 'C'}, 'd': [4]byte{'a', 'b', 'c', 'd'}, "aa": [3]string{"aa", "bb", "cc"}, } for val, list := range tests { is.True(narr.Contains(list, val)) is.False(narr.NotContains(list, val)) } is.False(narr.Contains(nil, []int{})) is.False(narr.Contains('a', []int{})) // is.False(narr.Contains([]int{2, 3}, []int{2})) is.False(narr.Contains([]int{2, 3}, "a")) is.False(narr.Contains([]string{"a", "b"}, 12)) is.False(narr.Contains(nil, 12)) is.False(narr.Contains(map[int]int{2: 3}, 12)) tests1 := map[any]any{ 2: []int{1, 3}, "a": []string{"b", "c"}, } for val, list := range tests1 { is.True(narr.NotContains(list, val)) is.False(narr.Contains(list, val)) } }