]> Cypherpunks repositories - gostls13.git/commitdiff
bytes: add endian base compare test
authorMeng Zhuo <mengzhuo1203@gmail.com>
Mon, 7 Oct 2019 03:30:01 +0000 (11:30 +0800)
committerCherry Zhang <cherryyz@google.com>
Mon, 7 Oct 2019 14:49:57 +0000 (14:49 +0000)
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>

src/bytes/compare_test.go

index a321f2e08618439c2e62413dfed0542bc33cc2e5..a595d575d07251f8ece63d82123c72fdf31a2462 100644 (file)
@@ -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!")