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