@@ -6,63 +6,162 @@ import (
6
6
)
7
7
8
8
func TestAppendUnique (t * testing.T ) {
9
- initial := []string {"a" , "b" }
10
- result := AppendUnique (initial , "b" , "c" , "d" )
11
- expected := []string {"a" , "b" , "c" , "d" }
12
-
13
- if ! reflect .DeepEqual (result , expected ) {
14
- t .Errorf ("AppendUnique failed, expected %v, got %v" , expected , result )
15
- }
16
- }
17
-
18
- func TestContains (t * testing.T ) {
19
- slice := []string {"a" , "b" , "c" }
20
-
21
- if ! Contains (slice , "b" ) {
22
- t .Error ("Contains failed, expected true, got false" )
9
+ tests := []struct {
10
+ name string
11
+ input interface {}
12
+ elems interface {}
13
+ expected interface {}
14
+ }{
15
+ {"AppendUnique with strings" , []string {"apple" , "banana" }, []string {"banana" , "cherry" , "apple" }, []string {"apple" , "banana" , "cherry" }},
16
+ {"AppendUnique with integers" , []int {1 , 2 , 3 }, []int {3 , 4 , 1 , 5 }, []int {1 , 2 , 3 , 4 , 5 }},
17
+ {"AppendUnique with empty slice" , []float64 {}, []float64 {1.1 , 2.2 , 1.1 }, []float64 {1.1 , 2.2 }},
23
18
}
24
19
25
- if Contains (slice , "d" ) {
26
- t .Error ("Contains failed, expected false, got true" )
20
+ for _ , tt := range tests {
21
+ t .Run (tt .name , func (t * testing.T ) {
22
+ // Type switch based on the input type
23
+ switch v := tt .input .(type ) {
24
+ case []string :
25
+ result := AppendUnique (v , tt .elems .([]string )... )
26
+ if ! reflect .DeepEqual (result , tt .expected ) {
27
+ t .Errorf ("AppendUnique() = %v, expected %v" , result , tt .expected )
28
+ }
29
+ case []int :
30
+ result := AppendUnique (v , tt .elems .([]int )... )
31
+ if ! reflect .DeepEqual (result , tt .expected ) {
32
+ t .Errorf ("AppendUnique() = %v, expected %v" , result , tt .expected )
33
+ }
34
+ case []float64 :
35
+ result := AppendUnique (v , tt .elems .([]float64 )... )
36
+ if ! reflect .DeepEqual (result , tt .expected ) {
37
+ t .Errorf ("AppendUnique() = %v, expected %v" , result , tt .expected )
38
+ }
39
+ default :
40
+ t .Errorf ("Unhandled type %T" , tt .input )
41
+ }
42
+ })
27
43
}
28
44
}
29
45
30
- func TestEqual (t * testing.T ) {
31
- if ! Equal ("a" , "a" , "b" , "c" ) {
32
- t .Error ("Equal failed, expected true, got false" )
46
+ // Test Contains with different data types
47
+ func TestContains (t * testing.T ) {
48
+ tests := []struct {
49
+ name string
50
+ input interface {}
51
+ item interface {}
52
+ expected bool
53
+ }{
54
+ {"Contains with strings" , []string {"apple" , "banana" , "cherry" }, "banana" , true },
55
+ {"Contains with strings (not present)" , []string {"apple" , "banana" , "cherry" }, "mango" , false },
56
+ {"Contains with integers" , []int {1 , 2 , 3 , 4 }, 3 , true },
57
+ {"Contains with integers (not present)" , []int {1 , 2 , 3 , 4 }, 5 , false },
33
58
}
34
59
35
- if Equal ("d" , "a" , "b" , "c" ) {
36
- t .Error ("Equal failed, expected false, got true" )
60
+ for _ , tt := range tests {
61
+ t .Run (tt .name , func (t * testing.T ) {
62
+ var result bool
63
+ switch v := tt .input .(type ) {
64
+ case []string :
65
+ result = Contains (v , tt .item .(string ))
66
+ case []int :
67
+ result = Contains (v , tt .item .(int ))
68
+ }
69
+ if result != tt .expected {
70
+ t .Errorf ("Contains() = %v, expected %v" , result , tt .expected )
71
+ }
72
+ })
37
73
}
38
74
}
39
75
76
+ // Test Remove with different data types
40
77
func TestRemove (t * testing.T ) {
41
- initial := []string {"a" , "b" , "c" }
42
- result := Remove (initial , "b" )
43
- expected := []string {"a" , "c" }
78
+ tests := []struct {
79
+ name string
80
+ input interface {}
81
+ item interface {}
82
+ expected interface {}
83
+ }{
84
+ {"Remove with strings" , []string {"apple" , "banana" , "cherry" }, "banana" , []string {"apple" , "cherry" }},
85
+ {"Remove with integers" , []int {1 , 2 , 3 , 4 }, 3 , []int {1 , 2 , 4 }},
86
+ {"Remove with float64" , []float64 {1.1 , 2.2 , 3.3 , 4.4 }, 2.2 , []float64 {1.1 , 3.3 , 4.4 }},
87
+ }
44
88
45
- if ! reflect .DeepEqual (result , expected ) {
46
- t .Errorf ("Remove failed, expected %v, got %v" , expected , result )
89
+ for _ , tt := range tests {
90
+ t .Run (tt .name , func (t * testing.T ) {
91
+ var result interface {}
92
+ switch v := tt .input .(type ) {
93
+ case []string :
94
+ result = Remove (v , tt .item .(string ))
95
+ case []int :
96
+ result = Remove (v , tt .item .(int ))
97
+ case []float64 :
98
+ result = Remove (v , tt .item .(float64 ))
99
+ }
100
+ if ! reflect .DeepEqual (result , tt .expected ) {
101
+ t .Errorf ("Remove() = %v, expected %v" , result , tt .expected )
102
+ }
103
+ })
47
104
}
48
105
}
49
106
107
+ // Test DeleteEmpty with different data types
50
108
func TestDeleteEmpty (t * testing.T ) {
51
- initial := []string {"a" , "" , "b" , "" , "c" }
52
- result := DeleteEmpty (initial )
53
- expected := []string {"a" , "b" , "c" }
109
+ tests := []struct {
110
+ name string
111
+ input interface {}
112
+ expected interface {}
113
+ }{
114
+ {"DeleteEmpty with strings" , []string {"apple" , "" , "banana" , "" , "cherry" }, []string {"apple" , "banana" , "cherry" }},
115
+ {"DeleteEmpty with integers" , []int {0 , 1 , 0 , 2 , 0 , 3 }, []int {1 , 2 , 3 }}, // zero-value for int is 0
116
+ {"DeleteEmpty with float64" , []float64 {0.0 , 1.1 , 0.0 , 2.2 }, []float64 {1.1 , 2.2 }}, // zero-value for float64 is 0.0
117
+ }
54
118
55
- if ! reflect .DeepEqual (result , expected ) {
56
- t .Errorf ("DeleteEmpty failed, expected %v, got %v" , expected , result )
119
+ for _ , tt := range tests {
120
+ t .Run (tt .name , func (t * testing.T ) {
121
+ var result interface {}
122
+ switch v := tt .input .(type ) {
123
+ case []string :
124
+ result = DeleteEmpty (v )
125
+ case []int :
126
+ result = DeleteEmpty (v )
127
+ case []float64 :
128
+ result = DeleteEmpty (v )
129
+ }
130
+ if ! reflect .DeepEqual (result , tt .expected ) {
131
+ t .Errorf ("DeleteEmpty() = %v, expected %v" , result , tt .expected )
132
+ }
133
+ })
57
134
}
58
135
}
59
136
137
+ // Test Unique with different data types
60
138
func TestUnique (t * testing.T ) {
61
- initial := []string {"a" , "b" , "a" , "c" , "b" }
62
- result := Unique (initial )
63
- expected := []string {"a" , "b" , "c" }
139
+ tests := []struct {
140
+ name string
141
+ input interface {}
142
+ expected interface {}
143
+ }{
144
+ {"Unique with strings" , []string {"apple" , "banana" , "apple" , "cherry" }, []string {"apple" , "banana" , "cherry" }},
145
+ {"Unique with integers" , []int {1 , 2 , 2 , 3 , 1 , 4 }, []int {1 , 2 , 3 , 4 }},
146
+ {"Unique with float64" , []float64 {1.1 , 2.2 , 1.1 , 3.3 }, []float64 {1.1 , 2.2 , 3.3 }},
147
+ }
64
148
65
- if ! reflect .DeepEqual (result , expected ) {
66
- t .Errorf ("Unique failed, expected %v, got %v" , expected , result )
149
+ for _ , tt := range tests {
150
+ t .Run (tt .name , func (t * testing.T ) {
151
+ var result interface {}
152
+ switch v := tt .input .(type ) {
153
+ case []string :
154
+ result = Unique (v )
155
+ case []int :
156
+ result = Unique (v )
157
+ case []float64 :
158
+ result = Unique (v )
159
+ default :
160
+ t .Fatalf ("Unhandled type %T" , tt .input )
161
+ }
162
+ if ! reflect .DeepEqual (result , tt .expected ) {
163
+ t .Errorf ("Unique() = %v, expected %v" , result , tt .expected )
164
+ }
165
+ })
67
166
}
68
167
}
0 commit comments