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
.Error("%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+%04x); got %c (U+%04x)", 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
.Error("%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+%04x); got %c (U+%04x)", s
, i
, expect
, expect
, got
, got
)
48 const randCount
= 100000
50 func TestRandomAccess(t
*testing
.T
) {
51 for _
, s
:= range testStrings
{
57 if str
.RuneCount() != len(runes
) {
58 t
.Error("%s: expected %d runes; got %d", s
, len(runes
), str
.RuneCount())
61 for j
:= 0; j
< randCount
; j
++ {
62 i
:= rand
.Intn(len(runes
))
66 t
.Errorf("%s[%d]: expected %c (U+%04x); got %c (U+%04x)", s
, i
, expect
, expect
, got
, got
)
72 func TestRandomSliceAccess(t
*testing
.T
) {
73 for _
, s
:= range testStrings
{
74 if len(s
) == 0 || s
[0] == '\x80' { // the bad-UTF-8 string fools this simple test
79 if str
.RuneCount() != len(runes
) {
80 t
.Error("%s: expected %d runes; got %d", s
, len(runes
), str
.RuneCount())
83 for k
:= 0; k
< randCount
; k
++ {
84 i
:= rand
.Intn(len(runes
))
85 j
:= rand
.Intn(len(runes
) + 1)
86 if i
> j
{ // include empty strings
89 expect
:= string(runes
[i
:j
])
90 got
:= str
.Slice(i
, j
)
92 t
.Errorf("%s[%d:%d]: expected %q got %q", s
, i
, j
, expect
, got
)
98 func TestLimitSliceAccess(t
*testing
.T
) {
99 for _
, s
:= range testStrings
{
101 if str
.Slice(0, 0) != "" {
102 t
.Error("failure with empty slice at beginning")
104 nr
:= RuneCountInString(s
)
105 if str
.Slice(nr
, nr
) != "" {
106 t
.Error("failure with empty slice at end")