Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 package string_test | |
2 | |
3 import ( | |
4 "fmt" | |
5 "strings" | |
6 "testing" | |
7 ) | |
8 | |
9 func TestIndex(t *testing.T) { | |
10 var tests = []struct { | |
Sameer at Google
2014/06/25 14:07:16
It's common to inline tests in the loop:
for _, te
| |
11 s string | |
12 sep string | |
13 out int | |
Sameer at Google
2014/06/25 14:07:16
call this want, like the previous slide?
| |
14 }{ | |
15 {"", "", 0}, | |
Sameer at Google
2014/06/25 14:07:16
Include {"chicken", "ken", 4} to show that this is
| |
16 {"", "a", -1}, | |
17 {"fo", "foo", -1}, | |
18 {"foo", "foo", 0}, | |
19 {"oofofoofooo", "f", 2}, | |
20 // etc | |
21 } | |
22 for _, test := range tests { | |
23 actual := strings.Index(test.s, test.sep) | |
24 if actual != test.out { | |
25 t.Errorf("Index(%q,%q) = %v; want %v", test.s, test.sep, actual, test.out) | |
Sameer at Google
2014/06/25 14:07:15
do you want a space between %q,%q to mirror the co
| |
26 } | |
27 } | |
28 } | |
29 | |
30 func BenchmarkIndex(b *testing.B) { | |
31 const s = "some_text=some☺value" | |
32 for i := 0; i < b.N; i++ { | |
33 strings.Index(s, "v") | |
34 } | |
35 } | |
36 | |
37 func ExampleIndex() { | |
38 fmt.Println(strings.Index("chicken", "ken")) | |
39 fmt.Println(strings.Index("chicken", "dmr")) | |
40 // Output: | |
41 // 4 | |
42 // -1 | |
43 } | |
OLD | NEW |