From 677e080dfed1f17de76d4914cf4935b4914e9ddb Mon Sep 17 00:00:00 2001 From: aimuz Date: Mon, 5 Aug 2024 03:00:42 +0000 Subject: [PATCH] bytes, strings: replace reflect.DeepEqual and custom eq with slices.Equal in tests Change-Id: I016672af79d49a00ddc2d0449cdaac61e98b4ba0 GitHub-Last-Rev: 38d15d9a03e5bd29e4b25f1d767e40cf7165a7a6 GitHub-Pull-Request: golang/go#68730 Reviewed-on: https://go-review.googlesource.com/c/go/+/602698 LUCI-TryBot-Result: Go LUCI Auto-Submit: Ian Lance Taylor Reviewed-by: Robert Griesemer Auto-Submit: Robert Griesemer Commit-Queue: Ian Lance Taylor Reviewed-by: Ian Lance Taylor --- src/bytes/bytes_test.go | 9 ++++----- src/strings/strings_test.go | 22 +++++----------------- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/src/bytes/bytes_test.go b/src/bytes/bytes_test.go index 94301358e03..6fb6140c188 100644 --- a/src/bytes/bytes_test.go +++ b/src/bytes/bytes_test.go @@ -10,7 +10,6 @@ import ( "internal/testenv" "math" "math/rand" - "reflect" "slices" "strings" "testing" @@ -814,8 +813,8 @@ func TestSplit(t *testing.T) { t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.sep, tt.n, tt.sep, s) } if tt.n < 0 { - b := Split([]byte(tt.s), []byte(tt.sep)) - if !reflect.DeepEqual(a, b) { + b := sliceOfString(Split([]byte(tt.s), []byte(tt.sep))) + if !slices.Equal(result, b) { t.Errorf("Split disagrees withSplitN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a) } } @@ -869,8 +868,8 @@ func TestSplitAfter(t *testing.T) { t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.sep, tt.n, tt.sep, s) } if tt.n < 0 { - b := SplitAfter([]byte(tt.s), []byte(tt.sep)) - if !reflect.DeepEqual(a, b) { + b := sliceOfString(SplitAfter([]byte(tt.s), []byte(tt.sep))) + if !slices.Equal(result, b) { t.Errorf("SplitAfter disagrees withSplitAfterN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a) } } diff --git a/src/strings/strings_test.go b/src/strings/strings_test.go index 4c8c25ee132..c9183722804 100644 --- a/src/strings/strings_test.go +++ b/src/strings/strings_test.go @@ -19,18 +19,6 @@ import ( "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 -} - var abcd = "abcd" var faces = "☺☻☹" var commas = "1,2,3,4" @@ -418,7 +406,7 @@ var splittests = []SplitTest{ func TestSplit(t *testing.T) { for _, tt := range splittests { a := SplitN(tt.s, tt.sep, tt.n) - if !eq(a, tt.a) { + if !slices.Equal(a, tt.a) { t.Errorf("Split(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, a, tt.a) continue } @@ -457,7 +445,7 @@ var splitaftertests = []SplitTest{ func TestSplitAfter(t *testing.T) { for _, tt := range splitaftertests { a := SplitAfterN(tt.s, tt.sep, tt.n) - if !eq(a, tt.a) { + if !slices.Equal(a, tt.a) { t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, a, tt.a) continue } @@ -500,7 +488,7 @@ var fieldstests = []FieldsTest{ func TestFields(t *testing.T) { for _, tt := range fieldstests { a := Fields(tt.s) - if !eq(a, tt.a) { + if !slices.Equal(a, tt.a) { t.Errorf("Fields(%q) = %v; want %v", tt.s, a, tt.a) continue } @@ -517,7 +505,7 @@ var FieldsFuncTests = []FieldsTest{ func TestFieldsFunc(t *testing.T) { for _, tt := range fieldstests { a := FieldsFunc(tt.s, unicode.IsSpace) - if !eq(a, tt.a) { + if !slices.Equal(a, tt.a) { t.Errorf("FieldsFunc(%q, unicode.IsSpace) = %v; want %v", tt.s, a, tt.a) continue } @@ -525,7 +513,7 @@ func TestFieldsFunc(t *testing.T) { pred := func(c rune) bool { return c == 'X' } for _, tt := range FieldsFuncTests { a := FieldsFunc(tt.s, pred) - if !eq(a, tt.a) { + if !slices.Equal(a, tt.a) { t.Errorf("FieldsFunc(%q) = %v, want %v", tt.s, a, tt.a) } }