diff options
author | Meng Zhuo <mengzhuo1203@gmail.com> | 2019-10-07 11:30:01 +0800 |
---|---|---|
committer | Cherry Zhang <cherryyz@google.com> | 2019-10-07 14:49:57 +0000 |
commit | fc2915fabdda25912058b4e51b385e73e8ed2b4b (patch) | |
tree | 80de99185907f82b1c6ee8934d0f5e5dbf193aaf /src/bytes | |
parent | 8b391060004dfc03c93a76faab4a0d208a60cc1b (diff) | |
download | go-fc2915fabdda25912058b4e51b385e73e8ed2b4b.tar.gz go-fc2915fabdda25912058b4e51b385e73e8ed2b4b.zip |
bytes: add endian base compare test
The current bytes test suit didn't come with endian based test
which causing #34549 can passed the try-bot.
This test will failed when little endian architecture simply using
load and compare uint.
Update #34549
Change-Id: I0973c2cd505ce21c2bed1deeb7d526f1e872118d
Reviewed-on: https://go-review.googlesource.com/c/go/+/198358
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/bytes')
-rw-r--r-- | src/bytes/compare_test.go | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/bytes/compare_test.go b/src/bytes/compare_test.go index a321f2e086..a595d575d0 100644 --- a/src/bytes/compare_test.go +++ b/src/bytes/compare_test.go @@ -120,6 +120,39 @@ func TestCompareBytes(t *testing.T) { } } +func TestEndianBaseCompare(t *testing.T) { + // This test compares byte slices that are almost identical, except one + // difference that for some j, a[j]>b[j] and a[j+1]<b[j+1]. If the implementation + // compares large chunks with wrong endianness, it gets wrong result. + // no vector register is larger than 512 bytes for now + const maxLength = 512 + a := make([]byte, maxLength) + b := make([]byte, maxLength) + // randomish but deterministic data. No 0 or 255. + for i := 0; i < maxLength; i++ { + a[i] = byte(1 + 31*i%254) + b[i] = byte(1 + 31*i%254) + } + for i := 2; i <= maxLength; i <<= 1 { + for j := 0; j < i-1; j++ { + a[j] = b[j] - 1 + a[j+1] = b[j+1] + 1 + cmp := Compare(a[:i], b[:i]) + if cmp != -1 { + t.Errorf(`CompareBbigger(%d,%d) = %d`, i, j, cmp) + } + a[j] = b[j] + 1 + a[j+1] = b[j+1] - 1 + cmp = Compare(a[:i], b[:i]) + if cmp != 1 { + t.Errorf(`CompareAbigger(%d,%d) = %d`, i, j, cmp) + } + a[j] = b[j] + a[j+1] = b[j+1] + } + } +} + func BenchmarkCompareBytesEqual(b *testing.B) { b1 := []byte("Hello Gophers!") b2 := []byte("Hello Gophers!") |