]> git.proxmox.com Git - rustc.git/blob - src/libcoretest/char.rs
Imported Upstream version 1.1.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 #[test]
12 fn test_is_lowercase() {
13 assert!('a'.is_lowercase());
14 assert!('ö'.is_lowercase());
15 assert!('ß'.is_lowercase());
16 assert!(!'Ü'.is_lowercase());
17 assert!(!'P'.is_lowercase());
18 }
19
20 #[test]
21 fn test_is_uppercase() {
22 assert!(!'h'.is_uppercase());
23 assert!(!'ä'.is_uppercase());
24 assert!(!'ß'.is_uppercase());
25 assert!('Ö'.is_uppercase());
26 assert!('T'.is_uppercase());
27 }
28
29 #[test]
30 fn test_is_whitespace() {
31 assert!(' '.is_whitespace());
32 assert!('\u{2007}'.is_whitespace());
33 assert!('\t'.is_whitespace());
34 assert!('\n'.is_whitespace());
35 assert!(!'a'.is_whitespace());
36 assert!(!'_'.is_whitespace());
37 assert!(!'\u{0}'.is_whitespace());
38 }
39
40 #[test]
41 fn test_to_digit() {
42 assert_eq!('0'.to_digit(10), Some(0));
43 assert_eq!('1'.to_digit(2), Some(1));
44 assert_eq!('2'.to_digit(3), Some(2));
45 assert_eq!('9'.to_digit(10), Some(9));
46 assert_eq!('a'.to_digit(16), Some(10));
47 assert_eq!('A'.to_digit(16), Some(10));
48 assert_eq!('b'.to_digit(16), Some(11));
49 assert_eq!('B'.to_digit(16), Some(11));
50 assert_eq!('z'.to_digit(36), Some(35));
51 assert_eq!('Z'.to_digit(36), Some(35));
52 assert_eq!(' '.to_digit(10), None);
53 assert_eq!('$'.to_digit(36), None);
54 }
55
56 #[test]
57 fn test_to_lowercase() {
58 fn lower(c: char) -> char {
59 let mut it = c.to_lowercase();
60 let c = it.next().unwrap();
61 assert!(it.next().is_none());
62 c
63 }
64 assert_eq!(lower('A'), 'a');
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('Σ'), 'σ');
76 }
77
78 #[test]
79 fn test_to_uppercase() {
80 fn upper(c: char) -> char {
81 let mut it = c.to_uppercase();
82 let c = it.next().unwrap();
83 assert!(it.next().is_none());
84 c
85 }
86 assert_eq!(upper('a'), 'A');
87 assert_eq!(upper('ö'), 'Ö');
88 assert_eq!(upper('ß'), 'ß'); // not ẞ: Latin capital letter sharp s
89 assert_eq!(upper('ü'), 'Ü');
90 assert_eq!(upper('💩'), '💩');
91
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('ς'), 'Σ');
99 }
100
101 #[test]
102 fn test_is_control() {
103 assert!('\u{0}'.is_control());
104 assert!('\u{3}'.is_control());
105 assert!('\u{6}'.is_control());
106 assert!('\u{9}'.is_control());
107 assert!('\u{7f}'.is_control());
108 assert!('\u{92}'.is_control());
109 assert!(!'\u{20}'.is_control());
110 assert!(!'\u{55}'.is_control());
111 assert!(!'\u{68}'.is_control());
112 }
113
114 #[test]
115 fn test_is_digit() {
116 assert!('2'.is_numeric());
117 assert!('7'.is_numeric());
118 assert!(!'c'.is_numeric());
119 assert!(!'i'.is_numeric());
120 assert!(!'z'.is_numeric());
121 assert!(!'Q'.is_numeric());
122 }
123
124 #[test]
125 fn test_escape_default() {
126 fn string(c: char) -> String {
127 c.escape_default().collect()
128 }
129 let s = string('\n');
130 assert_eq!(s, "\\n");
131 let s = string('\r');
132 assert_eq!(s, "\\r");
133 let s = string('\'');
134 assert_eq!(s, "\\'");
135 let s = string('"');
136 assert_eq!(s, "\\\"");
137 let s = string(' ');
138 assert_eq!(s, " ");
139 let s = string('a');
140 assert_eq!(s, "a");
141 let s = string('~');
142 assert_eq!(s, "~");
143 let s = string('\x00');
144 assert_eq!(s, "\\u{0}");
145 let s = string('\x1f');
146 assert_eq!(s, "\\u{1f}");
147 let s = string('\x7f');
148 assert_eq!(s, "\\u{7f}");
149 let s = string('\u{ff}');
150 assert_eq!(s, "\\u{ff}");
151 let s = string('\u{11b}');
152 assert_eq!(s, "\\u{11b}");
153 let s = string('\u{1d4b6}');
154 assert_eq!(s, "\\u{1d4b6}");
155 }
156
157 #[test]
158 fn test_escape_unicode() {
159 fn string(c: char) -> String { c.escape_unicode().collect() }
160
161 let s = string('\x00');
162 assert_eq!(s, "\\u{0}");
163 let s = string('\n');
164 assert_eq!(s, "\\u{a}");
165 let s = string(' ');
166 assert_eq!(s, "\\u{20}");
167 let s = string('a');
168 assert_eq!(s, "\\u{61}");
169 let s = string('\u{11b}');
170 assert_eq!(s, "\\u{11b}");
171 let s = string('\u{1d4b6}');
172 assert_eq!(s, "\\u{1d4b6}");
173 }
174
175 #[test]
176 fn test_encode_utf8() {
177 fn check(input: char, expect: &[u8]) {
178 let mut buf = [0; 4];
179 let n = input.encode_utf8(&mut buf).unwrap_or(0);
180 assert_eq!(&buf[..n], expect);
181 }
182
183 check('x', &[0x78]);
184 check('\u{e9}', &[0xc3, 0xa9]);
185 check('\u{a66e}', &[0xea, 0x99, 0xae]);
186 check('\u{1f4a9}', &[0xf0, 0x9f, 0x92, 0xa9]);
187 }
188
189 #[test]
190 fn test_encode_utf16() {
191 fn check(input: char, expect: &[u16]) {
192 let mut buf = [0; 2];
193 let n = input.encode_utf16(&mut buf).unwrap_or(0);
194 assert_eq!(&buf[..n], expect);
195 }
196
197 check('x', &[0x0078]);
198 check('\u{e9}', &[0x00e9]);
199 check('\u{a66e}', &[0xa66e]);
200 check('\u{1f4a9}', &[0xd83d, 0xdca9]);
201 }
202
203 #[test]
204 fn test_len_utf16() {
205 assert!('x'.len_utf16() == 1);
206 assert!('\u{e9}'.len_utf16() == 1);
207 assert!('\u{a66e}'.len_utf16() == 1);
208 assert!('\u{1f4a9}'.len_utf16() == 2);
209 }
210
211 #[allow(deprecated)]
212 #[test]
213 fn test_width() {
214 assert_eq!('\x00'.width(false),Some(0));
215 assert_eq!('\x00'.width(true),Some(0));
216
217 assert_eq!('\x0A'.width(false),None);
218 assert_eq!('\x0A'.width(true),None);
219
220 assert_eq!('w'.width(false),Some(1));
221 assert_eq!('w'.width(true),Some(1));
222
223 assert_eq!('h'.width(false),Some(2));
224 assert_eq!('h'.width(true),Some(2));
225
226 assert_eq!('\u{AD}'.width(false),Some(1));
227 assert_eq!('\u{AD}'.width(true),Some(1));
228
229 assert_eq!('\u{1160}'.width(false),Some(0));
230 assert_eq!('\u{1160}'.width(true),Some(0));
231
232 assert_eq!('\u{a1}'.width(false),Some(1));
233 assert_eq!('\u{a1}'.width(true),Some(2));
234
235 assert_eq!('\u{300}'.width(false),Some(0));
236 assert_eq!('\u{300}'.width(true),Some(0));
237 }