Debian Codegraph @

debian/missing-sources/futf/src/test.rs

raw
1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4 // option. This file may not be copied, modified, or distributed
5 // except according to those terms.
6
7 use super::{Meaning, Byte, classify, decode, all_cont};
8
9 use std::borrow::ToOwned;
10 use std::io::Write;
11 use std_test::Bencher;
12
13 #[test]
14 fn classify_all_bytes() {
15 for n in 0x00..0x80 { assert_eq!(Byte::classify(n), Some(Byte::Ascii)); }
16 for n in 0x80..0xC0 { assert_eq!(Byte::classify(n), Some(Byte::Cont)); }
17 for n in 0xC0..0xE0 { assert_eq!(Byte::classify(n), Some(Byte::Start(2))); }
18 for n in 0xE0..0xF0 { assert_eq!(Byte::classify(n), Some(Byte::Start(3))); }
19 for n in 0xF0..0xF8 { assert_eq!(Byte::classify(n), Some(Byte::Start(4))); }
20 for n in 0xF8..0xFF { assert_eq!(Byte::classify(n), None); }
21 assert_eq!(Byte::classify(0xFF), None);
22 }
23
24 #[test]
25 fn test_all_cont() {
26 assert!(all_cont(b""));
27 assert!(all_cont(b"\x80"));
28 assert!(all_cont(b"\xBF"));
29 assert!(all_cont(b"\x80\xBF\x80\xBF"));
30
31 assert!(!all_cont(b"z"));
32 assert!(!all_cont(b"\xC0\xBF"));
33 assert!(!all_cont(b"\xFF"));
34 assert!(!all_cont(b"\x80\xBFz\x80\xBF"));
35 assert!(!all_cont(b"\x80\xBF\xC0\x80\xBF"));
36 assert!(!all_cont(b"\x80\xBF\xFF\x80\xBF"));
37 assert!(!all_cont(b"\x80\xBF\x80\xBFz"));
38 assert!(!all_cont(b"\x80\xBF\x80\xBF\xC0"));
39 assert!(!all_cont(b"z\x80\xBF\x80\xBF"));
40 assert!(!all_cont(b"\xC0\x80\xBF\x80\xBF"));
41 }
42
43 #[test]
44 fn test_decode() {
45 unsafe {
46 assert_eq!(Some(Meaning::Whole('ő')), decode(b"\xC5\x91"));
47 assert_eq!(Some(Meaning::Whole('\u{a66e}')), decode(b"\xEA\x99\xAE"));
48 assert_eq!(Some(Meaning::Whole('\u{1f4a9}')), decode(b"\xF0\x9F\x92\xA9"));
49 assert_eq!(Some(Meaning::Whole('\u{10ffff}')), decode(b"\xF4\x8F\xBF\xBF"));
50
51 assert_eq!(Some(Meaning::LeadSurrogate(0x0000)), decode(b"\xED\xA0\x80"));
52 assert_eq!(Some(Meaning::LeadSurrogate(0x0001)), decode(b"\xED\xA0\x81"));
53 assert_eq!(Some(Meaning::LeadSurrogate(0x03FE)), decode(b"\xED\xAF\xBE"));
54 assert_eq!(Some(Meaning::LeadSurrogate(0x03FF)), decode(b"\xED\xAF\xBF"));
55
56 assert_eq!(Some(Meaning::TrailSurrogate(0x0000)), decode(b"\xED\xB0\x80"));
57 assert_eq!(Some(Meaning::TrailSurrogate(0x0001)), decode(b"\xED\xB0\x81"));
58 assert_eq!(Some(Meaning::TrailSurrogate(0x03FE)), decode(b"\xED\xBF\xBE"));
59 assert_eq!(Some(Meaning::TrailSurrogate(0x03FF)), decode(b"\xED\xBF\xBF"));
60
61 // The last 4-byte UTF-8 sequence. This would be U+1FFFFF, which is out of
62 // range.
63 assert_eq!(None, decode(b"\xF7\xBF\xBF\xBF"));
64
65 // First otherwise-valid sequence (would be U+110000) that is out of range
66 assert_eq!(None, decode(b"\xF4\x90\x80\x80"));
67
68 // Overlong sequences
69 assert_eq!(None, decode(b"\xC0\x80"));
70 assert_eq!(None, decode(b"\xC1\xBF"));
71 assert_eq!(None, decode(b"\xE0\x80\x80"));
72 assert_eq!(None, decode(b"\xE0\x9F\xBF"));
73 assert_eq!(None, decode(b"\xF0\x80\x80\x80"));
74 assert_eq!(None, decode(b"\xF0\x8F\xBF\xBF"));
75
76 // For not-overlong sequence for each sequence length
77 assert_eq!(Some(Meaning::Whole('\u{80}')), decode(b"\xC2\x80"));
78 assert_eq!(Some(Meaning::Whole('\u{800}')), decode(b"\xE0\xA0\x80"));
79 assert_eq!(Some(Meaning::Whole('\u{10000}')), decode(b"\xF0\x90\x80\x80"));
80 }
81 }
82
83 static JUNK: &'static [u8] = b"\
84 \xf8\x0d\x07\x25\xa6\x7b\x95\xeb\x47\x01\x7f\xee\
85 \x3b\x00\x60\x57\x1d\x9e\x5d\x0a\x0b\x0a\x7c\x75\
86 \x13\xa1\x82\x46\x27\x34\xe9\x52\x61\x0d\xec\x10\
87 \x54\x49\x6e\x54\xdf\x7b\xe1\x31\x8c\x06\x21\x83\
88 \x0f\xb5\x1f\x4c\x6a\x71\x52\x42\x74\xe7\x7b\x50\
89 \x59\x1f\x6a\xd4\xff\x06\x92\x33\xc4\x34\x97\xff\
90 \xcc\xb5\xc4\x00\x7b\xc3\x4a\x7f\x7e\x63\x96\x58\
91 \x51\x63\x21\x54\x53\x2f\x03\x8a\x7d\x41\x79\x98\
92 \x5b\xcb\xb8\x94\x6b\x73\xf3\x0c\x5a\xd7\xc4\x12\
93 \x7a\x2b\x9a\x2e\x67\x62\x2a\x00\x45\x2c\xfe\x7d\
94 \x8d\xd6\x51\x4e\x59\x36\x72\x1b\xae\xaa\x06\xe8\
95 \x71\x1b\x85\xd3\x35\xb5\xbe\x9e\x16\x96\x72\xd8\
96 \x1a\x48\xba\x4d\x55\x4f\x1b\xa2\x77\xfa\x8f\x71\
97 \x58\x7d\x03\x93\xa2\x3a\x76\x51\xda\x48\xe2\x3f\
98 \xeb\x8d\xda\x89\xae\xf7\xbd\x3d\xb6\x37\x97\xca\
99 \x99\xcc\x4a\x8d\x62\x89\x97\xe3\xc0\xd1\x8d\xc1\
100 \x26\x11\xbb\x8d\x53\x61\x4f\x76\x03\x00\x30\xd3\
101 \x5f\x86\x19\x52\x9c\x3e\x99\x8c\xb7\x21\x48\x1c\
102 \x85\xae\xad\xd5\x74\x00\x6c\x3e\xd0\x17\xff\x76\
103 \x5c\x32\xc3\xfb\x24\x99\xd4\x4c\xa4\x1f\x66\x46\
104 \xe7\x2d\x44\x56\x7d\x14\xd9\x76\x91\x37\x2f\xb7\
105 \xcc\x1b\xd3\xc2";
106
107 #[test]
108 fn classify_whole() {
109 assert_eq!(JUNK.len(), 256);
110
111 for &c in &['\0', '\x01', 'o', 'z', 'ő', '\u{2764}',
112 '\u{a66e}', '\u{1f4a9}', '\u{1f685}'] {
113 for idx in 0 .. JUNK.len() - 3 {
114 let mut buf = JUNK.to_owned();
115 let ch = format!("{}", c).into_bytes();
116 (&mut buf[idx..]).write_all(&ch).unwrap();
117
118 for j in 0 .. ch.len() {
119 let class = classify(&buf, idx+j).unwrap();
120 assert_eq!(class.bytes, &*ch);
121 assert_eq!(class.rewind, j);
122 assert_eq!(class.meaning, Meaning::Whole(c));
123 }
124 }
125 }
126 }
127
128 #[test]
129 fn classify_surrogates() {
130 for &(s, b) in &[
131 (Meaning::LeadSurrogate(0x0000), b"\xED\xA0\x80"),
132 (Meaning::LeadSurrogate(0x0001), b"\xED\xA0\x81"),
133 (Meaning::LeadSurrogate(0x03FE), b"\xED\xAF\xBE"),
134 (Meaning::LeadSurrogate(0x03FF), b"\xED\xAF\xBF"),
135
136 (Meaning::TrailSurrogate(0x0000), b"\xED\xB0\x80"),
137 (Meaning::TrailSurrogate(0x0001), b"\xED\xB0\x81"),
138 (Meaning::TrailSurrogate(0x03FE), b"\xED\xBF\xBE"),
139 (Meaning::TrailSurrogate(0x03FF), b"\xED\xBF\xBF"),
140 ] {
141 for idx in 0 .. JUNK.len() - 2 {
142 let mut buf = JUNK.to_owned();
143 (&mut buf[idx..]).write_all(b).unwrap();
144
145 let class = classify(&buf, idx).unwrap();
146 assert_eq!(class.bytes, b);
147 assert_eq!(class.rewind, 0);
148 assert_eq!(class.meaning, s);
149 }
150 }
151 }
152
153 #[test]
154 fn classify_prefix_suffix() {
155 for &c in &['ő', '\u{a66e}', '\u{1f4a9}'] {
156 let ch = format!("{}", c).into_bytes();
157 for pfx in 1 .. ch.len() - 1 {
158 let mut buf = JUNK.to_owned();
159 let buflen = buf.len();
160 (&mut buf[buflen - pfx .. buflen]).write_all(&ch[..pfx]).unwrap();
161 for j in 0 .. pfx {
162 let idx = buflen - 1 - j;
163 let class = classify(&buf, idx).unwrap();
164 assert_eq!(class.bytes, &ch[..pfx]);
165 assert_eq!(class.rewind, pfx - 1 - j);
166 assert_eq!(class.meaning, Meaning::Prefix(ch.len() - pfx));
167 }
168 }
169 for sfx in 1 .. ch.len() - 1 {
170 let ch_bytes = &ch[ch.len() - sfx ..];
171 let mut buf = JUNK.to_owned();
172 (&mut *buf).write_all(ch_bytes).unwrap();
173 for j in 0 .. sfx {
174 let class = classify(&buf, j).unwrap();
175 assert!(ch_bytes.starts_with(class.bytes));
176 assert_eq!(class.rewind, j);
177 assert_eq!(class.meaning, Meaning::Suffix);
178 }
179 }
180 }
181 }
182
183 #[test]
184 fn out_of_bounds() {
185 assert!(classify(b"", 0).is_none());
186 assert!(classify(b"", 7).is_none());
187 assert!(classify(b"aaaaaaa", 7).is_none());
188 }
189
190 #[test]
191 fn malformed() {
192 assert_eq!(None, classify(b"\xFF", 0));
193 assert_eq!(None, classify(b"\xC5\xC5", 0));
194 assert_eq!(None, classify(b"x\x91", 1));
195 assert_eq!(None, classify(b"\x91\x91\x91\x91", 3));
196 assert_eq!(None, classify(b"\x91\x91\x91\x91\x91", 4));
197 assert_eq!(None, classify(b"\xEA\x91\xFF", 1));
198 assert_eq!(None, classify(b"\xF0\x90\x90\xF0", 0));
199 assert_eq!(None, classify(b"\xF0\x90\x90\xF0", 1));
200 assert_eq!(None, classify(b"\xF0\x90\x90\xF0", 2));
201
202 for i in 0..4 {
203 // out of range: U+110000
204 assert_eq!(None, classify(b"\xF4\x90\x80\x80", i));
205
206 // out of range: U+1FFFFF
207 assert_eq!(None, classify(b"\xF7\xBF\xBF\xBF", i));
208
209 // Overlong sequences
210 assert_eq!(None, classify(b"\xC0\x80", i));
211 assert_eq!(None, classify(b"\xC1\xBF", i));
212 assert_eq!(None, classify(b"\xE0\x80\x80", i));
213 assert_eq!(None, classify(b"\xE0\x9F\xBF", i));
214 assert_eq!(None, classify(b"\xF0\x80\x80\x80", i));
215 assert_eq!(None, classify(b"\xF0\x8F\xBF\xBF", i));
216 }
217 }
218
219 static TEXT: &'static str = "
220 All human beings are born free and equal in dignity and rights.
221 They are endowed with reason and conscience and should act
222 towards one another in a spirit of brotherhood.
223
224 Minden emberi lény szabadon születik és egyenlő méltósága és
225 joga van. Az emberek, ésszel és lelkiismerettel bírván,
226 egymással szemben testvéri szellemben kell hogy viseltessenek.
227
228 เราทุกคนเกิดมาอย่างอิสระ เราทุกคนมีความคิดและความเข้าใจเป็นของเราเอง
229 เราทุกคนควรได้รับการปฏิบัติในทางเดียวกัน.
230
231 모든 인간은 태어날 때부터 자유로우며 그 존엄과 권리에 있어
232 동등하다. 인간은 천부적으로 이성과 양심을 부여받았으며 서로
233 형제애의 정신으로 행동하여야 한다.
234
235 ro remna cu se jinzi co zifre je simdu'i be le ry. nilselsi'a
236 .e lei ry. selcru .i ry. se menli gi'e se sezmarde .i .ei
237 jeseki'ubo ry. simyzu'e ta'i le tunba
238
239 ᏂᎦᏓ ᎠᏂᏴᏫ ᏂᎨᎫᏓᎸᎾ ᎠᎴ ᎤᏂᏠᏱ ᎤᎾᏕᎿ ᏚᏳᎧᏛ ᎨᏒᎢ. ᎨᏥᏁᎳ ᎤᎾᏓᏅᏖᏗ ᎠᎴ ᎤᏃᏟᏍᏗ
240 ᎠᎴ ᏌᏊ ᎨᏒ ᏧᏂᎸᏫᏍᏓᏁᏗ ᎠᎾᏟᏅᏢ ᎠᏓᏅᏙ ᎬᏗ.";
241
242 // random
243 static IXES: &'static [usize]
244 = &[778, 156, 87, 604, 1216, 365, 884, 311,
245 469, 515, 709, 162, 871, 206, 634, 442];
246
247 static BOUNDARY: &'static [bool]
248 = &[false, true, true, false, false, true, true, true,
249 true, false, false, true, true, true, false, false];
250
251 #[bench]
252 fn std_utf8_check(b: &mut Bencher) {
253 b.iter(|| {
254 assert!(IXES.iter().zip(BOUNDARY.iter()).all(|(&ix, &expect)| {
255 expect == TEXT.is_char_boundary(ix)
256 }));
257 });
258 }
259
260 // We don't expect to be as fast as is_char_boundary, because we provide more
261 // information. But we shouldn't be tremendously slower, either. A factor of
262 // 5-10 is expected on this text.
263 #[bench]
264 fn futf_check(b: &mut Bencher) {
265 b.iter(|| {
266 assert!(IXES.iter().zip(BOUNDARY.iter()).all(|(&ix, &expect)| {
267 expect == (::classify(TEXT.as_bytes(), ix).unwrap().rewind == 0)
268 }));
269 });
270 }