diff options
Diffstat (limited to 'src/strings/strings_test.go')
| -rw-r--r-- | src/strings/strings_test.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/strings/strings_test.go b/src/strings/strings_test.go index cf7fde5bbd..738185e5dd 100644 --- a/src/strings/strings_test.go +++ b/src/strings/strings_test.go @@ -6,6 +6,7 @@ package strings_test import ( "bytes" + "fmt" "io" "math/rand" "reflect" @@ -892,6 +893,54 @@ func TestRepeat(t *testing.T) { } } +func repeat(s string, count int) (err error) { + defer func() { + if r := recover(); r != nil { + switch v := r.(type) { + case error: + err = v + default: + err = fmt.Errorf("%s", v) + } + } + }() + + Repeat(s, count) + + return +} + +// See Issue golang.org/issue/16237 +func TestRepeatCatchesOverflow(t *testing.T) { + tests := [...]struct { + s string + count int + errStr string + }{ + 0: {"--", -2147483647, "negative"}, + 1: {"", int(^uint(0) >> 1), ""}, + 2: {"-", 10, ""}, + 3: {"gopher", 0, ""}, + 4: {"-", -1, "negative"}, + 5: {"--", -102, "negative"}, + 6: {string(make([]byte, 255)), int((^uint(0))/255 + 1), "overflow"}, + } + + for i, tt := range tests { + err := repeat(tt.s, tt.count) + if tt.errStr == "" { + if err != nil { + t.Errorf("#%d panicked %v", i, err) + } + continue + } + + if err == nil || !Contains(err.Error(), tt.errStr) { + t.Errorf("#%d expected %q got %q", i, tt.errStr, err) + } + } +} + func runesEqual(a, b []rune) bool { if len(a) != len(b) { return false |
