|
5 | 5 | package slices
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "cmp" |
8 | 9 | "internal/race"
|
9 | 10 | "internal/testenv"
|
10 | 11 | "math"
|
@@ -134,6 +135,213 @@ func BenchmarkEqualFunc_Large(b *testing.B) {
|
134 | 135 | }
|
135 | 136 | }
|
136 | 137 |
|
| 138 | +var compareIntTests = []struct { |
| 139 | + s1, s2 []int |
| 140 | + want int |
| 141 | +}{ |
| 142 | + { |
| 143 | + []int{1}, |
| 144 | + []int{1}, |
| 145 | + 0, |
| 146 | + }, |
| 147 | + { |
| 148 | + []int{1}, |
| 149 | + []int{}, |
| 150 | + 1, |
| 151 | + }, |
| 152 | + { |
| 153 | + []int{}, |
| 154 | + []int{1}, |
| 155 | + -1, |
| 156 | + }, |
| 157 | + { |
| 158 | + []int{}, |
| 159 | + []int{}, |
| 160 | + 0, |
| 161 | + }, |
| 162 | + { |
| 163 | + []int{1, 2, 3}, |
| 164 | + []int{1, 2, 3}, |
| 165 | + 0, |
| 166 | + }, |
| 167 | + { |
| 168 | + []int{1, 2, 3}, |
| 169 | + []int{1, 2, 3, 4}, |
| 170 | + -1, |
| 171 | + }, |
| 172 | + { |
| 173 | + []int{1, 2, 3, 4}, |
| 174 | + []int{1, 2, 3}, |
| 175 | + +1, |
| 176 | + }, |
| 177 | + { |
| 178 | + []int{1, 2, 3}, |
| 179 | + []int{1, 4, 3}, |
| 180 | + -1, |
| 181 | + }, |
| 182 | + { |
| 183 | + []int{1, 4, 3}, |
| 184 | + []int{1, 2, 3}, |
| 185 | + +1, |
| 186 | + }, |
| 187 | + { |
| 188 | + []int{1, 4, 3}, |
| 189 | + []int{1, 2, 3, 8, 9}, |
| 190 | + +1, |
| 191 | + }, |
| 192 | +} |
| 193 | + |
| 194 | +var compareFloatTests = []struct { |
| 195 | + s1, s2 []float64 |
| 196 | + want int |
| 197 | +}{ |
| 198 | + { |
| 199 | + []float64{}, |
| 200 | + []float64{}, |
| 201 | + 0, |
| 202 | + }, |
| 203 | + { |
| 204 | + []float64{1}, |
| 205 | + []float64{1}, |
| 206 | + 0, |
| 207 | + }, |
| 208 | + { |
| 209 | + []float64{math.NaN()}, |
| 210 | + []float64{math.NaN()}, |
| 211 | + 0, |
| 212 | + }, |
| 213 | + { |
| 214 | + []float64{1, 2, math.NaN()}, |
| 215 | + []float64{1, 2, math.NaN()}, |
| 216 | + 0, |
| 217 | + }, |
| 218 | + { |
| 219 | + []float64{1, math.NaN(), 3}, |
| 220 | + []float64{1, math.NaN(), 4}, |
| 221 | + -1, |
| 222 | + }, |
| 223 | + { |
| 224 | + []float64{1, math.NaN(), 3}, |
| 225 | + []float64{1, 2, 4}, |
| 226 | + -1, |
| 227 | + }, |
| 228 | + { |
| 229 | + []float64{1, math.NaN(), 3}, |
| 230 | + []float64{1, 2, math.NaN()}, |
| 231 | + -1, |
| 232 | + }, |
| 233 | + { |
| 234 | + []float64{1, 2, 3}, |
| 235 | + []float64{1, 2, math.NaN()}, |
| 236 | + +1, |
| 237 | + }, |
| 238 | + { |
| 239 | + []float64{1, 2, 3}, |
| 240 | + []float64{1, math.NaN(), 3}, |
| 241 | + +1, |
| 242 | + }, |
| 243 | + { |
| 244 | + []float64{1, math.NaN(), 3, 4}, |
| 245 | + []float64{1, 2, math.NaN()}, |
| 246 | + -1, |
| 247 | + }, |
| 248 | +} |
| 249 | + |
| 250 | +func TestCompare(t *testing.T) { |
| 251 | + intWant := func(want bool) string { |
| 252 | + if want { |
| 253 | + return "0" |
| 254 | + } |
| 255 | + return "!= 0" |
| 256 | + } |
| 257 | + for _, test := range equalIntTests { |
| 258 | + if got := Compare(test.s1, test.s2); (got == 0) != test.want { |
| 259 | + t.Errorf("Compare(%v, %v) = %d, want %s", test.s1, test.s2, got, intWant(test.want)) |
| 260 | + } |
| 261 | + } |
| 262 | + for _, test := range equalFloatTests { |
| 263 | + if got := Compare(test.s1, test.s2); (got == 0) != test.wantEqualNaN { |
| 264 | + t.Errorf("Compare(%v, %v) = %d, want %s", test.s1, test.s2, got, intWant(test.wantEqualNaN)) |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + for _, test := range compareIntTests { |
| 269 | + if got := Compare(test.s1, test.s2); got != test.want { |
| 270 | + t.Errorf("Compare(%v, %v) = %d, want %d", test.s1, test.s2, got, test.want) |
| 271 | + } |
| 272 | + } |
| 273 | + for _, test := range compareFloatTests { |
| 274 | + if got := Compare(test.s1, test.s2); got != test.want { |
| 275 | + t.Errorf("Compare(%v, %v) = %d, want %d", test.s1, test.s2, got, test.want) |
| 276 | + } |
| 277 | + } |
| 278 | +} |
| 279 | + |
| 280 | +func equalToCmp[T comparable](eq func(T, T) bool) func(T, T) int { |
| 281 | + return func(v1, v2 T) int { |
| 282 | + if eq(v1, v2) { |
| 283 | + return 0 |
| 284 | + } |
| 285 | + return 1 |
| 286 | + } |
| 287 | +} |
| 288 | + |
| 289 | +func TestCompareFunc(t *testing.T) { |
| 290 | + intWant := func(want bool) string { |
| 291 | + if want { |
| 292 | + return "0" |
| 293 | + } |
| 294 | + return "!= 0" |
| 295 | + } |
| 296 | + for _, test := range equalIntTests { |
| 297 | + if got := CompareFunc(test.s1, test.s2, equalToCmp(equal[int])); (got == 0) != test.want { |
| 298 | + t.Errorf("CompareFunc(%v, %v, equalToCmp(equal[int])) = %d, want %s", test.s1, test.s2, got, intWant(test.want)) |
| 299 | + } |
| 300 | + } |
| 301 | + for _, test := range equalFloatTests { |
| 302 | + if got := CompareFunc(test.s1, test.s2, equalToCmp(equal[float64])); (got == 0) != test.wantEqual { |
| 303 | + t.Errorf("CompareFunc(%v, %v, equalToCmp(equal[float64])) = %d, want %s", test.s1, test.s2, got, intWant(test.wantEqual)) |
| 304 | + } |
| 305 | + } |
| 306 | + |
| 307 | + for _, test := range compareIntTests { |
| 308 | + if got := CompareFunc(test.s1, test.s2, cmp.Compare[int]); got != test.want { |
| 309 | + t.Errorf("CompareFunc(%v, %v, cmp[int]) = %d, want %d", test.s1, test.s2, got, test.want) |
| 310 | + } |
| 311 | + } |
| 312 | + for _, test := range compareFloatTests { |
| 313 | + if got := CompareFunc(test.s1, test.s2, cmp.Compare[float64]); got != test.want { |
| 314 | + t.Errorf("CompareFunc(%v, %v, cmp[float64]) = %d, want %d", test.s1, test.s2, got, test.want) |
| 315 | + } |
| 316 | + } |
| 317 | + |
| 318 | + s1 := []int{1, 2, 3} |
| 319 | + s2 := []int{2, 3, 4} |
| 320 | + if got := CompareFunc(s1, s2, equalToCmp(offByOne)); got != 0 { |
| 321 | + t.Errorf("CompareFunc(%v, %v, offByOne) = %d, want 0", s1, s2, got) |
| 322 | + } |
| 323 | + |
| 324 | + s3 := []string{"a", "b", "c"} |
| 325 | + s4 := []string{"A", "B", "C"} |
| 326 | + if got := CompareFunc(s3, s4, strings.Compare); got != 1 { |
| 327 | + t.Errorf("CompareFunc(%v, %v, strings.Compare) = %d, want 1", s3, s4, got) |
| 328 | + } |
| 329 | + |
| 330 | + compareLower := func(v1, v2 string) int { |
| 331 | + return strings.Compare(strings.ToLower(v1), strings.ToLower(v2)) |
| 332 | + } |
| 333 | + if got := CompareFunc(s3, s4, compareLower); got != 0 { |
| 334 | + t.Errorf("CompareFunc(%v, %v, compareLower) = %d, want 0", s3, s4, got) |
| 335 | + } |
| 336 | + |
| 337 | + cmpIntString := func(v1 int, v2 string) int { |
| 338 | + return strings.Compare(string(rune(v1)-1+'a'), v2) |
| 339 | + } |
| 340 | + if got := CompareFunc(s1, s3, cmpIntString); got != 0 { |
| 341 | + t.Errorf("CompareFunc(%v, %v, cmpIntString) = %d, want 0", s1, s3, got) |
| 342 | + } |
| 343 | +} |
| 344 | + |
137 | 345 | var indexTests = []struct {
|
138 | 346 | s []int
|
139 | 347 | v int
|
|
0 commit comments