github.com/kaydxh/golang@v0.0.131/go/strings/string_slice_test.go (about) 1 /* 2 *Copyright (c) 2022, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 package strings_test 23 24 import ( 25 "fmt" 26 "testing" 27 28 strings_ "github.com/kaydxh/golang/go/strings" 29 "github.com/stretchr/testify/assert" 30 ) 31 32 func TestStringIntersection(t *testing.T) { 33 testCases := []struct { 34 name string 35 s1 []string 36 s2 []string 37 expected []string 38 }{ 39 { 40 name: "test string", 41 s1: []string{"1", "2", "3", "4"}, 42 s2: []string{"3", "4", "5", "6"}, 43 expected: []string{"3", "4"}, 44 }, 45 { 46 name: "test string2", 47 s1: []string{"1", "2", "3", "4"}, 48 s2: []string{"5", "6"}, 49 expected: []string{}, 50 }, 51 } 52 53 for _, testCase := range testCases { 54 t.Run(testCase.name, func(t *testing.T) { 55 intersection := strings_.SliceIntersection(testCase.s1, testCase.s2) 56 if len(intersection) != len(testCase.expected) { 57 t.Fatalf("Expected Intersection len: %v, got : %v", len(testCase.expected), len(intersection)) 58 } 59 t.Logf("intersection :%v", intersection) 60 }) 61 } 62 } 63 64 func TestSliceDifference(t *testing.T) { 65 testCases := []struct { 66 name string 67 s1 []string 68 s2 []string 69 expected []string 70 }{ 71 { 72 name: "test string", 73 s1: []string{"1", "2", "3", "4"}, 74 s2: []string{"3", "4", "5", "6"}, 75 expected: []string{"1", "2"}, 76 }, 77 } 78 79 for _, testCase := range testCases { 80 t.Run(testCase.name, func(t *testing.T) { 81 difference := strings_.SliceDifference(testCase.s1, testCase.s2) 82 if len(difference) != len(testCase.expected) { 83 t.Fatalf("Expected Difference len: %v, got : %v", len(testCase.expected), len(difference)) 84 } 85 t.Logf("difference :%v", difference) 86 }) 87 } 88 } 89 90 func TestRemoveEmpty(t *testing.T) { 91 testCases := []struct { 92 s []string 93 }{ 94 { 95 s: []string{"1", "", "3", "4"}, 96 }, 97 { 98 s: []string{"", "", "", ""}, 99 }, 100 } 101 102 for i, testCase := range testCases { 103 t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) { 104 intersection := strings_.RemoveEmpty(testCase.s) 105 t.Logf("intersection :%v", intersection) 106 }) 107 } 108 } 109 110 func TestSliceContains(t *testing.T) { 111 testCases := []struct { 112 s []string 113 target string 114 caseSensitive bool 115 expected bool 116 }{ 117 { 118 s: []string{"a", "bdx"}, 119 target: "bDx", 120 caseSensitive: false, 121 expected: true, 122 }, 123 { 124 s: []string{"a", "bdx"}, 125 target: "bdx", 126 caseSensitive: true, 127 expected: true, 128 }, 129 } 130 131 for i, testCase := range testCases { 132 t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) { 133 has := strings_.SliceContains(testCase.s, testCase.target, testCase.caseSensitive) 134 t.Logf("has :%v", has) 135 assert.Equal(t, testCase.expected, has) 136 }) 137 } 138 }