github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2014/testing/test2/string_test.go (about) 1 package strings_test 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 ) 8 9 func TestIndex(t *testing.T) { 10 var tests = []struct { 11 s string 12 sep string 13 out int 14 }{ 15 {"", "", 0}, 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) 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 }