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>
diff --git a/src/bytes/compare_test.go b/src/bytes/compare_test.go
index a321f2e..a595d57 100644
--- a/src/bytes/compare_test.go
+++ b/src/bytes/compare_test.go
@@ -120,6 +120,39 @@
}
}
+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!")