]> git.proxmox.com Git - rustc.git/blob - src/libcoretest/char.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / libcoretest / 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;
12
13 #[test]
14 fn test_is_lowercase() {
15 assert!('a'.is_lowercase());
16 assert!('ö'.is_lowercase());
17 assert!('ß'.is_lowercase());
18 assert!(!'Ü'.is_lowercase());
19 assert!(!'P'.is_lowercase());
20 }
21
22 #[test]
23 fn test_is_uppercase() {
24 assert!(!'h'.is_uppercase());
25 assert!(!'ä'.is_uppercase());
26 assert!(!'ß'.is_uppercase());
27 assert!('Ö'.is_uppercase());
28 assert!('T'.is_uppercase());
29 }
30
31 #[test]
32 fn test_is_whitespace() {
33 assert!(' '.is_whitespace());
34 assert!('\u{2007}'.is_whitespace());
35 assert!('\t'.is_whitespace());
36 assert!('\n'.is_whitespace());
37 assert!(!'a'.is_whitespace());
38 assert!(!'_'.is_whitespace());
39 assert!(!'\u{0}'.is_whitespace());
40 }
41
42 #[test]
43 fn test_to_digit() {
44 assert_eq!('0'.to_digit(10), Some(0));
45 assert_eq!('1'.to_digit(2), Some(1));
46 assert_eq!('2'.to_digit(3), Some(2));
47 assert_eq!('9'.to_digit(10), Some(9));
48 assert_eq!('a'.to_digit(16), Some(10));
49 assert_eq!('A'.to_digit(16), Some(10));
50 assert_eq!('b'.to_digit(16), Some(11));
51 assert_eq!('B'.to_digit(16), Some(11));
52 assert_eq!('z'.to_digit(36), Some(35));
53 assert_eq!('Z'.to_digit(36), Some(35));
54 assert_eq!(' '.to_digit(10), None);
55 assert_eq!('$'.to_digit(36), None);
56 }
57
58 #[test]
59 fn test_to_lowercase() {
60 fn lower(c: char) -> Vec<char> {
61 c.to_lowercase().collect()
62 }
63 assert_eq!(lower('A'), ['a']);
64 assert_eq!(lower('Ö'), ['ö']);
65 assert_eq!(lower('ß'), ['ß']);
66 assert_eq!(lower('Ü'), ['ü']);
67 assert_eq!(lower('💩'), ['💩']);
68 assert_eq!(lower('Σ'), ['σ']);
69 assert_eq!(lower('Τ'), ['τ']);
70 assert_eq!(lower('Ι'), ['ι']);
71 assert_eq!(lower('Γ'), ['γ']);
72 assert_eq!(lower('Μ'), ['μ']);
73 assert_eq!(lower('Α'), ['α']);
74 assert_eq!(lower('Σ'), ['σ']);
75 assert_eq!(lower('Dž'), ['dž']);
76 assert_eq!(lower('fi'), ['fi']);
77 assert_eq!(lower('İ'), ['i', '\u{307}']);
78 }
79
80 #[test]
81 fn test_to_uppercase() {
82 fn upper(c: char) -> Vec<char> {
83 c.to_uppercase().collect()
84 }
85 assert_eq!(upper('a'), ['A']);
86 assert_eq!(upper('ö'), ['Ö']);
87 assert_eq!(upper('ß'), ['S', 'S']); // not ẞ: Latin capital letter sharp s
88 assert_eq!(upper('ü'), ['Ü']);
89 assert_eq!(upper('💩'), ['💩']);
90
91 assert_eq!(upper('σ'), ['Σ']);
92 assert_eq!(upper('τ'), ['Τ']);
93 assert_eq!(upper('ι'), ['Ι']);
94 assert_eq!(upper('γ'), ['Γ']);
95 assert_eq!(upper('μ'), ['Μ']);
96 assert_eq!(upper('α'), ['Α']);
97 assert_eq!(upper('ς'), ['Σ']);
98 assert_eq!(upper('Dž'), ['DŽ']);
99 assert_eq!(upper('fi'), ['F', 'I']);
100 assert_eq!(upper('ᾀ'), ['Ἀ', 'Ι']);
101 }
102
103 #[test]
104 fn test_is_control() {
105 assert!('\u{0}'.is_control());
106 assert!('\u{3}'.is_control());
107 assert!('\u{6}'.is_control());
108 assert!('\u{9}'.is_control());
109 assert!('\u{7f}'.is_control());
110 assert!('\u{92}'.is_control());
111 assert!(!'\u{20}'.is_control());
112 assert!(!'\u{55}'.is_control());
113 assert!(!'\u{68}'.is_control());
114 }
115
116 #[test]
117 fn test_is_digit() {
118 assert!('2'.is_numeric());
119 assert!('7'.is_numeric());
120 assert!(!'c'.is_numeric());
121 assert!(!'i'.is_numeric());
122 assert!(!'z'.is_numeric());
123 assert!(!'Q'.is_numeric());
124 }
125
126 #[test]
127 fn test_escape_default() {
128 fn string(c: char) -> String {
129 c.escape_default().collect()
130 }
131 let s = string('\n');
132 assert_eq!(s, "\\n");
133 let s = string('\r');
134 assert_eq!(s, "\\r");
135 let s = string('\'');
136 assert_eq!(s, "\\'");
137 let s = string('"');
138 assert_eq!(s, "\\\"");
139 let s = string(' ');
140 assert_eq!(s, " ");
141 let s = string('a');
142 assert_eq!(s, "a");
143 let s = string('~');
144 assert_eq!(s, "~");
145 let s = string('\x00');
146 assert_eq!(s, "\\u{0}");
147 let s = string('\x1f');
148 assert_eq!(s, "\\u{1f}");
149 let s = string('\x7f');
150 assert_eq!(s, "\\u{7f}");
151 let s = string('\u{ff}');
152 assert_eq!(s, "\\u{ff}");
153 let s = string('\u{11b}');
154 assert_eq!(s, "\\u{11b}");
155 let s = string('\u{1d4b6}');
156 assert_eq!(s, "\\u{1d4b6}");
157 }
158
159 #[test]
160 fn test_escape_unicode() {
161 fn string(c: char) -> String { c.escape_unicode().collect() }
162
163 let s = string('\x00');
164 assert_eq!(s, "\\u{0}");
165 let s = string('\n');
166 assert_eq!(s, "\\u{a}");
167 let s = string(' ');
168 assert_eq!(s, "\\u{20}");
169 let s = string('a');
170 assert_eq!(s, "\\u{61}");
171 let s = string('\u{11b}');
172 assert_eq!(s, "\\u{11b}");
173 let s = string('\u{1d4b6}');
174 assert_eq!(s, "\\u{1d4b6}");
175 }
176
177 #[test]
178 fn test_encode_utf8() {
179 fn check(input: char, expect: &[u8]) {
180 assert_eq!(input.encode_utf8().as_slice(), expect);
181 for (a, b) in input.encode_utf8().zip(expect) {
182 assert_eq!(a, *b);
183 }
184 }
185
186 check('x', &[0x78]);
187 check('\u{e9}', &[0xc3, 0xa9]);
188 check('\u{a66e}', &[0xea, 0x99, 0xae]);
189 check('\u{1f4a9}', &[0xf0, 0x9f, 0x92, 0xa9]);
190 }
191
192 #[test]
193 fn test_encode_utf16() {
194 fn check(input: char, expect: &[u16]) {
195 assert_eq!(input.encode_utf16().as_slice(), expect);
196 for (a, b) in input.encode_utf16().zip(expect) {
197 assert_eq!(a, *b);
198 }
199 }
200
201 check('x', &[0x0078]);
202 check('\u{e9}', &[0x00e9]);
203 check('\u{a66e}', &[0xa66e]);
204 check('\u{1f4a9}', &[0xd83d, 0xdca9]);
205 }
206
207 #[test]
208 fn test_len_utf16() {
209 assert!('x'.len_utf16() == 1);
210 assert!('\u{e9}'.len_utf16() == 1);
211 assert!('\u{a66e}'.len_utf16() == 1);
212 assert!('\u{1f4a9}'.len_utf16() == 2);
213 }
214
215 #[test]
216 fn test_decode_utf16() {
217 fn check(s: &[u16], expected: &[Result<char, u16>]) {
218 let v = char::decode_utf16(s.iter().cloned())
219 .map(|r| r.map_err(|e| e.unpaired_surrogate()))
220 .collect::<Vec<_>>();
221 assert_eq!(v, expected);
222 }
223 check(&[0xD800, 0x41, 0x42], &[Err(0xD800), Ok('A'), Ok('B')]);
224 check(&[0xD800, 0], &[Err(0xD800), Ok('\0')]);
225 }
226
227 #[test]
228 fn ed_iterator_specializations() {
229 // Check counting
230 assert_eq!('\n'.escape_default().count(), 2);
231 assert_eq!('c'.escape_default().count(), 1);
232 assert_eq!(' '.escape_default().count(), 1);
233 assert_eq!('\\'.escape_default().count(), 2);
234 assert_eq!('\''.escape_default().count(), 2);
235
236 // Check nth
237
238 // Check that OoB is handled correctly
239 assert_eq!('\n'.escape_default().nth(2), None);
240 assert_eq!('c'.escape_default().nth(1), None);
241 assert_eq!(' '.escape_default().nth(1), None);
242 assert_eq!('\\'.escape_default().nth(2), None);
243 assert_eq!('\''.escape_default().nth(2), None);
244
245 // Check the first char
246 assert_eq!('\n'.escape_default().nth(0), Some('\\'));
247 assert_eq!('c'.escape_default().nth(0), Some('c'));
248 assert_eq!(' '.escape_default().nth(0), Some(' '));
249 assert_eq!('\\'.escape_default().nth(0), Some('\\'));
250 assert_eq!('\''.escape_default().nth(0), Some('\\'));
251
252 // Check the second char
253 assert_eq!('\n'.escape_default().nth(1), Some('n'));
254 assert_eq!('\\'.escape_default().nth(1), Some('\\'));
255 assert_eq!('\''.escape_default().nth(1), Some('\''));
256
257 // Check the last char
258 assert_eq!('\n'.escape_default().last(), Some('n'));
259 assert_eq!('c'.escape_default().last(), Some('c'));
260 assert_eq!(' '.escape_default().last(), Some(' '));
261 assert_eq!('\\'.escape_default().last(), Some('\\'));
262 assert_eq!('\''.escape_default().last(), Some('\''));
263 }
264
265