mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
An asked-for-in #go-nuts extension to quickly create a repeated
copy of a string or a byte array.
strings.Repeat("-", 50)
bytes.Repeat(b, 99)
R=rsc
https://golang.org/cl/155063
This commit is contained in:
parent
11c1aa9f6d
commit
37f71e8ad6
4 changed files with 79 additions and 0 deletions
|
|
@ -361,3 +361,30 @@ func TestAddByte(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
type RepeatTest struct {
|
||||
in, out string;
|
||||
count int;
|
||||
}
|
||||
|
||||
var RepeatTests = []RepeatTest{
|
||||
RepeatTest{"", "", 0},
|
||||
RepeatTest{"", "", 1},
|
||||
RepeatTest{"", "", 2},
|
||||
RepeatTest{"-", "", 0},
|
||||
RepeatTest{"-", "-", 1},
|
||||
RepeatTest{"-", "----------", 10},
|
||||
RepeatTest{"abc ", "abc abc abc ", 3},
|
||||
}
|
||||
|
||||
func TestRepeat(t *testing.T) {
|
||||
for _, tt := range RepeatTests {
|
||||
tin := strings.Bytes(tt.in);
|
||||
tout := strings.Bytes(tt.out);
|
||||
a := Repeat(tin, tt.count);
|
||||
if !Equal(a, tout) {
|
||||
t.Errorf("Repeat(%q, %d) = %q; want %q", tin, tt.count, a, tout);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue