libgo: Merge from revision 18783:00cce3a34d7e of master library.
[official-gcc.git] / libgo / go / bytes / compare_test.go
blob63522374ee0403f81210f9a6381578e974551527
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.
5 package bytes_test
7 import (
8 . "bytes"
9 "testing"
12 var compareTests = []struct {
13 a, b []byte
14 i int
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},
30 // nil tests
31 {nil, nil, 0},
32 {[]byte(""), nil, 0},
33 {nil, []byte(""), 0},
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)
41 if cmp != tt.i {
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 {
50 t.Error("b != b")
52 if Compare(b, b[:1]) != 1 {
53 t.Error("b > b[:1] failed")
57 func TestCompareBytes(t *testing.T) {
58 n := 128
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++ {
69 a[i] = 8
70 b[i] = 9
72 cmp := Compare(a[:len], b[:len])
73 if cmp != 0 {
74 t.Errorf(`CompareIdentical(%d) = %d`, len, cmp)
76 if len > 0 {
77 cmp = Compare(a[:len-1], b[:len])
78 if cmp != -1 {
79 t.Errorf(`CompareAshorter(%d) = %d`, len, cmp)
81 cmp = Compare(a[:len], b[:len-1])
82 if cmp != 1 {
83 t.Errorf(`CompareBshorter(%d) = %d`, len, cmp)
86 for k := 0; k < len; k++ {
87 b[k] = a[k] - 1
88 cmp = Compare(a[:len], b[:len])
89 if cmp != 1 {
90 t.Errorf(`CompareAbigger(%d,%d) = %d`, len, k, cmp)
92 b[k] = a[k] + 1
93 cmp = Compare(a[:len], b[:len])
94 if cmp != -1 {
95 t.Errorf(`CompareBbigger(%d,%d) = %d`, len, k, cmp)
97 b[k] = a[k]
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 {
107 b.Fatal("b1 != b2")
112 func BenchmarkCompareBytesToNil(b *testing.B) {
113 b1 := []byte("Hello Gophers!")
114 var b2 []byte
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) {
123 b1 := []byte("")
124 b2 := b1
125 for i := 0; i < b.N; i++ {
126 if Compare(b1, b2) != 0 {
127 b.Fatal("b1 != b2")
132 func BenchmarkCompareBytesIdentical(b *testing.B) {
133 b1 := []byte("Hello Gophers!")
134 b2 := b1
135 for i := 0; i < b.N; i++ {
136 if Compare(b1, b2) != 0 {
137 b.Fatal("b1 != b2")
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) {
163 b.StopTimer()
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...)
169 b.StartTimer()
170 for i := 0; i < b.N; i++ {
171 if Compare(b1, b2[len("hello"):]) != 0 {
172 b.Fatal("b1 != b2")
175 b.SetBytes(int64(len(b1)))
178 func BenchmarkCompareBytesBig(b *testing.B) {
179 b.StopTimer()
180 b1 := make([]byte, 0, 1<<20)
181 for len(b1) < 1<<20 {
182 b1 = append(b1, "Hello Gophers!"...)
184 b2 := append([]byte{}, b1...)
185 b.StartTimer()
186 for i := 0; i < b.N; i++ {
187 if Compare(b1, b2) != 0 {
188 b.Fatal("b1 != b2")
191 b.SetBytes(int64(len(b1)))
194 func BenchmarkCompareBytesBigIdentical(b *testing.B) {
195 b.StopTimer()
196 b1 := make([]byte, 0, 1<<20)
197 for len(b1) < 1<<20 {
198 b1 = append(b1, "Hello Gophers!"...)
200 b2 := b1
201 b.StartTimer()
202 for i := 0; i < b.N; i++ {
203 if Compare(b1, b2) != 0 {
204 b.Fatal("b1 != b2")
207 b.SetBytes(int64(len(b1)))