OSDN Git Service

Update Go library to r60.
[pf3gnuchains/gcc-fork.git] / libgo / go / strconv / quote_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 strconv_test
6
7 import (
8         "os"
9         . "strconv"
10         "testing"
11 )
12
13 type quoteTest struct {
14         in    string
15         out   string
16         ascii string
17 }
18
19 var quotetests = []quoteTest{
20         {"\a\b\f\r\n\t\v", `"\a\b\f\r\n\t\v"`, `"\a\b\f\r\n\t\v"`},
21         {"\\", `"\\"`, `"\\"`},
22         {"abc\xffdef", `"abc\xffdef"`, `"abc\xffdef"`},
23         {"\u263a", `"☺"`, `"\u263a"`},
24         {"\U0010ffff", `"\U0010ffff"`, `"\U0010ffff"`},
25         {"\x04", `"\x04"`, `"\x04"`},
26 }
27
28 func TestQuote(t *testing.T) {
29         for _, tt := range quotetests {
30                 if out := Quote(tt.in); out != tt.out {
31                         t.Errorf("Quote(%s) = %s, want %s", tt.in, out, tt.out)
32                 }
33         }
34 }
35
36 func TestQuoteToASCII(t *testing.T) {
37         for _, tt := range quotetests {
38                 if out := QuoteToASCII(tt.in); out != tt.ascii {
39                         t.Errorf("QuoteToASCII(%s) = %s, want %s", tt.in, out, tt.ascii)
40                 }
41         }
42 }
43
44 type quoteRuneTest struct {
45         in    int
46         out   string
47         ascii string
48 }
49
50 var quoterunetests = []quoteRuneTest{
51         {'a', `'a'`, `'a'`},
52         {'\a', `'\a'`, `'\a'`},
53         {'\\', `'\\'`, `'\\'`},
54         {0xFF, `'ÿ'`, `'\u00ff'`},
55         {0x263a, `'☺'`, `'\u263a'`},
56         {0xfffd, `'�'`, `'\ufffd'`},
57         {0x0010ffff, `'\U0010ffff'`, `'\U0010ffff'`},
58         {0x0010ffff + 1, `'�'`, `'\ufffd'`},
59         {0x04, `'\x04'`, `'\x04'`},
60 }
61
62 func TestQuoteRune(t *testing.T) {
63         for _, tt := range quoterunetests {
64                 if out := QuoteRune(tt.in); out != tt.out {
65                         t.Errorf("QuoteRune(%U) = %s, want %s", tt.in, out, tt.out)
66                 }
67         }
68 }
69
70 func TestQuoteRuneToASCII(t *testing.T) {
71         for _, tt := range quoterunetests {
72                 if out := QuoteRuneToASCII(tt.in); out != tt.ascii {
73                         t.Errorf("QuoteRuneToASCII(%U) = %s, want %s", tt.in, out, tt.ascii)
74                 }
75         }
76 }
77
78 type canBackquoteTest struct {
79         in  string
80         out bool
81 }
82
83 var canbackquotetests = []canBackquoteTest{
84         {"`", false},
85         {string(0), false},
86         {string(1), false},
87         {string(2), false},
88         {string(3), false},
89         {string(4), false},
90         {string(5), false},
91         {string(6), false},
92         {string(7), false},
93         {string(8), false},
94         {string(9), true}, // \t
95         {string(10), false},
96         {string(11), false},
97         {string(12), false},
98         {string(13), false},
99         {string(14), false},
100         {string(15), false},
101         {string(16), false},
102         {string(17), false},
103         {string(18), false},
104         {string(19), false},
105         {string(20), false},
106         {string(21), false},
107         {string(22), false},
108         {string(23), false},
109         {string(24), false},
110         {string(25), false},
111         {string(26), false},
112         {string(27), false},
113         {string(28), false},
114         {string(29), false},
115         {string(30), false},
116         {string(31), false},
117         {`' !"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, true},
118         {`0123456789`, true},
119         {`ABCDEFGHIJKLMNOPQRSTUVWXYZ`, true},
120         {`abcdefghijklmnopqrstuvwxyz`, true},
121         {`☺`, true},
122 }
123
124 func TestCanBackquote(t *testing.T) {
125         for _, tt := range canbackquotetests {
126                 if out := CanBackquote(tt.in); out != tt.out {
127                         t.Errorf("CanBackquote(%q) = %v, want %v", tt.in, out, tt.out)
128                 }
129         }
130 }
131
132 type unQuoteTest struct {
133         in  string
134         out string
135 }
136
137 var unquotetests = []unQuoteTest{
138         {`""`, ""},
139         {`"a"`, "a"},
140         {`"abc"`, "abc"},
141         {`"☺"`, "☺"},
142         {`"hello world"`, "hello world"},
143         {`"\xFF"`, "\xFF"},
144         {`"\377"`, "\377"},
145         {`"\u1234"`, "\u1234"},
146         {`"\U00010111"`, "\U00010111"},
147         {`"\U0001011111"`, "\U0001011111"},
148         {`"\a\b\f\n\r\t\v\\\""`, "\a\b\f\n\r\t\v\\\""},
149         {`"'"`, "'"},
150
151         {`'a'`, "a"},
152         {`'☹'`, "☹"},
153         {`'\a'`, "\a"},
154         {`'\x10'`, "\x10"},
155         {`'\377'`, "\377"},
156         {`'\u1234'`, "\u1234"},
157         {`'\U00010111'`, "\U00010111"},
158         {`'\t'`, "\t"},
159         {`' '`, " "},
160         {`'\''`, "'"},
161         {`'"'`, "\""},
162
163         {"``", ``},
164         {"`a`", `a`},
165         {"`abc`", `abc`},
166         {"`☺`", `☺`},
167         {"`hello world`", `hello world`},
168         {"`\\xFF`", `\xFF`},
169         {"`\\377`", `\377`},
170         {"`\\`", `\`},
171         {"`     `", `   `},
172         {"` `", ` `},
173 }
174
175 var misquoted = []string{
176         ``,
177         `"`,
178         `"a`,
179         `"'`,
180         `b"`,
181         `"\"`,
182         `'\'`,
183         `'ab'`,
184         `"\x1!"`,
185         `"\U12345678"`,
186         `"\z"`,
187         "`",
188         "`xxx",
189         "`\"",
190         `"\'"`,
191         `'\"'`,
192 }
193
194 func TestUnquote(t *testing.T) {
195         for _, tt := range unquotetests {
196                 if out, err := Unquote(tt.in); err != nil && out != tt.out {
197                         t.Errorf("Unquote(%#q) = %q, %v want %q, nil", tt.in, out, err, tt.out)
198                 }
199         }
200
201         // run the quote tests too, backward
202         for _, tt := range quotetests {
203                 if in, err := Unquote(tt.out); in != tt.in {
204                         t.Errorf("Unquote(%#q) = %q, %v, want %q, nil", tt.out, in, err, tt.in)
205                 }
206         }
207
208         for _, s := range misquoted {
209                 if out, err := Unquote(s); out != "" || err != os.EINVAL {
210                         t.Errorf("Unquote(%#q) = %q, %v want %q, %v", s, out, err, "", os.EINVAL)
211                 }
212         }
213 }