1 // Copyright 2017, 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.md file.
12 "github.com/google/go-cmp/cmp"
13 "github.com/google/go-cmp/cmp/internal/value"
16 func TestSortKeys(t *testing.T) {
29 cmp.Comparer(func(x, y float64) bool {
30 if math.IsNaN(x) && math.IsNaN(y) {
35 cmp.Comparer(func(x, y complex128) bool {
36 rx, ix, ry, iy := real(x), imag(x), real(y), imag(y)
37 if math.IsNaN(rx) && math.IsNaN(ry) {
40 if math.IsNaN(ix) && math.IsNaN(iy) {
43 return rx == ry && ix == iy
45 cmp.Comparer(func(x, y chan bool) bool { return true }),
46 cmp.Comparer(func(x, y chan int) bool { return true }),
47 cmp.Comparer(func(x, y chan float64) bool { return true }),
48 cmp.Comparer(func(x, y chan interface{}) bool { return true }),
49 cmp.Comparer(func(x, y *int) bool { return true }),
53 in map[interface{}]bool // Set of keys to sort
56 in: map[interface{}]bool{1: true, 2: true, 3: true},
57 want: []interface{}{1, 2, 3},
59 in: map[interface{}]bool{
74 MyString("abc"): true,
75 MyString("abcd"): true,
76 MyString("abcde"): true,
79 make(chan bool): true,
80 make(chan bool): true,
82 make(chan interface{}): true,
98 MyArray([2]int{2, 4}): true,
101 "bravo", [2]int{2, 3}, make(chan float64),
104 "alpha", [2]int{3, 3}, make(chan float64),
109 -555, -55, -5, uint(1), uint(11), uint(111),
110 math.Inf(-1), 1.2345, 12.345, 123.45, 1234.5, math.Inf(+1),
111 (0 + 0i), (0 + 1i), (0 + 2i), (0 + 3i), (1 + 0i), (2 + 0i),
112 [2]int{2, 3}, [2]int{2, 4}, [2]int{4, 0}, MyArray([2]int{2, 4}),
113 make(chan bool), make(chan bool), make(chan int), make(chan interface{}),
115 "abc", "abcd", "abcde", "bar", "foo",
116 MyString("abc"), MyString("abcd"), MyString("abcde"),
118 MyStruct{"alpha", [2]int{3, 3}, make(chan float64)},
119 MyStruct{"bravo", [2]int{2, 3}, make(chan float64)},
122 // NaN values cannot be properly deduplicated.
123 // This is okay since map entries with NaN in the keys cannot be
124 // retrieved anyways.
125 in: map[interface{}]bool{
128 complex(0, math.NaN()): true,
129 complex(0, math.NaN()): true,
130 complex(math.NaN(), 0): true,
131 complex(math.NaN(), 0): true,
132 complex(math.NaN(), math.NaN()): true,
136 complex(math.NaN(), math.NaN()),
137 complex(math.NaN(), 0),
138 complex(0, math.NaN()),
142 for i, tt := range tests {
143 // Intentionally pass the map via an unexported field to detect panics.
144 // Unfortunately, we cannot actually test the keys without using unsafe.
145 v := reflect.ValueOf(struct{ x map[interface{}]bool }{tt.in}).Field(0)
146 value.SortKeys(append(v.MapKeys(), v.MapKeys()...))
148 // Try again, with keys that have read-write access in reflect.
149 v = reflect.ValueOf(tt.in)
150 keys := append(v.MapKeys(), v.MapKeys()...)
151 var got []interface{}
152 for _, k := range value.SortKeys(keys) {
153 got = append(got, k.Interface())
155 if d := cmp.Diff(got, tt.want, opts...); d != "" {
156 t.Errorf("test %d, Sort() mismatch (-got +want):\n%s", i, d)