]> git.proxmox.com Git - rustc.git/blob - src/libcollectionstest/str.rs
New upstream version 1.13.0+dfsg1
[rustc.git] / src / libcollectionstest / str.rs
1 // Copyright 2012-2015 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 use std::borrow::Cow;
12 use std::cmp::Ordering::{Equal, Greater, Less};
13 use std::str::from_utf8;
14
15 #[test]
16 fn test_le() {
17 assert!("" <= "");
18 assert!("" <= "foo");
19 assert!("foo" <= "foo");
20 assert!("foo" != "bar");
21 }
22
23 #[test]
24 fn test_find() {
25 assert_eq!("hello".find('l'), Some(2));
26 assert_eq!("hello".find(|c:char| c == 'o'), Some(4));
27 assert!("hello".find('x').is_none());
28 assert!("hello".find(|c:char| c == 'x').is_none());
29 assert_eq!("ประเทศไทย中华Việt Nam".find('华'), Some(30));
30 assert_eq!("ประเทศไทย中华Việt Nam".find(|c: char| c == '华'), Some(30));
31 }
32
33 #[test]
34 fn test_rfind() {
35 assert_eq!("hello".rfind('l'), Some(3));
36 assert_eq!("hello".rfind(|c:char| c == 'o'), Some(4));
37 assert!("hello".rfind('x').is_none());
38 assert!("hello".rfind(|c:char| c == 'x').is_none());
39 assert_eq!("ประเทศไทย中华Việt Nam".rfind('华'), Some(30));
40 assert_eq!("ประเทศไทย中华Việt Nam".rfind(|c: char| c == '华'), Some(30));
41 }
42
43 #[test]
44 fn test_collect() {
45 let empty = "";
46 let s: String = empty.chars().collect();
47 assert_eq!(empty, s);
48 let data = "ประเทศไทย中";
49 let s: String = data.chars().collect();
50 assert_eq!(data, s);
51 }
52
53 #[test]
54 fn test_into_bytes() {
55 let data = String::from("asdf");
56 let buf = data.into_bytes();
57 assert_eq!(buf, b"asdf");
58 }
59
60 #[test]
61 fn test_find_str() {
62 // byte positions
63 assert_eq!("".find(""), Some(0));
64 assert!("banana".find("apple pie").is_none());
65
66 let data = "abcabc";
67 assert_eq!(data[0..6].find("ab"), Some(0));
68 assert_eq!(data[2..6].find("ab"), Some(3 - 2));
69 assert!(data[2..4].find("ab").is_none());
70
71 let string = "ประเทศไทย中华Việt Nam";
72 let mut data = String::from(string);
73 data.push_str(string);
74 assert!(data.find("ไท华").is_none());
75 assert_eq!(data[0..43].find(""), Some(0));
76 assert_eq!(data[6..43].find(""), Some(6 - 6));
77
78 assert_eq!(data[0..43].find("ประ"), Some( 0));
79 assert_eq!(data[0..43].find("ทศไ"), Some(12));
80 assert_eq!(data[0..43].find("ย中"), Some(24));
81 assert_eq!(data[0..43].find("iệt"), Some(34));
82 assert_eq!(data[0..43].find("Nam"), Some(40));
83
84 assert_eq!(data[43..86].find("ประ"), Some(43 - 43));
85 assert_eq!(data[43..86].find("ทศไ"), Some(55 - 43));
86 assert_eq!(data[43..86].find("ย中"), Some(67 - 43));
87 assert_eq!(data[43..86].find("iệt"), Some(77 - 43));
88 assert_eq!(data[43..86].find("Nam"), Some(83 - 43));
89
90 // find every substring -- assert that it finds it, or an earlier occurrence.
91 let string = "Việt Namacbaabcaabaaba";
92 for (i, ci) in string.char_indices() {
93 let ip = i + ci.len_utf8();
94 for j in string[ip..].char_indices()
95 .map(|(i, _)| i)
96 .chain(Some(string.len() - ip))
97 {
98 let pat = &string[i..ip + j];
99 assert!(match string.find(pat) {
100 None => false,
101 Some(x) => x <= i,
102 });
103 assert!(match string.rfind(pat) {
104 None => false,
105 Some(x) => x >= i,
106 });
107 }
108 }
109 }
110
111 fn s(x: &str) -> String { x.to_string() }
112
113 macro_rules! test_concat {
114 ($expected: expr, $string: expr) => {
115 {
116 let s: String = $string.concat();
117 assert_eq!($expected, s);
118 }
119 }
120 }
121
122 #[test]
123 fn test_concat_for_different_types() {
124 test_concat!("ab", vec![s("a"), s("b")]);
125 test_concat!("ab", vec!["a", "b"]);
126 }
127
128 #[test]
129 fn test_concat_for_different_lengths() {
130 let empty: &[&str] = &[];
131 test_concat!("", empty);
132 test_concat!("a", ["a"]);
133 test_concat!("ab", ["a", "b"]);
134 test_concat!("abc", ["", "a", "bc"]);
135 }
136
137 macro_rules! test_join {
138 ($expected: expr, $string: expr, $delim: expr) => {
139 {
140 let s = $string.join($delim);
141 assert_eq!($expected, s);
142 }
143 }
144 }
145
146 #[test]
147 fn test_join_for_different_types() {
148 test_join!("a-b", ["a", "b"], "-");
149 let hyphen = "-".to_string();
150 test_join!("a-b", [s("a"), s("b")], &*hyphen);
151 test_join!("a-b", vec!["a", "b"], &*hyphen);
152 test_join!("a-b", &*vec!["a", "b"], "-");
153 test_join!("a-b", vec![s("a"), s("b")], "-");
154 }
155
156 #[test]
157 fn test_join_for_different_lengths() {
158 let empty: &[&str] = &[];
159 test_join!("", empty, "-");
160 test_join!("a", ["a"], "-");
161 test_join!("a-b", ["a", "b"], "-");
162 test_join!("-a-bc", ["", "a", "bc"], "-");
163 }
164
165 #[test]
166 fn test_unsafe_slice() {
167 assert_eq!("ab", unsafe {"abc".slice_unchecked(0, 2)});
168 assert_eq!("bc", unsafe {"abc".slice_unchecked(1, 3)});
169 assert_eq!("", unsafe {"abc".slice_unchecked(1, 1)});
170 fn a_million_letter_a() -> String {
171 let mut i = 0;
172 let mut rs = String::new();
173 while i < 100000 {
174 rs.push_str("aaaaaaaaaa");
175 i += 1;
176 }
177 rs
178 }
179 fn half_a_million_letter_a() -> String {
180 let mut i = 0;
181 let mut rs = String::new();
182 while i < 100000 {
183 rs.push_str("aaaaa");
184 i += 1;
185 }
186 rs
187 }
188 let letters = a_million_letter_a();
189 assert_eq!(half_a_million_letter_a(),
190 unsafe { letters.slice_unchecked(0, 500000)});
191 }
192
193 #[test]
194 fn test_starts_with() {
195 assert!("".starts_with(""));
196 assert!("abc".starts_with(""));
197 assert!("abc".starts_with("a"));
198 assert!(!"a".starts_with("abc"));
199 assert!(!"".starts_with("abc"));
200 assert!(!"ödd".starts_with("-"));
201 assert!("ödd".starts_with("öd"));
202 }
203
204 #[test]
205 fn test_ends_with() {
206 assert!("".ends_with(""));
207 assert!("abc".ends_with(""));
208 assert!("abc".ends_with("c"));
209 assert!(!"a".ends_with("abc"));
210 assert!(!"".ends_with("abc"));
211 assert!(!"ddö".ends_with("-"));
212 assert!("ddö".ends_with("dö"));
213 }
214
215 #[test]
216 fn test_is_empty() {
217 assert!("".is_empty());
218 assert!(!"a".is_empty());
219 }
220
221 #[test]
222 fn test_replacen() {
223 assert_eq!("".replacen('a', "b", 5), "");
224 assert_eq!("acaaa".replacen("a", "b", 3), "bcbba");
225 assert_eq!("aaaa".replacen("a", "b", 0), "aaaa");
226
227 let test = "test";
228 assert_eq!(" test test ".replacen(test, "toast", 3), " toast toast ");
229 assert_eq!(" test test ".replacen(test, "toast", 0), " test test ");
230 assert_eq!(" test test ".replacen(test, "", 5), " ");
231
232 assert_eq!("qwer123zxc789".replacen(char::is_numeric, "", 3), "qwerzxc789");
233 }
234
235 #[test]
236 fn test_replace() {
237 let a = "a";
238 assert_eq!("".replace(a, "b"), "");
239 assert_eq!("a".replace(a, "b"), "b");
240 assert_eq!("ab".replace(a, "b"), "bb");
241 let test = "test";
242 assert_eq!(" test test ".replace(test, "toast"), " toast toast ");
243 assert_eq!(" test test ".replace(test, ""), " ");
244 }
245
246 #[test]
247 fn test_replace_2a() {
248 let data = "ประเทศไทย中华";
249 let repl = "دولة الكويت";
250
251 let a = "ประเ";
252 let a2 = "دولة الكويتทศไทย中华";
253 assert_eq!(data.replace(a, repl), a2);
254 }
255
256 #[test]
257 fn test_replace_2b() {
258 let data = "ประเทศไทย中华";
259 let repl = "دولة الكويت";
260
261 let b = "ะเ";
262 let b2 = "ปรدولة الكويتทศไทย中华";
263 assert_eq!(data.replace(b, repl), b2);
264 }
265
266 #[test]
267 fn test_replace_2c() {
268 let data = "ประเทศไทย中华";
269 let repl = "دولة الكويت";
270
271 let c = "中华";
272 let c2 = "ประเทศไทยدولة الكويت";
273 assert_eq!(data.replace(c, repl), c2);
274 }
275
276 #[test]
277 fn test_replace_2d() {
278 let data = "ประเทศไทย中华";
279 let repl = "دولة الكويت";
280
281 let d = "ไท华";
282 assert_eq!(data.replace(d, repl), data);
283 }
284
285 #[test]
286 fn test_replace_pattern() {
287 let data = "abcdαβγδabcdαβγδ";
288 assert_eq!(data.replace("dαβ", "😺😺😺"), "abc😺😺😺γδabc😺😺😺γδ");
289 assert_eq!(data.replace('γ', "😺😺😺"), "abcdαβ😺😺😺δabcdαβ😺😺😺δ");
290 assert_eq!(data.replace(&['a', 'γ'] as &[_], "😺😺😺"), "😺😺😺bcdαβ😺😺😺δ😺😺😺bcdαβ😺😺😺δ");
291 assert_eq!(data.replace(|c| c == 'γ', "😺😺😺"), "abcdαβ😺😺😺δabcdαβ😺😺😺δ");
292 }
293
294 #[test]
295 fn test_slice() {
296 assert_eq!("ab", &"abc"[0..2]);
297 assert_eq!("bc", &"abc"[1..3]);
298 assert_eq!("", &"abc"[1..1]);
299 assert_eq!("\u{65e5}", &"\u{65e5}\u{672c}"[0..3]);
300
301 let data = "ประเทศไทย中华";
302 assert_eq!("ป", &data[0..3]);
303 assert_eq!("ร", &data[3..6]);
304 assert_eq!("", &data[3..3]);
305 assert_eq!("华", &data[30..33]);
306
307 fn a_million_letter_x() -> String {
308 let mut i = 0;
309 let mut rs = String::new();
310 while i < 100000 {
311 rs.push_str("华华华华华华华华华华");
312 i += 1;
313 }
314 rs
315 }
316 fn half_a_million_letter_x() -> String {
317 let mut i = 0;
318 let mut rs = String::new();
319 while i < 100000 {
320 rs.push_str("华华华华华");
321 i += 1;
322 }
323 rs
324 }
325 let letters = a_million_letter_x();
326 assert_eq!(half_a_million_letter_x(), &letters[0..3 * 500000]);
327 }
328
329 #[test]
330 fn test_slice_2() {
331 let ss = "中华Việt Nam";
332
333 assert_eq!("华", &ss[3..6]);
334 assert_eq!("Việt Nam", &ss[6..16]);
335
336 assert_eq!("ab", &"abc"[0..2]);
337 assert_eq!("bc", &"abc"[1..3]);
338 assert_eq!("", &"abc"[1..1]);
339
340 assert_eq!("中", &ss[0..3]);
341 assert_eq!("华V", &ss[3..7]);
342 assert_eq!("", &ss[3..3]);
343 /*0: 中
344 3: 华
345 6: V
346 7: i
347 8: ệ
348 11: t
349 12:
350 13: N
351 14: a
352 15: m */
353 }
354
355 #[test]
356 #[should_panic]
357 fn test_slice_fail() {
358 &"中华Việt Nam"[0..2];
359 }
360
361
362 #[test]
363 fn test_is_char_boundary() {
364 let s = "ศไทย中华Việt Nam β-release 🐱123";
365 assert!(s.is_char_boundary(0));
366 assert!(s.is_char_boundary(s.len()));
367 assert!(!s.is_char_boundary(s.len() + 1));
368 for (i, ch) in s.char_indices() {
369 // ensure character locations are boundaries and continuation bytes are not
370 assert!(s.is_char_boundary(i), "{} is a char boundary in {:?}", i, s);
371 for j in 1..ch.len_utf8() {
372 assert!(!s.is_char_boundary(i + j),
373 "{} should not be a char boundary in {:?}", i + j, s);
374 }
375 }
376 }
377 const LOREM_PARAGRAPH: &'static str = "\
378 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse quis lorem sit amet dolor \
379 ultricies condimentum. Praesent iaculis purus elit, ac malesuada quam malesuada in. Duis sed orci \
380 eros. Suspendisse sit amet magna mollis, mollis nunc luctus, imperdiet mi. Integer fringilla non \
381 sem ut lacinia. Fusce varius tortor a risus porttitor hendrerit. Morbi mauris dui, ultricies nec \
382 tempus vel, gravida nec quam.";
383
384 // check the panic includes the prefix of the sliced string
385 #[test]
386 #[should_panic(expected="Lorem ipsum dolor sit amet")]
387 fn test_slice_fail_truncated_1() {
388 &LOREM_PARAGRAPH[..1024];
389 }
390 // check the truncation in the panic message
391 #[test]
392 #[should_panic(expected="luctus, im`[...] do not lie on character boundary")]
393 fn test_slice_fail_truncated_2() {
394 &LOREM_PARAGRAPH[..1024];
395 }
396
397 #[test]
398 fn test_slice_from() {
399 assert_eq!(&"abcd"[0..], "abcd");
400 assert_eq!(&"abcd"[2..], "cd");
401 assert_eq!(&"abcd"[4..], "");
402 }
403 #[test]
404 fn test_slice_to() {
405 assert_eq!(&"abcd"[..0], "");
406 assert_eq!(&"abcd"[..2], "ab");
407 assert_eq!(&"abcd"[..4], "abcd");
408 }
409
410 #[test]
411 fn test_trim_left_matches() {
412 let v: &[char] = &[];
413 assert_eq!(" *** foo *** ".trim_left_matches(v), " *** foo *** ");
414 let chars: &[char] = &['*', ' '];
415 assert_eq!(" *** foo *** ".trim_left_matches(chars), "foo *** ");
416 assert_eq!(" *** *** ".trim_left_matches(chars), "");
417 assert_eq!("foo *** ".trim_left_matches(chars), "foo *** ");
418
419 assert_eq!("11foo1bar11".trim_left_matches('1'), "foo1bar11");
420 let chars: &[char] = &['1', '2'];
421 assert_eq!("12foo1bar12".trim_left_matches(chars), "foo1bar12");
422 assert_eq!("123foo1bar123".trim_left_matches(|c: char| c.is_numeric()), "foo1bar123");
423 }
424
425 #[test]
426 fn test_trim_right_matches() {
427 let v: &[char] = &[];
428 assert_eq!(" *** foo *** ".trim_right_matches(v), " *** foo *** ");
429 let chars: &[char] = &['*', ' '];
430 assert_eq!(" *** foo *** ".trim_right_matches(chars), " *** foo");
431 assert_eq!(" *** *** ".trim_right_matches(chars), "");
432 assert_eq!(" *** foo".trim_right_matches(chars), " *** foo");
433
434 assert_eq!("11foo1bar11".trim_right_matches('1'), "11foo1bar");
435 let chars: &[char] = &['1', '2'];
436 assert_eq!("12foo1bar12".trim_right_matches(chars), "12foo1bar");
437 assert_eq!("123foo1bar123".trim_right_matches(|c: char| c.is_numeric()), "123foo1bar");
438 }
439
440 #[test]
441 fn test_trim_matches() {
442 let v: &[char] = &[];
443 assert_eq!(" *** foo *** ".trim_matches(v), " *** foo *** ");
444 let chars: &[char] = &['*', ' '];
445 assert_eq!(" *** foo *** ".trim_matches(chars), "foo");
446 assert_eq!(" *** *** ".trim_matches(chars), "");
447 assert_eq!("foo".trim_matches(chars), "foo");
448
449 assert_eq!("11foo1bar11".trim_matches('1'), "foo1bar");
450 let chars: &[char] = &['1', '2'];
451 assert_eq!("12foo1bar12".trim_matches(chars), "foo1bar");
452 assert_eq!("123foo1bar123".trim_matches(|c: char| c.is_numeric()), "foo1bar");
453 }
454
455 #[test]
456 fn test_trim_left() {
457 assert_eq!("".trim_left(), "");
458 assert_eq!("a".trim_left(), "a");
459 assert_eq!(" ".trim_left(), "");
460 assert_eq!(" blah".trim_left(), "blah");
461 assert_eq!(" \u{3000} wut".trim_left(), "wut");
462 assert_eq!("hey ".trim_left(), "hey ");
463 }
464
465 #[test]
466 fn test_trim_right() {
467 assert_eq!("".trim_right(), "");
468 assert_eq!("a".trim_right(), "a");
469 assert_eq!(" ".trim_right(), "");
470 assert_eq!("blah ".trim_right(), "blah");
471 assert_eq!("wut \u{3000} ".trim_right(), "wut");
472 assert_eq!(" hey".trim_right(), " hey");
473 }
474
475 #[test]
476 fn test_trim() {
477 assert_eq!("".trim(), "");
478 assert_eq!("a".trim(), "a");
479 assert_eq!(" ".trim(), "");
480 assert_eq!(" blah ".trim(), "blah");
481 assert_eq!("\nwut \u{3000} ".trim(), "wut");
482 assert_eq!(" hey dude ".trim(), "hey dude");
483 }
484
485 #[test]
486 fn test_is_whitespace() {
487 assert!("".chars().all(|c| c.is_whitespace()));
488 assert!(" ".chars().all(|c| c.is_whitespace()));
489 assert!("\u{2009}".chars().all(|c| c.is_whitespace())); // Thin space
490 assert!(" \n\t ".chars().all(|c| c.is_whitespace()));
491 assert!(!" _ ".chars().all(|c| c.is_whitespace()));
492 }
493
494 #[test]
495 fn test_is_utf8() {
496 // deny overlong encodings
497 assert!(from_utf8(&[0xc0, 0x80]).is_err());
498 assert!(from_utf8(&[0xc0, 0xae]).is_err());
499 assert!(from_utf8(&[0xe0, 0x80, 0x80]).is_err());
500 assert!(from_utf8(&[0xe0, 0x80, 0xaf]).is_err());
501 assert!(from_utf8(&[0xe0, 0x81, 0x81]).is_err());
502 assert!(from_utf8(&[0xf0, 0x82, 0x82, 0xac]).is_err());
503 assert!(from_utf8(&[0xf4, 0x90, 0x80, 0x80]).is_err());
504
505 // deny surrogates
506 assert!(from_utf8(&[0xED, 0xA0, 0x80]).is_err());
507 assert!(from_utf8(&[0xED, 0xBF, 0xBF]).is_err());
508
509 assert!(from_utf8(&[0xC2, 0x80]).is_ok());
510 assert!(from_utf8(&[0xDF, 0xBF]).is_ok());
511 assert!(from_utf8(&[0xE0, 0xA0, 0x80]).is_ok());
512 assert!(from_utf8(&[0xED, 0x9F, 0xBF]).is_ok());
513 assert!(from_utf8(&[0xEE, 0x80, 0x80]).is_ok());
514 assert!(from_utf8(&[0xEF, 0xBF, 0xBF]).is_ok());
515 assert!(from_utf8(&[0xF0, 0x90, 0x80, 0x80]).is_ok());
516 assert!(from_utf8(&[0xF4, 0x8F, 0xBF, 0xBF]).is_ok());
517 }
518
519 #[test]
520 fn from_utf8_mostly_ascii() {
521 // deny invalid bytes embedded in long stretches of ascii
522 for i in 32..64 {
523 let mut data = [0; 128];
524 data[i] = 0xC0;
525 assert!(from_utf8(&data).is_err());
526 data[i] = 0xC2;
527 assert!(from_utf8(&data).is_err());
528 }
529 }
530
531 #[test]
532 fn test_is_utf16() {
533 use rustc_unicode::str::is_utf16;
534
535 macro_rules! pos {
536 ($($e:expr),*) => { { $(assert!(is_utf16($e));)* } }
537 }
538
539 // non-surrogates
540 pos!(&[0x0000],
541 &[0x0001, 0x0002],
542 &[0xD7FF],
543 &[0xE000]);
544
545 // surrogate pairs (randomly generated with Python 3's
546 // .encode('utf-16be'))
547 pos!(&[0xdb54, 0xdf16, 0xd880, 0xdee0, 0xdb6a, 0xdd45],
548 &[0xd91f, 0xdeb1, 0xdb31, 0xdd84, 0xd8e2, 0xde14],
549 &[0xdb9f, 0xdc26, 0xdb6f, 0xde58, 0xd850, 0xdfae]);
550
551 // mixtures (also random)
552 pos!(&[0xd921, 0xdcc2, 0x002d, 0x004d, 0xdb32, 0xdf65],
553 &[0xdb45, 0xdd2d, 0x006a, 0xdacd, 0xddfe, 0x0006],
554 &[0x0067, 0xd8ff, 0xddb7, 0x000f, 0xd900, 0xdc80]);
555
556 // negative tests
557 macro_rules! neg {
558 ($($e:expr),*) => { { $(assert!(!is_utf16($e));)* } }
559 }
560
561 neg!(
562 // surrogate + regular unit
563 &[0xdb45, 0x0000],
564 // surrogate + lead surrogate
565 &[0xd900, 0xd900],
566 // unterminated surrogate
567 &[0xd8ff],
568 // trail surrogate without a lead
569 &[0xddb7]);
570
571 // random byte sequences that Python 3's .decode('utf-16be')
572 // failed on
573 neg!(&[0x5b3d, 0x0141, 0xde9e, 0x8fdc, 0xc6e7],
574 &[0xdf5a, 0x82a5, 0x62b9, 0xb447, 0x92f3],
575 &[0xda4e, 0x42bc, 0x4462, 0xee98, 0xc2ca],
576 &[0xbe00, 0xb04a, 0x6ecb, 0xdd89, 0xe278],
577 &[0x0465, 0xab56, 0xdbb6, 0xa893, 0x665e],
578 &[0x6b7f, 0x0a19, 0x40f4, 0xa657, 0xdcc5],
579 &[0x9b50, 0xda5e, 0x24ec, 0x03ad, 0x6dee],
580 &[0x8d17, 0xcaa7, 0xf4ae, 0xdf6e, 0xbed7],
581 &[0xdaee, 0x2584, 0x7d30, 0xa626, 0x121a],
582 &[0xd956, 0x4b43, 0x7570, 0xccd6, 0x4f4a],
583 &[0x9dcf, 0x1b49, 0x4ba5, 0xfce9, 0xdffe],
584 &[0x6572, 0xce53, 0xb05a, 0xf6af, 0xdacf],
585 &[0x1b90, 0x728c, 0x9906, 0xdb68, 0xf46e],
586 &[0x1606, 0xbeca, 0xbe76, 0x860f, 0xdfa5],
587 &[0x8b4f, 0xde7a, 0xd220, 0x9fac, 0x2b6f],
588 &[0xb8fe, 0xebbe, 0xda32, 0x1a5f, 0x8b8b],
589 &[0x934b, 0x8956, 0xc434, 0x1881, 0xddf7],
590 &[0x5a95, 0x13fc, 0xf116, 0xd89b, 0x93f9],
591 &[0xd640, 0x71f1, 0xdd7d, 0x77eb, 0x1cd8],
592 &[0x348b, 0xaef0, 0xdb2c, 0xebf1, 0x1282],
593 &[0x50d7, 0xd824, 0x5010, 0xb369, 0x22ea]);
594 }
595
596 #[test]
597 fn test_as_bytes() {
598 // no null
599 let v = [
600 224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228,
601 184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97,
602 109
603 ];
604 let b: &[u8] = &[];
605 assert_eq!("".as_bytes(), b);
606 assert_eq!("abc".as_bytes(), b"abc");
607 assert_eq!("ศไทย中华Việt Nam".as_bytes(), v);
608 }
609
610 #[test]
611 #[should_panic]
612 fn test_as_bytes_fail() {
613 // Don't double free. (I'm not sure if this exercises the
614 // original problem code path anymore.)
615 let s = String::from("");
616 let _bytes = s.as_bytes();
617 panic!();
618 }
619
620 #[test]
621 fn test_as_ptr() {
622 let buf = "hello".as_ptr();
623 unsafe {
624 assert_eq!(*buf.offset(0), b'h');
625 assert_eq!(*buf.offset(1), b'e');
626 assert_eq!(*buf.offset(2), b'l');
627 assert_eq!(*buf.offset(3), b'l');
628 assert_eq!(*buf.offset(4), b'o');
629 }
630 }
631
632 #[test]
633 fn vec_str_conversions() {
634 let s1: String = String::from("All mimsy were the borogoves");
635
636 let v: Vec<u8> = s1.as_bytes().to_vec();
637 let s2: String = String::from(from_utf8(&v).unwrap());
638 let mut i = 0;
639 let n1 = s1.len();
640 let n2 = v.len();
641 assert_eq!(n1, n2);
642 while i < n1 {
643 let a: u8 = s1.as_bytes()[i];
644 let b: u8 = s2.as_bytes()[i];
645 assert_eq!(a, b);
646 i += 1;
647 }
648 }
649
650 #[test]
651 fn test_contains() {
652 assert!("abcde".contains("bcd"));
653 assert!("abcde".contains("abcd"));
654 assert!("abcde".contains("bcde"));
655 assert!("abcde".contains(""));
656 assert!("".contains(""));
657 assert!(!"abcde".contains("def"));
658 assert!(!"".contains("a"));
659
660 let data = "ประเทศไทย中华Việt Nam";
661 assert!(data.contains("ประเ"));
662 assert!(data.contains("ะเ"));
663 assert!(data.contains("中华"));
664 assert!(!data.contains("ไท华"));
665 }
666
667 #[test]
668 fn test_contains_char() {
669 assert!("abc".contains('b'));
670 assert!("a".contains('a'));
671 assert!(!"abc".contains('d'));
672 assert!(!"".contains('a'));
673 }
674
675 #[test]
676 fn test_split_at() {
677 let s = "ศไทย中华Việt Nam";
678 for (index, _) in s.char_indices() {
679 let (a, b) = s.split_at(index);
680 assert_eq!(&s[..a.len()], a);
681 assert_eq!(&s[a.len()..], b);
682 }
683 let (a, b) = s.split_at(s.len());
684 assert_eq!(a, s);
685 assert_eq!(b, "");
686 }
687
688 #[test]
689 fn test_split_at_mut() {
690 use std::ascii::AsciiExt;
691 let mut s = "Hello World".to_string();
692 {
693 let (a, b) = s.split_at_mut(5);
694 a.make_ascii_uppercase();
695 b.make_ascii_lowercase();
696 }
697 assert_eq!(s, "HELLO world");
698 }
699
700 #[test]
701 #[should_panic]
702 fn test_split_at_boundscheck() {
703 let s = "ศไทย中华Việt Nam";
704 s.split_at(1);
705 }
706
707 #[test]
708 fn test_escape_unicode() {
709 assert_eq!("abc".escape_unicode(), "\\u{61}\\u{62}\\u{63}");
710 assert_eq!("a c".escape_unicode(), "\\u{61}\\u{20}\\u{63}");
711 assert_eq!("\r\n\t".escape_unicode(), "\\u{d}\\u{a}\\u{9}");
712 assert_eq!("'\"\\".escape_unicode(), "\\u{27}\\u{22}\\u{5c}");
713 assert_eq!("\x00\x01\u{fe}\u{ff}".escape_unicode(), "\\u{0}\\u{1}\\u{fe}\\u{ff}");
714 assert_eq!("\u{100}\u{ffff}".escape_unicode(), "\\u{100}\\u{ffff}");
715 assert_eq!("\u{10000}\u{10ffff}".escape_unicode(), "\\u{10000}\\u{10ffff}");
716 assert_eq!("ab\u{fb00}".escape_unicode(), "\\u{61}\\u{62}\\u{fb00}");
717 assert_eq!("\u{1d4ea}\r".escape_unicode(), "\\u{1d4ea}\\u{d}");
718 }
719
720 #[test]
721 fn test_escape_debug() {
722 assert_eq!("abc".escape_debug(), "abc");
723 assert_eq!("a c".escape_debug(), "a c");
724 assert_eq!("éèê".escape_debug(), "éèê");
725 assert_eq!("\r\n\t".escape_debug(), "\\r\\n\\t");
726 assert_eq!("'\"\\".escape_debug(), "\\'\\\"\\\\");
727 assert_eq!("\u{7f}\u{ff}".escape_debug(), "\\u{7f}\u{ff}");
728 assert_eq!("\u{100}\u{ffff}".escape_debug(), "\u{100}\\u{ffff}");
729 assert_eq!("\u{10000}\u{10ffff}".escape_debug(), "\u{10000}\\u{10ffff}");
730 assert_eq!("ab\u{200b}".escape_debug(), "ab\\u{200b}");
731 assert_eq!("\u{10d4ea}\r".escape_debug(), "\\u{10d4ea}\\r");
732 }
733
734 #[test]
735 fn test_escape_default() {
736 assert_eq!("abc".escape_default(), "abc");
737 assert_eq!("a c".escape_default(), "a c");
738 assert_eq!("éèê".escape_default(), "\\u{e9}\\u{e8}\\u{ea}");
739 assert_eq!("\r\n\t".escape_default(), "\\r\\n\\t");
740 assert_eq!("'\"\\".escape_default(), "\\'\\\"\\\\");
741 assert_eq!("\u{7f}\u{ff}".escape_default(), "\\u{7f}\\u{ff}");
742 assert_eq!("\u{100}\u{ffff}".escape_default(), "\\u{100}\\u{ffff}");
743 assert_eq!("\u{10000}\u{10ffff}".escape_default(), "\\u{10000}\\u{10ffff}");
744 assert_eq!("ab\u{200b}".escape_default(), "ab\\u{200b}");
745 assert_eq!("\u{10d4ea}\r".escape_default(), "\\u{10d4ea}\\r");
746 }
747
748 #[test]
749 fn test_total_ord() {
750 assert_eq!("1234".cmp("123"), Greater);
751 assert_eq!("123".cmp("1234"), Less);
752 assert_eq!("1234".cmp("1234"), Equal);
753 assert_eq!("12345555".cmp("123456"), Less);
754 assert_eq!("22".cmp("1234"), Greater);
755 }
756
757 #[test]
758 fn test_iterator() {
759 let s = "ศไทย中华Việt Nam";
760 let v = ['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m'];
761
762 let mut pos = 0;
763 let it = s.chars();
764
765 for c in it {
766 assert_eq!(c, v[pos]);
767 pos += 1;
768 }
769 assert_eq!(pos, v.len());
770 }
771
772 #[test]
773 fn test_rev_iterator() {
774 let s = "ศไทย中华Việt Nam";
775 let v = ['m', 'a', 'N', ' ', 't', 'ệ','i','V','华','中','ย','ท','ไ','ศ'];
776
777 let mut pos = 0;
778 let it = s.chars().rev();
779
780 for c in it {
781 assert_eq!(c, v[pos]);
782 pos += 1;
783 }
784 assert_eq!(pos, v.len());
785 }
786
787 #[test]
788 fn test_chars_decoding() {
789 for c in (0..0x110000).filter_map(::std::char::from_u32) {
790 let bytes = c.encode_utf8();
791 let s = ::std::str::from_utf8(bytes.as_slice()).unwrap();
792 if Some(c) != s.chars().next() {
793 panic!("character {:x}={} does not decode correctly", c as u32, c);
794 }
795 }
796 }
797
798 #[test]
799 fn test_chars_rev_decoding() {
800 for c in (0..0x110000).filter_map(::std::char::from_u32) {
801 let bytes = c.encode_utf8();
802 let s = ::std::str::from_utf8(bytes.as_slice()).unwrap();
803 if Some(c) != s.chars().rev().next() {
804 panic!("character {:x}={} does not decode correctly", c as u32, c);
805 }
806 }
807 }
808
809 #[test]
810 fn test_iterator_clone() {
811 let s = "ศไทย中华Việt Nam";
812 let mut it = s.chars();
813 it.next();
814 assert!(it.clone().zip(it).all(|(x,y)| x == y));
815 }
816
817 #[test]
818 fn test_bytesator() {
819 let s = "ศไทย中华Việt Nam";
820 let v = [
821 224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228,
822 184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97,
823 109
824 ];
825 let mut pos = 0;
826
827 for b in s.bytes() {
828 assert_eq!(b, v[pos]);
829 pos += 1;
830 }
831 }
832
833 #[test]
834 fn test_bytes_revator() {
835 let s = "ศไทย中华Việt Nam";
836 let v = [
837 224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228,
838 184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97,
839 109
840 ];
841 let mut pos = v.len();
842
843 for b in s.bytes().rev() {
844 pos -= 1;
845 assert_eq!(b, v[pos]);
846 }
847 }
848
849 #[test]
850 fn test_bytesator_nth() {
851 let s = "ศไทย中华Việt Nam";
852 let v = [
853 224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228,
854 184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97,
855 109
856 ];
857
858 let mut b = s.bytes();
859 assert_eq!(b.nth(2).unwrap(), v[2]);
860 assert_eq!(b.nth(10).unwrap(), v[10]);
861 assert_eq!(b.nth(200), None);
862 }
863
864 #[test]
865 fn test_bytesator_count() {
866 let s = "ศไทย中华Việt Nam";
867
868 let b = s.bytes();
869 assert_eq!(b.count(), 28)
870 }
871
872 #[test]
873 fn test_bytesator_last() {
874 let s = "ศไทย中华Việt Nam";
875
876 let b = s.bytes();
877 assert_eq!(b.last().unwrap(), 109)
878 }
879
880 #[test]
881 fn test_char_indicesator() {
882 let s = "ศไทย中华Việt Nam";
883 let p = [0, 3, 6, 9, 12, 15, 18, 19, 20, 23, 24, 25, 26, 27];
884 let v = ['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m'];
885
886 let mut pos = 0;
887 let it = s.char_indices();
888
889 for c in it {
890 assert_eq!(c, (p[pos], v[pos]));
891 pos += 1;
892 }
893 assert_eq!(pos, v.len());
894 assert_eq!(pos, p.len());
895 }
896
897 #[test]
898 fn test_char_indices_revator() {
899 let s = "ศไทย中华Việt Nam";
900 let p = [27, 26, 25, 24, 23, 20, 19, 18, 15, 12, 9, 6, 3, 0];
901 let v = ['m', 'a', 'N', ' ', 't', 'ệ','i','V','华','中','ย','ท','ไ','ศ'];
902
903 let mut pos = 0;
904 let it = s.char_indices().rev();
905
906 for c in it {
907 assert_eq!(c, (p[pos], v[pos]));
908 pos += 1;
909 }
910 assert_eq!(pos, v.len());
911 assert_eq!(pos, p.len());
912 }
913
914 #[test]
915 fn test_splitn_char_iterator() {
916 let data = "\nMäry häd ä little lämb\nLittle lämb\n";
917
918 let split: Vec<&str> = data.splitn(4, ' ').collect();
919 assert_eq!(split, ["\nMäry", "häd", "ä", "little lämb\nLittle lämb\n"]);
920
921 let split: Vec<&str> = data.splitn(4, |c: char| c == ' ').collect();
922 assert_eq!(split, ["\nMäry", "häd", "ä", "little lämb\nLittle lämb\n"]);
923
924 // Unicode
925 let split: Vec<&str> = data.splitn(4, 'ä').collect();
926 assert_eq!(split, ["\nM", "ry h", "d ", " little lämb\nLittle lämb\n"]);
927
928 let split: Vec<&str> = data.splitn(4, |c: char| c == 'ä').collect();
929 assert_eq!(split, ["\nM", "ry h", "d ", " little lämb\nLittle lämb\n"]);
930 }
931
932 #[test]
933 fn test_split_char_iterator_no_trailing() {
934 let data = "\nMäry häd ä little lämb\nLittle lämb\n";
935
936 let split: Vec<&str> = data.split('\n').collect();
937 assert_eq!(split, ["", "Märy häd ä little lämb", "Little lämb", ""]);
938
939 let split: Vec<&str> = data.split_terminator('\n').collect();
940 assert_eq!(split, ["", "Märy häd ä little lämb", "Little lämb"]);
941 }
942
943 #[test]
944 fn test_rsplit() {
945 let data = "\nMäry häd ä little lämb\nLittle lämb\n";
946
947 let split: Vec<&str> = data.rsplit(' ').collect();
948 assert_eq!(split, ["lämb\n", "lämb\nLittle", "little", "ä", "häd", "\nMäry"]);
949
950 let split: Vec<&str> = data.rsplit("lämb").collect();
951 assert_eq!(split, ["\n", "\nLittle ", "\nMäry häd ä little "]);
952
953 let split: Vec<&str> = data.rsplit(|c: char| c == 'ä').collect();
954 assert_eq!(split, ["mb\n", "mb\nLittle l", " little l", "d ", "ry h", "\nM"]);
955 }
956
957 #[test]
958 fn test_rsplitn() {
959 let data = "\nMäry häd ä little lämb\nLittle lämb\n";
960
961 let split: Vec<&str> = data.rsplitn(2, ' ').collect();
962 assert_eq!(split, ["lämb\n", "\nMäry häd ä little lämb\nLittle"]);
963
964 let split: Vec<&str> = data.rsplitn(2, "lämb").collect();
965 assert_eq!(split, ["\n", "\nMäry häd ä little lämb\nLittle "]);
966
967 let split: Vec<&str> = data.rsplitn(2, |c: char| c == 'ä').collect();
968 assert_eq!(split, ["mb\n", "\nMäry häd ä little lämb\nLittle l"]);
969 }
970
971 #[test]
972 fn test_split_whitespace() {
973 let data = "\n \tMäry häd\tä little lämb\nLittle lämb\n";
974 let words: Vec<&str> = data.split_whitespace().collect();
975 assert_eq!(words, ["Märy", "häd", "ä", "little", "lämb", "Little", "lämb"])
976 }
977
978 #[test]
979 fn test_lines() {
980 let data = "\nMäry häd ä little lämb\n\r\nLittle lämb\n";
981 let lines: Vec<&str> = data.lines().collect();
982 assert_eq!(lines, ["", "Märy häd ä little lämb", "", "Little lämb"]);
983
984 let data = "\r\nMäry häd ä little lämb\n\nLittle lämb"; // no trailing \n
985 let lines: Vec<&str> = data.lines().collect();
986 assert_eq!(lines, ["", "Märy häd ä little lämb", "", "Little lämb"]);
987 }
988
989 #[test]
990 fn test_splitator() {
991 fn t(s: &str, sep: &str, u: &[&str]) {
992 let v: Vec<&str> = s.split(sep).collect();
993 assert_eq!(v, u);
994 }
995 t("--1233345--", "12345", &["--1233345--"]);
996 t("abc::hello::there", "::", &["abc", "hello", "there"]);
997 t("::hello::there", "::", &["", "hello", "there"]);
998 t("hello::there::", "::", &["hello", "there", ""]);
999 t("::hello::there::", "::", &["", "hello", "there", ""]);
1000 t("ประเทศไทย中华Việt Nam", "中华", &["ประเทศไทย", "Việt Nam"]);
1001 t("zzXXXzzYYYzz", "zz", &["", "XXX", "YYY", ""]);
1002 t("zzXXXzYYYz", "XXX", &["zz", "zYYYz"]);
1003 t(".XXX.YYY.", ".", &["", "XXX", "YYY", ""]);
1004 t("", ".", &[""]);
1005 t("zz", "zz", &["",""]);
1006 t("ok", "z", &["ok"]);
1007 t("zzz", "zz", &["","z"]);
1008 t("zzzzz", "zz", &["","","z"]);
1009 }
1010
1011 #[test]
1012 fn test_str_default() {
1013 use std::default::Default;
1014
1015 fn t<S: Default + AsRef<str>>() {
1016 let s: S = Default::default();
1017 assert_eq!(s.as_ref(), "");
1018 }
1019
1020 t::<&str>();
1021 t::<String>();
1022 }
1023
1024 #[test]
1025 fn test_str_container() {
1026 fn sum_len(v: &[&str]) -> usize {
1027 v.iter().map(|x| x.len()).sum()
1028 }
1029
1030 let s = "01234";
1031 assert_eq!(5, sum_len(&["012", "", "34"]));
1032 assert_eq!(5, sum_len(&["01", "2", "34", ""]));
1033 assert_eq!(5, sum_len(&[s]));
1034 }
1035
1036 #[test]
1037 fn test_str_from_utf8() {
1038 let xs = b"hello";
1039 assert_eq!(from_utf8(xs), Ok("hello"));
1040
1041 let xs = "ศไทย中华Việt Nam".as_bytes();
1042 assert_eq!(from_utf8(xs), Ok("ศไทย中华Việt Nam"));
1043
1044 let xs = b"hello\xFF";
1045 assert!(from_utf8(xs).is_err());
1046 }
1047
1048 #[test]
1049 fn test_pattern_deref_forward() {
1050 let data = "aabcdaa";
1051 assert!(data.contains("bcd"));
1052 assert!(data.contains(&"bcd"));
1053 assert!(data.contains(&"bcd".to_string()));
1054 }
1055
1056 #[test]
1057 fn test_empty_match_indices() {
1058 let data = "aä中!";
1059 let vec: Vec<_> = data.match_indices("").collect();
1060 assert_eq!(vec, [(0, ""), (1, ""), (3, ""), (6, ""), (7, "")]);
1061 }
1062
1063 #[test]
1064 fn test_bool_from_str() {
1065 assert_eq!("true".parse().ok(), Some(true));
1066 assert_eq!("false".parse().ok(), Some(false));
1067 assert_eq!("not even a boolean".parse::<bool>().ok(), None);
1068 }
1069
1070 fn check_contains_all_substrings(s: &str) {
1071 assert!(s.contains(""));
1072 for i in 0..s.len() {
1073 for j in i+1..s.len() + 1 {
1074 assert!(s.contains(&s[i..j]));
1075 }
1076 }
1077 }
1078
1079 #[test]
1080 fn strslice_issue_16589() {
1081 assert!("bananas".contains("nana"));
1082
1083 // prior to the fix for #16589, x.contains("abcdabcd") returned false
1084 // test all substrings for good measure
1085 check_contains_all_substrings("012345678901234567890123456789bcdabcdabcd");
1086 }
1087
1088 #[test]
1089 fn strslice_issue_16878() {
1090 assert!(!"1234567ah012345678901ah".contains("hah"));
1091 assert!(!"00abc01234567890123456789abc".contains("bcabc"));
1092 }
1093
1094
1095 #[test]
1096 fn test_strslice_contains() {
1097 let x = "There are moments, Jeeves, when one asks oneself, 'Do trousers matter?'";
1098 check_contains_all_substrings(x);
1099 }
1100
1101 #[test]
1102 fn test_rsplitn_char_iterator() {
1103 let data = "\nMäry häd ä little lämb\nLittle lämb\n";
1104
1105 let mut split: Vec<&str> = data.rsplitn(4, ' ').collect();
1106 split.reverse();
1107 assert_eq!(split, ["\nMäry häd ä", "little", "lämb\nLittle", "lämb\n"]);
1108
1109 let mut split: Vec<&str> = data.rsplitn(4, |c: char| c == ' ').collect();
1110 split.reverse();
1111 assert_eq!(split, ["\nMäry häd ä", "little", "lämb\nLittle", "lämb\n"]);
1112
1113 // Unicode
1114 let mut split: Vec<&str> = data.rsplitn(4, 'ä').collect();
1115 split.reverse();
1116 assert_eq!(split, ["\nMäry häd ", " little l", "mb\nLittle l", "mb\n"]);
1117
1118 let mut split: Vec<&str> = data.rsplitn(4, |c: char| c == 'ä').collect();
1119 split.reverse();
1120 assert_eq!(split, ["\nMäry häd ", " little l", "mb\nLittle l", "mb\n"]);
1121 }
1122
1123 #[test]
1124 fn test_split_char_iterator() {
1125 let data = "\nMäry häd ä little lämb\nLittle lämb\n";
1126
1127 let split: Vec<&str> = data.split(' ').collect();
1128 assert_eq!( split, ["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
1129
1130 let mut rsplit: Vec<&str> = data.split(' ').rev().collect();
1131 rsplit.reverse();
1132 assert_eq!(rsplit, ["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
1133
1134 let split: Vec<&str> = data.split(|c: char| c == ' ').collect();
1135 assert_eq!( split, ["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
1136
1137 let mut rsplit: Vec<&str> = data.split(|c: char| c == ' ').rev().collect();
1138 rsplit.reverse();
1139 assert_eq!(rsplit, ["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
1140
1141 // Unicode
1142 let split: Vec<&str> = data.split('ä').collect();
1143 assert_eq!( split, ["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
1144
1145 let mut rsplit: Vec<&str> = data.split('ä').rev().collect();
1146 rsplit.reverse();
1147 assert_eq!(rsplit, ["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
1148
1149 let split: Vec<&str> = data.split(|c: char| c == 'ä').collect();
1150 assert_eq!( split, ["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
1151
1152 let mut rsplit: Vec<&str> = data.split(|c: char| c == 'ä').rev().collect();
1153 rsplit.reverse();
1154 assert_eq!(rsplit, ["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
1155 }
1156
1157 #[test]
1158 fn test_rev_split_char_iterator_no_trailing() {
1159 let data = "\nMäry häd ä little lämb\nLittle lämb\n";
1160
1161 let mut split: Vec<&str> = data.split('\n').rev().collect();
1162 split.reverse();
1163 assert_eq!(split, ["", "Märy häd ä little lämb", "Little lämb", ""]);
1164
1165 let mut split: Vec<&str> = data.split_terminator('\n').rev().collect();
1166 split.reverse();
1167 assert_eq!(split, ["", "Märy häd ä little lämb", "Little lämb"]);
1168 }
1169
1170 #[test]
1171 fn test_utf16_code_units() {
1172 use rustc_unicode::str::Utf16Encoder;
1173 assert_eq!(Utf16Encoder::new(vec!['é', '\u{1F4A9}'].into_iter()).collect::<Vec<u16>>(),
1174 [0xE9, 0xD83D, 0xDCA9])
1175 }
1176
1177 #[test]
1178 fn starts_with_in_unicode() {
1179 assert!(!"├── Cargo.toml".starts_with("# "));
1180 }
1181
1182 #[test]
1183 fn starts_short_long() {
1184 assert!(!"".starts_with("##"));
1185 assert!(!"##".starts_with("####"));
1186 assert!("####".starts_with("##"));
1187 assert!(!"##ä".starts_with("####"));
1188 assert!("####ä".starts_with("##"));
1189 assert!(!"##".starts_with("####ä"));
1190 assert!("##ä##".starts_with("##ä"));
1191
1192 assert!("".starts_with(""));
1193 assert!("ä".starts_with(""));
1194 assert!("#ä".starts_with(""));
1195 assert!("##ä".starts_with(""));
1196 assert!("ä###".starts_with(""));
1197 assert!("#ä##".starts_with(""));
1198 assert!("##ä#".starts_with(""));
1199 }
1200
1201 #[test]
1202 fn contains_weird_cases() {
1203 assert!("* \t".contains(' '));
1204 assert!(!"* \t".contains('?'));
1205 assert!(!"* \t".contains('\u{1F4A9}'));
1206 }
1207
1208 #[test]
1209 fn trim_ws() {
1210 assert_eq!(" \t a \t ".trim_left_matches(|c: char| c.is_whitespace()),
1211 "a \t ");
1212 assert_eq!(" \t a \t ".trim_right_matches(|c: char| c.is_whitespace()),
1213 " \t a");
1214 assert_eq!(" \t a \t ".trim_matches(|c: char| c.is_whitespace()),
1215 "a");
1216 assert_eq!(" \t \t ".trim_left_matches(|c: char| c.is_whitespace()),
1217 "");
1218 assert_eq!(" \t \t ".trim_right_matches(|c: char| c.is_whitespace()),
1219 "");
1220 assert_eq!(" \t \t ".trim_matches(|c: char| c.is_whitespace()),
1221 "");
1222 }
1223
1224 #[test]
1225 fn to_lowercase() {
1226 assert_eq!("".to_lowercase(), "");
1227 assert_eq!("AÉDžaé ".to_lowercase(), "aédžaé ");
1228
1229 // https://github.com/rust-lang/rust/issues/26035
1230 assert_eq!("ΑΣ".to_lowercase(), "ας");
1231 assert_eq!("Α'Σ".to_lowercase(), "α'ς");
1232 assert_eq!("Α''Σ".to_lowercase(), "α''ς");
1233
1234 assert_eq!("ΑΣ Α".to_lowercase(), "ας α");
1235 assert_eq!("Α'Σ Α".to_lowercase(), "α'ς α");
1236 assert_eq!("Α''Σ Α".to_lowercase(), "α''ς α");
1237
1238 assert_eq!("ΑΣ' Α".to_lowercase(), "ας' α");
1239 assert_eq!("ΑΣ'' Α".to_lowercase(), "ας'' α");
1240
1241 assert_eq!("Α'Σ' Α".to_lowercase(), "α'ς' α");
1242 assert_eq!("Α''Σ'' Α".to_lowercase(), "α''ς'' α");
1243
1244 assert_eq!("Α Σ".to_lowercase(), "α σ");
1245 assert_eq!("Α 'Σ".to_lowercase(), "α 'σ");
1246 assert_eq!("Α ''Σ".to_lowercase(), "α ''σ");
1247
1248 assert_eq!("Σ".to_lowercase(), "σ");
1249 assert_eq!("'Σ".to_lowercase(), "'σ");
1250 assert_eq!("''Σ".to_lowercase(), "''σ");
1251
1252 assert_eq!("ΑΣΑ".to_lowercase(), "ασα");
1253 assert_eq!("ΑΣ'Α".to_lowercase(), "ασ'α");
1254 assert_eq!("ΑΣ''Α".to_lowercase(), "ασ''α");
1255 }
1256
1257 #[test]
1258 fn to_uppercase() {
1259 assert_eq!("".to_uppercase(), "");
1260 assert_eq!("aéDžßfiᾀ".to_uppercase(), "AÉDŽSSFIἈΙ");
1261 }
1262
1263 #[test]
1264 fn test_into_string() {
1265 // The only way to acquire a Box<str> in the first place is through a String, so just
1266 // test that we can round-trip between Box<str> and String.
1267 let string = String::from("Some text goes here");
1268 assert_eq!(string.clone().into_boxed_str().into_string(), string);
1269 }
1270
1271 #[test]
1272 fn test_box_slice_clone() {
1273 let data = String::from("hello HELLO hello HELLO yes YES 5 中ä华!!!");
1274 let data2 = data.clone().into_boxed_str().clone().into_string();
1275
1276 assert_eq!(data, data2);
1277 }
1278
1279 #[test]
1280 fn test_cow_from() {
1281 let borrowed = "borrowed";
1282 let owned = String::from("owned");
1283 match (Cow::from(owned.clone()), Cow::from(borrowed)) {
1284 (Cow::Owned(o), Cow::Borrowed(b)) => assert!(o == owned && b == borrowed),
1285 _ => panic!("invalid `Cow::from`"),
1286 }
1287 }
1288
1289 mod pattern {
1290 use std::str::pattern::Pattern;
1291 use std::str::pattern::{Searcher, ReverseSearcher};
1292 use std::str::pattern::SearchStep::{self, Match, Reject, Done};
1293
1294 macro_rules! make_test {
1295 ($name:ident, $p:expr, $h:expr, [$($e:expr,)*]) => {
1296 #[allow(unused_imports)]
1297 mod $name {
1298 use std::str::pattern::SearchStep::{Match, Reject};
1299 use super::{cmp_search_to_vec};
1300 #[test]
1301 fn fwd() {
1302 cmp_search_to_vec(false, $p, $h, vec![$($e),*]);
1303 }
1304 #[test]
1305 fn bwd() {
1306 cmp_search_to_vec(true, $p, $h, vec![$($e),*]);
1307 }
1308 }
1309 }
1310 }
1311
1312 fn cmp_search_to_vec<'a, P: Pattern<'a>>(rev: bool, pat: P, haystack: &'a str,
1313 right: Vec<SearchStep>)
1314 where P::Searcher: ReverseSearcher<'a>
1315 {
1316 let mut searcher = pat.into_searcher(haystack);
1317 let mut v = vec![];
1318 loop {
1319 match if !rev {searcher.next()} else {searcher.next_back()} {
1320 Match(a, b) => v.push(Match(a, b)),
1321 Reject(a, b) => v.push(Reject(a, b)),
1322 Done => break,
1323 }
1324 }
1325 if rev {
1326 v.reverse();
1327 }
1328
1329 let mut first_index = 0;
1330 let mut err = None;
1331
1332 for (i, e) in right.iter().enumerate() {
1333 match *e {
1334 Match(a, b) | Reject(a, b)
1335 if a <= b && a == first_index => {
1336 first_index = b;
1337 }
1338 _ => {
1339 err = Some(i);
1340 break;
1341 }
1342 }
1343 }
1344
1345 if let Some(err) = err {
1346 panic!("Input skipped range at {}", err);
1347 }
1348
1349 if first_index != haystack.len() {
1350 panic!("Did not cover whole input");
1351 }
1352
1353 assert_eq!(v, right);
1354 }
1355
1356 make_test!(str_searcher_ascii_haystack, "bb", "abbcbbd", [
1357 Reject(0, 1),
1358 Match (1, 3),
1359 Reject(3, 4),
1360 Match (4, 6),
1361 Reject(6, 7),
1362 ]);
1363 make_test!(str_searcher_ascii_haystack_seq, "bb", "abbcbbbbd", [
1364 Reject(0, 1),
1365 Match (1, 3),
1366 Reject(3, 4),
1367 Match (4, 6),
1368 Match (6, 8),
1369 Reject(8, 9),
1370 ]);
1371 make_test!(str_searcher_empty_needle_ascii_haystack, "", "abbcbbd", [
1372 Match (0, 0),
1373 Reject(0, 1),
1374 Match (1, 1),
1375 Reject(1, 2),
1376 Match (2, 2),
1377 Reject(2, 3),
1378 Match (3, 3),
1379 Reject(3, 4),
1380 Match (4, 4),
1381 Reject(4, 5),
1382 Match (5, 5),
1383 Reject(5, 6),
1384 Match (6, 6),
1385 Reject(6, 7),
1386 Match (7, 7),
1387 ]);
1388 make_test!(str_searcher_mulibyte_haystack, " ", "├──", [
1389 Reject(0, 3),
1390 Reject(3, 6),
1391 Reject(6, 9),
1392 ]);
1393 make_test!(str_searcher_empty_needle_mulibyte_haystack, "", "├──", [
1394 Match (0, 0),
1395 Reject(0, 3),
1396 Match (3, 3),
1397 Reject(3, 6),
1398 Match (6, 6),
1399 Reject(6, 9),
1400 Match (9, 9),
1401 ]);
1402 make_test!(str_searcher_empty_needle_empty_haystack, "", "", [
1403 Match(0, 0),
1404 ]);
1405 make_test!(str_searcher_nonempty_needle_empty_haystack, "├", "", [
1406 ]);
1407 make_test!(char_searcher_ascii_haystack, 'b', "abbcbbd", [
1408 Reject(0, 1),
1409 Match (1, 2),
1410 Match (2, 3),
1411 Reject(3, 4),
1412 Match (4, 5),
1413 Match (5, 6),
1414 Reject(6, 7),
1415 ]);
1416 make_test!(char_searcher_mulibyte_haystack, ' ', "├──", [
1417 Reject(0, 3),
1418 Reject(3, 6),
1419 Reject(6, 9),
1420 ]);
1421 make_test!(char_searcher_short_haystack, '\u{1F4A9}', "* \t", [
1422 Reject(0, 1),
1423 Reject(1, 2),
1424 Reject(2, 3),
1425 ]);
1426
1427 }
1428
1429 macro_rules! generate_iterator_test {
1430 {
1431 $name:ident {
1432 $(
1433 ($($arg:expr),*) -> [$($t:tt)*];
1434 )*
1435 }
1436 with $fwd:expr, $bwd:expr;
1437 } => {
1438 #[test]
1439 fn $name() {
1440 $(
1441 {
1442 let res = vec![$($t)*];
1443
1444 let fwd_vec: Vec<_> = ($fwd)($($arg),*).collect();
1445 assert_eq!(fwd_vec, res);
1446
1447 let mut bwd_vec: Vec<_> = ($bwd)($($arg),*).collect();
1448 bwd_vec.reverse();
1449 assert_eq!(bwd_vec, res);
1450 }
1451 )*
1452 }
1453 };
1454 {
1455 $name:ident {
1456 $(
1457 ($($arg:expr),*) -> [$($t:tt)*];
1458 )*
1459 }
1460 with $fwd:expr;
1461 } => {
1462 #[test]
1463 fn $name() {
1464 $(
1465 {
1466 let res = vec![$($t)*];
1467
1468 let fwd_vec: Vec<_> = ($fwd)($($arg),*).collect();
1469 assert_eq!(fwd_vec, res);
1470 }
1471 )*
1472 }
1473 }
1474 }
1475
1476 generate_iterator_test! {
1477 double_ended_split {
1478 ("foo.bar.baz", '.') -> ["foo", "bar", "baz"];
1479 ("foo::bar::baz", "::") -> ["foo", "bar", "baz"];
1480 }
1481 with str::split, str::rsplit;
1482 }
1483
1484 generate_iterator_test! {
1485 double_ended_split_terminator {
1486 ("foo;bar;baz;", ';') -> ["foo", "bar", "baz"];
1487 }
1488 with str::split_terminator, str::rsplit_terminator;
1489 }
1490
1491 generate_iterator_test! {
1492 double_ended_matches {
1493 ("a1b2c3", char::is_numeric) -> ["1", "2", "3"];
1494 }
1495 with str::matches, str::rmatches;
1496 }
1497
1498 generate_iterator_test! {
1499 double_ended_match_indices {
1500 ("a1b2c3", char::is_numeric) -> [(1, "1"), (3, "2"), (5, "3")];
1501 }
1502 with str::match_indices, str::rmatch_indices;
1503 }
1504
1505 generate_iterator_test! {
1506 not_double_ended_splitn {
1507 ("foo::bar::baz", 2, "::") -> ["foo", "bar::baz"];
1508 }
1509 with str::splitn;
1510 }
1511
1512 generate_iterator_test! {
1513 not_double_ended_rsplitn {
1514 ("foo::bar::baz", 2, "::") -> ["baz", "foo::bar"];
1515 }
1516 with str::rsplitn;
1517 }
1518
1519 #[test]
1520 fn different_str_pattern_forwarding_lifetimes() {
1521 use std::str::pattern::Pattern;
1522
1523 fn foo<'a, P>(p: P) where for<'b> &'b P: Pattern<'a> {
1524 for _ in 0..3 {
1525 "asdf".find(&p);
1526 }
1527 }
1528
1529 foo::<&str>("x");
1530 }
1531
1532 mod bench {
1533 use test::{Bencher, black_box};
1534
1535 #[bench]
1536 fn char_iterator(b: &mut Bencher) {
1537 let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
1538
1539 b.iter(|| s.chars().count());
1540 }
1541
1542 #[bench]
1543 fn char_iterator_for(b: &mut Bencher) {
1544 let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
1545
1546 b.iter(|| {
1547 for ch in s.chars() { black_box(ch); }
1548 });
1549 }
1550
1551 #[bench]
1552 fn char_iterator_ascii(b: &mut Bencher) {
1553 let s = "Mary had a little lamb, Little lamb
1554 Mary had a little lamb, Little lamb
1555 Mary had a little lamb, Little lamb
1556 Mary had a little lamb, Little lamb
1557 Mary had a little lamb, Little lamb
1558 Mary had a little lamb, Little lamb";
1559
1560 b.iter(|| s.chars().count());
1561 }
1562
1563 #[bench]
1564 fn char_iterator_rev(b: &mut Bencher) {
1565 let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
1566
1567 b.iter(|| s.chars().rev().count());
1568 }
1569
1570 #[bench]
1571 fn char_iterator_rev_for(b: &mut Bencher) {
1572 let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
1573
1574 b.iter(|| {
1575 for ch in s.chars().rev() { black_box(ch); }
1576 });
1577 }
1578
1579 #[bench]
1580 fn char_indicesator(b: &mut Bencher) {
1581 let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
1582 let len = s.chars().count();
1583
1584 b.iter(|| assert_eq!(s.char_indices().count(), len));
1585 }
1586
1587 #[bench]
1588 fn char_indicesator_rev(b: &mut Bencher) {
1589 let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
1590 let len = s.chars().count();
1591
1592 b.iter(|| assert_eq!(s.char_indices().rev().count(), len));
1593 }
1594
1595 #[bench]
1596 fn split_unicode_ascii(b: &mut Bencher) {
1597 let s = "ประเทศไทย中华Việt Namประเทศไทย中华Việt Nam";
1598
1599 b.iter(|| assert_eq!(s.split('V').count(), 3));
1600 }
1601
1602 #[bench]
1603 fn split_ascii(b: &mut Bencher) {
1604 let s = "Mary had a little lamb, Little lamb, little-lamb.";
1605 let len = s.split(' ').count();
1606
1607 b.iter(|| assert_eq!(s.split(' ').count(), len));
1608 }
1609
1610 #[bench]
1611 fn split_extern_fn(b: &mut Bencher) {
1612 let s = "Mary had a little lamb, Little lamb, little-lamb.";
1613 let len = s.split(' ').count();
1614 fn pred(c: char) -> bool { c == ' ' }
1615
1616 b.iter(|| assert_eq!(s.split(pred).count(), len));
1617 }
1618
1619 #[bench]
1620 fn split_closure(b: &mut Bencher) {
1621 let s = "Mary had a little lamb, Little lamb, little-lamb.";
1622 let len = s.split(' ').count();
1623
1624 b.iter(|| assert_eq!(s.split(|c: char| c == ' ').count(), len));
1625 }
1626
1627 #[bench]
1628 fn split_slice(b: &mut Bencher) {
1629 let s = "Mary had a little lamb, Little lamb, little-lamb.";
1630 let len = s.split(' ').count();
1631
1632 let c: &[char] = &[' '];
1633 b.iter(|| assert_eq!(s.split(c).count(), len));
1634 }
1635
1636 #[bench]
1637 fn bench_join(b: &mut Bencher) {
1638 let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
1639 let sep = "→";
1640 let v = vec![s, s, s, s, s, s, s, s, s, s];
1641 b.iter(|| {
1642 assert_eq!(v.join(sep).len(), s.len() * 10 + sep.len() * 9);
1643 })
1644 }
1645
1646 #[bench]
1647 fn bench_contains_short_short(b: &mut Bencher) {
1648 let haystack = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
1649 let needle = "sit";
1650
1651 b.iter(|| {
1652 assert!(haystack.contains(needle));
1653 })
1654 }
1655
1656 #[bench]
1657 fn bench_contains_short_long(b: &mut Bencher) {
1658 let haystack = "\
1659 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse quis lorem sit amet dolor \
1660 ultricies condimentum. Praesent iaculis purus elit, ac malesuada quam malesuada in. Duis sed orci \
1661 eros. Suspendisse sit amet magna mollis, mollis nunc luctus, imperdiet mi. Integer fringilla non \
1662 sem ut lacinia. Fusce varius tortor a risus porttitor hendrerit. Morbi mauris dui, ultricies nec \
1663 tempus vel, gravida nec quam.
1664
1665 In est dui, tincidunt sed tempus interdum, adipiscing laoreet ante. Etiam tempor, tellus quis \
1666 sagittis interdum, nulla purus mattis sem, quis auctor erat odio ac tellus. In nec nunc sit amet \
1667 diam volutpat molestie at sed ipsum. Vestibulum laoreet consequat vulputate. Integer accumsan \
1668 lorem ac dignissim placerat. Suspendisse convallis faucibus lorem. Aliquam erat volutpat. In vel \
1669 eleifend felis. Sed suscipit nulla lorem, sed mollis est sollicitudin et. Nam fermentum egestas \
1670 interdum. Curabitur ut nisi justo.
1671
1672 Sed sollicitudin ipsum tellus, ut condimentum leo eleifend nec. Cras ut velit ante. Phasellus nec \
1673 mollis odio. Mauris molestie erat in arcu mattis, at aliquet dolor vehicula. Quisque malesuada \
1674 lectus sit amet nisi pretium, a condimentum ipsum porta. Morbi at dapibus diam. Praesent egestas \
1675 est sed risus elementum, eu rutrum metus ultrices. Etiam fermentum consectetur magna, id rutrum \
1676 felis accumsan a. Aliquam ut pellentesque libero. Sed mi nulla, lobortis eu tortor id, suscipit \
1677 ultricies neque. Morbi iaculis sit amet risus at iaculis. Praesent eget ligula quis turpis \
1678 feugiat suscipit vel non arcu. Interdum et malesuada fames ac ante ipsum primis in faucibus. \
1679 Aliquam sit amet placerat lorem.
1680
1681 Cras a lacus vel ante posuere elementum. Nunc est leo, bibendum ut facilisis vel, bibendum at \
1682 mauris. Nullam adipiscing diam vel odio ornare, luctus adipiscing mi luctus. Nulla facilisi. \
1683 Mauris adipiscing bibendum neque, quis adipiscing lectus tempus et. Sed feugiat erat et nisl \
1684 lobortis pharetra. Donec vitae erat enim. Nullam sit amet felis et quam lacinia tincidunt. Aliquam \
1685 suscipit dapibus urna. Sed volutpat urna in magna pulvinar volutpat. Phasellus nec tellus ac diam \
1686 cursus accumsan.
1687
1688 Nam lectus enim, dapibus non nisi tempor, consectetur convallis massa. Maecenas eleifend dictum \
1689 feugiat. Etiam quis mauris vel risus luctus mattis a a nunc. Nullam orci quam, imperdiet id \
1690 vehicula in, porttitor ut nibh. Duis sagittis adipiscing nisl vitae congue. Donec mollis risus eu \
1691 leo suscipit, varius porttitor nulla porta. Pellentesque ut sem nec nisi euismod vehicula. Nulla \
1692 malesuada sollicitudin quam eu fermentum.";
1693 let needle = "english";
1694
1695 b.iter(|| {
1696 assert!(!haystack.contains(needle));
1697 })
1698 }
1699
1700 #[bench]
1701 fn bench_contains_bad_naive(b: &mut Bencher) {
1702 let haystack = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
1703 let needle = "aaaaaaaab";
1704
1705 b.iter(|| {
1706 assert!(!haystack.contains(needle));
1707 })
1708 }
1709
1710 #[bench]
1711 fn bench_contains_equal(b: &mut Bencher) {
1712 let haystack = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
1713 let needle = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
1714
1715 b.iter(|| {
1716 assert!(haystack.contains(needle));
1717 })
1718 }
1719
1720 macro_rules! make_test_inner {
1721 ($s:ident, $code:expr, $name:ident, $str:expr) => {
1722 #[bench]
1723 fn $name(bencher: &mut Bencher) {
1724 let mut $s = $str;
1725 black_box(&mut $s);
1726 bencher.iter(|| $code);
1727 }
1728 }
1729 }
1730
1731 macro_rules! make_test {
1732 ($name:ident, $s:ident, $code:expr) => {
1733 mod $name {
1734 use test::Bencher;
1735 use test::black_box;
1736
1737 // Short strings: 65 bytes each
1738 make_test_inner!($s, $code, short_ascii,
1739 "Mary had a little lamb, Little lamb Mary had a littl lamb, lamb!");
1740 make_test_inner!($s, $code, short_mixed,
1741 "ศไทย中华Việt Nam; Mary had a little lamb, Little lam!");
1742 make_test_inner!($s, $code, short_pile_of_poo,
1743 "💩💩💩💩💩💩💩💩💩💩💩💩💩💩💩💩!");
1744 make_test_inner!($s, $code, long_lorem_ipsum,"\
1745 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse quis lorem sit amet dolor \
1746 ultricies condimentum. Praesent iaculis purus elit, ac malesuada quam malesuada in. Duis sed orci \
1747 eros. Suspendisse sit amet magna mollis, mollis nunc luctus, imperdiet mi. Integer fringilla non \
1748 sem ut lacinia. Fusce varius tortor a risus porttitor hendrerit. Morbi mauris dui, ultricies nec \
1749 tempus vel, gravida nec quam.
1750
1751 In est dui, tincidunt sed tempus interdum, adipiscing laoreet ante. Etiam tempor, tellus quis \
1752 sagittis interdum, nulla purus mattis sem, quis auctor erat odio ac tellus. In nec nunc sit amet \
1753 diam volutpat molestie at sed ipsum. Vestibulum laoreet consequat vulputate. Integer accumsan \
1754 lorem ac dignissim placerat. Suspendisse convallis faucibus lorem. Aliquam erat volutpat. In vel \
1755 eleifend felis. Sed suscipit nulla lorem, sed mollis est sollicitudin et. Nam fermentum egestas \
1756 interdum. Curabitur ut nisi justo.
1757
1758 Sed sollicitudin ipsum tellus, ut condimentum leo eleifend nec. Cras ut velit ante. Phasellus nec \
1759 mollis odio. Mauris molestie erat in arcu mattis, at aliquet dolor vehicula. Quisque malesuada \
1760 lectus sit amet nisi pretium, a condimentum ipsum porta. Morbi at dapibus diam. Praesent egestas \
1761 est sed risus elementum, eu rutrum metus ultrices. Etiam fermentum consectetur magna, id rutrum \
1762 felis accumsan a. Aliquam ut pellentesque libero. Sed mi nulla, lobortis eu tortor id, suscipit \
1763 ultricies neque. Morbi iaculis sit amet risus at iaculis. Praesent eget ligula quis turpis \
1764 feugiat suscipit vel non arcu. Interdum et malesuada fames ac ante ipsum primis in faucibus. \
1765 Aliquam sit amet placerat lorem.
1766
1767 Cras a lacus vel ante posuere elementum. Nunc est leo, bibendum ut facilisis vel, bibendum at \
1768 mauris. Nullam adipiscing diam vel odio ornare, luctus adipiscing mi luctus. Nulla facilisi. \
1769 Mauris adipiscing bibendum neque, quis adipiscing lectus tempus et. Sed feugiat erat et nisl \
1770 lobortis pharetra. Donec vitae erat enim. Nullam sit amet felis et quam lacinia tincidunt. Aliquam \
1771 suscipit dapibus urna. Sed volutpat urna in magna pulvinar volutpat. Phasellus nec tellus ac diam \
1772 cursus accumsan.
1773
1774 Nam lectus enim, dapibus non nisi tempor, consectetur convallis massa. Maecenas eleifend dictum \
1775 feugiat. Etiam quis mauris vel risus luctus mattis a a nunc. Nullam orci quam, imperdiet id \
1776 vehicula in, porttitor ut nibh. Duis sagittis adipiscing nisl vitae congue. Donec mollis risus eu \
1777 leo suscipit, varius porttitor nulla porta. Pellentesque ut sem nec nisi euismod vehicula. Nulla \
1778 malesuada sollicitudin quam eu fermentum!");
1779 }
1780 }
1781 }
1782
1783 make_test!(chars_count, s, s.chars().count());
1784
1785 make_test!(contains_bang_str, s, s.contains("!"));
1786 make_test!(contains_bang_char, s, s.contains('!'));
1787
1788 make_test!(match_indices_a_str, s, s.match_indices("a").count());
1789
1790 make_test!(split_a_str, s, s.split("a").count());
1791
1792 make_test!(trim_ascii_char, s, {
1793 use std::ascii::AsciiExt;
1794 s.trim_matches(|c: char| c.is_ascii())
1795 });
1796 make_test!(trim_left_ascii_char, s, {
1797 use std::ascii::AsciiExt;
1798 s.trim_left_matches(|c: char| c.is_ascii())
1799 });
1800 make_test!(trim_right_ascii_char, s, {
1801 use std::ascii::AsciiExt;
1802 s.trim_right_matches(|c: char| c.is_ascii())
1803 });
1804
1805 make_test!(find_underscore_char, s, s.find('_'));
1806 make_test!(rfind_underscore_char, s, s.rfind('_'));
1807 make_test!(find_underscore_str, s, s.find("_"));
1808
1809 make_test!(find_zzz_char, s, s.find('\u{1F4A4}'));
1810 make_test!(rfind_zzz_char, s, s.rfind('\u{1F4A4}'));
1811 make_test!(find_zzz_str, s, s.find("\u{1F4A4}"));
1812
1813 make_test!(split_space_char, s, s.split(' ').count());
1814 make_test!(split_terminator_space_char, s, s.split_terminator(' ').count());
1815
1816 make_test!(splitn_space_char, s, s.splitn(10, ' ').count());
1817 make_test!(rsplitn_space_char, s, s.rsplitn(10, ' ').count());
1818
1819 make_test!(split_space_str, s, s.split(" ").count());
1820 make_test!(split_ad_str, s, s.split("ad").count());
1821 }