1
16
17 package slices
18
19 import (
20 "fmt"
21 "net"
22 "reflect"
23 "regexp"
24 "strings"
25 "testing"
26
27 utilnet "k8s.io/utils/net"
28 )
29
30 func Example() {
31 a := []string{
32 "10.0.0.0", "FOO", "1000::1", "fd80::4%eth0", "BAR", "192.168.1.300",
33 "172.12.0.1", "fc00::5000",
34 }
35 v := func(s string) bool { return net.ParseIP(s) == nil }
36 if notIP := Filter(nil, a, v); len(notIP) > 0 {
37 fmt.Println("Invalid", notIP)
38 }
39 fmt.Println(Filter(nil, a, utilnet.IsIPv6String))
40
41
42
43 }
44
45 func Example_regexp() {
46 a := []string{
47 "10.0.0.0", "FOO", "1000::1", "fd80::4%eth0", "BAR", "192.168.1.300",
48 "172.12.0.1", "fc00::5000",
49 }
50 re := regexp.MustCompile("^[A-Z]+$")
51 fmt.Println(Filter(nil, a, re.MatchString))
52
53 }
54
55 func ExampleFilter_empty() {
56 s := []string{"FOO", "", "", "BAR", "fd80::8888", ""}
57 fmt.Println(Filter(nil, s, func(s string) bool { return s != "" }))
58
59 }
60
61 func TestFilter(t *testing.T) {
62 testCases := []struct {
63 validator func(string) bool
64 input []string
65 res []string
66 }{
67
68 {
69 regexp.MustCompile("zzz.*").MatchString,
70 []string{"FOO", "1000::", "BAR", "fd80::8888"},
71 nil,
72 },
73
74 {
75 regexp.MustCompile(".*").MatchString,
76 []string{"FOO", "1000::", "BAR", "fd80::8888"},
77 []string{"FOO", "1000::", "BAR", "fd80::8888"},
78 },
79
80 {
81 func(s string) bool { return strings.Contains(s, ".") },
82 []string{"10.0.0.0", "1000::", "8.8.8.8", "fd80::8888"},
83 []string{"10.0.0.0", "8.8.8.8"},
84 },
85 }
86 for i, tc := range testCases {
87 res := Filter(nil, tc.input, tc.validator)
88 if !reflect.DeepEqual(tc.res, res) {
89 t.Errorf("TC %d: %v expected %v", i, res, tc.res)
90 }
91 }
92 }
93
94 func TestFilterInplace(t *testing.T) {
95 testCases := []struct {
96 validator func(string) bool
97 input []string
98 res []string
99 inputAfter []string
100 }{
101
102 {
103 func(s string) bool { return s != "" },
104 []string{"FOO", "", "", "BAR", "fd80::8888", ""},
105 []string{"FOO", "BAR", "fd80::8888"},
106 []string{"FOO", "BAR", "fd80::8888", "BAR", "fd80::8888", ""},
107 },
108 }
109 for i, tc := range testCases {
110 res := Filter(tc.input[:0], tc.input, tc.validator)
111 if !reflect.DeepEqual(tc.res, res) {
112 t.Errorf("TC %d: %v expected %v", i, res, tc.res)
113 }
114 if !reflect.DeepEqual(tc.input, tc.inputAfter) {
115 t.Errorf("TC %d: mutated input %v expected %v", i, tc.input, tc.inputAfter)
116 }
117 }
118 }
119
120 func TestContains(t *testing.T) {
121 testCases := []struct {
122 input []string
123 what string
124 res bool
125 }{
126
127 {
128 nil,
129 "",
130 false,
131 },
132
133 {
134 []string{"FOO", "BAR", ""},
135 "",
136 true,
137 },
138
139 {
140 []string{"FOO", "BAR", ""},
141 "NOPE",
142 false,
143 },
144 }
145 for i, tc := range testCases {
146 res := Contains(tc.input, tc.what)
147 if res != tc.res {
148 t.Errorf("TC %d: %v expected %v", i, res, tc.res)
149 }
150 }
151 }
152
153 func TestClone(t *testing.T) {
154 testCases := []struct {
155 input []string
156 res []string
157 }{
158 {
159 nil,
160 nil,
161 },
162 {
163 []string{},
164 []string{},
165 },
166 {
167 []string{"", "FOO", "BAR", ""},
168 []string{"", "FOO", "BAR", ""},
169 },
170 }
171 for i, tc := range testCases {
172 res := Clone(tc.input)
173 if !reflect.DeepEqual(tc.res, res) {
174 t.Errorf("TC %d: %v expected %v", i, res, tc.res)
175 }
176 if len(tc.input) > 0 {
177 tc.input[0] = "NOPE"
178 if tc.res[0] == "NOPE" {
179 t.Errorf("TC %d: Clone is not cloned", i)
180 }
181 }
182 }
183 }
184
View as plain text