]> Cypherpunks.ru repositories - gostls13.git/blob - test/utf.go
test: use testlib (final 61)
[gostls13.git] / test / utf.go
1 // run
2
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 package main
8
9 import "unicode/utf8"
10
11 func main() {
12         var chars [6]rune
13         chars[0] = 'a'
14         chars[1] = 'b'
15         chars[2] = 'c'
16         chars[3] = '\u65e5'
17         chars[4] = '\u672c'
18         chars[5] = '\u8a9e'
19         s := ""
20         for i := 0; i < 6; i++ {
21                 s += string(chars[i])
22         }
23         var l = len(s)
24         for w, i, j := 0, 0, 0; i < l; i += w {
25                 var r rune
26                 r, w = utf8.DecodeRuneInString(s[i:len(s)])
27                 if w == 0 {
28                         panic("zero width in string")
29                 }
30                 if r != chars[j] {
31                         panic("wrong value from string")
32                 }
33                 j++
34         }
35         // encoded as bytes:  'a' 'b' 'c' e6 97 a5 e6 9c ac e8 aa 9e
36         const L = 12
37         if L != l {
38                 panic("wrong length constructing array")
39         }
40         a := make([]byte, L)
41         a[0] = 'a'
42         a[1] = 'b'
43         a[2] = 'c'
44         a[3] = 0xe6
45         a[4] = 0x97
46         a[5] = 0xa5
47         a[6] = 0xe6
48         a[7] = 0x9c
49         a[8] = 0xac
50         a[9] = 0xe8
51         a[10] = 0xaa
52         a[11] = 0x9e
53         for w, i, j := 0, 0, 0; i < L; i += w {
54                 var r rune
55                 r, w = utf8.DecodeRune(a[i:L])
56                 if w == 0 {
57                         panic("zero width in bytes")
58                 }
59                 if r != chars[j] {
60                         panic("wrong value from bytes")
61                 }
62                 j++
63         }
64 }