]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
1a4d82fc JJ |
10 | |
11 | #[test] | |
12 | fn test_is_lowercase() { | |
13 | assert!('a'.is_lowercase()); | |
14 | assert!('ö'.is_lowercase()); | |
15 | assert!('ß'.is_lowercase()); | |
16 | assert!(!'Ü'.is_lowercase()); | |
17 | assert!(!'P'.is_lowercase()); | |
18 | } | |
19 | ||
20 | #[test] | |
21 | fn test_is_uppercase() { | |
22 | assert!(!'h'.is_uppercase()); | |
23 | assert!(!'ä'.is_uppercase()); | |
24 | assert!(!'ß'.is_uppercase()); | |
25 | assert!('Ö'.is_uppercase()); | |
26 | assert!('T'.is_uppercase()); | |
27 | } | |
28 | ||
29 | #[test] | |
30 | fn test_is_whitespace() { | |
31 | assert!(' '.is_whitespace()); | |
32 | assert!('\u{2007}'.is_whitespace()); | |
33 | assert!('\t'.is_whitespace()); | |
34 | assert!('\n'.is_whitespace()); | |
35 | assert!(!'a'.is_whitespace()); | |
36 | assert!(!'_'.is_whitespace()); | |
37 | assert!(!'\u{0}'.is_whitespace()); | |
38 | } | |
39 | ||
40 | #[test] | |
41 | fn test_to_digit() { | |
85aaf69f SL |
42 | assert_eq!('0'.to_digit(10), Some(0)); |
43 | assert_eq!('1'.to_digit(2), Some(1)); | |
44 | assert_eq!('2'.to_digit(3), Some(2)); | |
45 | assert_eq!('9'.to_digit(10), Some(9)); | |
46 | assert_eq!('a'.to_digit(16), Some(10)); | |
47 | assert_eq!('A'.to_digit(16), Some(10)); | |
48 | assert_eq!('b'.to_digit(16), Some(11)); | |
49 | assert_eq!('B'.to_digit(16), Some(11)); | |
50 | assert_eq!('z'.to_digit(36), Some(35)); | |
51 | assert_eq!('Z'.to_digit(36), Some(35)); | |
52 | assert_eq!(' '.to_digit(10), None); | |
53 | assert_eq!('$'.to_digit(36), None); | |
1a4d82fc JJ |
54 | } |
55 | ||
56 | #[test] | |
57 | fn test_to_lowercase() { | |
c34b1796 AL |
58 | fn lower(c: char) -> char { |
59 | let mut it = c.to_lowercase(); | |
60 | let c = it.next().unwrap(); | |
62682a34 SL |
61 | // As of Unicode version 7.0.0, `SpecialCasing.txt` has no lower-case mapping |
62 | // to multiple code points. | |
c34b1796 AL |
63 | assert!(it.next().is_none()); |
64 | c | |
65 | } | |
66 | assert_eq!(lower('A'), 'a'); | |
67 | assert_eq!(lower('Ö'), 'ö'); | |
68 | assert_eq!(lower('ß'), 'ß'); | |
69 | assert_eq!(lower('Ü'), 'ü'); | |
70 | assert_eq!(lower('💩'), '💩'); | |
71 | assert_eq!(lower('Σ'), 'σ'); | |
72 | assert_eq!(lower('Τ'), 'τ'); | |
73 | assert_eq!(lower('Ι'), 'ι'); | |
74 | assert_eq!(lower('Γ'), 'γ'); | |
75 | assert_eq!(lower('Μ'), 'μ'); | |
76 | assert_eq!(lower('Α'), 'α'); | |
77 | assert_eq!(lower('Σ'), 'σ'); | |
62682a34 SL |
78 | assert_eq!(lower('Dž'), 'dž'); |
79 | assert_eq!(lower('fi'), 'fi'); | |
1a4d82fc JJ |
80 | } |
81 | ||
82 | #[test] | |
83 | fn test_to_uppercase() { | |
62682a34 SL |
84 | fn upper(c: char) -> Vec<char> { |
85 | c.to_uppercase().collect() | |
86 | } | |
87 | assert_eq!(upper('a'), ['A']); | |
88 | assert_eq!(upper('ö'), ['Ö']); | |
89 | assert_eq!(upper('ß'), ['S', 'S']); // not ẞ: Latin capital letter sharp s | |
90 | assert_eq!(upper('ü'), ['Ü']); | |
91 | assert_eq!(upper('💩'), ['💩']); | |
92 | ||
93 | assert_eq!(upper('σ'), ['Σ']); | |
94 | assert_eq!(upper('τ'), ['Τ']); | |
95 | assert_eq!(upper('ι'), ['Ι']); | |
96 | assert_eq!(upper('γ'), ['Γ']); | |
97 | assert_eq!(upper('μ'), ['Μ']); | |
98 | assert_eq!(upper('α'), ['Α']); | |
99 | assert_eq!(upper('ς'), ['Σ']); | |
100 | assert_eq!(upper('Dž'), ['DŽ']); | |
101 | assert_eq!(upper('fi'), ['F', 'I']); | |
102 | assert_eq!(upper('ᾀ'), ['Ἀ', 'Ι']); | |
103 | } | |
104 | ||
105 | #[test] | |
106 | fn test_to_titlecase() { | |
107 | fn title(c: char) -> Vec<char> { | |
108 | c.to_titlecase().collect() | |
c34b1796 | 109 | } |
62682a34 SL |
110 | assert_eq!(title('a'), ['A']); |
111 | assert_eq!(title('ö'), ['Ö']); | |
112 | assert_eq!(title('ß'), ['S', 's']); // not ẞ: Latin capital letter sharp s | |
113 | assert_eq!(title('ü'), ['Ü']); | |
114 | assert_eq!(title('💩'), ['💩']); | |
c34b1796 | 115 | |
62682a34 SL |
116 | assert_eq!(title('σ'), ['Σ']); |
117 | assert_eq!(title('τ'), ['Τ']); | |
118 | assert_eq!(title('ι'), ['Ι']); | |
119 | assert_eq!(title('γ'), ['Γ']); | |
120 | assert_eq!(title('μ'), ['Μ']); | |
121 | assert_eq!(title('α'), ['Α']); | |
122 | assert_eq!(title('ς'), ['Σ']); | |
123 | assert_eq!(title('DŽ'), ['Dž']); | |
124 | assert_eq!(title('fi'), ['F', 'i']); | |
125 | assert_eq!(title('ᾀ'), ['ᾈ']); | |
1a4d82fc JJ |
126 | } |
127 | ||
128 | #[test] | |
129 | fn test_is_control() { | |
130 | assert!('\u{0}'.is_control()); | |
131 | assert!('\u{3}'.is_control()); | |
132 | assert!('\u{6}'.is_control()); | |
133 | assert!('\u{9}'.is_control()); | |
134 | assert!('\u{7f}'.is_control()); | |
135 | assert!('\u{92}'.is_control()); | |
136 | assert!(!'\u{20}'.is_control()); | |
137 | assert!(!'\u{55}'.is_control()); | |
138 | assert!(!'\u{68}'.is_control()); | |
139 | } | |
140 | ||
141 | #[test] | |
142 | fn test_is_digit() { | |
143 | assert!('2'.is_numeric()); | |
144 | assert!('7'.is_numeric()); | |
145 | assert!(!'c'.is_numeric()); | |
146 | assert!(!'i'.is_numeric()); | |
147 | assert!(!'z'.is_numeric()); | |
148 | assert!(!'Q'.is_numeric()); | |
149 | } | |
150 | ||
151 | #[test] | |
152 | fn test_escape_default() { | |
153 | fn string(c: char) -> String { | |
154 | c.escape_default().collect() | |
155 | } | |
156 | let s = string('\n'); | |
157 | assert_eq!(s, "\\n"); | |
158 | let s = string('\r'); | |
159 | assert_eq!(s, "\\r"); | |
160 | let s = string('\''); | |
161 | assert_eq!(s, "\\'"); | |
162 | let s = string('"'); | |
163 | assert_eq!(s, "\\\""); | |
164 | let s = string(' '); | |
165 | assert_eq!(s, " "); | |
166 | let s = string('a'); | |
167 | assert_eq!(s, "a"); | |
168 | let s = string('~'); | |
169 | assert_eq!(s, "~"); | |
170 | let s = string('\x00'); | |
171 | assert_eq!(s, "\\u{0}"); | |
172 | let s = string('\x1f'); | |
173 | assert_eq!(s, "\\u{1f}"); | |
174 | let s = string('\x7f'); | |
175 | assert_eq!(s, "\\u{7f}"); | |
176 | let s = string('\u{ff}'); | |
177 | assert_eq!(s, "\\u{ff}"); | |
178 | let s = string('\u{11b}'); | |
179 | assert_eq!(s, "\\u{11b}"); | |
180 | let s = string('\u{1d4b6}'); | |
181 | assert_eq!(s, "\\u{1d4b6}"); | |
182 | } | |
183 | ||
184 | #[test] | |
185 | fn test_escape_unicode() { | |
186 | fn string(c: char) -> String { c.escape_unicode().collect() } | |
187 | ||
188 | let s = string('\x00'); | |
189 | assert_eq!(s, "\\u{0}"); | |
190 | let s = string('\n'); | |
191 | assert_eq!(s, "\\u{a}"); | |
192 | let s = string(' '); | |
193 | assert_eq!(s, "\\u{20}"); | |
194 | let s = string('a'); | |
195 | assert_eq!(s, "\\u{61}"); | |
196 | let s = string('\u{11b}'); | |
197 | assert_eq!(s, "\\u{11b}"); | |
198 | let s = string('\u{1d4b6}'); | |
199 | assert_eq!(s, "\\u{1d4b6}"); | |
200 | } | |
201 | ||
202 | #[test] | |
203 | fn test_encode_utf8() { | |
204 | fn check(input: char, expect: &[u8]) { | |
c34b1796 | 205 | let mut buf = [0; 4]; |
85aaf69f SL |
206 | let n = input.encode_utf8(&mut buf).unwrap_or(0); |
207 | assert_eq!(&buf[..n], expect); | |
1a4d82fc JJ |
208 | } |
209 | ||
210 | check('x', &[0x78]); | |
211 | check('\u{e9}', &[0xc3, 0xa9]); | |
212 | check('\u{a66e}', &[0xea, 0x99, 0xae]); | |
213 | check('\u{1f4a9}', &[0xf0, 0x9f, 0x92, 0xa9]); | |
214 | } | |
215 | ||
216 | #[test] | |
217 | fn test_encode_utf16() { | |
218 | fn check(input: char, expect: &[u16]) { | |
c34b1796 | 219 | let mut buf = [0; 2]; |
85aaf69f SL |
220 | let n = input.encode_utf16(&mut buf).unwrap_or(0); |
221 | assert_eq!(&buf[..n], expect); | |
1a4d82fc JJ |
222 | } |
223 | ||
224 | check('x', &[0x0078]); | |
225 | check('\u{e9}', &[0x00e9]); | |
226 | check('\u{a66e}', &[0xa66e]); | |
227 | check('\u{1f4a9}', &[0xd83d, 0xdca9]); | |
228 | } | |
229 | ||
230 | #[test] | |
231 | fn test_len_utf16() { | |
232 | assert!('x'.len_utf16() == 1); | |
233 | assert!('\u{e9}'.len_utf16() == 1); | |
234 | assert!('\u{a66e}'.len_utf16() == 1); | |
235 | assert!('\u{1f4a9}'.len_utf16() == 2); | |
236 | } | |
237 | ||
d9579d0f | 238 | #[allow(deprecated)] |
1a4d82fc JJ |
239 | #[test] |
240 | fn test_width() { | |
241 | assert_eq!('\x00'.width(false),Some(0)); | |
242 | assert_eq!('\x00'.width(true),Some(0)); | |
243 | ||
244 | assert_eq!('\x0A'.width(false),None); | |
245 | assert_eq!('\x0A'.width(true),None); | |
246 | ||
247 | assert_eq!('w'.width(false),Some(1)); | |
248 | assert_eq!('w'.width(true),Some(1)); | |
249 | ||
250 | assert_eq!('h'.width(false),Some(2)); | |
251 | assert_eq!('h'.width(true),Some(2)); | |
252 | ||
253 | assert_eq!('\u{AD}'.width(false),Some(1)); | |
254 | assert_eq!('\u{AD}'.width(true),Some(1)); | |
255 | ||
256 | assert_eq!('\u{1160}'.width(false),Some(0)); | |
257 | assert_eq!('\u{1160}'.width(true),Some(0)); | |
258 | ||
259 | assert_eq!('\u{a1}'.width(false),Some(1)); | |
260 | assert_eq!('\u{a1}'.width(true),Some(2)); | |
261 | ||
262 | assert_eq!('\u{300}'.width(false),Some(0)); | |
263 | assert_eq!('\u{300}'.width(true),Some(0)); | |
264 | } |