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.
13 var compareTests = []struct {
17 {[]byte(""), []byte(""), 0},
18 {[]byte("a"), []byte(""), 1},
19 {[]byte(""), []byte("a"), -1},
20 {[]byte("abc"), []byte("abc"), 0},
21 {[]byte("abd"), []byte("abc"), 1},
22 {[]byte("abc"), []byte("abd"), -1},
23 {[]byte("ab"), []byte("abc"), -1},
24 {[]byte("abc"), []byte("ab"), 1},
25 {[]byte("x"), []byte("ab"), 1},
26 {[]byte("ab"), []byte("x"), -1},
27 {[]byte("x"), []byte("a"), 1},
28 {[]byte("b"), []byte("x"), -1},
29 // test runtime·memeq's chunked implementation
30 {[]byte("abcdefgh"), []byte("abcdefgh"), 0},
31 {[]byte("abcdefghi"), []byte("abcdefghi"), 0},
32 {[]byte("abcdefghi"), []byte("abcdefghj"), -1},
33 {[]byte("abcdefghj"), []byte("abcdefghi"), 1},
38 {[]byte("a"), nil, 1},
39 {nil, []byte("a"), -1},
42 func TestCompare(t *testing.T) {
43 for _, tt := range compareTests {
45 buffer := make([]byte, len(tt.b)+numShifts)
46 // vary the input alignment of tt.b
47 for offset := 0; offset <= numShifts; offset++ {
48 shiftedB := buffer[offset : len(tt.b)+offset]
50 cmp := Compare(tt.a, shiftedB)
52 t.Errorf(`Compare(%q, %q), offset %d = %v; want %v`, tt.a, tt.b, offset, cmp, tt.i)
58 func TestCompareIdenticalSlice(t *testing.T) {
59 var b = []byte("Hello Gophers!")
60 if Compare(b, b) != 0 {
63 if Compare(b, b[:1]) != 1 {
64 t.Error("b > b[:1] failed")
68 func TestCompareBytes(t *testing.T) {
69 lengths := make([]int, 0) // lengths to test in ascending order
70 for i := 0; i <= 128; i++ {
71 lengths = append(lengths, i)
73 lengths = append(lengths, 256, 512, 1024, 1333, 4095, 4096, 4097)
75 if !testing.Short() || testenv.Builder() != "" {
76 lengths = append(lengths, 65535, 65536, 65537, 99999)
79 n := lengths[len(lengths)-1]
80 a := make([]byte, n+1)
81 b := make([]byte, n+1)
82 for _, len := range lengths {
83 // randomish but deterministic data. No 0 or 255.
84 for i := 0; i < len; i++ {
85 a[i] = byte(1 + 31*i%254)
86 b[i] = byte(1 + 31*i%254)
88 // data past the end is different
89 for i := len; i <= n; i++ {
93 cmp := Compare(a[:len], b[:len])
95 t.Errorf(`CompareIdentical(%d) = %d`, len, cmp)
98 cmp = Compare(a[:len-1], b[:len])
100 t.Errorf(`CompareAshorter(%d) = %d`, len, cmp)
102 cmp = Compare(a[:len], b[:len-1])
104 t.Errorf(`CompareBshorter(%d) = %d`, len, cmp)
107 for k := 0; k < len; k++ {
109 cmp = Compare(a[:len], b[:len])
111 t.Errorf(`CompareAbigger(%d,%d) = %d`, len, k, cmp)
114 cmp = Compare(a[:len], b[:len])
116 t.Errorf(`CompareBbigger(%d,%d) = %d`, len, k, cmp)
123 func BenchmarkCompareBytesEqual(b *testing.B) {
124 b1 := []byte("Hello Gophers!")
125 b2 := []byte("Hello Gophers!")
126 for i := 0; i < b.N; i++ {
127 if Compare(b1, b2) != 0 {
133 func BenchmarkCompareBytesToNil(b *testing.B) {
134 b1 := []byte("Hello Gophers!")
136 for i := 0; i < b.N; i++ {
137 if Compare(b1, b2) != 1 {
138 b.Fatal("b1 > b2 failed")
143 func BenchmarkCompareBytesEmpty(b *testing.B) {
146 for i := 0; i < b.N; i++ {
147 if Compare(b1, b2) != 0 {
153 func BenchmarkCompareBytesIdentical(b *testing.B) {
154 b1 := []byte("Hello Gophers!")
156 for i := 0; i < b.N; i++ {
157 if Compare(b1, b2) != 0 {
163 func BenchmarkCompareBytesSameLength(b *testing.B) {
164 b1 := []byte("Hello Gophers!")
165 b2 := []byte("Hello, Gophers")
166 for i := 0; i < b.N; i++ {
167 if Compare(b1, b2) != -1 {
168 b.Fatal("b1 < b2 failed")
173 func BenchmarkCompareBytesDifferentLength(b *testing.B) {
174 b1 := []byte("Hello Gophers!")
175 b2 := []byte("Hello, Gophers!")
176 for i := 0; i < b.N; i++ {
177 if Compare(b1, b2) != -1 {
178 b.Fatal("b1 < b2 failed")
183 func BenchmarkCompareBytesBigUnaligned(b *testing.B) {
185 b1 := make([]byte, 0, 1<<20)
186 for len(b1) < 1<<20 {
187 b1 = append(b1, "Hello Gophers!"...)
189 b2 := append([]byte("hello"), b1...)
191 for i := 0; i < b.N; i++ {
192 if Compare(b1, b2[len("hello"):]) != 0 {
196 b.SetBytes(int64(len(b1)))
199 func BenchmarkCompareBytesBig(b *testing.B) {
201 b1 := make([]byte, 0, 1<<20)
202 for len(b1) < 1<<20 {
203 b1 = append(b1, "Hello Gophers!"...)
205 b2 := append([]byte{}, b1...)
207 for i := 0; i < b.N; i++ {
208 if Compare(b1, b2) != 0 {
212 b.SetBytes(int64(len(b1)))
215 func BenchmarkCompareBytesBigIdentical(b *testing.B) {
217 b1 := make([]byte, 0, 1<<20)
218 for len(b1) < 1<<20 {
219 b1 = append(b1, "Hello Gophers!"...)
223 for i := 0; i < b.N; i++ {
224 if Compare(b1, b2) != 0 {
228 b.SetBytes(int64(len(b1)))