1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 var negzero = math.Copysign(0, -1)
16 var tests = []struct {
29 {math.Inf(1), math.Inf(1), 0},
30 {math.Inf(-1), math.Inf(-1), 0},
31 {math.Inf(-1), 1.0, -1},
32 {1.0, math.Inf(-1), +1},
33 {math.Inf(1), 1.0, +1},
34 {1.0, math.Inf(1), -1},
35 {math.NaN(), math.NaN(), 0},
36 {0.0, math.NaN(), +1},
37 {math.NaN(), 0.0, -1},
38 {math.NaN(), math.Inf(-1), -1},
39 {math.Inf(-1), math.NaN(), +1},
41 {negzero, negzero, 0},
48 func TestLess(t *testing.T) {
49 for _, test := range tests {
51 switch test.x.(type) {
53 b = cmp.Less(test.x.(int), test.y.(int))
55 b = cmp.Less(test.x.(string), test.y.(string))
57 b = cmp.Less(test.x.(float64), test.y.(float64))
59 if b != (test.compare < 0) {
60 t.Errorf("Less(%v, %v) == %t, want %t", test.x, test.y, b, test.compare < 0)
65 func TestCompare(t *testing.T) {
66 for _, test := range tests {
68 switch test.x.(type) {
70 c = cmp.Compare(test.x.(int), test.y.(int))
72 c = cmp.Compare(test.x.(string), test.y.(string))
74 c = cmp.Compare(test.x.(float64), test.y.(float64))
76 if c != test.compare {
77 t.Errorf("Compare(%v, %v) == %d, want %d", test.x, test.y, c, test.compare)
82 func TestSort(t *testing.T) {
83 // Test that our comparison function is consistent with
85 input := []float64{1.0, 0.0, negzero, math.Inf(1), math.Inf(-1), math.NaN()}
87 for i := 0; i < len(input)-1; i++ {
88 if cmp.Less(input[i+1], input[i]) {
89 t.Errorf("Less sort mismatch at %d in %v", i, input)
91 if cmp.Compare(input[i], input[i+1]) > 0 {
92 t.Errorf("Compare sort mismatch at %d in %v", i, input)