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.
13 // Verify that our IsPrint agrees with unicode.IsPrint.
14 func TestIsPrint(t
*testing
.T
) {
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
))
27 // Verify that our IsGraphic agrees with unicode.IsGraphic.
28 func TestIsGraphic(t
*testing
.T
) {
30 for r
:= rune(0); r
<= unicode
.MaxRune
; r
++ {
31 if IsGraphic(r
) != unicode
.IsGraphic(r
) {
32 t
.Errorf("IsGraphic(%U)=%t incorrect", r
, IsGraphic(r
))
41 type quoteTest
struct {
48 var quotetests
= []quoteTest
{
49 {"\a\b\f\r\n\t\v", `"\a\b\f\r\n\t\v"`, `"\a\b\f\r\n\t\v"`, `"\a\b\f\r\n\t\v"`},
50 {"\\", `"\\"`, `"\\"`, `"\\"`},
51 {"abc\xffdef", `"abc\xffdef"`, `"abc\xffdef"`, `"abc\xffdef"`},
52 {"\u263a", `"☺"`, `"\u263a"`, `"☺"`},
53 {"\U0010ffff", `"\U0010ffff"`, `"\U0010ffff"`, `"\U0010ffff"`},
54 {"\x04", `"\x04"`, `"\x04"`, `"\x04"`},
55 // Some non-printable but graphic runes. Final column is double-quoted.
56 {"!\u00a0!\u2000!\u3000!", `"!\u00a0!\u2000!\u3000!"`, `"!\u00a0!\u2000!\u3000!"`, "\"!\u00a0!\u2000!\u3000!\""},
59 func TestQuote(t
*testing
.T
) {
60 for _
, tt
:= range quotetests
{
61 if out
:= Quote(tt
.in
); out
!= tt
.out
{
62 t
.Errorf("Quote(%s) = %s, want %s", tt
.in
, out
, tt
.out
)
64 if out
:= AppendQuote([]byte("abc"), tt
.in
); string(out
) != "abc"+tt
.out
{
65 t
.Errorf("AppendQuote(%q, %s) = %s, want %s", "abc", tt
.in
, out
, "abc"+tt
.out
)
70 func TestQuoteToASCII(t
*testing
.T
) {
71 for _
, tt
:= range quotetests
{
72 if out
:= QuoteToASCII(tt
.in
); out
!= tt
.ascii
{
73 t
.Errorf("QuoteToASCII(%s) = %s, want %s", tt
.in
, out
, tt
.ascii
)
75 if out
:= AppendQuoteToASCII([]byte("abc"), tt
.in
); string(out
) != "abc"+tt
.ascii
{
76 t
.Errorf("AppendQuoteToASCII(%q, %s) = %s, want %s", "abc", tt
.in
, out
, "abc"+tt
.ascii
)
81 func TestQuoteToGraphic(t
*testing
.T
) {
82 for _
, tt
:= range quotetests
{
83 if out
:= QuoteToGraphic(tt
.in
); out
!= tt
.graphic
{
84 t
.Errorf("QuoteToGraphic(%s) = %s, want %s", tt
.in
, out
, tt
.graphic
)
86 if out
:= AppendQuoteToGraphic([]byte("abc"), tt
.in
); string(out
) != "abc"+tt
.graphic
{
87 t
.Errorf("AppendQuoteToGraphic(%q, %s) = %s, want %s", "abc", tt
.in
, out
, "abc"+tt
.graphic
)
92 func BenchmarkQuote(b
*testing
.B
) {
93 for i
:= 0; i
< b
.N
; i
++ {
94 Quote("\a\b\f\r\n\t\v\a\b\f\r\n\t\v\a\b\f\r\n\t\v")
98 func BenchmarkQuoteRune(b
*testing
.B
) {
99 for i
:= 0; i
< b
.N
; i
++ {
104 var benchQuoteBuf
[]byte
106 func BenchmarkAppendQuote(b
*testing
.B
) {
107 for i
:= 0; i
< b
.N
; i
++ {
108 benchQuoteBuf
= AppendQuote(benchQuoteBuf
[:0], "\a\b\f\r\n\t\v\a\b\f\r\n\t\v\a\b\f\r\n\t\v")
112 var benchQuoteRuneBuf
[]byte
114 func BenchmarkAppendQuoteRune(b
*testing
.B
) {
115 for i
:= 0; i
< b
.N
; i
++ {
116 benchQuoteRuneBuf
= AppendQuoteRune(benchQuoteRuneBuf
[:0], '\a')
120 type quoteRuneTest
struct {
127 var quoterunetests
= []quoteRuneTest
{
128 {'a', `'a'`, `'a'`, `'a'`},
129 {'\a', `'\a'`, `'\a'`, `'\a'`},
130 {'\\', `'\\'`, `'\\'`, `'\\'`},
131 {0xFF, `'ÿ'`, `'\u00ff'`, `'ÿ'`},
132 {0x263a, `'☺'`, `'\u263a'`, `'☺'`},
133 {0xfffd, `'�'`, `'\ufffd'`, `'�'`},
134 {0x0010ffff, `'\U0010ffff'`, `'\U0010ffff'`, `'\U0010ffff'`},
135 {0x0010ffff + 1, `'�'`, `'\ufffd'`, `'�'`},
136 {0x04, `'\x04'`, `'\x04'`, `'\x04'`},
137 // Some differences between graphic and printable. Note the last column is double-quoted.
138 {'\u00a0', `'\u00a0'`, `'\u00a0'`, "'\u00a0'"},
139 {'\u2000', `'\u2000'`, `'\u2000'`, "'\u2000'"},
140 {'\u3000', `'\u3000'`, `'\u3000'`, "'\u3000'"},
143 func TestQuoteRune(t
*testing
.T
) {
144 for _
, tt
:= range quoterunetests
{
145 if out
:= QuoteRune(tt
.in
); out
!= tt
.out
{
146 t
.Errorf("QuoteRune(%U) = %s, want %s", tt
.in
, out
, tt
.out
)
148 if out
:= AppendQuoteRune([]byte("abc"), tt
.in
); string(out
) != "abc"+tt
.out
{
149 t
.Errorf("AppendQuoteRune(%q, %U) = %s, want %s", "abc", tt
.in
, out
, "abc"+tt
.out
)
154 func TestQuoteRuneToASCII(t
*testing
.T
) {
155 for _
, tt
:= range quoterunetests
{
156 if out
:= QuoteRuneToASCII(tt
.in
); out
!= tt
.ascii
{
157 t
.Errorf("QuoteRuneToASCII(%U) = %s, want %s", tt
.in
, out
, tt
.ascii
)
159 if out
:= AppendQuoteRuneToASCII([]byte("abc"), tt
.in
); string(out
) != "abc"+tt
.ascii
{
160 t
.Errorf("AppendQuoteRuneToASCII(%q, %U) = %s, want %s", "abc", tt
.in
, out
, "abc"+tt
.ascii
)
165 func TestQuoteRuneToGraphic(t
*testing
.T
) {
166 for _
, tt
:= range quoterunetests
{
167 if out
:= QuoteRuneToGraphic(tt
.in
); out
!= tt
.graphic
{
168 t
.Errorf("QuoteRuneToGraphic(%U) = %s, want %s", tt
.in
, out
, tt
.graphic
)
170 if out
:= AppendQuoteRuneToGraphic([]byte("abc"), tt
.in
); string(out
) != "abc"+tt
.graphic
{
171 t
.Errorf("AppendQuoteRuneToGraphic(%q, %U) = %s, want %s", "abc", tt
.in
, out
, "abc"+tt
.graphic
)
176 type canBackquoteTest
struct {
181 var canbackquotetests
= []canBackquoteTest
{
192 {string(9), true}, // \t
215 {string(0x7F), false},
216 {`' !"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, true},
217 {`0123456789`, true},
218 {`ABCDEFGHIJKLMNOPQRSTUVWXYZ`, true},
219 {`abcdefghijklmnopqrstuvwxyz`, true},
222 {"a\xe0\xa0z", false},
223 {"\ufeffabc", false},
227 func TestCanBackquote(t
*testing
.T
) {
228 for _
, tt
:= range canbackquotetests
{
229 if out
:= CanBackquote(tt
.in
); out
!= tt
.out
{
230 t
.Errorf("CanBackquote(%q) = %v, want %v", tt
.in
, out
, tt
.out
)
235 type unQuoteTest
struct {
240 var unquotetests
= []unQuoteTest
{
245 {`"hello world"`, "hello world"},
248 {`"\u1234"`, "\u1234"},
249 {`"\U00010111"`, "\U00010111"},
250 {`"\U0001011111"`, "\U0001011111"},
251 {`"\a\b\f\n\r\t\v\\\""`, "\a\b\f\n\r\t\v\\\""},
259 {`'\u1234'`, "\u1234"},
260 {`'\U00010111'`, "\U00010111"},
270 {"`hello world`", `hello world`},
280 var misquoted
= []string{
308 func TestUnquote(t
*testing
.T
) {
309 for _
, tt
:= range unquotetests
{
310 if out
, err
:= Unquote(tt
.in
); err
!= nil || out
!= tt
.out
{
311 t
.Errorf("Unquote(%#q) = %q, %v want %q, nil", tt
.in
, out
, err
, tt
.out
)
315 // run the quote tests too, backward
316 for _
, tt
:= range quotetests
{
317 if in
, err
:= Unquote(tt
.out
); in
!= tt
.in
{
318 t
.Errorf("Unquote(%#q) = %q, %v, want %q, nil", tt
.out
, in
, err
, tt
.in
)
322 for _
, s
:= range misquoted
{
323 if out
, err
:= Unquote(s
); out
!= "" || err
!= ErrSyntax
{
324 t
.Errorf("Unquote(%#q) = %q, %v want %q, %v", s
, out
, err
, "", ErrSyntax
)
329 func BenchmarkUnquoteEasy(b
*testing
.B
) {
330 for i
:= 0; i
< b
.N
; i
++ {
331 Unquote(`"Give me a rock, paper and scissors and I will move the world."`)
335 func BenchmarkUnquoteHard(b
*testing
.B
) {
336 for i
:= 0; i
< b
.N
; i
++ {
337 Unquote(`"\x47ive me a \x72ock, \x70aper and \x73cissors and \x49 will move the world."`)