OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [unicode/] [utf8/] [utf8_test.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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
        . "unicode/utf8"
11
)
12
 
13
type Utf8Map struct {
14
        r   rune
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.r)
62
                }
63
                s := m.str
64
                if !FullRuneInString(s) {
65
                        t.Errorf("FullRuneInString(%q) (%U) = false, want true", s, m.r)
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.r)
84
                b1 := buf[0:n]
85
                if !bytes.Equal(b, b1) {
86
                        t.Errorf("EncodeRune(%#04x) = %q want %q", m.r, 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
                r, size := DecodeRune(b)
96
                if r != m.r || size != len(b) {
97
                        t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b, r, size, m.r, len(b))
98
                }
99
                s := m.str
100
                r, size = DecodeRuneInString(s)
101
                if r != m.r || size != len(b) {
102
                        t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", s, r, size, m.r, len(b))
103
                }
104
 
105
                // there's an extra byte that bytes left behind - make sure trailing byte works
106
                r, size = DecodeRune(b[0:cap(b)])
107
                if r != m.r || size != len(b) {
108
                        t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b, r, size, m.r, len(b))
109
                }
110
                s = m.str + "\x00"
111
                r, size = DecodeRuneInString(s)
112
                if r != m.r || size != len(b) {
113
                        t.Errorf("DecodeRuneInString(%q) = %#04x, %d want %#04x, %d", s, r, size, m.r, len(b))
114
                }
115
 
116
                // make sure missing bytes fail
117
                wantsize := 1
118
                if wantsize >= len(b) {
119
                        wantsize = 0
120
                }
121
                r, size = DecodeRune(b[0 : len(b)-1])
122
                if r != RuneError || size != wantsize {
123
                        t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b[0:len(b)-1], r, size, RuneError, wantsize)
124
                }
125
                s = m.str[0 : len(m.str)-1]
126
                r, size = DecodeRuneInString(s)
127
                if r != RuneError || size != wantsize {
128
                        t.Errorf("DecodeRuneInString(%q) = %#04x, %d want %#04x, %d", s, r, 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
                r, size = DecodeRune(b)
138
                if r != RuneError || size != 1 {
139
                        t.Errorf("DecodeRune(%q) = %#04x, %d want %#04x, %d", b, r, size, RuneError, 1)
140
                }
141
                s = string(b)
142
                r, size = DecodeRune(b)
143
                if r != RuneError || size != 1 {
144
                        t.Errorf("DecodeRuneInString(%q) = %#04x, %d want %#04x, %d", s, r, 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 := []rune(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
                r     rune
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
                r1, size1 := DecodeRune(b[i:])
199
                if r != r1 {
200
                        t.Errorf("DecodeRune(%q) = %#04x, want %#04x", s[i:], r1, r)
201
                        return
202
                }
203
                r2, size2 := DecodeRuneInString(s[i:])
204
                if r != r2 {
205
                        t.Errorf("DecodeRuneInString(%q) = %#04x, want %#04x", s[i:], r2, 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
                r1, size1 := DecodeLastRune(b[0:si])
217
                r2, 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 r1 != index[j].r {
223
                        t.Errorf("DecodeLastRune(%q, %d) = %#04x, want %#04x", s, si, r1, index[j].r)
224
                        return
225
                }
226
                if r2 != index[j].r {
227
                        t.Errorf("DecodeLastRuneInString(%q, %d) = %#04x, want %#04x", s, si, r2, index[j].r)
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
type ValidTest struct {
278
        in  string
279
        out bool
280
}
281
 
282
var validTests = []ValidTest{
283
        {"", true},
284
        {"a", true},
285
        {"abc", true},
286
        {"Ж", true},
287
        {"ЖЖ", true},
288
        {"брэд-ЛГТМ", true},
289
        {"☺☻☹", true},
290
        {string([]byte{66, 250}), false},
291
        {string([]byte{66, 250, 67}), false},
292
        {"a\uFFFDb", true},
293
}
294
 
295
func TestValid(t *testing.T) {
296
        for i, tt := range validTests {
297
                if Valid([]byte(tt.in)) != tt.out {
298
                        t.Errorf("%d. Valid(%q) = %v; want %v", i, tt.in, !tt.out, tt.out)
299
                }
300
                if ValidString(tt.in) != tt.out {
301
                        t.Errorf("%d. ValidString(%q) = %v; want %v", i, tt.in, !tt.out, tt.out)
302
                }
303
        }
304
}
305
 
306
func BenchmarkRuneCountTenASCIIChars(b *testing.B) {
307
        for i := 0; i < b.N; i++ {
308
                RuneCountInString("0123456789")
309
        }
310
}
311
 
312
func BenchmarkRuneCountTenJapaneseChars(b *testing.B) {
313
        for i := 0; i < b.N; i++ {
314
                RuneCountInString("日本語日本語日本語日")
315
        }
316
}
317
 
318
func BenchmarkEncodeASCIIRune(b *testing.B) {
319
        buf := make([]byte, UTFMax)
320
        for i := 0; i < b.N; i++ {
321
                EncodeRune(buf, 'a')
322
        }
323
}
324
 
325
func BenchmarkEncodeJapaneseRune(b *testing.B) {
326
        buf := make([]byte, UTFMax)
327
        for i := 0; i < b.N; i++ {
328
                EncodeRune(buf, '本')
329
        }
330
}
331
 
332
func BenchmarkDecodeASCIIRune(b *testing.B) {
333
        a := []byte{'a'}
334
        for i := 0; i < b.N; i++ {
335
                DecodeRune(a)
336
        }
337
}
338
 
339
func BenchmarkDecodeJapaneseRune(b *testing.B) {
340
        nihon := []byte("本")
341
        for i := 0; i < b.N; i++ {
342
                DecodeRune(nihon)
343
        }
344
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.