github.com/kaydxh/golang@v0.0.131/go/slices/slices_test.go (about) 1 package slices_test 2 3 import ( 4 "fmt" 5 "testing" 6 7 slices_ "github.com/kaydxh/golang/go/slices" 8 ) 9 10 func TestSliceIntersection(t *testing.T) { 11 testCases := []struct { 12 name string 13 s1 []string 14 s2 []string 15 expected []string 16 }{ 17 { 18 name: "test string", 19 s1: []string{"1", "2", "3", "4"}, 20 s2: []string{"3", "4", "5", "6"}, 21 expected: []string{"3", "4"}, 22 }, 23 { 24 name: "test string2", 25 s1: []string{"1", "2", "3", "4"}, 26 s2: []string{"5", "6"}, 27 expected: []string{}, 28 }, 29 } 30 31 for _, testCase := range testCases { 32 t.Run(testCase.name, func(t *testing.T) { 33 intersection := slices_.SliceIntersection(testCase.s1, testCase.s2) 34 if len(intersection) != len(testCase.expected) { 35 t.Fatalf("Expected Intersection len: %v, got : %v", len(testCase.expected), len(intersection)) 36 } 37 t.Logf("intersection :%v", intersection) 38 }) 39 } 40 } 41 42 func TestSliceDifference(t *testing.T) { 43 testCases := []struct { 44 name string 45 s1 []string 46 s2 []string 47 expected []string 48 }{ 49 { 50 name: "test string", 51 s1: []string{"1", "2", "3", "4"}, 52 s2: []string{"3", "4", "5", "6"}, 53 expected: []string{"1", "2"}, 54 }, 55 } 56 57 for _, testCase := range testCases { 58 t.Run(testCase.name, func(t *testing.T) { 59 difference := slices_.SliceDifference(testCase.s1, testCase.s2) 60 if len(difference) != len(testCase.expected) { 61 t.Fatalf("Expected Difference len: %v, got : %v", len(testCase.expected), len(difference)) 62 } 63 t.Logf("difference :%v", difference) 64 }) 65 } 66 } 67 68 func TestRemoveEmpty(t *testing.T) { 69 testCases := []struct { 70 s []string 71 }{ 72 { 73 s: []string{"1", "", "3", "4"}, 74 }, 75 { 76 s: []string{"", "", "", ""}, 77 }, 78 } 79 80 for i, testCase := range testCases { 81 t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) { 82 intersection := slices_.RemoveEmpty(testCase.s) 83 t.Logf("intersection :%v", intersection) 84 }) 85 } 86 } 87 88 func TestFirstOrDefaultZero(t *testing.T) { 89 testCases := []struct { 90 s []string 91 }{ 92 { 93 s: []string{"1", "", "3", "4"}, 94 }, 95 { 96 s: []string{"", "", "", "2"}, 97 }, 98 } 99 100 for i, testCase := range testCases { 101 t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) { 102 intersection := slices_.FirstOrDefaultZero(testCase.s) 103 t.Logf("FirstOrDefaultZero :%v", intersection) 104 }) 105 } 106 }