1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 func TestScanForwards(t *testing.T) {
14 for _, s := range testStrings {
17 if str.RuneCount() != len(runes) {
18 t.Errorf("%s: expected %d runes; got %d", s, len(runes), str.RuneCount())
21 for i, expect := range runes {
24 t.Errorf("%s[%d]: expected %c (%U); got %c (%U)", s, i, expect, expect, got, got)
30 func TestScanBackwards(t *testing.T) {
31 for _, s := range testStrings {
34 if str.RuneCount() != len(runes) {
35 t.Errorf("%s: expected %d runes; got %d", s, len(runes), str.RuneCount())
38 for i := len(runes) - 1; i >= 0; i-- {
42 t.Errorf("%s[%d]: expected %c (%U); got %c (%U)", s, i, expect, expect, got, got)
48 func randCount() int {
55 func TestRandomAccess(t *testing.T) {
56 for _, s := range testStrings {
62 if str.RuneCount() != len(runes) {
63 t.Errorf("%s: expected %d runes; got %d", s, len(runes), str.RuneCount())
66 for j := 0; j < randCount(); j++ {
67 i := rand.Intn(len(runes))
71 t.Errorf("%s[%d]: expected %c (%U); got %c (%U)", s, i, expect, expect, got, got)
77 func TestRandomSliceAccess(t *testing.T) {
78 for _, s := range testStrings {
79 if len(s) == 0 || s[0] == '\x80' { // the bad-UTF-8 string fools this simple test
84 if str.RuneCount() != len(runes) {
85 t.Errorf("%s: expected %d runes; got %d", s, len(runes), str.RuneCount())
88 for k := 0; k < randCount(); k++ {
89 i := rand.Intn(len(runes))
90 j := rand.Intn(len(runes) + 1)
91 if i > j { // include empty strings
94 expect := string(runes[i:j])
95 got := str.Slice(i, j)
97 t.Errorf("%s[%d:%d]: expected %q got %q", s, i, j, expect, got)
103 func TestLimitSliceAccess(t *testing.T) {
104 for _, s := range testStrings {
106 if str.Slice(0, 0) != "" {
107 t.Error("failure with empty slice at beginning")
109 nr := RuneCountInString(s)
110 if str.Slice(nr, nr) != "" {
111 t.Error("failure with empty slice at end")