]> git.lizzy.rs Git - rust.git/blob - src/libcore/tests/char.rs
Auto merge of #52712 - oli-obk:const_eval_cleanups, r=RalfJung
[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_numeric() {
152    assert!('2'.is_numeric());
153    assert!('7'.is_numeric());
154    assert!('¾'.is_numeric());
155    assert!(!'c'.is_numeric());
156    assert!(!'i'.is_numeric());
157    assert!(!'z'.is_numeric());
158    assert!(!'Q'.is_numeric());
159 }
160
161 #[test]
162 fn test_escape_debug() {
163     fn string(c: char) -> String {
164         let iter: String = c.escape_debug().collect();
165         let disp: String = c.escape_debug().to_string();
166         assert_eq!(iter, disp);
167         iter
168     }
169     assert_eq!(string('\n'), "\\n");
170     assert_eq!(string('\r'), "\\r");
171     assert_eq!(string('\''), "\\'");
172     assert_eq!(string('"'), "\\\"");
173     assert_eq!(string(' '), " ");
174     assert_eq!(string('a'), "a");
175     assert_eq!(string('~'), "~");
176     assert_eq!(string('é'), "é");
177     assert_eq!(string('文'), "文");
178     assert_eq!(string('\x00'), "\\u{0}");
179     assert_eq!(string('\x1f'), "\\u{1f}");
180     assert_eq!(string('\x7f'), "\\u{7f}");
181     assert_eq!(string('\u{80}'), "\\u{80}");
182     assert_eq!(string('\u{ff}'), "\u{ff}");
183     assert_eq!(string('\u{11b}'), "\u{11b}");
184     assert_eq!(string('\u{1d4b6}'), "\u{1d4b6}");
185     assert_eq!(string('\u{301}'), "\\u{301}");     // combining character
186     assert_eq!(string('\u{200b}'),"\\u{200b}");      // zero width space
187     assert_eq!(string('\u{e000}'), "\\u{e000}");     // private use 1
188     assert_eq!(string('\u{100000}'), "\\u{100000}"); // private use 2
189 }
190
191 #[test]
192 fn test_escape_default() {
193     fn string(c: char) -> String {
194         let iter: String = c.escape_default().collect();
195         let disp: String = c.escape_default().to_string();
196         assert_eq!(iter, disp);
197         iter
198     }
199     assert_eq!(string('\n'), "\\n");
200     assert_eq!(string('\r'), "\\r");
201     assert_eq!(string('\''), "\\'");
202     assert_eq!(string('"'), "\\\"");
203     assert_eq!(string(' '), " ");
204     assert_eq!(string('a'), "a");
205     assert_eq!(string('~'), "~");
206     assert_eq!(string('é'), "\\u{e9}");
207     assert_eq!(string('\x00'), "\\u{0}");
208     assert_eq!(string('\x1f'), "\\u{1f}");
209     assert_eq!(string('\x7f'), "\\u{7f}");
210     assert_eq!(string('\u{80}'), "\\u{80}");
211     assert_eq!(string('\u{ff}'), "\\u{ff}");
212     assert_eq!(string('\u{11b}'), "\\u{11b}");
213     assert_eq!(string('\u{1d4b6}'), "\\u{1d4b6}");
214     assert_eq!(string('\u{200b}'), "\\u{200b}"); // zero width space
215     assert_eq!(string('\u{e000}'), "\\u{e000}"); // private use 1
216     assert_eq!(string('\u{100000}'), "\\u{100000}"); // private use 2
217 }
218
219 #[test]
220 fn test_escape_unicode() {
221     fn string(c: char) -> String {
222         let iter: String = c.escape_unicode().collect();
223         let disp: String = c.escape_unicode().to_string();
224         assert_eq!(iter, disp);
225         iter
226     }
227
228     assert_eq!(string('\x00'), "\\u{0}");
229     assert_eq!(string('\n'), "\\u{a}");
230     assert_eq!(string(' '), "\\u{20}");
231     assert_eq!(string('a'), "\\u{61}");
232     assert_eq!(string('\u{11b}'), "\\u{11b}");
233     assert_eq!(string('\u{1d4b6}'), "\\u{1d4b6}");
234 }
235
236 #[test]
237 fn test_encode_utf8() {
238     fn check(input: char, expect: &[u8]) {
239         let mut buf = [0; 4];
240         let ptr = buf.as_ptr();
241         let s = input.encode_utf8(&mut buf);
242         assert_eq!(s.as_ptr() as usize, ptr as usize);
243         assert!(str::from_utf8(s.as_bytes()).is_ok());
244         assert_eq!(s.as_bytes(), expect);
245     }
246
247     check('x', &[0x78]);
248     check('\u{e9}', &[0xc3, 0xa9]);
249     check('\u{a66e}', &[0xea, 0x99, 0xae]);
250     check('\u{1f4a9}', &[0xf0, 0x9f, 0x92, 0xa9]);
251 }
252
253 #[test]
254 fn test_encode_utf16() {
255     fn check(input: char, expect: &[u16]) {
256         let mut buf = [0; 2];
257         let ptr = buf.as_mut_ptr();
258         let b = input.encode_utf16(&mut buf);
259         assert_eq!(b.as_mut_ptr() as usize, ptr as usize);
260         assert_eq!(b, expect);
261     }
262
263     check('x', &[0x0078]);
264     check('\u{e9}', &[0x00e9]);
265     check('\u{a66e}', &[0xa66e]);
266     check('\u{1f4a9}', &[0xd83d, 0xdca9]);
267 }
268
269 #[test]
270 fn test_len_utf16() {
271     assert!('x'.len_utf16() == 1);
272     assert!('\u{e9}'.len_utf16() == 1);
273     assert!('\u{a66e}'.len_utf16() == 1);
274     assert!('\u{1f4a9}'.len_utf16() == 2);
275 }
276
277 #[test]
278 fn test_decode_utf16() {
279     fn check(s: &[u16], expected: &[Result<char, u16>]) {
280         let v = char::decode_utf16(s.iter().cloned())
281                      .map(|r| r.map_err(|e| e.unpaired_surrogate()))
282                      .collect::<Vec<_>>();
283         assert_eq!(v, expected);
284     }
285     check(&[0xD800, 0x41, 0x42], &[Err(0xD800), Ok('A'), Ok('B')]);
286     check(&[0xD800, 0], &[Err(0xD800), Ok('\0')]);
287 }
288
289 #[test]
290 fn ed_iterator_specializations() {
291     // Check counting
292     assert_eq!('\n'.escape_default().count(), 2);
293     assert_eq!('c'.escape_default().count(), 1);
294     assert_eq!(' '.escape_default().count(), 1);
295     assert_eq!('\\'.escape_default().count(), 2);
296     assert_eq!('\''.escape_default().count(), 2);
297
298     // Check nth
299
300     // Check that OoB is handled correctly
301     assert_eq!('\n'.escape_default().nth(2), None);
302     assert_eq!('c'.escape_default().nth(1), None);
303     assert_eq!(' '.escape_default().nth(1), None);
304     assert_eq!('\\'.escape_default().nth(2), None);
305     assert_eq!('\''.escape_default().nth(2), None);
306
307     // Check the first char
308     assert_eq!('\n'.escape_default().nth(0), Some('\\'));
309     assert_eq!('c'.escape_default().nth(0), Some('c'));
310     assert_eq!(' '.escape_default().nth(0), Some(' '));
311     assert_eq!('\\'.escape_default().nth(0), Some('\\'));
312     assert_eq!('\''.escape_default().nth(0), Some('\\'));
313
314     // Check the second char
315     assert_eq!('\n'.escape_default().nth(1), Some('n'));
316     assert_eq!('\\'.escape_default().nth(1), Some('\\'));
317     assert_eq!('\''.escape_default().nth(1), Some('\''));
318
319     // Check the last char
320     assert_eq!('\n'.escape_default().last(), Some('n'));
321     assert_eq!('c'.escape_default().last(), Some('c'));
322     assert_eq!(' '.escape_default().last(), Some(' '));
323     assert_eq!('\\'.escape_default().last(), Some('\\'));
324     assert_eq!('\''.escape_default().last(), Some('\''));
325 }
326
327 #[test]
328 fn eu_iterator_specializations() {
329     fn check(c: char) {
330         let len = c.escape_unicode().count();
331
332         // Check OoB
333         assert_eq!(c.escape_unicode().nth(len), None);
334
335         // For all possible in-bound offsets
336         let mut iter = c.escape_unicode();
337         for offset in 0..len {
338             // Check last
339             assert_eq!(iter.clone().last(), Some('}'));
340
341             // Check len
342             assert_eq!(iter.len(), len - offset);
343
344             // Check size_hint (= len in ExactSizeIterator)
345             assert_eq!(iter.size_hint(), (iter.len(), Some(iter.len())));
346
347             // Check counting
348             assert_eq!(iter.clone().count(), len - offset);
349
350             // Check nth
351             assert_eq!(c.escape_unicode().nth(offset), iter.next());
352         }
353
354         // Check post-last
355         assert_eq!(iter.clone().last(), None);
356         assert_eq!(iter.clone().count(), 0);
357     }
358
359     check('\u{0}');
360     check('\u{1}');
361     check('\u{12}');
362     check('\u{123}');
363     check('\u{1234}');
364     check('\u{12340}');
365     check('\u{10FFFF}');
366 }