Skip to content

Commit

Permalink
bytes, strings: fix comparison of long byte slices on s390x
Browse files Browse the repository at this point in the history
The existing implementation of bytes.Compare on s390x doesn't work properly for slices longer
than 256 elements. This change fixes that. Added tests for long strings and slices of bytes.

Fixes #26114

Change-Id: If6d8b68ee6dbcf99a24f867a1d3438b1f208954f
Reviewed-on: https://go-review.googlesource.com/121495
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
  • Loading branch information
wgo authored and bradfitz committed Jun 29, 2018
1 parent d4d8237 commit a5f8128
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/bytes/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package bytes_test

import (
. "bytes"
"internal/testenv"
"testing"
)

Expand Down Expand Up @@ -58,10 +59,20 @@ func TestCompareIdenticalSlice(t *testing.T) {
}

func TestCompareBytes(t *testing.T) {
n := 128
lengths := make([]int, 0) // lengths to test in ascending order
for i := 0; i <= 128; i++ {
lengths = append(lengths, i)
}
lengths = append(lengths, 256, 512, 1024, 1333, 4095, 4096, 4097)

if !testing.Short() || testenv.Builder() != "" {
lengths = append(lengths, 65535, 65536, 65537, 99999)
}

n := lengths[len(lengths)-1]
a := make([]byte, n+1)
b := make([]byte, n+1)
for len := 0; len < 128; len++ {
for _, len := range lengths {
// randomish but deterministic data. No 0 or 255.
for i := 0; i < len; i++ {
a[i] = byte(1 + 31*i%254)
Expand Down
2 changes: 2 additions & 0 deletions src/internal/bytealg/compare_s390x.s
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ loop:
BGT gt
BLT lt
SUB $256, R8
MOVD $256(R3), R3
MOVD $256(R5), R5
CMP R8, $256
BGT loop
tail:
Expand Down
15 changes: 13 additions & 2 deletions src/strings/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package strings_test
// Benchmarks omitted since the underlying implementation is identical.

import (
"internal/testenv"
. "strings"
"testing"
)
Expand Down Expand Up @@ -52,10 +53,20 @@ func TestCompareIdenticalString(t *testing.T) {
}

func TestCompareStrings(t *testing.T) {
n := 128
lengths := make([]int, 0) // lengths to test in ascending order
for i := 0; i <= 128; i++ {
lengths = append(lengths, i)
}
lengths = append(lengths, 256, 512, 1024, 1333, 4095, 4096, 4097)

if !testing.Short() || testenv.Builder() != "" {
lengths = append(lengths, 65535, 65536, 65537, 99999)
}

n := lengths[len(lengths)-1]
a := make([]byte, n+1)
b := make([]byte, n+1)
for len := 0; len < 128; len++ {
for _, len := range lengths {
// randomish but deterministic data. No 0 or 255.
for i := 0; i < len; i++ {
a[i] = byte(1 + 31*i%254)
Expand Down

0 comments on commit a5f8128

Please sign in to comment.