]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmp/cmp_test.go
cmp: new package
[gostls13.git] / src / cmp / cmp_test.go
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.
4
5 package cmp_test
6
7 import (
8         "cmp"
9         "math"
10         "sort"
11         "testing"
12 )
13
14 var negzero = math.Copysign(0, -1)
15
16 var tests = []struct {
17         x, y    any
18         compare int
19 }{
20         {1, 2, -1},
21         {1, 1, 0},
22         {2, 1, +1},
23         {"a", "aa", -1},
24         {"a", "a", 0},
25         {"aa", "a", +1},
26         {1.0, 1.1, -1},
27         {1.1, 1.1, 0},
28         {1.1, 1.0, +1},
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},
40         {0.0, 0.0, 0},
41         {negzero, negzero, 0},
42         {negzero, 0.0, 0},
43         {0.0, negzero, 0},
44         {negzero, 1.0, -1},
45         {negzero, -1.0, +1},
46 }
47
48 func TestLess(t *testing.T) {
49         for _, test := range tests {
50                 var b bool
51                 switch test.x.(type) {
52                 case int:
53                         b = cmp.Less(test.x.(int), test.y.(int))
54                 case string:
55                         b = cmp.Less(test.x.(string), test.y.(string))
56                 case float64:
57                         b = cmp.Less(test.x.(float64), test.y.(float64))
58                 }
59                 if b != (test.compare < 0) {
60                         t.Errorf("Less(%v, %v) == %t, want %t", test.x, test.y, b, test.compare < 0)
61                 }
62         }
63 }
64
65 func TestCompare(t *testing.T) {
66         for _, test := range tests {
67                 var c int
68                 switch test.x.(type) {
69                 case int:
70                         c = cmp.Compare(test.x.(int), test.y.(int))
71                 case string:
72                         c = cmp.Compare(test.x.(string), test.y.(string))
73                 case float64:
74                         c = cmp.Compare(test.x.(float64), test.y.(float64))
75                 }
76                 if c != test.compare {
77                         t.Errorf("Compare(%v, %v) == %d, want %d", test.x, test.y, c, test.compare)
78                 }
79         }
80 }
81
82 func TestSort(t *testing.T) {
83         // Test that our comparison function is consistent with
84         // sort.Float64s.
85         input := []float64{1.0, 0.0, negzero, math.Inf(1), math.Inf(-1), math.NaN()}
86         sort.Float64s(input)
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)
90                 }
91                 if cmp.Compare(input[i], input[i+1]) > 0 {
92                         t.Errorf("Compare sort mismatch at %d in %v", i, input)
93                 }
94         }
95 }