]> git.lizzy.rs Git - rust.git/blob - src/libcore/tests/ascii.rs
4d43067ad2cf3e7b4467581b0d2e190af19d0a59
[rust.git] / src / libcore / tests / ascii.rs
1 // Copyright 2013-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 core::char::from_u32;
12 use std::ascii::AsciiExt;
13
14 #[test]
15 fn test_is_ascii() {
16     assert!(b"".is_ascii());
17     assert!(b"banana\0\x7F".is_ascii());
18     assert!(b"banana\0\x7F".iter().all(|b| b.is_ascii()));
19     assert!(!b"Vi\xe1\xbb\x87t Nam".is_ascii());
20     assert!(!b"Vi\xe1\xbb\x87t Nam".iter().all(|b| b.is_ascii()));
21     assert!(!b"\xe1\xbb\x87".iter().any(|b| b.is_ascii()));
22
23     assert!("".is_ascii());
24     assert!("banana\0\u{7F}".is_ascii());
25     assert!("banana\0\u{7F}".chars().all(|c| c.is_ascii()));
26     assert!(!"ประเทศไทย中华Việt Nam".chars().all(|c| c.is_ascii()));
27     assert!(!"ประเทศไทย中华ệ ".chars().any(|c| c.is_ascii()));
28 }
29
30 #[test]
31 fn test_to_ascii_uppercase() {
32     assert_eq!("url()URL()uRl()ürl".to_ascii_uppercase(), "URL()URL()URL()üRL");
33     assert_eq!("hıKß".to_ascii_uppercase(), "HıKß");
34
35     for i in 0..501 {
36         let upper = if 'a' as u32 <= i && i <= 'z' as u32 { i + 'A' as u32 - 'a' as u32 }
37                     else { i };
38         assert_eq!((from_u32(i).unwrap()).to_string().to_ascii_uppercase(),
39                    (from_u32(upper).unwrap()).to_string());
40     }
41 }
42
43 #[test]
44 fn test_to_ascii_lowercase() {
45     assert_eq!("url()URL()uRl()Ürl".to_ascii_lowercase(), "url()url()url()Ürl");
46     // Dotted capital I, Kelvin sign, Sharp S.
47     assert_eq!("HİKß".to_ascii_lowercase(), "hİKß");
48
49     for i in 0..501 {
50         let lower = if 'A' as u32 <= i && i <= 'Z' as u32 { i + 'a' as u32 - 'A' as u32 }
51                     else { i };
52         assert_eq!((from_u32(i).unwrap()).to_string().to_ascii_lowercase(),
53                    (from_u32(lower).unwrap()).to_string());
54     }
55 }
56
57 #[test]
58 fn test_make_ascii_lower_case() {
59     macro_rules! test {
60         ($from: expr, $to: expr) => {
61             {
62                 let mut x = $from;
63                 x.make_ascii_lowercase();
64                 assert_eq!(x, $to);
65             }
66         }
67     }
68     test!(b'A', b'a');
69     test!(b'a', b'a');
70     test!(b'!', b'!');
71     test!('A', 'a');
72     test!('À', 'À');
73     test!('a', 'a');
74     test!('!', '!');
75     test!(b"H\xc3\x89".to_vec(), b"h\xc3\x89");
76     test!("HİKß".to_string(), "hİKß");
77 }
78
79
80 #[test]
81 fn test_make_ascii_upper_case() {
82     macro_rules! test {
83         ($from: expr, $to: expr) => {
84             {
85                 let mut x = $from;
86                 x.make_ascii_uppercase();
87                 assert_eq!(x, $to);
88             }
89         }
90     }
91     test!(b'a', b'A');
92     test!(b'A', b'A');
93     test!(b'!', b'!');
94     test!('a', 'A');
95     test!('à', 'à');
96     test!('A', 'A');
97     test!('!', '!');
98     test!(b"h\xc3\xa9".to_vec(), b"H\xc3\xa9");
99     test!("hıKß".to_string(), "HıKß");
100
101     let mut x = "Hello".to_string();
102     x[..3].make_ascii_uppercase();  // Test IndexMut on String.
103     assert_eq!(x, "HELlo")
104 }
105
106 #[test]
107 fn test_eq_ignore_ascii_case() {
108     assert!("url()URL()uRl()Ürl".eq_ignore_ascii_case("url()url()url()Ürl"));
109     assert!(!"Ürl".eq_ignore_ascii_case("ürl"));
110     // Dotted capital I, Kelvin sign, Sharp S.
111     assert!("HİKß".eq_ignore_ascii_case("hİKß"));
112     assert!(!"İ".eq_ignore_ascii_case("i"));
113     assert!(!"K".eq_ignore_ascii_case("k"));
114     assert!(!"ß".eq_ignore_ascii_case("s"));
115
116     for i in 0..501 {
117         let lower = if 'A' as u32 <= i && i <= 'Z' as u32 { i + 'a' as u32 - 'A' as u32 }
118                     else { i };
119         assert!((from_u32(i).unwrap()).to_string().eq_ignore_ascii_case(
120                 &from_u32(lower).unwrap().to_string()));
121     }
122 }
123
124 #[test]
125 fn inference_works() {
126     let x = "a".to_string();
127     x.eq_ignore_ascii_case("A");
128 }
129
130 // Shorthands used by the is_ascii_* tests.
131 macro_rules! assert_all {
132     ($what:ident, $($str:tt),+) => {{
133         $(
134             for b in $str.chars() {
135                 if !b.$what() {
136                     panic!("expected {}({}) but it isn't",
137                            stringify!($what), b);
138                 }
139             }
140             for b in $str.as_bytes().iter() {
141                 if !b.$what() {
142                     panic!("expected {}(0x{:02x})) but it isn't",
143                            stringify!($what), b);
144                 }
145             }
146             assert!($str.$what());
147             assert!($str.as_bytes().$what());
148         )+
149     }};
150     ($what:ident, $($str:tt),+,) => (assert_all!($what,$($str),+))
151 }
152 macro_rules! assert_none {
153     ($what:ident, $($str:tt),+) => {{
154         $(
155             for b in $str.chars() {
156                 if b.$what() {
157                     panic!("expected not-{}({}) but it is",
158                            stringify!($what), b);
159                 }
160             }
161             for b in $str.as_bytes().iter() {
162                 if b.$what() {
163                     panic!("expected not-{}(0x{:02x})) but it is",
164                            stringify!($what), b);
165                 }
166             }
167         )*
168     }};
169     ($what:ident, $($str:tt),+,) => (assert_none!($what,$($str),+))
170 }
171
172 #[test]
173 fn test_is_ascii_alphabetic() {
174     assert_all!(is_ascii_alphabetic,
175         "",
176         "abcdefghijklmnopqrstuvwxyz",
177         "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
178     );
179     assert_none!(is_ascii_alphabetic,
180         "0123456789",
181         "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
182         " \t\n\x0c\r",
183         "\x00\x01\x02\x03\x04\x05\x06\x07",
184         "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
185         "\x10\x11\x12\x13\x14\x15\x16\x17",
186         "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
187         "\x7f",
188     );
189 }
190
191 #[test]
192 fn test_is_ascii_uppercase() {
193     assert_all!(is_ascii_uppercase,
194         "",
195         "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
196     );
197     assert_none!(is_ascii_uppercase,
198         "abcdefghijklmnopqrstuvwxyz",
199         "0123456789",
200         "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
201         " \t\n\x0c\r",
202         "\x00\x01\x02\x03\x04\x05\x06\x07",
203         "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
204         "\x10\x11\x12\x13\x14\x15\x16\x17",
205         "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
206         "\x7f",
207     );
208 }
209
210 #[test]
211 fn test_is_ascii_lowercase() {
212     assert_all!(is_ascii_lowercase,
213         "abcdefghijklmnopqrstuvwxyz",
214     );
215     assert_none!(is_ascii_lowercase,
216         "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
217         "0123456789",
218         "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
219         " \t\n\x0c\r",
220         "\x00\x01\x02\x03\x04\x05\x06\x07",
221         "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
222         "\x10\x11\x12\x13\x14\x15\x16\x17",
223         "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
224         "\x7f",
225     );
226 }
227
228 #[test]
229 fn test_is_ascii_alphanumeric() {
230     assert_all!(is_ascii_alphanumeric,
231         "",
232         "abcdefghijklmnopqrstuvwxyz",
233         "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
234         "0123456789",
235     );
236     assert_none!(is_ascii_alphanumeric,
237         "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
238         " \t\n\x0c\r",
239         "\x00\x01\x02\x03\x04\x05\x06\x07",
240         "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
241         "\x10\x11\x12\x13\x14\x15\x16\x17",
242         "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
243         "\x7f",
244     );
245 }
246
247 #[test]
248 fn test_is_ascii_digit() {
249     assert_all!(is_ascii_digit,
250         "",
251         "0123456789",
252     );
253     assert_none!(is_ascii_digit,
254         "abcdefghijklmnopqrstuvwxyz",
255         "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
256         "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
257         " \t\n\x0c\r",
258         "\x00\x01\x02\x03\x04\x05\x06\x07",
259         "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
260         "\x10\x11\x12\x13\x14\x15\x16\x17",
261         "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
262         "\x7f",
263     );
264 }
265
266 #[test]
267 fn test_is_ascii_hexdigit() {
268     assert_all!(is_ascii_hexdigit,
269         "",
270         "0123456789",
271         "abcdefABCDEF",
272     );
273     assert_none!(is_ascii_hexdigit,
274         "ghijklmnopqrstuvwxyz",
275         "GHIJKLMNOQPRSTUVWXYZ",
276         "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
277         " \t\n\x0c\r",
278         "\x00\x01\x02\x03\x04\x05\x06\x07",
279         "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
280         "\x10\x11\x12\x13\x14\x15\x16\x17",
281         "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
282         "\x7f",
283     );
284 }
285
286 #[test]
287 fn test_is_ascii_punctuation() {
288     assert_all!(is_ascii_punctuation,
289         "",
290         "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
291     );
292     assert_none!(is_ascii_punctuation,
293         "abcdefghijklmnopqrstuvwxyz",
294         "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
295         "0123456789",
296         " \t\n\x0c\r",
297         "\x00\x01\x02\x03\x04\x05\x06\x07",
298         "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
299         "\x10\x11\x12\x13\x14\x15\x16\x17",
300         "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
301         "\x7f",
302     );
303 }
304
305 #[test]
306 fn test_is_ascii_graphic() {
307     assert_all!(is_ascii_graphic,
308         "",
309         "abcdefghijklmnopqrstuvwxyz",
310         "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
311         "0123456789",
312         "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
313     );
314     assert_none!(is_ascii_graphic,
315         " \t\n\x0c\r",
316         "\x00\x01\x02\x03\x04\x05\x06\x07",
317         "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
318         "\x10\x11\x12\x13\x14\x15\x16\x17",
319         "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
320         "\x7f",
321     );
322 }
323
324 #[test]
325 fn test_is_ascii_whitespace() {
326     assert_all!(is_ascii_whitespace,
327         "",
328         " \t\n\x0c\r",
329     );
330     assert_none!(is_ascii_whitespace,
331         "abcdefghijklmnopqrstuvwxyz",
332         "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
333         "0123456789",
334         "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
335         "\x00\x01\x02\x03\x04\x05\x06\x07",
336         "\x08\x0b\x0e\x0f",
337         "\x10\x11\x12\x13\x14\x15\x16\x17",
338         "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
339         "\x7f",
340     );
341 }
342
343 #[test]
344 fn test_is_ascii_control() {
345     assert_all!(is_ascii_control,
346         "",
347         "\x00\x01\x02\x03\x04\x05\x06\x07",
348         "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
349         "\x10\x11\x12\x13\x14\x15\x16\x17",
350         "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
351         "\x7f",
352     );
353     assert_none!(is_ascii_control,
354         "abcdefghijklmnopqrstuvwxyz",
355         "ABCDEFGHIJKLMNOQPRSTUVWXYZ",
356         "0123456789",
357         "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
358         " ",
359     );
360 }