OSDN Git Service

Remove the types float and complex.
[pf3gnuchains/gcc-fork.git] / libgo / go / utf8 / utf8_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         "bytes"
9         "testing"
10         . "utf8"
11 )
12
13 type Utf8Map struct {
14         rune int
15         str  string
16 }
17
18 var utf8map = []Utf8Map{
19         {0x0000, "\x00"},
20         {0x0001, "\x01"},
21         {0x007e, "\x7e"},
22         {0x007f, "\x7f"},
23         {0x0080, "\xc2\x80"},
24         {0x0081, "\xc2\x81"},
25         {0x00bf, "\xc2\xbf"},
26         {0x00c0, "\xc3\x80"},
27         {0x00c1, "\xc3\x81"},
28         {0x00c8, "\xc3\x88"},
29         {0x00d0, "\xc3\x90"},
30         {0x00e0, "\xc3\xa0"},
31         {0x00f0, "\xc3\xb0"},
32         {0x00f8, "\xc3\xb8"},
33         {0x00ff, "\xc3\xbf"},
34         {0x0100, "\xc4\x80"},
35         {0x07ff, "\xdf\xbf"},
36         {0x0800, "\xe0\xa0\x80"},
37         {0x0801, "\xe0\xa0\x81"},
38         {0xfffe, "\xef\xbf\xbe"},
39         {0xffff, "\xef\xbf\xbf"},
40         {0x10000, "\xf0\x90\x80\x80"},
41         {0x10001, "\xf0\x90\x80\x81"},
42         {0x10fffe, "\xf4\x8f\xbf\xbe"},
43         {0x10ffff, "\xf4\x8f\xbf\xbf"},
44         {0xFFFD, "\xef\xbf\xbd"},
45 }
46
47 var testStrings = []string{
48         "",
49         "abcd",
50         "☺☻☹",
51         "日a本b語ç日ð本Ê語þ日¥本¼語i日©",
52         "日a本b語ç日ð本Ê語þ日¥本¼語i日©日a本b語ç日ð本Ê語þ日¥本¼語i日©日a本b語ç日ð本Ê語þ日¥本¼語i日©",
53         "\x80\x80\x80\x80",
54 }
55
56 func TestFullRune(t *testing.T) {
57         for i := 0; i < len(utf8map); i++ {
58                 m := utf8map[i]
59                 b := []byte(m.str)
60                 if !FullRune(b) {
61                         t.Errorf("FullRune(%q) (%U) = false, want true", b, m.rune)
62                 }
63                 s := m.str
64                 if !FullRuneInString(s) {
65                         t.Errorf("FullRuneInString(%q) (%U) = false, want true", s, m.rune)
66                 }
67                 b1 := b[0 : len(b)-1]
68                 if FullRune(b1) {
69                         t.Errorf("FullRune(%q) = true, want false", b1)
70                 }
71                 s1 := string(b1)
72                 if FullRuneInString(s1) {
73                         t.Errorf("FullRune(%q) = true, want false", s1)
74                 }
75         }
76 }
77
78 func TestEncodeRune(t *testing.T) {
79         for i := 0; i < len(utf8map); i++ {
80                 m := utf8map[i]
81                 b := []byte(m.str)
82                 var buf [10]byte
83                 n := EncodeRune(buf[0:], m.rune)
84                 b1 := buf[0:n]
85                 if !bytes.Equal(b, b1) {
86                         t.Errorf("EncodeRune(%#04x) = %q want %q", m.rune, b1, b)
87                 }
88         }
89 }
90
91 func TestDecodeRune(t *testing.T) {
92         for i := 0; i < len(utf8map); i++ {
93                 m := utf8map[i]
94                 b := []byte(m.str)
95                 rune, size := DecodeRune(b)
96                 if rune != m.rune || size != len(b) {
97                         t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b, rune, size, m.rune, len(b))
98                 }
99                 s := m.str
100                 rune, size = DecodeRuneInString(s)
101                 if rune != m.rune || size != len(b) {
102                         t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", s, rune, size, m.rune, len(b))
103                 }
104
105                 // there's an extra byte that bytes left behind - make sure trailing byte works
106                 rune, size = DecodeRune(b[0:cap(b)])
107                 if rune != m.rune || size != len(b) {
108                         t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b, rune, size, m.rune, len(b))
109                 }
110                 s = m.str + "\x00"
111                 rune, size = DecodeRuneInString(s)
112                 if rune != m.rune || size != len(b) {
113                         t.Errorf("DecodeRuneInString(%q) = %#04x, %d want %#04x, %d", s, rune, size, m.rune, len(b))
114                 }
115
116                 // make sure missing bytes fail
117                 wantsize := 1
118                 if wantsize >= len(b) {
119                         wantsize = 0
120                 }
121                 rune, size = DecodeRune(b[0 : len(b)-1])
122                 if rune != RuneError || size != wantsize {
123                         t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b[0:len(b)-1], rune, size, RuneError, wantsize)
124                 }
125                 s = m.str[0 : len(m.str)-1]
126                 rune, size = DecodeRuneInString(s)
127                 if rune != RuneError || size != wantsize {
128                         t.Errorf("DecodeRuneInString(%q) = %#04x, %d want %#04x, %d", s, rune, size, RuneError, wantsize)
129                 }
130
131                 // make sure bad sequences fail
132                 if len(b) == 1 {
133                         b[0] = 0x80
134                 } else {
135                         b[len(b)-1] = 0x7F
136                 }
137                 rune, size = DecodeRune(b)
138                 if rune != RuneError || size != 1 {
139                         t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b, rune, size, RuneError, 1)
140                 }
141                 s = string(b)
142                 rune, size = DecodeRune(b)
143                 if rune != RuneError || size != 1 {
144                         t.Errorf("DecodeRuneInString(%q) = %#04x, %d want %#04x, %d", s, rune, size, RuneError, 1)
145                 }
146
147         }
148 }
149
150 // Check that DecodeRune and DecodeLastRune correspond to
151 // the equivalent range loop.
152 func TestSequencing(t *testing.T) {
153         for _, ts := range testStrings {
154                 for _, m := range utf8map {
155                         for _, s := range []string{ts + m.str, m.str + ts, ts + m.str + ts} {
156                                 testSequence(t, s)
157                         }
158                 }
159         }
160 }
161
162 // Check that a range loop and a []int conversion visit the same runes.
163 // Not really a test of this package, but the assumption is used here and
164 // it's good to verify
165 func TestIntConversion(t *testing.T) {
166         for _, ts := range testStrings {
167                 runes := []int(ts)
168                 if RuneCountInString(ts) != len(runes) {
169                         t.Errorf("%q: expected %d runes; got %d", ts, len(runes), RuneCountInString(ts))
170                         break
171                 }
172                 i := 0
173                 for _, r := range ts {
174                         if r != runes[i] {
175                                 t.Errorf("%q[%d]: expected %c (%U); got %c (%U)", ts, i, runes[i], runes[i], r, r)
176                         }
177                         i++
178                 }
179         }
180 }
181
182 func testSequence(t *testing.T, s string) {
183         type info struct {
184                 index int
185                 rune  int
186         }
187         index := make([]info, len(s))
188         b := []byte(s)
189         si := 0
190         j := 0
191         for i, r := range s {
192                 if si != i {
193                         t.Errorf("Sequence(%q) mismatched index %d, want %d", s, si, i)
194                         return
195                 }
196                 index[j] = info{i, r}
197                 j++
198                 rune1, size1 := DecodeRune(b[i:])
199                 if r != rune1 {
200                         t.Errorf("DecodeRune(%q) = %#04x, want %#04x", s[i:], rune1, r)
201                         return
202                 }
203                 rune2, size2 := DecodeRuneInString(s[i:])
204                 if r != rune2 {
205                         t.Errorf("DecodeRuneInString(%q) = %#04x, want %#04x", s[i:], rune2, r)
206                         return
207                 }
208                 if size1 != size2 {
209                         t.Errorf("DecodeRune/DecodeRuneInString(%q) size mismatch %d/%d", s[i:], size1, size2)
210                         return
211                 }
212                 si += size1
213         }
214         j--
215         for si = len(s); si > 0; {
216                 rune1, size1 := DecodeLastRune(b[0:si])
217                 rune2, size2 := DecodeLastRuneInString(s[0:si])
218                 if size1 != size2 {
219                         t.Errorf("DecodeLastRune/DecodeLastRuneInString(%q, %d) size mismatch %d/%d", s, si, size1, size2)
220                         return
221                 }
222                 if rune1 != index[j].rune {
223                         t.Errorf("DecodeLastRune(%q, %d) = %#04x, want %#04x", s, si, rune1, index[j].rune)
224                         return
225                 }
226                 if rune2 != index[j].rune {
227                         t.Errorf("DecodeLastRuneInString(%q, %d) = %#04x, want %#04x", s, si, rune2, index[j].rune)
228                         return
229                 }
230                 si -= size1
231                 if si != index[j].index {
232                         t.Errorf("DecodeLastRune(%q) index mismatch at %d, want %d", s, si, index[j].index)
233                         return
234                 }
235                 j--
236         }
237         if si != 0 {
238                 t.Errorf("DecodeLastRune(%q) finished at %d, not 0", s, si)
239         }
240 }
241
242 // Check that negative runes encode as U+FFFD.
243 func TestNegativeRune(t *testing.T) {
244         errorbuf := make([]byte, UTFMax)
245         errorbuf = errorbuf[0:EncodeRune(errorbuf, RuneError)]
246         buf := make([]byte, UTFMax)
247         buf = buf[0:EncodeRune(buf, -1)]
248         if !bytes.Equal(buf, errorbuf) {
249                 t.Errorf("incorrect encoding [% x] for -1; expected [% x]", buf, errorbuf)
250         }
251 }
252
253 type RuneCountTest struct {
254         in  string
255         out int
256 }
257
258 var runecounttests = []RuneCountTest{
259         {"abcd", 4},
260         {"☺☻☹", 3},
261         {"1,2,3,4", 7},
262         {"\xe2\x00", 2},
263 }
264
265 func TestRuneCount(t *testing.T) {
266         for i := 0; i < len(runecounttests); i++ {
267                 tt := runecounttests[i]
268                 if out := RuneCountInString(tt.in); out != tt.out {
269                         t.Errorf("RuneCountInString(%q) = %d, want %d", tt.in, out, tt.out)
270                 }
271                 if out := RuneCount([]byte(tt.in)); out != tt.out {
272                         t.Errorf("RuneCount(%q) = %d, want %d", tt.in, out, tt.out)
273                 }
274         }
275 }
276
277 func BenchmarkRuneCountTenASCIIChars(b *testing.B) {
278         for i := 0; i < b.N; i++ {
279                 RuneCountInString("0123456789")
280         }
281 }
282
283 func BenchmarkRuneCountTenJapaneseChars(b *testing.B) {
284         for i := 0; i < b.N; i++ {
285                 RuneCountInString("日本語日本語日本語日")
286         }
287 }
288
289 func BenchmarkEncodeASCIIRune(b *testing.B) {
290         buf := make([]byte, UTFMax)
291         for i := 0; i < b.N; i++ {
292                 EncodeRune(buf, 'a')
293         }
294 }
295
296 func BenchmarkEncodeJapaneseRune(b *testing.B) {
297         buf := make([]byte, UTFMax)
298         for i := 0; i < b.N; i++ {
299                 EncodeRune(buf, '本')
300         }
301 }
302
303 func BenchmarkDecodeASCIIRune(b *testing.B) {
304         a := []byte{'a'}
305         for i := 0; i < b.N; i++ {
306                 DecodeRune(a)
307         }
308 }
309
310 func BenchmarkDecodeJapaneseRune(b *testing.B) {
311         nihon := []byte("本")
312         for i := 0; i < b.N; i++ {
313                 DecodeRune(nihon)
314         }
315 }