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("ab"), []byte("abc"), -1},
21 {[]byte("abc"), []byte("ab"), 1},
22 {[]byte("x"), []byte("ab"), 1},
23 {[]byte("ab"), []byte("x"), -1},
24 {[]byte("x"), []byte("a"), 1},
25 {[]byte("b"), []byte("x"), -1},
26 // test runtime·memeq's chunked implementation
27 {[]byte("abcdefgh"), []byte("abcdefgh"), 0},
28 {[]byte("abcdefghi"), []byte("abcdefghi"), 0},
29 {[]byte("abcdefghi"), []byte("abcdefghj"), -1},
34 {[]byte("a"), nil, 1},
35 {nil, []byte("a"), -1},
38 func TestCompare(t
*testing
.T
) {
39 for _
, tt
:= range compareTests
{
40 cmp
:= Compare(tt
.a
, tt
.b
)
42 t
.Errorf(`Compare(%q, %q) = %v`, tt
.a
, tt
.b
, cmp
)
47 func TestCompareIdenticalSlice(t
*testing
.T
) {
48 var b
= []byte("Hello Gophers!")
49 if Compare(b
, b
) != 0 {
52 if Compare(b
, b
[:1]) != 1 {
53 t
.Error("b > b[:1] failed")
57 func TestCompareBytes(t
*testing
.T
) {
59 a
:= make([]byte, n
+1)
60 b
:= make([]byte, n
+1)
61 for len := 0; len < 128; len++ {
62 // randomish but deterministic data. No 0 or 255.
63 for i
:= 0; i
< len; i
++ {
64 a
[i
] = byte(1 + 31*i%254
)
65 b
[i
] = byte(1 + 31*i%254
)
67 // data past the end is different
68 for i
:= len; i
<= n
; i
++ {
72 cmp
:= Compare(a
[:len], b
[:len])
74 t
.Errorf(`CompareIdentical(%d) = %d`, len, cmp
)
77 cmp
= Compare(a
[:len-1], b
[:len])
79 t
.Errorf(`CompareAshorter(%d) = %d`, len, cmp
)
81 cmp
= Compare(a
[:len], b
[:len-1])
83 t
.Errorf(`CompareBshorter(%d) = %d`, len, cmp
)
86 for k
:= 0; k
< len; k
++ {
88 cmp
= Compare(a
[:len], b
[:len])
90 t
.Errorf(`CompareAbigger(%d,%d) = %d`, len, k
, cmp
)
93 cmp
= Compare(a
[:len], b
[:len])
95 t
.Errorf(`CompareBbigger(%d,%d) = %d`, len, k
, cmp
)
102 func BenchmarkCompareBytesEqual(b
*testing
.B
) {
103 b1
:= []byte("Hello Gophers!")
104 b2
:= []byte("Hello Gophers!")
105 for i
:= 0; i
< b
.N
; i
++ {
106 if Compare(b1
, b2
) != 0 {
112 func BenchmarkCompareBytesToNil(b
*testing
.B
) {
113 b1
:= []byte("Hello Gophers!")
115 for i
:= 0; i
< b
.N
; i
++ {
116 if Compare(b1
, b2
) != 1 {
117 b
.Fatal("b1 > b2 failed")
122 func BenchmarkCompareBytesEmpty(b
*testing
.B
) {
125 for i
:= 0; i
< b
.N
; i
++ {
126 if Compare(b1
, b2
) != 0 {
132 func BenchmarkCompareBytesIdentical(b
*testing
.B
) {
133 b1
:= []byte("Hello Gophers!")
135 for i
:= 0; i
< b
.N
; i
++ {
136 if Compare(b1
, b2
) != 0 {
142 func BenchmarkCompareBytesSameLength(b
*testing
.B
) {
143 b1
:= []byte("Hello Gophers!")
144 b2
:= []byte("Hello, Gophers")
145 for i
:= 0; i
< b
.N
; i
++ {
146 if Compare(b1
, b2
) != -1 {
147 b
.Fatal("b1 < b2 failed")
152 func BenchmarkCompareBytesDifferentLength(b
*testing
.B
) {
153 b1
:= []byte("Hello Gophers!")
154 b2
:= []byte("Hello, Gophers!")
155 for i
:= 0; i
< b
.N
; i
++ {
156 if Compare(b1
, b2
) != -1 {
157 b
.Fatal("b1 < b2 failed")
162 func BenchmarkCompareBytesBigUnaligned(b
*testing
.B
) {
164 b1
:= make([]byte, 0, 1<<20)
165 for len(b1
) < 1<<20 {
166 b1
= append(b1
, "Hello Gophers!"...)
168 b2
:= append([]byte("hello"), b1
...)
170 for i
:= 0; i
< b
.N
; i
++ {
171 if Compare(b1
, b2
[len("hello"):]) != 0 {
175 b
.SetBytes(int64(len(b1
)))
178 func BenchmarkCompareBytesBig(b
*testing
.B
) {
180 b1
:= make([]byte, 0, 1<<20)
181 for len(b1
) < 1<<20 {
182 b1
= append(b1
, "Hello Gophers!"...)
184 b2
:= append([]byte{}, b1
...)
186 for i
:= 0; i
< b
.N
; i
++ {
187 if Compare(b1
, b2
) != 0 {
191 b
.SetBytes(int64(len(b1
)))
194 func BenchmarkCompareBytesBigIdentical(b
*testing
.B
) {
196 b1
:= make([]byte, 0, 1<<20)
197 for len(b1
) < 1<<20 {
198 b1
= append(b1
, "Hello Gophers!"...)
202 for i
:= 0; i
< b
.N
; i
++ {
203 if Compare(b1
, b2
) != 0 {
207 b
.SetBytes(int64(len(b1
)))