2014-04-11 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / libgo / go / bytes / compare_test.go
blob0a36f5ad39be6a1da630e394b65c2e3c6ee08bf7
1 package bytes_test
3 import (
4 . "bytes"
5 "testing"
8 var compareTests = []struct {
9 a, b []byte
10 i int
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},
26 // nil tests
27 {nil, nil, 0},
28 {[]byte(""), nil, 0},
29 {nil, []byte(""), 0},
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)
37 if cmp != tt.i {
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 {
46 t.Error("b != b")
48 if Compare(b, b[:1]) != 1 {
49 t.Error("b > b[:1] failed")
53 func TestCompareBytes(t *testing.T) {
54 n := 128
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++ {
65 a[i] = 8
66 b[i] = 9
68 cmp := Compare(a[:len], b[:len])
69 if cmp != 0 {
70 t.Errorf(`CompareIdentical(%d) = %d`, len, cmp)
72 if len > 0 {
73 cmp = Compare(a[:len-1], b[:len])
74 if cmp != -1 {
75 t.Errorf(`CompareAshorter(%d) = %d`, len, cmp)
77 cmp = Compare(a[:len], b[:len-1])
78 if cmp != 1 {
79 t.Errorf(`CompareBshorter(%d) = %d`, len, cmp)
82 for k := 0; k < len; k++ {
83 b[k] = a[k] - 1
84 cmp = Compare(a[:len], b[:len])
85 if cmp != 1 {
86 t.Errorf(`CompareAbigger(%d,%d) = %d`, len, k, cmp)
88 b[k] = a[k] + 1
89 cmp = Compare(a[:len], b[:len])
90 if cmp != -1 {
91 t.Errorf(`CompareBbigger(%d,%d) = %d`, len, k, cmp)
93 b[k] = a[k]
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 {
103 b.Fatal("b1 != b2")
108 func BenchmarkCompareBytesToNil(b *testing.B) {
109 b1 := []byte("Hello Gophers!")
110 var b2 []byte
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) {
119 b1 := []byte("")
120 b2 := b1
121 for i := 0; i < b.N; i++ {
122 if Compare(b1, b2) != 0 {
123 b.Fatal("b1 != b2")
128 func BenchmarkCompareBytesIdentical(b *testing.B) {
129 b1 := []byte("Hello Gophers!")
130 b2 := b1
131 for i := 0; i < b.N; i++ {
132 if Compare(b1, b2) != 0 {
133 b.Fatal("b1 != b2")
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) {
159 b.StopTimer()
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...)
165 b.StartTimer()
166 for i := 0; i < b.N; i++ {
167 if Compare(b1, b2[len("hello"):]) != 0 {
168 b.Fatal("b1 != b2")
171 b.SetBytes(int64(len(b1)))
174 func BenchmarkCompareBytesBig(b *testing.B) {
175 b.StopTimer()
176 b1 := make([]byte, 0, 1<<20)
177 for len(b1) < 1<<20 {
178 b1 = append(b1, "Hello Gophers!"...)
180 b2 := append([]byte{}, b1...)
181 b.StartTimer()
182 for i := 0; i < b.N; i++ {
183 if Compare(b1, b2) != 0 {
184 b.Fatal("b1 != b2")
187 b.SetBytes(int64(len(b1)))
190 func BenchmarkCompareBytesBigIdentical(b *testing.B) {
191 b.StopTimer()
192 b1 := make([]byte, 0, 1<<20)
193 for len(b1) < 1<<20 {
194 b1 = append(b1, "Hello Gophers!"...)
196 b2 := b1
197 b.StartTimer()
198 for i := 0; i < b.N; i++ {
199 if Compare(b1, b2) != 0 {
200 b.Fatal("b1 != b2")
203 b.SetBytes(int64(len(b1)))