8 var compareTests
= []struct {
12 {[]byte(""), []byte(""), 0},
13 {[]byte("a"), []byte(""), 1},
14 {[]byte(""), []byte("a"), -1},
15 {[]byte("abc"), []byte("abc"), 0},
16 {[]byte("ab"), []byte("abc"), -1},
17 {[]byte("abc"), []byte("ab"), 1},
18 {[]byte("x"), []byte("ab"), 1},
19 {[]byte("ab"), []byte("x"), -1},
20 {[]byte("x"), []byte("a"), 1},
21 {[]byte("b"), []byte("x"), -1},
22 // test runtime·memeq's chunked implementation
23 {[]byte("abcdefgh"), []byte("abcdefgh"), 0},
24 {[]byte("abcdefghi"), []byte("abcdefghi"), 0},
25 {[]byte("abcdefghi"), []byte("abcdefghj"), -1},
30 {[]byte("a"), nil, 1},
31 {nil, []byte("a"), -1},
34 func TestCompare(t
*testing
.T
) {
35 for _
, tt
:= range compareTests
{
36 cmp
:= Compare(tt
.a
, tt
.b
)
38 t
.Errorf(`Compare(%q, %q) = %v`, tt
.a
, tt
.b
, cmp
)
43 func TestCompareIdenticalSlice(t
*testing
.T
) {
44 var b
= []byte("Hello Gophers!")
45 if Compare(b
, b
) != 0 {
48 if Compare(b
, b
[:1]) != 1 {
49 t
.Error("b > b[:1] failed")
53 func TestCompareBytes(t
*testing
.T
) {
55 a
:= make([]byte, n
+1)
56 b
:= make([]byte, n
+1)
57 for len := 0; len < 128; len++ {
58 // randomish but deterministic data. No 0 or 255.
59 for i
:= 0; i
< len; i
++ {
60 a
[i
] = byte(1 + 31*i%254
)
61 b
[i
] = byte(1 + 31*i%254
)
63 // data past the end is different
64 for i
:= len; i
<= n
; i
++ {
68 cmp
:= Compare(a
[:len], b
[:len])
70 t
.Errorf(`CompareIdentical(%d) = %d`, len, cmp
)
73 cmp
= Compare(a
[:len-1], b
[:len])
75 t
.Errorf(`CompareAshorter(%d) = %d`, len, cmp
)
77 cmp
= Compare(a
[:len], b
[:len-1])
79 t
.Errorf(`CompareBshorter(%d) = %d`, len, cmp
)
82 for k
:= 0; k
< len; k
++ {
84 cmp
= Compare(a
[:len], b
[:len])
86 t
.Errorf(`CompareAbigger(%d,%d) = %d`, len, k
, cmp
)
89 cmp
= Compare(a
[:len], b
[:len])
91 t
.Errorf(`CompareBbigger(%d,%d) = %d`, len, k
, cmp
)
98 func BenchmarkCompareBytesEqual(b
*testing
.B
) {
99 b1
:= []byte("Hello Gophers!")
100 b2
:= []byte("Hello Gophers!")
101 for i
:= 0; i
< b
.N
; i
++ {
102 if Compare(b1
, b2
) != 0 {
108 func BenchmarkCompareBytesToNil(b
*testing
.B
) {
109 b1
:= []byte("Hello Gophers!")
111 for i
:= 0; i
< b
.N
; i
++ {
112 if Compare(b1
, b2
) != 1 {
113 b
.Fatal("b1 > b2 failed")
118 func BenchmarkCompareBytesEmpty(b
*testing
.B
) {
121 for i
:= 0; i
< b
.N
; i
++ {
122 if Compare(b1
, b2
) != 0 {
128 func BenchmarkCompareBytesIdentical(b
*testing
.B
) {
129 b1
:= []byte("Hello Gophers!")
131 for i
:= 0; i
< b
.N
; i
++ {
132 if Compare(b1
, b2
) != 0 {
138 func BenchmarkCompareBytesSameLength(b
*testing
.B
) {
139 b1
:= []byte("Hello Gophers!")
140 b2
:= []byte("Hello, Gophers")
141 for i
:= 0; i
< b
.N
; i
++ {
142 if Compare(b1
, b2
) != -1 {
143 b
.Fatal("b1 < b2 failed")
148 func BenchmarkCompareBytesDifferentLength(b
*testing
.B
) {
149 b1
:= []byte("Hello Gophers!")
150 b2
:= []byte("Hello, Gophers!")
151 for i
:= 0; i
< b
.N
; i
++ {
152 if Compare(b1
, b2
) != -1 {
153 b
.Fatal("b1 < b2 failed")
158 func BenchmarkCompareBytesBigUnaligned(b
*testing
.B
) {
160 b1
:= make([]byte, 0, 1<<20)
161 for len(b1
) < 1<<20 {
162 b1
= append(b1
, "Hello Gophers!"...)
164 b2
:= append([]byte("hello"), b1
...)
166 for i
:= 0; i
< b
.N
; i
++ {
167 if Compare(b1
, b2
[len("hello"):]) != 0 {
171 b
.SetBytes(int64(len(b1
)))
174 func BenchmarkCompareBytesBig(b
*testing
.B
) {
176 b1
:= make([]byte, 0, 1<<20)
177 for len(b1
) < 1<<20 {
178 b1
= append(b1
, "Hello Gophers!"...)
180 b2
:= append([]byte{}, b1
...)
182 for i
:= 0; i
< b
.N
; i
++ {
183 if Compare(b1
, b2
) != 0 {
187 b
.SetBytes(int64(len(b1
)))
190 func BenchmarkCompareBytesBigIdentical(b
*testing
.B
) {
192 b1
:= make([]byte, 0, 1<<20)
193 for len(b1
) < 1<<20 {
194 b1
= append(b1
, "Hello Gophers!"...)
198 for i
:= 0; i
< b
.N
; i
++ {
199 if Compare(b1
, b2
) != 0 {
203 b
.SetBytes(int64(len(b1
)))