github.com/remobjects/goldbaselibrary@v0.0.0-20230924164425-d458680a936b/Source/Gold/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 "internal/testenv" 10 "testing" 11 ) 12 13 var compareTests = []struct { 14 a, b []byte 15 i int 16 }{ 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}, 34 // nil tests 35 {nil, nil, 0}, 36 {[]byte(""), nil, 0}, 37 {nil, []byte(""), 0}, 38 {[]byte("a"), nil, 1}, 39 {nil, []byte("a"), -1}, 40 } 41 42 func TestCompare(t *testing.T) { 43 for _, tt := range compareTests { 44 numShifts := 16 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] 49 copy(shiftedB, tt.b) 50 cmp := Compare(tt.a, shiftedB) 51 if cmp != tt.i { 52 t.Errorf(`Compare(%q, %q), offset %d = %v; want %v`, tt.a, tt.b, offset, cmp, tt.i) 53 } 54 } 55 } 56 } 57 58 func TestCompareIdenticalSlice(t *testing.T) { 59 var b = []byte("Hello Gophers!") 60 if Compare(b, b) != 0 { 61 t.Error("b != b") 62 } 63 if Compare(b, b[:1]) != 1 { 64 t.Error("b > b[:1] failed") 65 } 66 } 67 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) 72 } 73 lengths = append(lengths, 256, 512, 1024, 1333, 4095, 4096, 4097) 74 75 if !testing.Short() || testenv.Builder() != "" { 76 lengths = append(lengths, 65535, 65536, 65537, 99999) 77 } 78 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) 87 } 88 // data past the end is different 89 for i := len; i <= n; i++ { 90 a[i] = 8 91 b[i] = 9 92 } 93 cmp := Compare(a[:len], b[:len]) 94 if cmp != 0 { 95 t.Errorf(`CompareIdentical(%d) = %d`, len, cmp) 96 } 97 if len > 0 { 98 cmp = Compare(a[:len-1], b[:len]) 99 if cmp != -1 { 100 t.Errorf(`CompareAshorter(%d) = %d`, len, cmp) 101 } 102 cmp = Compare(a[:len], b[:len-1]) 103 if cmp != 1 { 104 t.Errorf(`CompareBshorter(%d) = %d`, len, cmp) 105 } 106 } 107 for k := 0; k < len; k++ { 108 b[k] = a[k] - 1 109 cmp = Compare(a[:len], b[:len]) 110 if cmp != 1 { 111 t.Errorf(`CompareAbigger(%d,%d) = %d`, len, k, cmp) 112 } 113 b[k] = a[k] + 1 114 cmp = Compare(a[:len], b[:len]) 115 if cmp != -1 { 116 t.Errorf(`CompareBbigger(%d,%d) = %d`, len, k, cmp) 117 } 118 b[k] = a[k] 119 } 120 } 121 } 122 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 { 128 b.Fatal("b1 != b2") 129 } 130 } 131 } 132 133 func BenchmarkCompareBytesToNil(b *testing.B) { 134 b1 := []byte("Hello Gophers!") 135 var b2 []byte 136 for i := 0; i < b.N; i++ { 137 if Compare(b1, b2) != 1 { 138 b.Fatal("b1 > b2 failed") 139 } 140 } 141 } 142 143 func BenchmarkCompareBytesEmpty(b *testing.B) { 144 b1 := []byte("") 145 b2 := b1 146 for i := 0; i < b.N; i++ { 147 if Compare(b1, b2) != 0 { 148 b.Fatal("b1 != b2") 149 } 150 } 151 } 152 153 func BenchmarkCompareBytesIdentical(b *testing.B) { 154 b1 := []byte("Hello Gophers!") 155 b2 := b1 156 for i := 0; i < b.N; i++ { 157 if Compare(b1, b2) != 0 { 158 b.Fatal("b1 != b2") 159 } 160 } 161 } 162 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") 169 } 170 } 171 } 172 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") 179 } 180 } 181 } 182 183 func BenchmarkCompareBytesBigUnaligned(b *testing.B) { 184 b.StopTimer() 185 b1 := make([]byte, 0, 1<<20) 186 for len(b1) < 1<<20 { 187 b1 = append(b1, "Hello Gophers!"...) 188 } 189 b2 := append([]byte("hello"), b1...) 190 b.StartTimer() 191 for i := 0; i < b.N; i++ { 192 if Compare(b1, b2[len("hello"):]) != 0 { 193 b.Fatal("b1 != b2") 194 } 195 } 196 b.SetBytes(int64(len(b1))) 197 } 198 199 func BenchmarkCompareBytesBig(b *testing.B) { 200 b.StopTimer() 201 b1 := make([]byte, 0, 1<<20) 202 for len(b1) < 1<<20 { 203 b1 = append(b1, "Hello Gophers!"...) 204 } 205 b2 := append([]byte{}, b1...) 206 b.StartTimer() 207 for i := 0; i < b.N; i++ { 208 if Compare(b1, b2) != 0 { 209 b.Fatal("b1 != b2") 210 } 211 } 212 b.SetBytes(int64(len(b1))) 213 } 214 215 func BenchmarkCompareBytesBigIdentical(b *testing.B) { 216 b.StopTimer() 217 b1 := make([]byte, 0, 1<<20) 218 for len(b1) < 1<<20 { 219 b1 = append(b1, "Hello Gophers!"...) 220 } 221 b2 := b1 222 b.StartTimer() 223 for i := 0; i < b.N; i++ { 224 if Compare(b1, b2) != 0 { 225 b.Fatal("b1 != b2") 226 } 227 } 228 b.SetBytes(int64(len(b1))) 229 }