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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [strconv/] [quote_test.go] - Blame information for rev 774

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

powered by: WebSVN 2.1.0

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