1 // Copyright 2013 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.
12 var compareTests
= []struct {
16 {[]byte(""), []byte(""), 0},
17 {[]byte("a"), []byte(""), 1},
18 {[]byte(""), []byte("a"), -1},
19 {[]byte("abc"), []byte("abc"), 0},
20 {[]byte("abd"), []byte("abc"), 1},
21 {[]byte("abc"), []byte("abd"), -1},
22 {[]byte("ab"), []byte("abc"), -1},
23 {[]byte("abc"), []byte("ab"), 1},
24 {[]byte("x"), []byte("ab"), 1},
25 {[]byte("ab"), []byte("x"), -1},
26 {[]byte("x"), []byte("a"), 1},
27 {[]byte("b"), []byte("x"), -1},
28 // test runtime·memeq's chunked implementation
29 {[]byte("abcdefgh"), []byte("abcdefgh"), 0},
30 {[]byte("abcdefghi"), []byte("abcdefghi"), 0},
31 {[]byte("abcdefghi"), []byte("abcdefghj"), -1},
32 {[]byte("abcdefghj"), []byte("abcdefghi"), 1},
37 {[]byte("a"), nil, 1},
38 {nil, []byte("a"), -1},
41 func TestCompare(t
*testing
.T
) {
42 for _
, tt
:= range compareTests
{
43 cmp
:= Compare(tt
.a
, tt
.b
)
45 t
.Errorf(`Compare(%q, %q) = %v`, tt
.a
, tt
.b
, cmp
)
50 func TestCompareIdenticalSlice(t
*testing
.T
) {
51 var b
= []byte("Hello Gophers!")
52 if Compare(b
, b
) != 0 {
55 if Compare(b
, b
[:1]) != 1 {
56 t
.Error("b > b[:1] failed")
60 func TestCompareBytes(t
*testing
.T
) {
62 a
:= make([]byte, n
+1)
63 b
:= make([]byte, n
+1)
64 for len := 0; len < 128; len++ {
65 // randomish but deterministic data. No 0 or 255.
66 for i
:= 0; i
< len; i
++ {
67 a
[i
] = byte(1 + 31*i%254
)
68 b
[i
] = byte(1 + 31*i%254
)
70 // data past the end is different
71 for i
:= len; i
<= n
; i
++ {
75 cmp
:= Compare(a
[:len], b
[:len])
77 t
.Errorf(`CompareIdentical(%d) = %d`, len, cmp
)
80 cmp
= Compare(a
[:len-1], b
[:len])
82 t
.Errorf(`CompareAshorter(%d) = %d`, len, cmp
)
84 cmp
= Compare(a
[:len], b
[:len-1])
86 t
.Errorf(`CompareBshorter(%d) = %d`, len, cmp
)
89 for k
:= 0; k
< len; k
++ {
91 cmp
= Compare(a
[:len], b
[:len])
93 t
.Errorf(`CompareAbigger(%d,%d) = %d`, len, k
, cmp
)
96 cmp
= Compare(a
[:len], b
[:len])
98 t
.Errorf(`CompareBbigger(%d,%d) = %d`, len, k
, cmp
)
105 func BenchmarkCompareBytesEqual(b
*testing
.B
) {
106 b1
:= []byte("Hello Gophers!")
107 b2
:= []byte("Hello Gophers!")
108 for i
:= 0; i
< b
.N
; i
++ {
109 if Compare(b1
, b2
) != 0 {
115 func BenchmarkCompareBytesToNil(b
*testing
.B
) {
116 b1
:= []byte("Hello Gophers!")
118 for i
:= 0; i
< b
.N
; i
++ {
119 if Compare(b1
, b2
) != 1 {
120 b
.Fatal("b1 > b2 failed")
125 func BenchmarkCompareBytesEmpty(b
*testing
.B
) {
128 for i
:= 0; i
< b
.N
; i
++ {
129 if Compare(b1
, b2
) != 0 {
135 func BenchmarkCompareBytesIdentical(b
*testing
.B
) {
136 b1
:= []byte("Hello Gophers!")
138 for i
:= 0; i
< b
.N
; i
++ {
139 if Compare(b1
, b2
) != 0 {
145 func BenchmarkCompareBytesSameLength(b
*testing
.B
) {
146 b1
:= []byte("Hello Gophers!")
147 b2
:= []byte("Hello, Gophers")
148 for i
:= 0; i
< b
.N
; i
++ {
149 if Compare(b1
, b2
) != -1 {
150 b
.Fatal("b1 < b2 failed")
155 func BenchmarkCompareBytesDifferentLength(b
*testing
.B
) {
156 b1
:= []byte("Hello Gophers!")
157 b2
:= []byte("Hello, Gophers!")
158 for i
:= 0; i
< b
.N
; i
++ {
159 if Compare(b1
, b2
) != -1 {
160 b
.Fatal("b1 < b2 failed")
165 func BenchmarkCompareBytesBigUnaligned(b
*testing
.B
) {
167 b1
:= make([]byte, 0, 1<<20)
168 for len(b1
) < 1<<20 {
169 b1
= append(b1
, "Hello Gophers!"...)
171 b2
:= append([]byte("hello"), b1
...)
173 for i
:= 0; i
< b
.N
; i
++ {
174 if Compare(b1
, b2
[len("hello"):]) != 0 {
178 b
.SetBytes(int64(len(b1
)))
181 func BenchmarkCompareBytesBig(b
*testing
.B
) {
183 b1
:= make([]byte, 0, 1<<20)
184 for len(b1
) < 1<<20 {
185 b1
= append(b1
, "Hello Gophers!"...)
187 b2
:= append([]byte{}, b1
...)
189 for i
:= 0; i
< b
.N
; i
++ {
190 if Compare(b1
, b2
) != 0 {
194 b
.SetBytes(int64(len(b1
)))
197 func BenchmarkCompareBytesBigIdentical(b
*testing
.B
) {
199 b1
:= make([]byte, 0, 1<<20)
200 for len(b1
) < 1<<20 {
201 b1
= append(b1
, "Hello Gophers!"...)
205 for i
:= 0; i
< b
.N
; i
++ {
206 if Compare(b1
, b2
) != 0 {
210 b
.SetBytes(int64(len(b1
)))