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