Fix bootstrap/PR63632
[official-gcc.git] / libgo / go / strconv / quote_test.go
blobe4b5b6b9fd2d0d2fb9e05ba5f9e1ba589d5b8458
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.
5 package strconv_test
7 import (
8 . "strconv"
9 "testing"
10 "unicode"
13 // Verify that our isPrint agrees with unicode.IsPrint
14 func TestIsPrint(t *testing.T) {
15 n := 0
16 for r := rune(0); r <= unicode.MaxRune; r++ {
17 if IsPrint(r) != unicode.IsPrint(r) {
18 t.Errorf("IsPrint(%U)=%t incorrect", r, IsPrint(r))
19 n++
20 if n > 10 {
21 return
27 type quoteTest struct {
28 in string
29 out string
30 ascii string
33 var quotetests = []quoteTest{
34 {"\a\b\f\r\n\t\v", `"\a\b\f\r\n\t\v"`, `"\a\b\f\r\n\t\v"`},
35 {"\\", `"\\"`, `"\\"`},
36 {"abc\xffdef", `"abc\xffdef"`, `"abc\xffdef"`},
37 {"\u263a", `"☺"`, `"\u263a"`},
38 {"\U0010ffff", `"\U0010ffff"`, `"\U0010ffff"`},
39 {"\x04", `"\x04"`, `"\x04"`},
42 func TestQuote(t *testing.T) {
43 for _, tt := range quotetests {
44 if out := Quote(tt.in); out != tt.out {
45 t.Errorf("Quote(%s) = %s, want %s", tt.in, out, tt.out)
47 if out := AppendQuote([]byte("abc"), tt.in); string(out) != "abc"+tt.out {
48 t.Errorf("AppendQuote(%q, %s) = %s, want %s", "abc", tt.in, out, "abc"+tt.out)
53 func TestQuoteToASCII(t *testing.T) {
54 for _, tt := range quotetests {
55 if out := QuoteToASCII(tt.in); out != tt.ascii {
56 t.Errorf("QuoteToASCII(%s) = %s, want %s", tt.in, out, tt.ascii)
58 if out := AppendQuoteToASCII([]byte("abc"), tt.in); string(out) != "abc"+tt.ascii {
59 t.Errorf("AppendQuoteToASCII(%q, %s) = %s, want %s", "abc", tt.in, out, "abc"+tt.ascii)
64 type quoteRuneTest struct {
65 in rune
66 out string
67 ascii string
70 var quoterunetests = []quoteRuneTest{
71 {'a', `'a'`, `'a'`},
72 {'\a', `'\a'`, `'\a'`},
73 {'\\', `'\\'`, `'\\'`},
74 {0xFF, `'ÿ'`, `'\u00ff'`},
75 {0x263a, `'☺'`, `'\u263a'`},
76 {0xfffd, `'�'`, `'\ufffd'`},
77 {0x0010ffff, `'\U0010ffff'`, `'\U0010ffff'`},
78 {0x0010ffff + 1, `'�'`, `'\ufffd'`},
79 {0x04, `'\x04'`, `'\x04'`},
82 func TestQuoteRune(t *testing.T) {
83 for _, tt := range quoterunetests {
84 if out := QuoteRune(tt.in); out != tt.out {
85 t.Errorf("QuoteRune(%U) = %s, want %s", tt.in, out, tt.out)
87 if out := AppendQuoteRune([]byte("abc"), tt.in); string(out) != "abc"+tt.out {
88 t.Errorf("AppendQuoteRune(%q, %U) = %s, want %s", "abc", tt.in, out, "abc"+tt.out)
93 func TestQuoteRuneToASCII(t *testing.T) {
94 for _, tt := range quoterunetests {
95 if out := QuoteRuneToASCII(tt.in); out != tt.ascii {
96 t.Errorf("QuoteRuneToASCII(%U) = %s, want %s", tt.in, out, tt.ascii)
98 if out := AppendQuoteRuneToASCII([]byte("abc"), tt.in); string(out) != "abc"+tt.ascii {
99 t.Errorf("AppendQuoteRuneToASCII(%q, %U) = %s, want %s", "abc", tt.in, out, "abc"+tt.ascii)
104 type canBackquoteTest struct {
105 in string
106 out bool
109 var canbackquotetests = []canBackquoteTest{
110 {"`", false},
111 {string(0), false},
112 {string(1), false},
113 {string(2), false},
114 {string(3), false},
115 {string(4), false},
116 {string(5), false},
117 {string(6), false},
118 {string(7), false},
119 {string(8), false},
120 {string(9), true}, // \t
121 {string(10), false},
122 {string(11), false},
123 {string(12), false},
124 {string(13), false},
125 {string(14), false},
126 {string(15), false},
127 {string(16), false},
128 {string(17), false},
129 {string(18), false},
130 {string(19), false},
131 {string(20), false},
132 {string(21), false},
133 {string(22), false},
134 {string(23), false},
135 {string(24), false},
136 {string(25), false},
137 {string(26), false},
138 {string(27), false},
139 {string(28), false},
140 {string(29), false},
141 {string(30), false},
142 {string(31), false},
143 {string(0x7F), false},
144 {`' !"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, true},
145 {`0123456789`, true},
146 {`ABCDEFGHIJKLMNOPQRSTUVWXYZ`, true},
147 {`abcdefghijklmnopqrstuvwxyz`, true},
148 {`☺`, true},
151 func TestCanBackquote(t *testing.T) {
152 for _, tt := range canbackquotetests {
153 if out := CanBackquote(tt.in); out != tt.out {
154 t.Errorf("CanBackquote(%q) = %v, want %v", tt.in, out, tt.out)
159 type unQuoteTest struct {
160 in string
161 out string
164 var unquotetests = []unQuoteTest{
165 {`""`, ""},
166 {`"a"`, "a"},
167 {`"abc"`, "abc"},
168 {`"☺"`, "☺"},
169 {`"hello world"`, "hello world"},
170 {`"\xFF"`, "\xFF"},
171 {`"\377"`, "\377"},
172 {`"\u1234"`, "\u1234"},
173 {`"\U00010111"`, "\U00010111"},
174 {`"\U0001011111"`, "\U0001011111"},
175 {`"\a\b\f\n\r\t\v\\\""`, "\a\b\f\n\r\t\v\\\""},
176 {`"'"`, "'"},
178 {`'a'`, "a"},
179 {`'☹'`, "☹"},
180 {`'\a'`, "\a"},
181 {`'\x10'`, "\x10"},
182 {`'\377'`, "\377"},
183 {`'\u1234'`, "\u1234"},
184 {`'\U00010111'`, "\U00010111"},
185 {`'\t'`, "\t"},
186 {`' '`, " "},
187 {`'\''`, "'"},
188 {`'"'`, "\""},
190 {"``", ``},
191 {"`a`", `a`},
192 {"`abc`", `abc`},
193 {"`☺`", `☺`},
194 {"`hello world`", `hello world`},
195 {"`\\xFF`", `\xFF`},
196 {"`\\377`", `\377`},
197 {"`\\`", `\`},
198 {"`\n`", "\n"},
199 {"` `", ` `},
200 {"` `", ` `},
203 var misquoted = []string{
205 `"`,
206 `"a`,
207 `"'`,
208 `b"`,
209 `"\"`,
210 `"\9"`,
211 `"\19"`,
212 `"\129"`,
213 `'\'`,
214 `'\9'`,
215 `'\19'`,
216 `'\129'`,
217 `'ab'`,
218 `"\x1!"`,
219 `"\U12345678"`,
220 `"\z"`,
221 "`",
222 "`xxx",
223 "`\"",
224 `"\'"`,
225 `'\"'`,
226 "\"\n\"",
227 "\"\\n\n\"",
228 "'\n'",
231 func TestUnquote(t *testing.T) {
232 for _, tt := range unquotetests {
233 if out, err := Unquote(tt.in); err != nil && out != tt.out {
234 t.Errorf("Unquote(%#q) = %q, %v want %q, nil", tt.in, out, err, tt.out)
238 // run the quote tests too, backward
239 for _, tt := range quotetests {
240 if in, err := Unquote(tt.out); in != tt.in {
241 t.Errorf("Unquote(%#q) = %q, %v, want %q, nil", tt.out, in, err, tt.in)
245 for _, s := range misquoted {
246 if out, err := Unquote(s); out != "" || err != ErrSyntax {
247 t.Errorf("Unquote(%#q) = %q, %v want %q, %v", s, out, err, "", ErrSyntax)
252 func BenchmarkUnquoteEasy(b *testing.B) {
253 for i := 0; i < b.N; i++ {
254 Unquote(`"Give me a rock, paper and scissors and I will move the world."`)
258 func BenchmarkUnquoteHard(b *testing.B) {
259 for i := 0; i < b.N; i++ {
260 Unquote(`"\x47ive me a \x72ock, \x70aper and \x73cissors and \x49 will move the world."`)