-
Notifications
You must be signed in to change notification settings - Fork 53
/
sort_test.go
133 lines (121 loc) · 2.83 KB
/
sort_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2022 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 benchproc
import (
"math/rand"
"reflect"
"testing"
)
func TestSort(t *testing.T) {
check := func(keys []Key, want ...string) {
SortKeys(keys)
var got []string
for _, key := range keys {
got = append(got, key.String())
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
}
// Observation order.
s, _ := mustParse(t, "a")
k := []Key{
p(t, s, "", "a", "1"),
p(t, s, "", "a", "3"),
p(t, s, "", "a", "2"),
}
check(k, "a:1", "a:3", "a:2")
// Tuple observation order.
s, _ = mustParse(t, "a,b")
// Prepare order.
p(t, s, "", "a", "1")
p(t, s, "", "a", "2")
p(t, s, "", "b", "1")
p(t, s, "", "b", "2")
k = []Key{
p(t, s, "", "a", "2", "b", "1"),
p(t, s, "", "a", "1", "b", "2"),
}
check(k, "a:1 b:2", "a:2 b:1")
// Alphabetic
s, _ = mustParse(t, "a@alpha")
k = []Key{
p(t, s, "", "a", "c"),
p(t, s, "", "a", "b"),
p(t, s, "", "a", "a"),
}
check(k, "a:a", "a:b", "a:c")
// Numeric.
s, _ = mustParse(t, "a@num")
k = []Key{
p(t, s, "", "a", "100"),
p(t, s, "", "a", "20"),
p(t, s, "", "a", "3"),
}
check(k, "a:3", "a:20", "a:100")
// Numeric with strings.
s, _ = mustParse(t, "a@num")
k = []Key{
p(t, s, "", "a", "b"),
p(t, s, "", "a", "a"),
p(t, s, "", "a", "100"),
p(t, s, "", "a", "20"),
}
check(k, "a:20", "a:100", "a:a", "a:b")
// Numeric with weird cases.
s, _ = mustParse(t, "a@num")
k = []Key{
p(t, s, "", "a", "1"),
p(t, s, "", "a", "-inf"),
p(t, s, "", "a", "-infinity"),
p(t, s, "", "a", "inf"),
p(t, s, "", "a", "infinity"),
p(t, s, "", "a", "1.0"),
p(t, s, "", "a", "NaN"),
p(t, s, "", "a", "nan"),
}
// Shuffle the slice to exercise any instabilities.
for try := 0; try < 10; try++ {
for i := 1; i < len(k); i++ {
p := rand.Intn(i)
k[p], k[i] = k[i], k[p]
}
check(k, "a:-inf", "a:-infinity", "a:1", "a:1.0", "a:inf", "a:infinity", "a:NaN", "a:nan")
}
// Fixed.
s, _ = mustParse(t, "a@(c b a)")
k = []Key{
p(t, s, "", "a", "a"),
p(t, s, "", "a", "b"),
p(t, s, "", "a", "c"),
}
check(k, "a:c", "a:b", "a:a")
}
func TestParseNum(t *testing.T) {
check := func(x string, want float64) {
t.Helper()
got, err := parseNum(x)
if err != nil {
t.Errorf("%s: want %v, got error %s", x, want, err)
} else if want != got {
t.Errorf("%s: want %v, got %v", x, want, got)
}
}
check("1", 1)
check("1B", 1)
check("1b", 1)
check("100.5", 100.5)
check("1k", 1000)
check("1K", 1000)
check("1ki", 1024)
check("1kiB", 1024)
check("1M", 1000000)
check("1Mi", 1<<20)
check("1G", 1000000000)
check("1T", 1000000000000)
check("1P", 1000000000000000)
check("1E", 1000000000000000000)
check("1Z", 1000000000000000000000)
check("1Y", 1000000000000000000000000)
}