github.com/shijuvar/go@v0.0.0-20141209052335-e8f13700b70c/src/bytes/compare_test.go (about) 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. 4 5 package bytes_test 6 7 import ( 8 . "bytes" 9 "testing" 10 ) 11 12 var compareTests = []struct { 13 a, b []byte 14 i int 15 }{ 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}, 36 } 37 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) 43 } 44 } 45 } 46 47 func TestCompareIdenticalSlice(t *testing.T) { 48 var b = []byte("Hello Gophers!") 49 if Compare(b, b) != 0 { 50 t.Error("b != b") 51 } 52 if Compare(b, b[:1]) != 1 { 53 t.Error("b > b[:1] failed") 54 } 55 } 56 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) 66 } 67 // data past the end is different 68 for i := len; i <= n; i++ { 69 a[i] = 8 70 b[i] = 9 71 } 72 cmp := Compare(a[:len], b[:len]) 73 if cmp != 0 { 74 t.Errorf(`CompareIdentical(%d) = %d`, len, cmp) 75 } 76 if len > 0 { 77 cmp = Compare(a[:len-1], b[:len]) 78 if cmp != -1 { 79 t.Errorf(`CompareAshorter(%d) = %d`, len, cmp) 80 } 81 cmp = Compare(a[:len], b[:len-1]) 82 if cmp != 1 { 83 t.Errorf(`CompareBshorter(%d) = %d`, len, cmp) 84 } 85 } 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) 91 } 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) 96 } 97 b[k] = a[k] 98 } 99 } 100 } 101 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") 108 } 109 } 110 } 111 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") 118 } 119 } 120 } 121 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") 128 } 129 } 130 } 131 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") 138 } 139 } 140 } 141 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") 148 } 149 } 150 } 151 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") 158 } 159 } 160 } 161 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!"...) 167 } 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") 173 } 174 } 175 b.SetBytes(int64(len(b1))) 176 } 177 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!"...) 183 } 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") 189 } 190 } 191 b.SetBytes(int64(len(b1))) 192 } 193 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!"...) 199 } 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") 205 } 206 } 207 b.SetBytes(int64(len(b1))) 208 }