OSDN Git Service

libgo: Update to weekly 2011-11-09.
[pf3gnuchains/gcc-fork.git] / libgo / go / unicode / utf8 / string_test.go
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.
4
5 package utf8_test
6
7 import (
8         "math/rand"
9         "testing"
10         . "unicode/utf8"
11 )
12
13 func TestScanForwards(t *testing.T) {
14         for _, s := range testStrings {
15                 runes := []rune(s)
16                 str := NewString(s)
17                 if str.RuneCount() != len(runes) {
18                         t.Errorf("%s: expected %d runes; got %d", s, len(runes), str.RuneCount())
19                         break
20                 }
21                 for i, expect := range runes {
22                         got := str.At(i)
23                         if got != expect {
24                                 t.Errorf("%s[%d]: expected %c (%U); got %c (%U)", s, i, expect, expect, got, got)
25                         }
26                 }
27         }
28 }
29
30 func TestScanBackwards(t *testing.T) {
31         for _, s := range testStrings {
32                 runes := []rune(s)
33                 str := NewString(s)
34                 if str.RuneCount() != len(runes) {
35                         t.Errorf("%s: expected %d runes; got %d", s, len(runes), str.RuneCount())
36                         break
37                 }
38                 for i := len(runes) - 1; i >= 0; i-- {
39                         expect := runes[i]
40                         got := str.At(i)
41                         if got != expect {
42                                 t.Errorf("%s[%d]: expected %c (%U); got %c (%U)", s, i, expect, expect, got, got)
43                         }
44                 }
45         }
46 }
47
48 func randCount() int {
49         if testing.Short() {
50                 return 100
51         }
52         return 100000
53 }
54
55 func TestRandomAccess(t *testing.T) {
56         for _, s := range testStrings {
57                 if len(s) == 0 {
58                         continue
59                 }
60                 runes := []rune(s)
61                 str := NewString(s)
62                 if str.RuneCount() != len(runes) {
63                         t.Errorf("%s: expected %d runes; got %d", s, len(runes), str.RuneCount())
64                         break
65                 }
66                 for j := 0; j < randCount(); j++ {
67                         i := rand.Intn(len(runes))
68                         expect := runes[i]
69                         got := str.At(i)
70                         if got != expect {
71                                 t.Errorf("%s[%d]: expected %c (%U); got %c (%U)", s, i, expect, expect, got, got)
72                         }
73                 }
74         }
75 }
76
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
80                         continue
81                 }
82                 runes := []rune(s)
83                 str := NewString(s)
84                 if str.RuneCount() != len(runes) {
85                         t.Errorf("%s: expected %d runes; got %d", s, len(runes), str.RuneCount())
86                         break
87                 }
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
92                                 continue
93                         }
94                         expect := string(runes[i:j])
95                         got := str.Slice(i, j)
96                         if got != expect {
97                                 t.Errorf("%s[%d:%d]: expected %q got %q", s, i, j, expect, got)
98                         }
99                 }
100         }
101 }
102
103 func TestLimitSliceAccess(t *testing.T) {
104         for _, s := range testStrings {
105                 str := NewString(s)
106                 if str.Slice(0, 0) != "" {
107                         t.Error("failure with empty slice at beginning")
108                 }
109                 nr := RuneCountInString(s)
110                 if str.Slice(nr, nr) != "" {
111                         t.Error("failure with empty slice at end")
112                 }
113         }
114 }