| 1 |
package bytealg |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
var CountTests = []struct { |
| 9 |
s string |
| 10 |
sep byte |
| 11 |
num int |
| 12 |
}{ |
| 13 |
{"12345678987654321", '6', 2}, |
| 14 |
{"611161116", '6', 3}, |
| 15 |
{"11111", '1', 5}, |
| 16 |
{"aBaB", 'a', 2}, |
| 17 |
{"ABAB", 'a', 2}, |
| 18 |
{strings.Repeat("AB", 256), 'a', 256}, |
| 19 |
{strings.Repeat("ab", 256), 'A', 256}, |
| 20 |
} |
| 21 |
|
| 22 |
func TestCount(t *testing.T) { |
| 23 |
for _, tt := range CountTests { |
| 24 |
if num := CountString(tt.s, tt.sep); num != tt.num { |
| 25 |
t.Errorf("Count(%q, %q) = %d, want %d", tt.s, tt.sep, num, tt.num) |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
func TestCountHard(t *testing.T) { |
| 31 |
s := strings.Repeat("AB", 32*1024) |
| 32 |
lower := strings.Repeat("ab", 32*1024) |
| 33 |
n := 0 |
| 34 |
for i := 0; i < len(s); i += 7 { |
| 35 |
want := strings.Count(lower[:i], "a") |
| 36 |
got := CountString(s[:i], 'a') |
| 37 |
if got != want { |
| 38 |
if n < 30 { |
| 39 |
t.Errorf("%d: want: %d got: %d", i, want, got) |
| 40 |
} |
| 41 |
n++ |
| 42 |
} |
| 43 |
} |
| 44 |
if n > 0 { |
| 45 |
t.Errorf("Failed %d/%d tests", n, len(s)) |
| 46 |
} |
| 47 |
} |