package narr_test import ( "fmt" "git.noahlan.cn/noahlan/ntool/narr" "git.noahlan.cn/noahlan/ntool/ntest/assert" "testing" ) func TestReverse(t *testing.T) { ss := []string{"a", "b", "c"} narr.Reverse(ss) assert.Eq(t, []string{"c", "b", "a"}, ss) } func TestStringsRemove(t *testing.T) { ss := []string{"a", "b", "c"} ns := narr.StringsRemove(ss, "b") assert.Contains(t, ns, "a") assert.NotContains(t, ns, "b") assert.Len(t, ns, 2) } func TestStringsFilter(t *testing.T) { is := assert.New(t) ss := narr.StringsFilter([]string{"a", "", "b", ""}) is.Eq([]string{"a", "b"}, ss) } func TestTrimStrings(t *testing.T) { is := assert.New(t) // TrimStrings ss := narr.TrimStrings([]string{" a", "b ", " c "}) is.Eq("[a b c]", fmt.Sprint(ss)) ss = narr.TrimStrings([]string{",a", "b.", ",.c,"}, ",.") is.Eq("[a b c]", fmt.Sprint(ss)) ss = narr.TrimStrings([]string{",a", "b.", ",.c,"}, ",", ".") is.Eq("[a b c]", fmt.Sprint(ss)) } func TestGetRandomOne(t *testing.T) { is := assert.New(t) // int slice intSlice := []int{1, 2, 3, 4, 5, 6} intVal := narr.GetRandomOne(intSlice) intVal1 := narr.GetRandomOne(intSlice) for intVal == intVal1 { intVal1 = narr.GetRandomOne(intSlice) } assert.IsType(t, 0, intVal) is.True(narr.HasValue(intSlice, intVal)) assert.IsType(t, 0, intVal1) is.True(narr.HasValue(intSlice, intVal1)) assert.NotEq(t, intVal, intVal1) // int array intArray := []int{1, 2, 3, 4, 5, 6} intReturned := narr.GetRandomOne(intArray) intReturned1 := narr.GetRandomOne(intArray) for intReturned == intReturned1 { intReturned1 = narr.GetRandomOne(intArray) } assert.IsType(t, 0, intReturned) is.True(narr.Contains(intArray, intReturned)) assert.IsType(t, 0, intReturned1) is.True(narr.Contains(intArray, intReturned1)) assert.NotEq(t, intReturned, intReturned1) // string slice strSlice := []string{"aa", "bb", "cc", "dd"} strVal := narr.GetRandomOne(strSlice) strVal1 := narr.GetRandomOne(strSlice) for strVal == strVal1 { strVal1 = narr.GetRandomOne(strSlice) } assert.IsType(t, "", strVal) is.True(narr.Contains(strSlice, strVal)) assert.IsType(t, "", strVal1) is.True(narr.Contains(strSlice, strVal1)) assert.NotEq(t, strVal, strVal1) // string array strArray := []string{"aa", "bb", "cc", "dd"} strReturned := narr.GetRandomOne(strArray) strReturned1 := narr.GetRandomOne(strArray) for strReturned == strReturned1 { strReturned1 = narr.GetRandomOne(strArray) } assert.IsType(t, "", strReturned) is.True(narr.Contains(strArray, strReturned)) assert.IsType(t, "", strReturned1) is.True(narr.Contains(strArray, strReturned1)) assert.NotEq(t, strReturned, strReturned1) // byte slice byteSlice := []byte("abcdefg") byteVal := narr.GetRandomOne(byteSlice) byteVal1 := narr.GetRandomOne(byteSlice) for byteVal == byteVal1 { byteVal1 = narr.GetRandomOne(byteSlice) } assert.IsType(t, byte('a'), byteVal) is.True(narr.Contains(byteSlice, byteVal)) assert.IsType(t, byte('a'), byteVal1) is.True(narr.Contains(byteSlice, byteVal1)) assert.NotEq(t, byteVal, byteVal1) is.Panics(func() { narr.RandomOne([]int{}) }) } func TestUnique(t *testing.T) { assert.Eq(t, []int{2, 3, 4}, narr.Unique[int]([]int{2, 3, 2, 4})) assert.Eq(t, []uint{2, 3, 4}, narr.Unique([]uint{2, 3, 2, 4})) assert.Eq(t, []string{"ab", "bc", "cd"}, narr.Unique([]string{"ab", "bc", "ab", "cd"})) assert.Eq(t, 1, narr.IndexOf(3, []int{2, 3, 4})) assert.Eq(t, -1, narr.IndexOf(5, []int{2, 3, 4})) }