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