]> git.lizzy.rs Git - rust.git/blob - src/libcore/tests/char.rs
Unify API of `Scalar` and `ScalarMaybeUndef`
[rust.git] / src / libcore / tests / char.rs
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.
10
11 use std::{char,str};
12 use std::convert::TryFrom;
13 use std::str::FromStr;
14
15 #[test]
16 fn test_convert() {
17     assert_eq!(u32::from('a'), 0x61);
18     assert_eq!(char::from(b'\0'), '\0');
19     assert_eq!(char::from(b'a'), 'a');
20     assert_eq!(char::from(b'\xFF'), '\u{FF}');
21     assert_eq!(char::try_from(0_u32), Ok('\0'));
22     assert_eq!(char::try_from(0x61_u32), Ok('a'));
23     assert_eq!(char::try_from(0xD7FF_u32), Ok('\u{D7FF}'));
24     assert!(char::try_from(0xD800_u32).is_err());
25     assert!(char::try_from(0xDFFF_u32).is_err());
26     assert_eq!(char::try_from(0xE000_u32), Ok('\u{E000}'));
27     assert_eq!(char::try_from(0x10FFFF_u32), Ok('\u{10FFFF}'));
28     assert!(char::try_from(0x110000_u32).is_err());
29     assert!(char::try_from(0xFFFF_FFFF_u32).is_err());
30 }
31
32 #[test]
33 fn test_from_str() {
34     assert_eq!(char::from_str("a").unwrap(), 'a');
35     assert_eq!(char::from_str("\0").unwrap(), '\0');
36     assert_eq!(char::from_str("\u{D7FF}").unwrap(), '\u{d7FF}');
37     assert!(char::from_str("").is_err());
38     assert!(char::from_str("abc").is_err());
39 }
40
41 #[test]
42 fn test_is_lowercase() {
43     assert!('a'.is_lowercase());
44     assert!('ö'.is_lowercase());
45     assert!('ß'.is_lowercase());
46     assert!(!'Ü'.is_lowercase());
47     assert!(!'P'.is_lowercase());
48 }
49
50 #[test]
51 fn test_is_uppercase() {
52     assert!(!'h'.is_uppercase());
53     assert!(!'ä'.is_uppercase());
54     assert!(!'ß'.is_uppercase());
55     assert!('Ö'.is_uppercase());
56     assert!('T'.is_uppercase());
57 }
58
59 #[test]
60 fn test_is_whitespace() {
61     assert!(' '.is_whitespace());
62     assert!('\u{2007}'.is_whitespace());
63     assert!('\t'.is_whitespace());
64     assert!('\n'.is_whitespace());
65     assert!(!'a'.is_whitespace());
66     assert!(!'_'.is_whitespace());
67     assert!(!'\u{0}'.is_whitespace());
68 }
69
70 #[test]
71 fn test_to_digit() {
72     assert_eq!('0'.to_digit(10), Some(0));
73     assert_eq!('1'.to_digit(2), Some(1));
74     assert_eq!('2'.to_digit(3), Some(2));
75     assert_eq!('9'.to_digit(10), Some(9));
76     assert_eq!('a'.to_digit(16), Some(10));
77     assert_eq!('A'.to_digit(16), Some(10));
78     assert_eq!('b'.to_digit(16), Some(11));
79     assert_eq!('B'.to_digit(16), Some(11));
80     assert_eq!('z'.to_digit(36), Some(35));
81     assert_eq!('Z'.to_digit(36), Some(35));
82     assert_eq!(' '.to_digit(10), None);
83     assert_eq!('$'.to_digit(36), None);
84 }
85
86 #[test]
87 fn test_to_lowercase() {
88     fn lower(c: char) -> String {
89         let iter: String = c.to_lowercase().collect();
90         let disp: String = c.to_lowercase().to_string();
91         assert_eq!(iter, disp);
92         iter
93     }
94     assert_eq!(lower('A'), "a");
95     assert_eq!(lower('Ö'), "ö");
96     assert_eq!(lower('ß'), "ß");
97     assert_eq!(lower('Ü'), "ü");
98     assert_eq!(lower('💩'), "💩");
99     assert_eq!(lower('Σ'), "σ");
100     assert_eq!(lower('Τ'), "τ");
101     assert_eq!(lower('Ι'), "ι");
102     assert_eq!(lower('Γ'), "γ");
103     assert_eq!(lower('Μ'), "μ");
104     assert_eq!(lower('Α'), "α");
105     assert_eq!(lower('Σ'), "σ");
106     assert_eq!(lower('Dž'), "dž");
107     assert_eq!(lower('fi'), "fi");
108     assert_eq!(lower('İ'), "i\u{307}");
109 }
110
111 #[test]
112 fn test_to_uppercase() {
113     fn upper(c: char) -> String {
114         let iter: String = c.to_uppercase().collect();
115         let disp: String = c.to_uppercase().to_string();
116         assert_eq!(iter, disp);
117         iter
118     }
119     assert_eq!(upper('a'), "A");
120     assert_eq!(upper('ö'), "Ö");
121     assert_eq!(upper('ß'), "SS"); // not ẞ: Latin capital letter sharp s
122     assert_eq!(upper('ü'), "Ü");
123     assert_eq!(upper('💩'), "💩");
124
125     assert_eq!(upper('σ'), "Σ");
126     assert_eq!(upper('τ'), "Τ");
127     assert_eq!(upper('ι'), "Ι");
128     assert_eq!(upper('γ'), "Γ");
129     assert_eq!(upper('μ'), "Μ");
130     assert_eq!(upper('α'), "Α");
131     assert_eq!(upper('ς'), "Σ");
132     assert_eq!(upper('Dž'), "DŽ");
133     assert_eq!(upper('fi'), "FI");
134     assert_eq!(upper('ᾀ'), "ἈΙ");
135 }
136
137 #[test]
138 fn test_is_control() {
139     assert!('\u{0}'.is_control());
140     assert!('\u{3}'.is_control());
141     assert!('\u{6}'.is_control());
142     assert!('\u{9}'.is_control());
143     assert!('\u{7f}'.is_control());
144     assert!('\u{92}'.is_control());
145     assert!(!'\u{20}'.is_control());
146     assert!(!'\u{55}'.is_control());
147     assert!(!'\u{68}'.is_control());
148 }
149
150 #[test]
151 fn test_is_digit() {
152    assert!('2'.is_numeric());
153    assert!('7'.is_numeric());
154    assert!(!'c'.is_numeric());
155    assert!(!'i'.is_numeric());
156    assert!(!'z'.is_numeric());
157    assert!(!'Q'.is_numeric());
158 }
159
160 #[test]
161 fn test_escape_debug() {
162     fn string(c: char) -> String {
163         let iter: String = c.escape_debug().collect();
164         let disp: String = c.escape_debug().to_string();
165         assert_eq!(iter, disp);
166         iter
167     }
168     assert_eq!(string('\n'), "\\n");
169     assert_eq!(string('\r'), "\\r");
170     assert_eq!(string('\''), "\\'");
171     assert_eq!(string('"'), "\\\"");
172     assert_eq!(string(' '), " ");
173     assert_eq!(string('a'), "a");
174     assert_eq!(string('~'), "~");
175     assert_eq!(string('é'), "é");
176     assert_eq!(string('文'), "文");
177     assert_eq!(string('\x00'), "\\u{0}");
178     assert_eq!(string('\x1f'), "\\u{1f}");
179     assert_eq!(string('\x7f'), "\\u{7f}");
180     assert_eq!(string('\u{80}'), "\\u{80}");
181     assert_eq!(string('\u{ff}'), "\u{ff}");
182     assert_eq!(string('\u{11b}'), "\u{11b}");
183     assert_eq!(string('\u{1d4b6}'), "\u{1d4b6}");
184     assert_eq!(string('\u{301}'), "\\u{301}");     // combining character
185     assert_eq!(string('\u{200b}'),"\\u{200b}");      // zero width space
186     assert_eq!(string('\u{e000}'), "\\u{e000}");     // private use 1
187     assert_eq!(string('\u{100000}'), "\\u{100000}"); // private use 2
188 }
189
190 #[test]
191 fn test_escape_default() {
192     fn string(c: char) -> String {
193         let iter: String = c.escape_default().collect();
194         let disp: String = c.escape_default().to_string();
195         assert_eq!(iter, disp);
196         iter
197     }
198     assert_eq!(string('\n'), "\\n");
199     assert_eq!(string('\r'), "\\r");
200     assert_eq!(string('\''), "\\'");
201     assert_eq!(string('"'), "\\\"");
202     assert_eq!(string(' '), " ");
203     assert_eq!(string('a'), "a");
204     assert_eq!(string('~'), "~");
205     assert_eq!(string('é'), "\\u{e9}");
206     assert_eq!(string('\x00'), "\\u{0}");
207     assert_eq!(string('\x1f'), "\\u{1f}");
208     assert_eq!(string('\x7f'), "\\u{7f}");
209     assert_eq!(string('\u{80}'), "\\u{80}");
210     assert_eq!(string('\u{ff}'), "\\u{ff}");
211     assert_eq!(string('\u{11b}'), "\\u{11b}");
212     assert_eq!(string('\u{1d4b6}'), "\\u{1d4b6}");
213     assert_eq!(string('\u{200b}'), "\\u{200b}"); // zero width space
214     assert_eq!(string('\u{e000}'), "\\u{e000}"); // private use 1
215     assert_eq!(string('\u{100000}'), "\\u{100000}"); // private use 2
216 }
217
218 #[test]
219 fn test_escape_unicode() {
220     fn string(c: char) -> String {
221         let iter: String = c.escape_unicode().collect();
222         let disp: String = c.escape_unicode().to_string();
223         assert_eq!(iter, disp);
224         iter
225     }
226
227     assert_eq!(string('\x00'), "\\u{0}");
228     assert_eq!(string('\n'), "\\u{a}");
229     assert_eq!(string(' '), "\\u{20}");
230     assert_eq!(string('a'), "\\u{61}");
231     assert_eq!(string('\u{11b}'), "\\u{11b}");
232     assert_eq!(string('\u{1d4b6}'), "\\u{1d4b6}");
233 }
234
235 #[test]
236 fn test_encode_utf8() {
237     fn check(input: char, expect: &[u8]) {
238         let mut buf = [0; 4];
239         let ptr = buf.as_ptr();
240         let s = input.encode_utf8(&mut buf);
241         assert_eq!(s.as_ptr() as usize, ptr as usize);
242         assert!(str::from_utf8(s.as_bytes()).is_ok());
243         assert_eq!(s.as_bytes(), expect);
244     }
245
246     check('x', &[0x78]);
247     check('\u{e9}', &[0xc3, 0xa9]);
248     check('\u{a66e}', &[0xea, 0x99, 0xae]);
249     check('\u{1f4a9}', &[0xf0, 0x9f, 0x92, 0xa9]);
250 }
251
252 #[test]
253 fn test_encode_utf16() {
254     fn check(input: char, expect: &[u16]) {
255         let mut buf = [0; 2];
256         let ptr = buf.as_mut_ptr();
257         let b = input.encode_utf16(&mut buf);
258         assert_eq!(b.as_mut_ptr() as usize, ptr as usize);
259         assert_eq!(b, expect);
260     }
261
262     check('x', &[0x0078]);
263     check('\u{e9}', &[0x00e9]);
264     check('\u{a66e}', &[0xa66e]);
265     check('\u{1f4a9}', &[0xd83d, 0xdca9]);
266 }
267
268 #[test]
269 fn test_len_utf16() {
270     assert!('x'.len_utf16() == 1);
271     assert!('\u{e9}'.len_utf16() == 1);
272     assert!('\u{a66e}'.len_utf16() == 1);
273     assert!('\u{1f4a9}'.len_utf16() == 2);
274 }
275
276 #[test]
277 fn test_decode_utf16() {
278     fn check(s: &[u16], expected: &[Result<char, u16>]) {
279         let v = char::decode_utf16(s.iter().cloned())
280                      .map(|r| r.map_err(|e| e.unpaired_surrogate()))
281                      .collect::<Vec<_>>();
282         assert_eq!(v, expected);
283     }
284     check(&[0xD800, 0x41, 0x42], &[Err(0xD800), Ok('A'), Ok('B')]);
285     check(&[0xD800, 0], &[Err(0xD800), Ok('\0')]);
286 }
287
288 #[test]
289 fn ed_iterator_specializations() {
290     // Check counting
291     assert_eq!('\n'.escape_default().count(), 2);
292     assert_eq!('c'.escape_default().count(), 1);
293     assert_eq!(' '.escape_default().count(), 1);
294     assert_eq!('\\'.escape_default().count(), 2);
295     assert_eq!('\''.escape_default().count(), 2);
296
297     // Check nth
298
299     // Check that OoB is handled correctly
300     assert_eq!('\n'.escape_default().nth(2), None);
301     assert_eq!('c'.escape_default().nth(1), None);
302     assert_eq!(' '.escape_default().nth(1), None);
303     assert_eq!('\\'.escape_default().nth(2), None);
304     assert_eq!('\''.escape_default().nth(2), None);
305
306     // Check the first char
307     assert_eq!('\n'.escape_default().nth(0), Some('\\'));
308     assert_eq!('c'.escape_default().nth(0), Some('c'));
309     assert_eq!(' '.escape_default().nth(0), Some(' '));
310     assert_eq!('\\'.escape_default().nth(0), Some('\\'));
311     assert_eq!('\''.escape_default().nth(0), Some('\\'));
312
313     // Check the second char
314     assert_eq!('\n'.escape_default().nth(1), Some('n'));
315     assert_eq!('\\'.escape_default().nth(1), Some('\\'));
316     assert_eq!('\''.escape_default().nth(1), Some('\''));
317
318     // Check the last char
319     assert_eq!('\n'.escape_default().last(), Some('n'));
320     assert_eq!('c'.escape_default().last(), Some('c'));
321     assert_eq!(' '.escape_default().last(), Some(' '));
322     assert_eq!('\\'.escape_default().last(), Some('\\'));
323     assert_eq!('\''.escape_default().last(), Some('\''));
324 }
325
326 #[test]
327 fn eu_iterator_specializations() {
328     fn check(c: char) {
329         let len = c.escape_unicode().count();
330
331         // Check OoB
332         assert_eq!(c.escape_unicode().nth(len), None);
333
334         // For all possible in-bound offsets
335         let mut iter = c.escape_unicode();
336         for offset in 0..len {
337             // Check last
338             assert_eq!(iter.clone().last(), Some('}'));
339
340             // Check len
341             assert_eq!(iter.len(), len - offset);
342
343             // Check size_hint (= len in ExactSizeIterator)
344             assert_eq!(iter.size_hint(), (iter.len(), Some(iter.len())));
345
346             // Check counting
347             assert_eq!(iter.clone().count(), len - offset);
348
349             // Check nth
350             assert_eq!(c.escape_unicode().nth(offset), iter.next());
351         }
352
353         // Check post-last
354         assert_eq!(iter.clone().last(), None);
355         assert_eq!(iter.clone().count(), 0);
356     }
357
358     check('\u{0}');
359     check('\u{1}');
360     check('\u{12}');
361     check('\u{123}');
362     check('\u{1234}');
363     check('\u{12340}');
364     check('\u{10FFFF}');
365 }