]> git.lizzy.rs Git - rust.git/blob - src/libcoretest/char.rs
Merge pull request #20510 from tshepang/patch-6
[rust.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 // ignore-lexer-test FIXME #15679
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(10u), Some(0u));
45     assert_eq!('1'.to_digit(2u), Some(1u));
46     assert_eq!('2'.to_digit(3u), Some(2u));
47     assert_eq!('9'.to_digit(10u), Some(9u));
48     assert_eq!('a'.to_digit(16u), Some(10u));
49     assert_eq!('A'.to_digit(16u), Some(10u));
50     assert_eq!('b'.to_digit(16u), Some(11u));
51     assert_eq!('B'.to_digit(16u), Some(11u));
52     assert_eq!('z'.to_digit(36u), Some(35u));
53     assert_eq!('Z'.to_digit(36u), Some(35u));
54     assert_eq!(' '.to_digit(10u), None);
55     assert_eq!('$'.to_digit(36u), None);
56 }
57
58 #[test]
59 fn test_to_lowercase() {
60     assert_eq!('A'.to_lowercase(), 'a');
61     assert_eq!('Ö'.to_lowercase(), 'ö');
62     assert_eq!('ß'.to_lowercase(), 'ß');
63     assert_eq!('Ü'.to_lowercase(), 'ü');
64     assert_eq!('💩'.to_lowercase(), '💩');
65     assert_eq!('Σ'.to_lowercase(), 'σ');
66     assert_eq!('Τ'.to_lowercase(), 'τ');
67     assert_eq!('Ι'.to_lowercase(), 'ι');
68     assert_eq!('Γ'.to_lowercase(), 'γ');
69     assert_eq!('Μ'.to_lowercase(), 'μ');
70     assert_eq!('Α'.to_lowercase(), 'α');
71     assert_eq!('Σ'.to_lowercase(), 'σ');
72 }
73
74 #[test]
75 fn test_to_uppercase() {
76     assert_eq!('a'.to_uppercase(), 'A');
77     assert_eq!('ö'.to_uppercase(), 'Ö');
78     assert_eq!('ß'.to_uppercase(), 'ß'); // not ẞ: Latin capital letter sharp s
79     assert_eq!('ü'.to_uppercase(), 'Ü');
80     assert_eq!('💩'.to_uppercase(), '💩');
81
82     assert_eq!('σ'.to_uppercase(), 'Σ');
83     assert_eq!('τ'.to_uppercase(), 'Τ');
84     assert_eq!('ι'.to_uppercase(), 'Ι');
85     assert_eq!('γ'.to_uppercase(), 'Γ');
86     assert_eq!('μ'.to_uppercase(), 'Μ');
87     assert_eq!('α'.to_uppercase(), 'Α');
88     assert_eq!('ς'.to_uppercase(), 'Σ');
89 }
90
91 #[test]
92 fn test_is_control() {
93     assert!('\u{0}'.is_control());
94     assert!('\u{3}'.is_control());
95     assert!('\u{6}'.is_control());
96     assert!('\u{9}'.is_control());
97     assert!('\u{7f}'.is_control());
98     assert!('\u{92}'.is_control());
99     assert!(!'\u{20}'.is_control());
100     assert!(!'\u{55}'.is_control());
101     assert!(!'\u{68}'.is_control());
102 }
103
104 #[test]
105 fn test_is_digit() {
106    assert!('2'.is_numeric());
107    assert!('7'.is_numeric());
108    assert!(!'c'.is_numeric());
109    assert!(!'i'.is_numeric());
110    assert!(!'z'.is_numeric());
111    assert!(!'Q'.is_numeric());
112 }
113
114 #[test]
115 fn test_escape_default() {
116     fn string(c: char) -> String {
117         c.escape_default().collect()
118     }
119     let s = string('\n');
120     assert_eq!(s, "\\n");
121     let s = string('\r');
122     assert_eq!(s, "\\r");
123     let s = string('\'');
124     assert_eq!(s, "\\'");
125     let s = string('"');
126     assert_eq!(s, "\\\"");
127     let s = string(' ');
128     assert_eq!(s, " ");
129     let s = string('a');
130     assert_eq!(s, "a");
131     let s = string('~');
132     assert_eq!(s, "~");
133     let s = string('\x00');
134     assert_eq!(s, "\\u{0}");
135     let s = string('\x1f');
136     assert_eq!(s, "\\u{1f}");
137     let s = string('\x7f');
138     assert_eq!(s, "\\u{7f}");
139     let s = string('\u{ff}');
140     assert_eq!(s, "\\u{ff}");
141     let s = string('\u{11b}');
142     assert_eq!(s, "\\u{11b}");
143     let s = string('\u{1d4b6}');
144     assert_eq!(s, "\\u{1d4b6}");
145 }
146
147 #[test]
148 fn test_escape_unicode() {
149     fn string(c: char) -> String { c.escape_unicode().collect() }
150
151     let s = string('\x00');
152     assert_eq!(s, "\\u{0}");
153     let s = string('\n');
154     assert_eq!(s, "\\u{a}");
155     let s = string(' ');
156     assert_eq!(s, "\\u{20}");
157     let s = string('a');
158     assert_eq!(s, "\\u{61}");
159     let s = string('\u{11b}');
160     assert_eq!(s, "\\u{11b}");
161     let s = string('\u{1d4b6}');
162     assert_eq!(s, "\\u{1d4b6}");
163 }
164
165 #[test]
166 fn test_encode_utf8() {
167     fn check(input: char, expect: &[u8]) {
168         let mut buf = [0u8; 4];
169         let n = input.encode_utf8(buf.as_mut_slice()).unwrap_or(0);
170         assert_eq!(buf[..n], expect);
171     }
172
173     check('x', &[0x78]);
174     check('\u{e9}', &[0xc3, 0xa9]);
175     check('\u{a66e}', &[0xea, 0x99, 0xae]);
176     check('\u{1f4a9}', &[0xf0, 0x9f, 0x92, 0xa9]);
177 }
178
179 #[test]
180 fn test_encode_utf16() {
181     fn check(input: char, expect: &[u16]) {
182         let mut buf = [0u16; 2];
183         let n = input.encode_utf16(buf.as_mut_slice()).unwrap_or(0);
184         assert_eq!(buf[..n], expect);
185     }
186
187     check('x', &[0x0078]);
188     check('\u{e9}', &[0x00e9]);
189     check('\u{a66e}', &[0xa66e]);
190     check('\u{1f4a9}', &[0xd83d, 0xdca9]);
191 }
192
193 #[test]
194 fn test_len_utf16() {
195     assert!('x'.len_utf16() == 1);
196     assert!('\u{e9}'.len_utf16() == 1);
197     assert!('\u{a66e}'.len_utf16() == 1);
198     assert!('\u{1f4a9}'.len_utf16() == 2);
199 }
200
201 #[test]
202 fn test_width() {
203     assert_eq!('\x00'.width(false),Some(0));
204     assert_eq!('\x00'.width(true),Some(0));
205
206     assert_eq!('\x0A'.width(false),None);
207     assert_eq!('\x0A'.width(true),None);
208
209     assert_eq!('w'.width(false),Some(1));
210     assert_eq!('w'.width(true),Some(1));
211
212     assert_eq!('h'.width(false),Some(2));
213     assert_eq!('h'.width(true),Some(2));
214
215     assert_eq!('\u{AD}'.width(false),Some(1));
216     assert_eq!('\u{AD}'.width(true),Some(1));
217
218     assert_eq!('\u{1160}'.width(false),Some(0));
219     assert_eq!('\u{1160}'.width(true),Some(0));
220
221     assert_eq!('\u{a1}'.width(false),Some(1));
222     assert_eq!('\u{a1}'.width(true),Some(2));
223
224     assert_eq!('\u{300}'.width(false),Some(0));
225     assert_eq!('\u{300}'.width(true),Some(0));
226 }