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:
David G. Andersen 2009-11-16 12:40:01 -08:00 committed by Russ Cox
parent 11c1aa9f6d
commit 37f71e8ad6
4 changed files with 79 additions and 0 deletions

View file

@ -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;
}
}
}