From 9becf401de6080cb9edd524def7e77aeaede4126 Mon Sep 17 00:00:00 2001 From: aimuz Date: Fri, 9 Aug 2024 07:33:36 +0000 Subject: bytes, strings: add Lines, SplitSeq, SplitAfterSeq, FieldsSeq, FieldsFuncSeq Fixes #61901. Change-Id: I4db21c91fd21079f2aa3bc81fb03dd6f40423a38 GitHub-Last-Rev: ed3df560a40ea10cdcb8ad476ba6849463f3c761 GitHub-Pull-Request: golang/go#67543 Reviewed-on: https://go-review.googlesource.com/c/go/+/587095 Auto-Submit: Ian Lance Taylor LUCI-TryBot-Result: Go LUCI Reviewed-by: Carlos Amedee Reviewed-by: Ian Lance Taylor --- src/bytes/bytes_test.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'src/bytes/bytes_test.go') diff --git a/src/bytes/bytes_test.go b/src/bytes/bytes_test.go index 6fb6140c18..637880a4f7 100644 --- a/src/bytes/bytes_test.go +++ b/src/bytes/bytes_test.go @@ -8,6 +8,7 @@ import ( . "bytes" "fmt" "internal/testenv" + "iter" "math" "math/rand" "slices" @@ -26,6 +27,37 @@ func sliceOfString(s [][]byte) []string { return result } +func collect(t *testing.T, seq iter.Seq[[]byte]) [][]byte { + out := slices.Collect(seq) + out1 := slices.Collect(seq) + if !slices.Equal(sliceOfString(out), sliceOfString(out1)) { + t.Fatalf("inconsistent seq:\n%s\n%s", out, out1) + } + return out +} + +type LinesTest struct { + a string + b []string +} + +var linesTests = []LinesTest{ + {a: "abc\nabc\n", b: []string{"abc\n", "abc\n"}}, + {a: "abc\r\nabc", b: []string{"abc\r\n", "abc"}}, + {a: "abc\r\n", b: []string{"abc\r\n"}}, + {a: "\nabc", b: []string{"\n", "abc"}}, + {a: "\nabc\n\n", b: []string{"\n", "abc\n", "\n"}}, +} + +func TestLines(t *testing.T) { + for _, s := range linesTests { + result := sliceOfString(slices.Collect(Lines([]byte(s.a)))) + if !slices.Equal(result, s.b) { + t.Errorf(`slices.Collect(Lines(%q)) = %q; want %q`, s.a, result, s.b) + } + } +} + // For ease of reading, the test cases use strings that are converted to byte // slices before invoking the functions. @@ -800,6 +832,14 @@ func TestSplit(t *testing.T) { t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, result, tt.a) continue } + + if tt.n < 0 { + b := sliceOfString(slices.Collect(SplitSeq([]byte(tt.s), []byte(tt.sep)))) + if !slices.Equal(b, tt.a) { + t.Errorf(`collect(SplitSeq(%q, %q)) = %v; want %v`, tt.s, tt.sep, b, tt.a) + } + } + if tt.n == 0 || len(a) == 0 { continue } @@ -859,6 +899,13 @@ func TestSplitAfter(t *testing.T) { continue } + if tt.n < 0 { + b := sliceOfString(slices.Collect(SplitAfterSeq([]byte(tt.s), []byte(tt.sep)))) + if !slices.Equal(b, tt.a) { + t.Errorf(`collect(SplitAfterSeq(%q, %q)) = %v; want %v`, tt.s, tt.sep, b, tt.a) + } + } + if want := tt.a[len(tt.a)-1] + "z"; string(x) != want { t.Errorf("last appended result was %s; want %s", x, want) } @@ -912,6 +959,11 @@ func TestFields(t *testing.T) { continue } + result2 := sliceOfString(collect(t, FieldsSeq([]byte(tt.s)))) + if !slices.Equal(result2, tt.a) { + t.Errorf(`collect(FieldsSeq(%q)) = %v; want %v`, tt.s, result2, tt.a) + } + if string(b) != tt.s { t.Errorf("slice changed to %s; want %s", string(b), tt.s) } @@ -954,6 +1006,11 @@ func TestFieldsFunc(t *testing.T) { t.Errorf("FieldsFunc(%q) = %v, want %v", tt.s, a, tt.a) } + result2 := sliceOfString(collect(t, FieldsFuncSeq([]byte(tt.s), pred))) + if !slices.Equal(result2, tt.a) { + t.Errorf(`collect(FieldsFuncSeq(%q)) = %v; want %v`, tt.s, result2, tt.a) + } + if string(b) != tt.s { t.Errorf("slice changed to %s; want %s", b, tt.s) } -- cgit v1.3