diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-16 19:19:13 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-16 19:19:13 +0000 |
commit | ccd992355df7192993c666236047820244914598 (patch) | |
tree | f00fea65147227b7743083c6148396f74cd66935 /src/cmp/cmp_test.go | |
parent | Initial commit. (diff) | |
download | golang-1.21-ccd992355df7192993c666236047820244914598.tar.xz golang-1.21-ccd992355df7192993c666236047820244914598.zip |
Adding upstream version 1.21.8.upstream/1.21.8
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/cmp/cmp_test.go')
-rw-r--r-- | src/cmp/cmp_test.go | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/src/cmp/cmp_test.go b/src/cmp/cmp_test.go new file mode 100644 index 0000000..b0c0dc3 --- /dev/null +++ b/src/cmp/cmp_test.go @@ -0,0 +1,95 @@ +// Copyright 2023 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package cmp_test + +import ( + "cmp" + "math" + "sort" + "testing" +) + +var negzero = math.Copysign(0, -1) + +var tests = []struct { + x, y any + compare int +}{ + {1, 2, -1}, + {1, 1, 0}, + {2, 1, +1}, + {"a", "aa", -1}, + {"a", "a", 0}, + {"aa", "a", +1}, + {1.0, 1.1, -1}, + {1.1, 1.1, 0}, + {1.1, 1.0, +1}, + {math.Inf(1), math.Inf(1), 0}, + {math.Inf(-1), math.Inf(-1), 0}, + {math.Inf(-1), 1.0, -1}, + {1.0, math.Inf(-1), +1}, + {math.Inf(1), 1.0, +1}, + {1.0, math.Inf(1), -1}, + {math.NaN(), math.NaN(), 0}, + {0.0, math.NaN(), +1}, + {math.NaN(), 0.0, -1}, + {math.NaN(), math.Inf(-1), -1}, + {math.Inf(-1), math.NaN(), +1}, + {0.0, 0.0, 0}, + {negzero, negzero, 0}, + {negzero, 0.0, 0}, + {0.0, negzero, 0}, + {negzero, 1.0, -1}, + {negzero, -1.0, +1}, +} + +func TestLess(t *testing.T) { + for _, test := range tests { + var b bool + switch test.x.(type) { + case int: + b = cmp.Less(test.x.(int), test.y.(int)) + case string: + b = cmp.Less(test.x.(string), test.y.(string)) + case float64: + b = cmp.Less(test.x.(float64), test.y.(float64)) + } + if b != (test.compare < 0) { + t.Errorf("Less(%v, %v) == %t, want %t", test.x, test.y, b, test.compare < 0) + } + } +} + +func TestCompare(t *testing.T) { + for _, test := range tests { + var c int + switch test.x.(type) { + case int: + c = cmp.Compare(test.x.(int), test.y.(int)) + case string: + c = cmp.Compare(test.x.(string), test.y.(string)) + case float64: + c = cmp.Compare(test.x.(float64), test.y.(float64)) + } + if c != test.compare { + t.Errorf("Compare(%v, %v) == %d, want %d", test.x, test.y, c, test.compare) + } + } +} + +func TestSort(t *testing.T) { + // Test that our comparison function is consistent with + // sort.Float64s. + input := []float64{1.0, 0.0, negzero, math.Inf(1), math.Inf(-1), math.NaN()} + sort.Float64s(input) + for i := 0; i < len(input)-1; i++ { + if cmp.Less(input[i+1], input[i]) { + t.Errorf("Less sort mismatch at %d in %v", i, input) + } + if cmp.Compare(input[i], input[i+1]) > 0 { + t.Errorf("Compare sort mismatch at %d in %v", i, input) + } + } +} |