mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
bytes,strings,unicode/utf16: use slices to clean up tests
Replace reflect.DeepEqual with slices.Equal, which is much faster.
Remove some redundant helper functions.
Change-Id: I51b32a3d0c3fc5ad0d3b6ff0dd03f39c507e5762
GitHub-Last-Rev: e21f46d4a0
GitHub-Pull-Request: golang/go#67609
Reviewed-on: https://go-review.googlesource.com/c/go/+/587937
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Commit-Queue: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
This commit is contained in:
parent
910e6b5fae
commit
c0eac35a4c
4 changed files with 16 additions and 39 deletions
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"math"
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
@ -18,18 +19,6 @@ import (
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
func eq(a, b []string) bool {
|
|
||||||
if len(a) != len(b) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
for i := 0; i < len(a); i++ {
|
|
||||||
if a[i] != b[i] {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func sliceOfString(s [][]byte) []string {
|
func sliceOfString(s [][]byte) []string {
|
||||||
result := make([]string, len(s))
|
result := make([]string, len(s))
|
||||||
for i, v := range s {
|
for i, v := range s {
|
||||||
|
|
@ -808,7 +797,7 @@ func TestSplit(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
result := sliceOfString(a)
|
result := sliceOfString(a)
|
||||||
if !eq(result, tt.a) {
|
if !slices.Equal(result, tt.a) {
|
||||||
t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, result, tt.a)
|
t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, result, tt.a)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
@ -866,7 +855,7 @@ func TestSplitAfter(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
result := sliceOfString(a)
|
result := sliceOfString(a)
|
||||||
if !eq(result, tt.a) {
|
if !slices.Equal(result, tt.a) {
|
||||||
t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, result, tt.a)
|
t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, result, tt.a)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
@ -919,7 +908,7 @@ func TestFields(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
result := sliceOfString(a)
|
result := sliceOfString(a)
|
||||||
if !eq(result, tt.a) {
|
if !slices.Equal(result, tt.a) {
|
||||||
t.Errorf("Fields(%q) = %v; want %v", tt.s, a, tt.a)
|
t.Errorf("Fields(%q) = %v; want %v", tt.s, a, tt.a)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
@ -939,7 +928,7 @@ func TestFieldsFunc(t *testing.T) {
|
||||||
for _, tt := range fieldstests {
|
for _, tt := range fieldstests {
|
||||||
a := FieldsFunc([]byte(tt.s), unicode.IsSpace)
|
a := FieldsFunc([]byte(tt.s), unicode.IsSpace)
|
||||||
result := sliceOfString(a)
|
result := sliceOfString(a)
|
||||||
if !eq(result, tt.a) {
|
if !slices.Equal(result, tt.a) {
|
||||||
t.Errorf("FieldsFunc(%q, unicode.IsSpace) = %v; want %v", tt.s, a, tt.a)
|
t.Errorf("FieldsFunc(%q, unicode.IsSpace) = %v; want %v", tt.s, a, tt.a)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
@ -962,7 +951,7 @@ func TestFieldsFunc(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
result := sliceOfString(a)
|
result := sliceOfString(a)
|
||||||
if !eq(result, tt.a) {
|
if !slices.Equal(result, tt.a) {
|
||||||
t.Errorf("FieldsFunc(%q) = %v, want %v", tt.s, a, tt.a)
|
t.Errorf("FieldsFunc(%q) = %v, want %v", tt.s, a, tt.a)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1286,18 +1275,6 @@ func TestRepeatCatchesOverflow(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func runesEqual(a, b []rune) bool {
|
|
||||||
if len(a) != len(b) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
for i, r := range a {
|
|
||||||
if r != b[i] {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
type RunesTest struct {
|
type RunesTest struct {
|
||||||
in string
|
in string
|
||||||
out []rune
|
out []rune
|
||||||
|
|
@ -1318,7 +1295,7 @@ func TestRunes(t *testing.T) {
|
||||||
for _, tt := range RunesTests {
|
for _, tt := range RunesTests {
|
||||||
tin := []byte(tt.in)
|
tin := []byte(tt.in)
|
||||||
a := Runes(tin)
|
a := Runes(tin)
|
||||||
if !runesEqual(a, tt.out) {
|
if !slices.Equal(a, tt.out) {
|
||||||
t.Errorf("Runes(%q) = %v; want %v", tin, a, tt.out)
|
t.Errorf("Runes(%q) = %v; want %v", tin, a, tt.out)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
package strings_test
|
package strings_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"slices"
|
||||||
. "strings"
|
. "strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
@ -83,7 +83,7 @@ func TestFinderCreation(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !reflect.DeepEqual(good, tc.suf) {
|
if !slices.Equal(good, tc.suf) {
|
||||||
t.Errorf("boyerMoore(%q) got %v want %v", tc.pattern, good, tc.suf)
|
t.Errorf("boyerMoore(%q) got %v want %v", tc.pattern, good, tc.suf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"reflect"
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
. "strings"
|
. "strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -431,7 +431,7 @@ func TestSplit(t *testing.T) {
|
||||||
}
|
}
|
||||||
if tt.n < 0 {
|
if tt.n < 0 {
|
||||||
b := Split(tt.s, tt.sep)
|
b := Split(tt.s, tt.sep)
|
||||||
if !reflect.DeepEqual(a, b) {
|
if !slices.Equal(a, b) {
|
||||||
t.Errorf("Split disagrees with SplitN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
|
t.Errorf("Split disagrees with SplitN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -467,7 +467,7 @@ func TestSplitAfter(t *testing.T) {
|
||||||
}
|
}
|
||||||
if tt.n < 0 {
|
if tt.n < 0 {
|
||||||
b := SplitAfter(tt.s, tt.sep)
|
b := SplitAfter(tt.s, tt.sep)
|
||||||
if !reflect.DeepEqual(a, b) {
|
if !slices.Equal(a, b) {
|
||||||
t.Errorf("SplitAfter disagrees with SplitAfterN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
|
t.Errorf("SplitAfter disagrees with SplitAfterN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ package utf16_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"internal/testenv"
|
"internal/testenv"
|
||||||
"reflect"
|
"slices"
|
||||||
"testing"
|
"testing"
|
||||||
"unicode"
|
"unicode"
|
||||||
. "unicode/utf16"
|
. "unicode/utf16"
|
||||||
|
|
@ -58,7 +58,7 @@ var encodeTests = []encodeTest{
|
||||||
func TestEncode(t *testing.T) {
|
func TestEncode(t *testing.T) {
|
||||||
for _, tt := range encodeTests {
|
for _, tt := range encodeTests {
|
||||||
out := Encode(tt.in)
|
out := Encode(tt.in)
|
||||||
if !reflect.DeepEqual(out, tt.out) {
|
if !slices.Equal(out, tt.out) {
|
||||||
t.Errorf("Encode(%x) = %x; want %x", tt.in, out, tt.out)
|
t.Errorf("Encode(%x) = %x; want %x", tt.in, out, tt.out)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -70,7 +70,7 @@ func TestAppendRune(t *testing.T) {
|
||||||
for _, u := range tt.in {
|
for _, u := range tt.in {
|
||||||
out = AppendRune(out, u)
|
out = AppendRune(out, u)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(out, tt.out) {
|
if !slices.Equal(out, tt.out) {
|
||||||
t.Errorf("AppendRune(%x) = %x; want %x", tt.in, out, tt.out)
|
t.Errorf("AppendRune(%x) = %x; want %x", tt.in, out, tt.out)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -143,7 +143,7 @@ func TestAllocationsDecode(t *testing.T) {
|
||||||
func TestDecode(t *testing.T) {
|
func TestDecode(t *testing.T) {
|
||||||
for _, tt := range decodeTests {
|
for _, tt := range decodeTests {
|
||||||
out := Decode(tt.in)
|
out := Decode(tt.in)
|
||||||
if !reflect.DeepEqual(out, tt.out) {
|
if !slices.Equal(out, tt.out) {
|
||||||
t.Errorf("Decode(%x) = %x; want %x", tt.in, out, tt.out)
|
t.Errorf("Decode(%x) = %x; want %x", tt.in, out, tt.out)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue