]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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. | |
1a4d82fc JJ |
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() { | |
85aaf69f SL |
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); | |
1a4d82fc JJ |
54 | } |
55 | ||
56 | #[test] | |
57 | fn test_to_lowercase() { | |
c34b1796 AL |
58 | fn lower(c: char) -> char { |
59 | let mut it = c.to_lowercase(); | |
60 | let c = it.next().unwrap(); | |
62682a34 SL |
61 | // As of Unicode version 7.0.0, `SpecialCasing.txt` has no lower-case mapping |
62 | // to multiple code points. | |
c34b1796 AL |
63 | assert!(it.next().is_none()); |
64 | c | |
65 | } | |
66 | assert_eq!(lower('A'), 'a'); | |
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 | assert_eq!(lower('Α'), 'α'); | |
77 | assert_eq!(lower('Σ'), 'σ'); | |
62682a34 SL |
78 | assert_eq!(lower('Dž'), 'dž'); |
79 | assert_eq!(lower('fi'), 'fi'); | |
1a4d82fc JJ |
80 | } |
81 | ||
82 | #[test] | |
83 | fn test_to_uppercase() { | |
62682a34 SL |
84 | fn upper(c: char) -> Vec<char> { |
85 | c.to_uppercase().collect() | |
86 | } | |
87 | assert_eq!(upper('a'), ['A']); | |
88 | assert_eq!(upper('ö'), ['Ö']); | |
89 | assert_eq!(upper('ß'), ['S', 'S']); // not ẞ: Latin capital letter sharp s | |
90 | assert_eq!(upper('ü'), ['Ü']); | |
91 | assert_eq!(upper('💩'), ['💩']); | |
92 | ||
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 | assert_eq!(upper('ς'), ['Σ']); | |
100 | assert_eq!(upper('Dž'), ['DŽ']); | |
101 | assert_eq!(upper('fi'), ['F', 'I']); | |
102 | assert_eq!(upper('ᾀ'), ['Ἀ', 'Ι']); | |
103 | } | |
104 | ||
1a4d82fc JJ |
105 | #[test] |
106 | fn test_is_control() { | |
107 | assert!('\u{0}'.is_control()); | |
108 | assert!('\u{3}'.is_control()); | |
109 | assert!('\u{6}'.is_control()); | |
110 | assert!('\u{9}'.is_control()); | |
111 | assert!('\u{7f}'.is_control()); | |
112 | assert!('\u{92}'.is_control()); | |
113 | assert!(!'\u{20}'.is_control()); | |
114 | assert!(!'\u{55}'.is_control()); | |
115 | assert!(!'\u{68}'.is_control()); | |
116 | } | |
117 | ||
118 | #[test] | |
119 | fn test_is_digit() { | |
120 | assert!('2'.is_numeric()); | |
121 | assert!('7'.is_numeric()); | |
122 | assert!(!'c'.is_numeric()); | |
123 | assert!(!'i'.is_numeric()); | |
124 | assert!(!'z'.is_numeric()); | |
125 | assert!(!'Q'.is_numeric()); | |
126 | } | |
127 | ||
128 | #[test] | |
129 | fn test_escape_default() { | |
130 | fn string(c: char) -> String { | |
131 | c.escape_default().collect() | |
132 | } | |
133 | let s = string('\n'); | |
134 | assert_eq!(s, "\\n"); | |
135 | let s = string('\r'); | |
136 | assert_eq!(s, "\\r"); | |
137 | let s = string('\''); | |
138 | assert_eq!(s, "\\'"); | |
139 | let s = string('"'); | |
140 | assert_eq!(s, "\\\""); | |
141 | let s = string(' '); | |
142 | assert_eq!(s, " "); | |
143 | let s = string('a'); | |
144 | assert_eq!(s, "a"); | |
145 | let s = string('~'); | |
146 | assert_eq!(s, "~"); | |
147 | let s = string('\x00'); | |
148 | assert_eq!(s, "\\u{0}"); | |
149 | let s = string('\x1f'); | |
150 | assert_eq!(s, "\\u{1f}"); | |
151 | let s = string('\x7f'); | |
152 | assert_eq!(s, "\\u{7f}"); | |
153 | let s = string('\u{ff}'); | |
154 | assert_eq!(s, "\\u{ff}"); | |
155 | let s = string('\u{11b}'); | |
156 | assert_eq!(s, "\\u{11b}"); | |
157 | let s = string('\u{1d4b6}'); | |
158 | assert_eq!(s, "\\u{1d4b6}"); | |
159 | } | |
160 | ||
161 | #[test] | |
162 | fn test_escape_unicode() { | |
163 | fn string(c: char) -> String { c.escape_unicode().collect() } | |
164 | ||
165 | let s = string('\x00'); | |
166 | assert_eq!(s, "\\u{0}"); | |
167 | let s = string('\n'); | |
168 | assert_eq!(s, "\\u{a}"); | |
169 | let s = string(' '); | |
170 | assert_eq!(s, "\\u{20}"); | |
171 | let s = string('a'); | |
172 | assert_eq!(s, "\\u{61}"); | |
173 | let s = string('\u{11b}'); | |
174 | assert_eq!(s, "\\u{11b}"); | |
175 | let s = string('\u{1d4b6}'); | |
176 | assert_eq!(s, "\\u{1d4b6}"); | |
177 | } | |
178 | ||
179 | #[test] | |
180 | fn test_encode_utf8() { | |
181 | fn check(input: char, expect: &[u8]) { | |
c34b1796 | 182 | let mut buf = [0; 4]; |
85aaf69f SL |
183 | let n = input.encode_utf8(&mut buf).unwrap_or(0); |
184 | assert_eq!(&buf[..n], expect); | |
1a4d82fc JJ |
185 | } |
186 | ||
187 | check('x', &[0x78]); | |
188 | check('\u{e9}', &[0xc3, 0xa9]); | |
189 | check('\u{a66e}', &[0xea, 0x99, 0xae]); | |
190 | check('\u{1f4a9}', &[0xf0, 0x9f, 0x92, 0xa9]); | |
191 | } | |
192 | ||
193 | #[test] | |
194 | fn test_encode_utf16() { | |
195 | fn check(input: char, expect: &[u16]) { | |
c34b1796 | 196 | let mut buf = [0; 2]; |
85aaf69f SL |
197 | let n = input.encode_utf16(&mut buf).unwrap_or(0); |
198 | assert_eq!(&buf[..n], expect); | |
1a4d82fc JJ |
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 | ||
d9579d0f | 215 | #[allow(deprecated)] |
1a4d82fc JJ |
216 | #[test] |
217 | fn test_width() { | |
218 | assert_eq!('\x00'.width(false),Some(0)); | |
219 | assert_eq!('\x00'.width(true),Some(0)); | |
220 | ||
221 | assert_eq!('\x0A'.width(false),None); | |
222 | assert_eq!('\x0A'.width(true),None); | |
223 | ||
224 | assert_eq!('w'.width(false),Some(1)); | |
225 | assert_eq!('w'.width(true),Some(1)); | |
226 | ||
227 | assert_eq!('h'.width(false),Some(2)); | |
228 | assert_eq!('h'.width(true),Some(2)); | |
229 | ||
230 | assert_eq!('\u{AD}'.width(false),Some(1)); | |
231 | assert_eq!('\u{AD}'.width(true),Some(1)); | |
232 | ||
233 | assert_eq!('\u{1160}'.width(false),Some(0)); | |
234 | assert_eq!('\u{1160}'.width(true),Some(0)); | |
235 | ||
236 | assert_eq!('\u{a1}'.width(false),Some(1)); | |
237 | assert_eq!('\u{a1}'.width(true),Some(2)); | |
238 | ||
239 | assert_eq!('\u{300}'.width(false),Some(0)); | |
240 | assert_eq!('\u{300}'.width(true),Some(0)); | |
241 | } |