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.
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.
11 use std
::cmp
::Ordering
::{Equal, Greater, Less}
;
12 use std
::str::from_utf8
;
18 assert
!("foo" <= "foo");
19 assert
!("foo" != "bar");
24 assert_eq
!("hello".find('l'
), Some(2));
25 assert_eq
!("hello".find(|c
:char| c
== 'o'
), Some(4));
26 assert
!("hello".find('x'
).is_none());
27 assert
!("hello".find(|c
:char| c
== 'x'
).is_none());
28 assert_eq
!("ประเทศไทย中华Việt Nam".find('华'
), Some(30));
29 assert_eq
!("ประเทศไทย中华Việt Nam".find(|c
: char| c
== '华'
), Some(30));
34 assert_eq
!("hello".rfind('l'
), Some(3));
35 assert_eq
!("hello".rfind(|c
:char| c
== 'o'
), Some(4));
36 assert
!("hello".rfind('x'
).is_none());
37 assert
!("hello".rfind(|c
:char| c
== 'x'
).is_none());
38 assert_eq
!("ประเทศไทย中华Việt Nam".rfind('华'
), Some(30));
39 assert_eq
!("ประเทศไทย中华Việt Nam".rfind(|c
: char| c
== '华'
), Some(30));
44 let empty
= String
::from("");
45 let s
: String
= empty
.chars().collect();
47 let data
= String
::from("ประเทศไทย中");
48 let s
: String
= data
.chars().collect();
53 fn test_into_bytes() {
54 let data
= String
::from("asdf");
55 let buf
= data
.into_bytes();
56 assert_eq
!(buf
, b
"asdf");
62 assert_eq
!("".find(""), Some(0));
63 assert
!("banana".find("apple pie").is_none());
66 assert_eq
!(data
[0..6].find("ab"), Some(0));
67 assert_eq
!(data
[2..6].find("ab"), Some(3 - 2));
68 assert
!(data
[2..4].find("ab").is_none());
70 let string
= "ประเทศไทย中华Việt Nam";
71 let mut data
= String
::from(string
);
72 data
.push_str(string
);
73 assert
!(data
.find("ไท华").is_none());
74 assert_eq
!(data
[0..43].find(""), Some(0));
75 assert_eq
!(data
[6..43].find(""), Some(6 - 6));
77 assert_eq
!(data
[0..43].find("ประ"), Some( 0));
78 assert_eq
!(data
[0..43].find("ทศไ"), Some(12));
79 assert_eq
!(data
[0..43].find("ย中"), Some(24));
80 assert_eq
!(data
[0..43].find("iệt"), Some(34));
81 assert_eq
!(data
[0..43].find("Nam"), Some(40));
83 assert_eq
!(data
[43..86].find("ประ"), Some(43 - 43));
84 assert_eq
!(data
[43..86].find("ทศไ"), Some(55 - 43));
85 assert_eq
!(data
[43..86].find("ย中"), Some(67 - 43));
86 assert_eq
!(data
[43..86].find("iệt"), Some(77 - 43));
87 assert_eq
!(data
[43..86].find("Nam"), Some(83 - 43));
89 // find every substring -- assert that it finds it, or an earlier occurence.
90 let string
= "Việt Namacbaabcaabaaba";
91 for (i
, ci
) in string
.char_indices() {
92 let ip
= i
+ ci
.len_utf8();
93 for j
in string
[ip
..].char_indices()
95 .chain(Some(string
.len() - ip
))
97 let pat
= &string
[i
..ip
+ j
];
98 assert
!(match string
.find(pat
) {
102 assert
!(match string
.rfind(pat
) {
110 fn s(x
: &str) -> String { x.to_string() }
112 macro_rules
! test_concat
{
113 ($expected
: expr
, $string
: expr
) => {
115 let s
: String
= $string
.concat();
116 assert_eq
!($expected
, s
);
122 fn test_concat_for_different_types() {
123 test_concat
!("ab", vec
![s("a"), s("b")]);
124 test_concat
!("ab", vec
!["a", "b"]);
125 test_concat
!("ab", vec
!["a", "b"]);
126 test_concat
!("ab", vec
![s("a"), s("b")]);
130 fn test_concat_for_different_lengths() {
131 let empty
: &[&str] = &[];
132 test_concat
!("", empty
);
133 test_concat
!("a", ["a"]);
134 test_concat
!("ab", ["a", "b"]);
135 test_concat
!("abc", ["", "a", "bc"]);
138 macro_rules
! test_join
{
139 ($expected
: expr
, $string
: expr
, $delim
: expr
) => {
141 let s
= $string
.join($delim
);
142 assert_eq
!($expected
, s
);
148 fn test_join_for_different_types() {
149 test_join
!("a-b", ["a", "b"], "-");
150 let hyphen
= "-".to_string();
151 test_join
!("a-b", [s("a"), s("b")], &*hyphen
);
152 test_join
!("a-b", vec
!["a", "b"], &*hyphen
);
153 test_join
!("a-b", &*vec
!["a", "b"], "-");
154 test_join
!("a-b", vec
![s("a"), s("b")], "-");
158 fn test_join_for_different_lengths() {
159 let empty
: &[&str] = &[];
160 test_join
!("", empty
, "-");
161 test_join
!("a", ["a"], "-");
162 test_join
!("a-b", ["a", "b"], "-");
163 test_join
!("-a-bc", ["", "a", "bc"], "-");
167 fn test_unsafe_slice() {
168 assert_eq
!("ab", unsafe {"abc".slice_unchecked(0, 2)}
);
169 assert_eq
!("bc", unsafe {"abc".slice_unchecked(1, 3)}
);
170 assert_eq
!("", unsafe {"abc".slice_unchecked(1, 1)}
);
171 fn a_million_letter_a() -> String
{
173 let mut rs
= String
::new();
175 rs
.push_str("aaaaaaaaaa");
180 fn half_a_million_letter_a() -> String
{
182 let mut rs
= String
::new();
184 rs
.push_str("aaaaa");
189 let letters
= a_million_letter_a();
190 assert
!(half_a_million_letter_a() ==
191 unsafe {String
::from(letters
.slice_unchecked(
197 fn test_starts_with() {
198 assert
!(("".starts_with("")));
199 assert
!(("abc".starts_with("")));
200 assert
!(("abc".starts_with("a")));
201 assert
!((!"a".starts_with("abc")));
202 assert
!((!"".starts_with("abc")));
203 assert
!((!"ödd".starts_with("-")));
204 assert
!(("ödd".starts_with("öd")));
208 fn test_ends_with() {
209 assert
!(("".ends_with("")));
210 assert
!(("abc".ends_with("")));
211 assert
!(("abc".ends_with("c")));
212 assert
!((!"a".ends_with("abc")));
213 assert
!((!"".ends_with("abc")));
214 assert
!((!"ddö".ends_with("-")));
215 assert
!(("ddö".ends_with("dö")));
220 assert
!("".is_empty());
221 assert
!(!"a".is_empty());
227 assert_eq
!("".replace(a
, "b"), String
::from(""));
228 assert_eq
!("a".replace(a
, "b"), String
::from("b"));
229 assert_eq
!("ab".replace(a
, "b"), String
::from("bb"));
231 assert
!(" test test ".replace(test
, "toast") ==
232 String
::from(" toast toast "));
233 assert_eq
!(" test test ".replace(test
, ""), String
::from(" "));
237 fn test_replace_2a() {
238 let data
= "ประเทศไทย中华";
239 let repl
= "دولة الكويت";
242 let a2
= "دولة الكويتทศไทย中华";
243 assert_eq
!(data
.replace(a
, repl
), a2
);
247 fn test_replace_2b() {
248 let data
= "ประเทศไทย中华";
249 let repl
= "دولة الكويت";
252 let b2
= "ปรدولة الكويتทศไทย中华";
253 assert_eq
!(data
.replace(b
, repl
), b2
);
257 fn test_replace_2c() {
258 let data
= "ประเทศไทย中华";
259 let repl
= "دولة الكويت";
262 let c2
= "ประเทศไทยدولة الكويت";
263 assert_eq
!(data
.replace(c
, repl
), c2
);
267 fn test_replace_2d() {
268 let data
= "ประเทศไทย中华";
269 let repl
= "دولة الكويت";
272 assert_eq
!(data
.replace(d
, repl
), data
);
277 assert_eq
!("ab", &"abc"[0..2]);
278 assert_eq
!("bc", &"abc"[1..3]);
279 assert_eq
!("", &"abc"[1..1]);
280 assert_eq
!("\u{65e5}", &"\u{65e5}\u{672c}"[0..3]);
282 let data
= "ประเทศไทย中华";
283 assert_eq
!("ป", &data
[0..3]);
284 assert_eq
!("ร", &data
[3..6]);
285 assert_eq
!("", &data
[3..3]);
286 assert_eq
!("华", &data
[30..33]);
288 fn a_million_letter_x() -> String
{
290 let mut rs
= String
::new();
292 rs
.push_str("华华华华华华华华华华");
297 fn half_a_million_letter_x() -> String
{
299 let mut rs
= String
::new();
301 rs
.push_str("华华华华华");
306 let letters
= a_million_letter_x();
307 assert
!(half_a_million_letter_x() ==
308 String
::from(&letters
[0..3 * 500000]));
313 let ss
= "中华Việt Nam";
315 assert_eq
!("华", &ss
[3..6]);
316 assert_eq
!("Việt Nam", &ss
[6..16]);
318 assert_eq
!("ab", &"abc"[0..2]);
319 assert_eq
!("bc", &"abc"[1..3]);
320 assert_eq
!("", &"abc"[1..1]);
322 assert_eq
!("中", &ss
[0..3]);
323 assert_eq
!("华V", &ss
[3..7]);
324 assert_eq
!("", &ss
[3..3]);
339 fn test_slice_fail() {
344 fn test_slice_from() {
345 assert_eq
!(&"abcd"[0..], "abcd");
346 assert_eq
!(&"abcd"[2..], "cd");
347 assert_eq
!(&"abcd"[4..], "");
351 assert_eq
!(&"abcd"[..0], "");
352 assert_eq
!(&"abcd"[..2], "ab");
353 assert_eq
!(&"abcd"[..4], "abcd");
357 fn test_trim_left_matches() {
358 let v
: &[char] = &[];
359 assert_eq
!(" *** foo *** ".trim_left_matches(v
), " *** foo *** ");
360 let chars
: &[char] = &['
*'
, ' '
];
361 assert_eq
!(" *** foo *** ".trim_left_matches(chars
), "foo *** ");
362 assert_eq
!(" *** *** ".trim_left_matches(chars
), "");
363 assert_eq
!("foo *** ".trim_left_matches(chars
), "foo *** ");
365 assert_eq
!("11foo1bar11".trim_left_matches('
1'
), "foo1bar11");
366 let chars
: &[char] = &['
1'
, '
2'
];
367 assert_eq
!("12foo1bar12".trim_left_matches(chars
), "foo1bar12");
368 assert_eq
!("123foo1bar123".trim_left_matches(|c
: char| c
.is_numeric()), "foo1bar123");
372 fn test_trim_right_matches() {
373 let v
: &[char] = &[];
374 assert_eq
!(" *** foo *** ".trim_right_matches(v
), " *** foo *** ");
375 let chars
: &[char] = &['
*'
, ' '
];
376 assert_eq
!(" *** foo *** ".trim_right_matches(chars
), " *** foo");
377 assert_eq
!(" *** *** ".trim_right_matches(chars
), "");
378 assert_eq
!(" *** foo".trim_right_matches(chars
), " *** foo");
380 assert_eq
!("11foo1bar11".trim_right_matches('
1'
), "11foo1bar");
381 let chars
: &[char] = &['
1'
, '
2'
];
382 assert_eq
!("12foo1bar12".trim_right_matches(chars
), "12foo1bar");
383 assert_eq
!("123foo1bar123".trim_right_matches(|c
: char| c
.is_numeric()), "123foo1bar");
387 fn test_trim_matches() {
388 let v
: &[char] = &[];
389 assert_eq
!(" *** foo *** ".trim_matches(v
), " *** foo *** ");
390 let chars
: &[char] = &['
*'
, ' '
];
391 assert_eq
!(" *** foo *** ".trim_matches(chars
), "foo");
392 assert_eq
!(" *** *** ".trim_matches(chars
), "");
393 assert_eq
!("foo".trim_matches(chars
), "foo");
395 assert_eq
!("11foo1bar11".trim_matches('
1'
), "foo1bar");
396 let chars
: &[char] = &['
1'
, '
2'
];
397 assert_eq
!("12foo1bar12".trim_matches(chars
), "foo1bar");
398 assert_eq
!("123foo1bar123".trim_matches(|c
: char| c
.is_numeric()), "foo1bar");
402 fn test_trim_left() {
403 assert_eq
!("".trim_left(), "");
404 assert_eq
!("a".trim_left(), "a");
405 assert_eq
!(" ".trim_left(), "");
406 assert_eq
!(" blah".trim_left(), "blah");
407 assert_eq
!(" \u{3000} wut".trim_left(), "wut");
408 assert_eq
!("hey ".trim_left(), "hey ");
412 fn test_trim_right() {
413 assert_eq
!("".trim_right(), "");
414 assert_eq
!("a".trim_right(), "a");
415 assert_eq
!(" ".trim_right(), "");
416 assert_eq
!("blah ".trim_right(), "blah");
417 assert_eq
!("wut \u{3000} ".trim_right(), "wut");
418 assert_eq
!(" hey".trim_right(), " hey");
423 assert_eq
!("".trim(), "");
424 assert_eq
!("a".trim(), "a");
425 assert_eq
!(" ".trim(), "");
426 assert_eq
!(" blah ".trim(), "blah");
427 assert_eq
!("\nwut \u{3000} ".trim(), "wut");
428 assert_eq
!(" hey dude ".trim(), "hey dude");
432 fn test_is_whitespace() {
433 assert
!("".chars().all(|c
| c
.is_whitespace()));
434 assert
!(" ".chars().all(|c
| c
.is_whitespace()));
435 assert
!("\u{2009}".chars().all(|c
| c
.is_whitespace())); // Thin space
436 assert
!(" \n\t ".chars().all(|c
| c
.is_whitespace()));
437 assert
!(!" _ ".chars().all(|c
| c
.is_whitespace()));
441 fn test_slice_shift_char() {
442 let data
= "ประเทศไทย中";
443 assert_eq
!(data
.slice_shift_char(), Some(('ป'
, "ระเทศไทย中")));
447 fn test_slice_shift_char_2() {
449 assert_eq
!(empty
.slice_shift_char(), None
);
454 // deny overlong encodings
455 assert
!(from_utf8(&[0xc0, 0x80]).is_err());
456 assert
!(from_utf8(&[0xc0, 0xae]).is_err());
457 assert
!(from_utf8(&[0xe0, 0x80, 0x80]).is_err());
458 assert
!(from_utf8(&[0xe0, 0x80, 0xaf]).is_err());
459 assert
!(from_utf8(&[0xe0, 0x81, 0x81]).is_err());
460 assert
!(from_utf8(&[0xf0, 0x82, 0x82, 0xac]).is_err());
461 assert
!(from_utf8(&[0xf4, 0x90, 0x80, 0x80]).is_err());
464 assert
!(from_utf8(&[0xED, 0xA0, 0x80]).is_err());
465 assert
!(from_utf8(&[0xED, 0xBF, 0xBF]).is_err());
467 assert
!(from_utf8(&[0xC2, 0x80]).is_ok());
468 assert
!(from_utf8(&[0xDF, 0xBF]).is_ok());
469 assert
!(from_utf8(&[0xE0, 0xA0, 0x80]).is_ok());
470 assert
!(from_utf8(&[0xED, 0x9F, 0xBF]).is_ok());
471 assert
!(from_utf8(&[0xEE, 0x80, 0x80]).is_ok());
472 assert
!(from_utf8(&[0xEF, 0xBF, 0xBF]).is_ok());
473 assert
!(from_utf8(&[0xF0, 0x90, 0x80, 0x80]).is_ok());
474 assert
!(from_utf8(&[0xF4, 0x8F, 0xBF, 0xBF]).is_ok());
479 use rustc_unicode
::str::is_utf16
;
482 ($
($e
:expr
),*) => { { $(assert!(is_utf16($e));)* }
}
491 // surrogate pairs (randomly generated with Python 3's
492 // .encode('utf-16be'))
493 pos
!(&[0xdb54, 0xdf16, 0xd880, 0xdee0, 0xdb6a, 0xdd45],
494 &[0xd91f, 0xdeb1, 0xdb31, 0xdd84, 0xd8e2, 0xde14],
495 &[0xdb9f, 0xdc26, 0xdb6f, 0xde58, 0xd850, 0xdfae]);
497 // mixtures (also random)
498 pos
!(&[0xd921, 0xdcc2, 0x002d, 0x004d, 0xdb32, 0xdf65],
499 &[0xdb45, 0xdd2d, 0x006a, 0xdacd, 0xddfe, 0x0006],
500 &[0x0067, 0xd8ff, 0xddb7, 0x000f, 0xd900, 0xdc80]);
504 ($
($e
:expr
),*) => { { $(assert!(!is_utf16($e));)* }
}
508 // surrogate + regular unit
510 // surrogate + lead surrogate
512 // unterminated surrogate
514 // trail surrogate without a lead
517 // random byte sequences that Python 3's .decode('utf-16be')
519 neg
!(&[0x5b3d, 0x0141, 0xde9e, 0x8fdc, 0xc6e7],
520 &[0xdf5a, 0x82a5, 0x62b9, 0xb447, 0x92f3],
521 &[0xda4e, 0x42bc, 0x4462, 0xee98, 0xc2ca],
522 &[0xbe00, 0xb04a, 0x6ecb, 0xdd89, 0xe278],
523 &[0x0465, 0xab56, 0xdbb6, 0xa893, 0x665e],
524 &[0x6b7f, 0x0a19, 0x40f4, 0xa657, 0xdcc5],
525 &[0x9b50, 0xda5e, 0x24ec, 0x03ad, 0x6dee],
526 &[0x8d17, 0xcaa7, 0xf4ae, 0xdf6e, 0xbed7],
527 &[0xdaee, 0x2584, 0x7d30, 0xa626, 0x121a],
528 &[0xd956, 0x4b43, 0x7570, 0xccd6, 0x4f4a],
529 &[0x9dcf, 0x1b49, 0x4ba5, 0xfce9, 0xdffe],
530 &[0x6572, 0xce53, 0xb05a, 0xf6af, 0xdacf],
531 &[0x1b90, 0x728c, 0x9906, 0xdb68, 0xf46e],
532 &[0x1606, 0xbeca, 0xbe76, 0x860f, 0xdfa5],
533 &[0x8b4f, 0xde7a, 0xd220, 0x9fac, 0x2b6f],
534 &[0xb8fe, 0xebbe, 0xda32, 0x1a5f, 0x8b8b],
535 &[0x934b, 0x8956, 0xc434, 0x1881, 0xddf7],
536 &[0x5a95, 0x13fc, 0xf116, 0xd89b, 0x93f9],
537 &[0xd640, 0x71f1, 0xdd7d, 0x77eb, 0x1cd8],
538 &[0x348b, 0xaef0, 0xdb2c, 0xebf1, 0x1282],
539 &[0x50d7, 0xd824, 0x5010, 0xb369, 0x22ea]);
546 224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228,
547 184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97,
551 assert_eq
!("".as_bytes(), b
);
552 assert_eq
!("abc".as_bytes(), b
"abc");
553 assert_eq
!("ศไทย中华Việt Nam".as_bytes(), v
);
558 fn test_as_bytes_fail() {
559 // Don't double free. (I'm not sure if this exercises the
560 // original problem code path anymore.)
561 let s
= String
::from("");
562 let _bytes
= s
.as_bytes();
568 let buf
= "hello".as_ptr();
570 assert_eq
!(*buf
.offset(0), b'h'
);
571 assert_eq
!(*buf
.offset(1), b'e'
);
572 assert_eq
!(*buf
.offset(2), b'l'
);
573 assert_eq
!(*buf
.offset(3), b'l'
);
574 assert_eq
!(*buf
.offset(4), b'o'
);
579 fn vec_str_conversions() {
580 let s1
: String
= String
::from("All mimsy were the borogoves");
582 let v
: Vec
<u8> = s1
.as_bytes().to_vec();
583 let s2
: String
= String
::from(from_utf8(&v
).unwrap());
589 let a
: u8 = s1
.as_bytes()[i
];
590 let b
: u8 = s2
.as_bytes()[i
];
600 assert
!("abcde".contains("bcd"));
601 assert
!("abcde".contains("abcd"));
602 assert
!("abcde".contains("bcde"));
603 assert
!("abcde".contains(""));
604 assert
!("".contains(""));
605 assert
!(!"abcde".contains("def"));
606 assert
!(!"".contains("a"));
608 let data
= "ประเทศไทย中华Việt Nam";
609 assert
!(data
.contains("ประเ"));
610 assert
!(data
.contains("ะเ"));
611 assert
!(data
.contains("中华"));
612 assert
!(!data
.contains("ไท华"));
616 fn test_contains_char() {
617 assert
!("abc".contains('b'
));
618 assert
!("a".contains('a'
));
619 assert
!(!"abc".contains('d'
));
620 assert
!(!"".contains('a'
));
625 let s
= "ศไทย中华Việt Nam";
626 let v
= vec
!['ศ'
,'ไ'
,'ท'
,'ย'
,'中'
,'华'
,'V'
,'i'
,'ệ'
,'t'
,' '
,'N'
,'a'
,'m'
];
629 assert
!(s
.char_at(pos
) == *ch
);
630 pos
+= ch
.to_string().len();
635 fn test_char_at_reverse() {
636 let s
= "ศไทย中华Việt Nam";
637 let v
= vec
!['ศ'
,'ไ'
,'ท'
,'ย'
,'中'
,'华'
,'V'
,'i'
,'ệ'
,'t'
,' '
,'N'
,'a'
,'m'
];
638 let mut pos
= s
.len();
639 for ch
in v
.iter().rev() {
640 assert
!(s
.char_at_reverse(pos
) == *ch
);
641 pos
-= ch
.to_string().len();
647 let s
= "ศไทย中华Việt Nam";
648 for (index
, _
) in s
.char_indices() {
649 let (a
, b
) = s
.split_at(index
);
650 assert_eq
!(&s
[..a
.len()], a
);
651 assert_eq
!(&s
[a
.len()..], b
);
653 let (a
, b
) = s
.split_at(s
.len());
659 fn test_split_at_mut() {
660 use std
::ascii
::AsciiExt
;
661 let mut s
= "Hello World".to_string();
663 let (a
, b
) = s
.split_at_mut(5);
664 a
.make_ascii_uppercase();
665 b
.make_ascii_lowercase();
667 assert_eq
!(s
, "HELLO world");
672 fn test_split_at_boundscheck() {
673 let s
= "ศไทย中华Việt Nam";
678 fn test_escape_unicode() {
679 assert_eq
!("abc".escape_unicode(),
680 String
::from("\\u{61}\\u{62}\\u{63}"));
681 assert_eq
!("a c".escape_unicode(),
682 String
::from("\\u{61}\\u{20}\\u{63}"));
683 assert_eq
!("\r\n\t".escape_unicode(),
684 String
::from("\\u{d}\\u{a}\\u{9}"));
685 assert_eq
!("'\"\\".escape_unicode(),
686 String
::from("\\u{27}\\u{22}\\u{5c}"));
687 assert_eq
!("\x00\x01\u{fe}\u{ff}".escape_unicode(),
688 String
::from("\\u{0}\\u{1}\\u{fe}\\u{ff}"));
689 assert_eq
!("\u{100}\u{ffff}".escape_unicode(),
690 String
::from("\\u{100}\\u{ffff}"));
691 assert_eq
!("\u{10000}\u{10ffff}".escape_unicode(),
692 String
::from("\\u{10000}\\u{10ffff}"));
693 assert_eq
!("ab\u{fb00}".escape_unicode(),
694 String
::from("\\u{61}\\u{62}\\u{fb00}"));
695 assert_eq
!("\u{1d4ea}\r".escape_unicode(),
696 String
::from("\\u{1d4ea}\\u{d}"));
700 fn test_escape_default() {
701 assert_eq
!("abc".escape_default(), String
::from("abc"));
702 assert_eq
!("a c".escape_default(), String
::from("a c"));
703 assert_eq
!("\r\n\t".escape_default(), String
::from("\\r\\n\\t"));
704 assert_eq
!("'\"\\".escape_default(), String
::from("\\'\\\"\\\\"));
705 assert_eq
!("\u{100}\u{ffff}".escape_default(),
706 String
::from("\\u{100}\\u{ffff}"));
707 assert_eq
!("\u{10000}\u{10ffff}".escape_default(),
708 String
::from("\\u{10000}\\u{10ffff}"));
709 assert_eq
!("ab\u{fb00}".escape_default(),
710 String
::from("ab\\u{fb00}"));
711 assert_eq
!("\u{1d4ea}\r".escape_default(),
712 String
::from("\\u{1d4ea}\\r"));
716 fn test_total_ord() {
717 "1234".cmp("123") == Greater
;
718 "123".cmp("1234") == Less
;
719 "1234".cmp("1234") == Equal
;
720 "12345555".cmp("123456") == Less
;
721 "22".cmp("1234") == Greater
;
725 fn test_char_range_at() {
726 let data
= "b¢€𤭢𤭢€¢b";
727 assert_eq
!('b'
, data
.char_range_at(0).ch
);
728 assert_eq
!('¢'
, data
.char_range_at(1).ch
);
729 assert_eq
!('€'
, data
.char_range_at(3).ch
);
730 assert_eq
!('𤭢'
, data
.char_range_at(6).ch
);
731 assert_eq
!('𤭢'
, data
.char_range_at(10).ch
);
732 assert_eq
!('€'
, data
.char_range_at(14).ch
);
733 assert_eq
!('¢'
, data
.char_range_at(17).ch
);
734 assert_eq
!('b'
, data
.char_range_at(19).ch
);
738 fn test_char_range_at_reverse_underflow() {
739 assert_eq
!("abc".char_range_at_reverse(0).next
, 0);
744 let s
= "ศไทย中华Việt Nam";
745 let v
= ['ศ'
,'ไ'
,'ท'
,'ย'
,'中'
,'华'
,'V'
,'i'
,'ệ'
,'t'
,' '
,'N'
,'a'
,'m'
];
751 assert_eq
!(c
, v
[pos
]);
754 assert_eq
!(pos
, v
.len());
758 fn test_rev_iterator() {
759 let s
= "ศไทย中华Việt Nam";
760 let v
= ['m'
, 'a'
, 'N'
, ' '
, 't'
, 'ệ'
,'i'
,'V'
,'华'
,'中'
,'ย'
,'ท'
,'ไ'
,'ศ'
];
763 let it
= s
.chars().rev();
766 assert_eq
!(c
, v
[pos
]);
769 assert_eq
!(pos
, v
.len());
773 fn test_chars_decoding() {
774 let mut bytes
= [0; 4];
775 for c
in (0..0x110000).filter_map(::std
::char::from_u32
) {
776 let len
= c
.encode_utf8(&mut bytes
).unwrap_or(0);
777 let s
= ::std
::str::from_utf8(&bytes
[..len
]).unwrap();
778 if Some(c
) != s
.chars().next() {
779 panic
!("character {:x}={} does not decode correctly", c
as u32, c
);
785 fn test_chars_rev_decoding() {
786 let mut bytes
= [0; 4];
787 for c
in (0..0x110000).filter_map(::std
::char::from_u32
) {
788 let len
= c
.encode_utf8(&mut bytes
).unwrap_or(0);
789 let s
= ::std
::str::from_utf8(&bytes
[..len
]).unwrap();
790 if Some(c
) != s
.chars().rev().next() {
791 panic
!("character {:x}={} does not decode correctly", c
as u32, c
);
797 fn test_iterator_clone() {
798 let s
= "ศไทย中华Việt Nam";
799 let mut it
= s
.chars();
801 assert
!(it
.clone().zip(it
).all(|(x
,y
)| x
== y
));
805 fn test_bytesator() {
806 let s
= "ศไทย中华Việt Nam";
808 224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228,
809 184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97,
815 assert_eq
!(b
, v
[pos
]);
821 fn test_bytes_revator() {
822 let s
= "ศไทย中华Việt Nam";
824 224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228,
825 184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97,
828 let mut pos
= v
.len();
830 for b
in s
.bytes().rev() {
832 assert_eq
!(b
, v
[pos
]);
837 fn test_bytesator_nth() {
838 let s
= "ศไทย中华Việt Nam";
840 224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228,
841 184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97,
845 let mut b
= s
.bytes();
846 assert_eq
!(b
.nth(2).unwrap(), v
[2]);
847 assert_eq
!(b
.nth(10).unwrap(), v
[10]);
848 assert_eq
!(b
.nth(200), None
);
852 fn test_bytesator_count() {
853 let s
= "ศไทย中华Việt Nam";
856 assert_eq
!(b
.count(), 28)
860 fn test_bytesator_last() {
861 let s
= "ศไทย中华Việt Nam";
864 assert_eq
!(b
.last().unwrap(), 109)
868 fn test_char_indicesator() {
869 let s
= "ศไทย中华Việt Nam";
870 let p
= [0, 3, 6, 9, 12, 15, 18, 19, 20, 23, 24, 25, 26, 27];
871 let v
= ['ศ'
,'ไ'
,'ท'
,'ย'
,'中'
,'华'
,'V'
,'i'
,'ệ'
,'t'
,' '
,'N'
,'a'
,'m'
];
874 let it
= s
.char_indices();
877 assert_eq
!(c
, (p
[pos
], v
[pos
]));
880 assert_eq
!(pos
, v
.len());
881 assert_eq
!(pos
, p
.len());
885 fn test_char_indices_revator() {
886 let s
= "ศไทย中华Việt Nam";
887 let p
= [27, 26, 25, 24, 23, 20, 19, 18, 15, 12, 9, 6, 3, 0];
888 let v
= ['m'
, 'a'
, 'N'
, ' '
, 't'
, 'ệ'
,'i'
,'V'
,'华'
,'中'
,'ย'
,'ท'
,'ไ'
,'ศ'
];
891 let it
= s
.char_indices().rev();
894 assert_eq
!(c
, (p
[pos
], v
[pos
]));
897 assert_eq
!(pos
, v
.len());
898 assert_eq
!(pos
, p
.len());
902 fn test_splitn_char_iterator() {
903 let data
= "\nMäry häd ä little lämb\nLittle lämb\n";
905 let split
: Vec
<&str> = data
.splitn(4, ' '
).collect();
906 assert_eq
!(split
, ["\nMäry", "häd", "ä", "little lämb\nLittle lämb\n"]);
908 let split
: Vec
<&str> = data
.splitn(4, |c
: char| c
== ' '
).collect();
909 assert_eq
!(split
, ["\nMäry", "häd", "ä", "little lämb\nLittle lämb\n"]);
912 let split
: Vec
<&str> = data
.splitn(4, 'ä'
).collect();
913 assert_eq
!(split
, ["\nM", "ry h", "d ", " little lämb\nLittle lämb\n"]);
915 let split
: Vec
<&str> = data
.splitn(4, |c
: char| c
== 'ä'
).collect();
916 assert_eq
!(split
, ["\nM", "ry h", "d ", " little lämb\nLittle lämb\n"]);
920 fn test_split_char_iterator_no_trailing() {
921 let data
= "\nMäry häd ä little lämb\nLittle lämb\n";
923 let split
: Vec
<&str> = data
.split('
\n'
).collect();
924 assert_eq
!(split
, ["", "Märy häd ä little lämb", "Little lämb", ""]);
926 let split
: Vec
<&str> = data
.split_terminator('
\n'
).collect();
927 assert_eq
!(split
, ["", "Märy häd ä little lämb", "Little lämb"]);
932 let data
= "\nMäry häd ä little lämb\nLittle lämb\n";
934 let split
: Vec
<&str> = data
.rsplit(' '
).collect();
935 assert_eq
!(split
, ["lämb\n", "lämb\nLittle", "little", "ä", "häd", "\nMäry"]);
937 let split
: Vec
<&str> = data
.rsplit("lämb").collect();
938 assert_eq
!(split
, ["\n", "\nLittle ", "\nMäry häd ä little "]);
940 let split
: Vec
<&str> = data
.rsplit(|c
: char| c
== 'ä'
).collect();
941 assert_eq
!(split
, ["mb\n", "mb\nLittle l", " little l", "d ", "ry h", "\nM"]);
946 let data
= "\nMäry häd ä little lämb\nLittle lämb\n";
948 let split
: Vec
<&str> = data
.rsplitn(2, ' '
).collect();
949 assert_eq
!(split
, ["lämb\n", "\nMäry häd ä little lämb\nLittle"]);
951 let split
: Vec
<&str> = data
.rsplitn(2, "lämb").collect();
952 assert_eq
!(split
, ["\n", "\nMäry häd ä little lämb\nLittle "]);
954 let split
: Vec
<&str> = data
.rsplitn(2, |c
: char| c
== 'ä'
).collect();
955 assert_eq
!(split
, ["mb\n", "\nMäry häd ä little lämb\nLittle l"]);
959 fn test_split_whitespace() {
960 let data
= "\n \tMäry häd\tä little lämb\nLittle lämb\n";
961 let words
: Vec
<&str> = data
.split_whitespace().collect();
962 assert_eq
!(words
, ["Märy", "häd", "ä", "little", "lämb", "Little", "lämb"])
967 let data
= "\nMäry häd ä little lämb\n\r\nLittle lämb\n";
968 let lines
: Vec
<&str> = data
.lines().collect();
969 assert_eq
!(lines
, ["", "Märy häd ä little lämb", "", "Little lämb"]);
971 let data
= "\r\nMäry häd ä little lämb\n\nLittle lämb"; // no trailing \n
972 let lines
: Vec
<&str> = data
.lines().collect();
973 assert_eq
!(lines
, ["", "Märy häd ä little lämb", "", "Little lämb"]);
977 fn test_splitator() {
978 fn t(s
: &str, sep
: &str, u
: &[&str]) {
979 let v
: Vec
<&str> = s
.split(sep
).collect();
982 t("--1233345--", "12345", &["--1233345--"]);
983 t("abc::hello::there", "::", &["abc", "hello", "there"]);
984 t("::hello::there", "::", &["", "hello", "there"]);
985 t("hello::there::", "::", &["hello", "there", ""]);
986 t("::hello::there::", "::", &["", "hello", "there", ""]);
987 t("ประเทศไทย中华Việt Nam", "中华", &["ประเทศไทย", "Việt Nam"]);
988 t("zzXXXzzYYYzz", "zz", &["", "XXX", "YYY", ""]);
989 t("zzXXXzYYYz", "XXX", &["zz", "zYYYz"]);
990 t(".XXX.YYY.", ".", &["", "XXX", "YYY", ""]);
992 t("zz", "zz", &["",""]);
993 t("ok", "z", &["ok"]);
994 t("zzz", "zz", &["","z"]);
995 t("zzzzz", "zz", &["","","z"]);
999 fn test_str_default() {
1000 use std
::default::Default
;
1002 fn t
<S
: Default
+ AsRef
<str>>() {
1003 let s
: S
= Default
::default();
1004 assert_eq
!(s
.as_ref(), "");
1012 fn test_str_container() {
1013 fn sum_len(v
: &[&str]) -> usize {
1014 v
.iter().map(|x
| x
.len()).sum()
1017 let s
= String
::from("01234");
1018 assert_eq
!(5, sum_len(&["012", "", "34"]));
1019 assert_eq
!(5, sum_len(&[&String
::from("01"),
1021 &String
::from("34"),
1022 &String
::from("")]));
1023 assert_eq
!(5, sum_len(&[&s
]));
1027 fn test_str_from_utf8() {
1029 assert_eq
!(from_utf8(xs
), Ok("hello"));
1031 let xs
= "ศไทย中华Việt Nam".as_bytes();
1032 assert_eq
!(from_utf8(xs
), Ok("ศไทย中华Việt Nam"));
1034 let xs
= b
"hello\xFF";
1035 assert
!(from_utf8(xs
).is_err());
1039 fn test_pattern_deref_forward() {
1040 let data
= "aabcdaa";
1041 assert
!(data
.contains("bcd"));
1042 assert
!(data
.contains(&"bcd"));
1043 assert
!(data
.contains(&"bcd".to_string()));
1047 fn test_empty_match_indices() {
1049 let vec
: Vec
<_
> = data
.match_indices("").collect();
1050 assert_eq
!(vec
, [(0, 0), (1, 1), (3, 3), (6, 6), (7, 7)]);
1054 fn test_bool_from_str() {
1055 assert_eq
!("true".parse().ok(), Some(true));
1056 assert_eq
!("false".parse().ok(), Some(false));
1057 assert_eq
!("not even a boolean".parse
::<bool
>().ok(), None
);
1060 fn check_contains_all_substrings(s
: &str) {
1061 assert
!(s
.contains(""));
1062 for i
in 0..s
.len() {
1063 for j
in i
+1..s
.len() + 1 {
1064 assert
!(s
.contains(&s
[i
..j
]));
1070 fn strslice_issue_16589() {
1071 assert
!("bananas".contains("nana"));
1073 // prior to the fix for #16589, x.contains("abcdabcd") returned false
1074 // test all substrings for good measure
1075 check_contains_all_substrings("012345678901234567890123456789bcdabcdabcd");
1079 fn strslice_issue_16878() {
1080 assert
!(!"1234567ah012345678901ah".contains("hah"));
1081 assert
!(!"00abc01234567890123456789abc".contains("bcabc"));
1086 fn test_strslice_contains() {
1087 let x
= "There are moments, Jeeves, when one asks oneself, 'Do trousers matter?'";
1088 check_contains_all_substrings(x
);
1092 fn test_rsplitn_char_iterator() {
1093 let data
= "\nMäry häd ä little lämb\nLittle lämb\n";
1095 let mut split
: Vec
<&str> = data
.rsplitn(4, ' '
).collect();
1097 assert_eq
!(split
, ["\nMäry häd ä", "little", "lämb\nLittle", "lämb\n"]);
1099 let mut split
: Vec
<&str> = data
.rsplitn(4, |c
: char| c
== ' '
).collect();
1101 assert_eq
!(split
, ["\nMäry häd ä", "little", "lämb\nLittle", "lämb\n"]);
1104 let mut split
: Vec
<&str> = data
.rsplitn(4, 'ä'
).collect();
1106 assert_eq
!(split
, ["\nMäry häd ", " little l", "mb\nLittle l", "mb\n"]);
1108 let mut split
: Vec
<&str> = data
.rsplitn(4, |c
: char| c
== 'ä'
).collect();
1110 assert_eq
!(split
, ["\nMäry häd ", " little l", "mb\nLittle l", "mb\n"]);
1114 fn test_split_char_iterator() {
1115 let data
= "\nMäry häd ä little lämb\nLittle lämb\n";
1117 let split
: Vec
<&str> = data
.split(' '
).collect();
1118 assert_eq
!( split
, ["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
1120 let mut rsplit
: Vec
<&str> = data
.split(' '
).rev().collect();
1122 assert_eq
!(rsplit
, ["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
1124 let split
: Vec
<&str> = data
.split(|c
: char| c
== ' '
).collect();
1125 assert_eq
!( split
, ["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
1127 let mut rsplit
: Vec
<&str> = data
.split(|c
: char| c
== ' '
).rev().collect();
1129 assert_eq
!(rsplit
, ["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
1132 let split
: Vec
<&str> = data
.split('ä'
).collect();
1133 assert_eq
!( split
, ["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
1135 let mut rsplit
: Vec
<&str> = data
.split('ä'
).rev().collect();
1137 assert_eq
!(rsplit
, ["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
1139 let split
: Vec
<&str> = data
.split(|c
: char| c
== 'ä'
).collect();
1140 assert_eq
!( split
, ["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
1142 let mut rsplit
: Vec
<&str> = data
.split(|c
: char| c
== 'ä'
).rev().collect();
1144 assert_eq
!(rsplit
, ["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
1148 fn test_rev_split_char_iterator_no_trailing() {
1149 let data
= "\nMäry häd ä little lämb\nLittle lämb\n";
1151 let mut split
: Vec
<&str> = data
.split('
\n'
).rev().collect();
1153 assert_eq
!(split
, ["", "Märy häd ä little lämb", "Little lämb", ""]);
1155 let mut split
: Vec
<&str> = data
.split_terminator('
\n'
).rev().collect();
1157 assert_eq
!(split
, ["", "Märy häd ä little lämb", "Little lämb"]);
1161 fn test_utf16_code_units() {
1162 use rustc_unicode
::str::Utf16Encoder
;
1163 assert_eq
!(Utf16Encoder
::new(vec
!['é'
, '
\u{1F4A9}'
].into_iter()).collect
::<Vec
<u16>>(),
1164 [0xE9, 0xD83D, 0xDCA9])
1168 fn starts_with_in_unicode() {
1169 assert
!(!"├── Cargo.toml".starts_with("# "));
1173 fn starts_short_long() {
1174 assert
!(!"".starts_with("##"));
1175 assert
!(!"##".starts_with("####"));
1176 assert
!("####".starts_with("##"));
1177 assert
!(!"##ä".starts_with("####"));
1178 assert
!("####ä".starts_with("##"));
1179 assert
!(!"##".starts_with("####ä"));
1180 assert
!("##ä##".starts_with("##ä"));
1182 assert
!("".starts_with(""));
1183 assert
!("ä".starts_with(""));
1184 assert
!("#ä".starts_with(""));
1185 assert
!("##ä".starts_with(""));
1186 assert
!("ä###".starts_with(""));
1187 assert
!("#ä##".starts_with(""));
1188 assert
!("##ä#".starts_with(""));
1192 fn contains_weird_cases() {
1193 assert
!("* \t".contains(' '
));
1194 assert
!(!"* \t".contains('?'
));
1195 assert
!(!"* \t".contains('
\u{1F4A9}'
));
1200 assert_eq
!(" \t a \t ".trim_left_matches(|c
: char| c
.is_whitespace()),
1202 assert_eq
!(" \t a \t ".trim_right_matches(|c
: char| c
.is_whitespace()),
1204 assert_eq
!(" \t a \t ".trim_matches(|c
: char| c
.is_whitespace()),
1206 assert_eq
!(" \t \t ".trim_left_matches(|c
: char| c
.is_whitespace()),
1208 assert_eq
!(" \t \t ".trim_right_matches(|c
: char| c
.is_whitespace()),
1210 assert_eq
!(" \t \t ".trim_matches(|c
: char| c
.is_whitespace()),
1216 assert_eq
!("".to_lowercase(), "");
1217 assert_eq
!("AÉDžaé ".to_lowercase(), "aédžaé ");
1219 // https://github.com/rust-lang/rust/issues/26035
1220 assert_eq
!("ΑΣ".to_lowercase(), "ας");
1221 assert_eq
!("Α'Σ".to_lowercase(), "α'ς");
1222 assert_eq
!("Α''Σ".to_lowercase(), "α''ς");
1224 assert_eq
!("ΑΣ Α".to_lowercase(), "ας α");
1225 assert_eq
!("Α'Σ Α".to_lowercase(), "α'ς α");
1226 assert_eq
!("Α''Σ Α".to_lowercase(), "α''ς α");
1228 assert_eq
!("ΑΣ' Α".to_lowercase(), "ας' α");
1229 assert_eq
!("ΑΣ'' Α".to_lowercase(), "ας'' α");
1231 assert_eq
!("Α'Σ' Α".to_lowercase(), "α'ς' α");
1232 assert_eq
!("Α''Σ'' Α".to_lowercase(), "α''ς'' α");
1234 assert_eq
!("Α Σ".to_lowercase(), "α σ");
1235 assert_eq
!("Α 'Σ".to_lowercase(), "α 'σ");
1236 assert_eq
!("Α ''Σ".to_lowercase(), "α ''σ");
1238 assert_eq
!("Σ".to_lowercase(), "σ");
1239 assert_eq
!("'Σ".to_lowercase(), "'σ");
1240 assert_eq
!("''Σ".to_lowercase(), "''σ");
1242 assert_eq
!("ΑΣΑ".to_lowercase(), "ασα");
1243 assert_eq
!("ΑΣ'Α".to_lowercase(), "ασ'α");
1244 assert_eq
!("ΑΣ''Α".to_lowercase(), "ασ''α");
1249 assert_eq
!("".to_uppercase(), "");
1250 assert_eq
!("aéDžßfiᾀ".to_uppercase(), "AÉDŽSSFIἈΙ");
1254 fn test_into_string() {
1255 // The only way to acquire a Box<str> in the first place is through a String, so just
1256 // test that we can round-trip between Box<str> and String.
1257 let string
= String
::from("Some text goes here");
1258 assert_eq
!(string
.clone().into_boxed_str().into_string(), string
);
1262 fn test_box_slice_clone() {
1263 let data
= String
::from("hello HELLO hello HELLO yes YES 5 中ä华!!!");
1264 let data2
= data
.clone().into_boxed_str().clone().into_string();
1266 assert_eq
!(data
, data2
);
1270 use std
::str::pattern
::Pattern
;
1271 use std
::str::pattern
::{Searcher, ReverseSearcher}
;
1272 use std
::str::pattern
::SearchStep
::{self, Match, Reject, Done}
;
1274 macro_rules
! make_test
{
1275 ($name
:ident
, $p
:expr
, $h
:expr
, [$
($e
:expr
,)*]) => {
1276 #[allow(unused_imports)]
1278 use std
::str::pattern
::SearchStep
::{Match, Reject}
;
1279 use super::{cmp_search_to_vec}
;
1282 cmp_search_to_vec(false, $p
, $h
, vec
![$
($e
),*]);
1286 cmp_search_to_vec(true, $p
, $h
, vec
![$
($e
),*]);
1292 fn cmp_search_to_vec
<'a
, P
: Pattern
<'a
>>(rev
: bool
, pat
: P
, haystack
: &'a
str,
1293 right
: Vec
<SearchStep
>)
1294 where P
::Searcher
: ReverseSearcher
<'a
>
1296 let mut searcher
= pat
.into_searcher(haystack
);
1299 match if !rev {searcher.next()}
else {searcher.next_back()}
{
1300 Match(a
, b
) => v
.push(Match(a
, b
)),
1301 Reject(a
, b
) => v
.push(Reject(a
, b
)),
1309 let mut first_index
= 0;
1312 for (i
, e
) in right
.iter().enumerate() {
1314 Match(a
, b
) | Reject(a
, b
)
1315 if a
<= b
&& a
== first_index
=> {
1325 if let Some(err
) = err
{
1326 panic
!("Input skipped range at {}", err
);
1329 if first_index
!= haystack
.len() {
1330 panic
!("Did not cover whole input");
1333 assert_eq
!(v
, right
);
1336 make_test
!(str_searcher_ascii_haystack
, "bb", "abbcbbd", [
1343 make_test
!(str_searcher_ascii_haystack_seq
, "bb", "abbcbbbbd", [
1351 make_test
!(str_searcher_empty_needle_ascii_haystack
, "", "abbcbbd", [
1368 make_test
!(str_searcher_mulibyte_haystack
, " ", "├──", [
1373 make_test
!(str_searcher_empty_needle_mulibyte_haystack
, "", "├──", [
1382 make_test
!(str_searcher_empty_needle_empty_haystack
, "", "", [
1385 make_test
!(str_searcher_nonempty_needle_empty_haystack
, "├", "", [
1387 make_test
!(char_searcher_ascii_haystack
, 'b'
, "abbcbbd", [
1396 make_test
!(char_searcher_mulibyte_haystack
, ' '
, "├──", [
1401 make_test
!(char_searcher_short_haystack
, '
\u{1F4A9}'
, "* \t", [
1409 macro_rules
! generate_iterator_test
{
1413 ($
($arg
:expr
),*) -> [$
($t
:tt
)*];
1416 with $fwd
:expr
, $bwd
:expr
;
1422 let res
= vec
![$
($t
)*];
1424 let fwd_vec
: Vec
<_
> = ($fwd
)($
($arg
),*).collect();
1425 assert_eq
!(fwd_vec
, res
);
1427 let mut bwd_vec
: Vec
<_
> = ($bwd
)($
($arg
),*).collect();
1429 assert_eq
!(bwd_vec
, res
);
1437 ($
($arg
:expr
),*) -> [$
($t
:tt
)*];
1446 let res
= vec
![$
($t
)*];
1448 let fwd_vec
: Vec
<_
> = ($fwd
)($
($arg
),*).collect();
1449 assert_eq
!(fwd_vec
, res
);
1456 generate_iterator_test
! {
1457 double_ended_split
{
1458 ("foo.bar.baz", '
.'
) -> ["foo", "bar", "baz"];
1459 ("foo::bar::baz", "::") -> ["foo", "bar", "baz"];
1461 with
str::split
, str::rsplit
;
1464 generate_iterator_test
! {
1465 double_ended_split_terminator
{
1466 ("foo;bar;baz;", '
;'
) -> ["foo", "bar", "baz"];
1468 with
str::split_terminator
, str::rsplit_terminator
;
1471 generate_iterator_test
! {
1472 double_ended_matches
{
1473 ("a1b2c3", char::is_numeric
) -> ["1", "2", "3"];
1475 with
str::matches
, str::rmatches
;
1478 generate_iterator_test
! {
1479 double_ended_match_indices
{
1480 ("a1b2c3", char::is_numeric
) -> [(1, 2), (3, 4), (5, 6)];
1482 with
str::match_indices
, str::rmatch_indices
;
1485 generate_iterator_test
! {
1486 not_double_ended_splitn
{
1487 ("foo::bar::baz", 2, "::") -> ["foo", "bar::baz"];
1492 generate_iterator_test
! {
1493 not_double_ended_rsplitn
{
1494 ("foo::bar::baz", 2, "::") -> ["baz", "foo::bar"];
1500 use test
::{Bencher, black_box}
;
1503 fn char_iterator(b
: &mut Bencher
) {
1504 let s
= "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
1506 b
.iter(|| s
.chars().count());
1510 fn char_iterator_for(b
: &mut Bencher
) {
1511 let s
= "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
1514 for ch
in s
.chars() { black_box(ch); }
1519 fn char_iterator_ascii(b
: &mut Bencher
) {
1520 let s
= "Mary had a little lamb, Little lamb
1521 Mary had a little lamb, Little lamb
1522 Mary had a little lamb, Little lamb
1523 Mary had a little lamb, Little lamb
1524 Mary had a little lamb, Little lamb
1525 Mary had a little lamb, Little lamb";
1527 b
.iter(|| s
.chars().count());
1531 fn char_iterator_rev(b
: &mut Bencher
) {
1532 let s
= "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
1534 b
.iter(|| s
.chars().rev().count());
1538 fn char_iterator_rev_for(b
: &mut Bencher
) {
1539 let s
= "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
1542 for ch
in s
.chars().rev() { black_box(ch); }
1547 fn char_indicesator(b
: &mut Bencher
) {
1548 let s
= "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
1549 let len
= s
.chars().count();
1551 b
.iter(|| assert_eq
!(s
.char_indices().count(), len
));
1555 fn char_indicesator_rev(b
: &mut Bencher
) {
1556 let s
= "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
1557 let len
= s
.chars().count();
1559 b
.iter(|| assert_eq
!(s
.char_indices().rev().count(), len
));
1563 fn split_unicode_ascii(b
: &mut Bencher
) {
1564 let s
= "ประเทศไทย中华Việt Namประเทศไทย中华Việt Nam";
1566 b
.iter(|| assert_eq
!(s
.split('V'
).count(), 3));
1570 fn split_ascii(b
: &mut Bencher
) {
1571 let s
= "Mary had a little lamb, Little lamb, little-lamb.";
1572 let len
= s
.split(' '
).count();
1574 b
.iter(|| assert_eq
!(s
.split(' '
).count(), len
));
1578 fn split_extern_fn(b
: &mut Bencher
) {
1579 let s
= "Mary had a little lamb, Little lamb, little-lamb.";
1580 let len
= s
.split(' '
).count();
1581 fn pred(c
: char) -> bool { c == ' ' }
1583 b
.iter(|| assert_eq
!(s
.split(pred
).count(), len
));
1587 fn split_closure(b
: &mut Bencher
) {
1588 let s
= "Mary had a little lamb, Little lamb, little-lamb.";
1589 let len
= s
.split(' '
).count();
1591 b
.iter(|| assert_eq
!(s
.split(|c
: char| c
== ' '
).count(), len
));
1595 fn split_slice(b
: &mut Bencher
) {
1596 let s
= "Mary had a little lamb, Little lamb, little-lamb.";
1597 let len
= s
.split(' '
).count();
1599 let c
: &[char] = &[' '
];
1600 b
.iter(|| assert_eq
!(s
.split(c
).count(), len
));
1604 fn bench_join(b
: &mut Bencher
) {
1605 let s
= "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
1607 let v
= vec
![s
, s
, s
, s
, s
, s
, s
, s
, s
, s
];
1609 assert_eq
!(v
.join(sep
).len(), s
.len() * 10 + sep
.len() * 9);
1614 fn bench_contains_short_short(b
: &mut Bencher
) {
1615 let haystack
= "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
1619 assert
!(haystack
.contains(needle
));
1624 fn bench_contains_short_long(b
: &mut Bencher
) {
1626 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse quis lorem sit amet dolor \
1627 ultricies condimentum. Praesent iaculis purus elit, ac malesuada quam malesuada in. Duis sed orci \
1628 eros. Suspendisse sit amet magna mollis, mollis nunc luctus, imperdiet mi. Integer fringilla non \
1629 sem ut lacinia. Fusce varius tortor a risus porttitor hendrerit. Morbi mauris dui, ultricies nec \
1630 tempus vel, gravida nec quam.
1632 In est dui, tincidunt sed tempus interdum, adipiscing laoreet ante. Etiam tempor, tellus quis \
1633 sagittis interdum, nulla purus mattis sem, quis auctor erat odio ac tellus. In nec nunc sit amet \
1634 diam volutpat molestie at sed ipsum. Vestibulum laoreet consequat vulputate. Integer accumsan \
1635 lorem ac dignissim placerat. Suspendisse convallis faucibus lorem. Aliquam erat volutpat. In vel \
1636 eleifend felis. Sed suscipit nulla lorem, sed mollis est sollicitudin et. Nam fermentum egestas \
1637 interdum. Curabitur ut nisi justo.
1639 Sed sollicitudin ipsum tellus, ut condimentum leo eleifend nec. Cras ut velit ante. Phasellus nec \
1640 mollis odio. Mauris molestie erat in arcu mattis, at aliquet dolor vehicula. Quisque malesuada \
1641 lectus sit amet nisi pretium, a condimentum ipsum porta. Morbi at dapibus diam. Praesent egestas \
1642 est sed risus elementum, eu rutrum metus ultrices. Etiam fermentum consectetur magna, id rutrum \
1643 felis accumsan a. Aliquam ut pellentesque libero. Sed mi nulla, lobortis eu tortor id, suscipit \
1644 ultricies neque. Morbi iaculis sit amet risus at iaculis. Praesent eget ligula quis turpis \
1645 feugiat suscipit vel non arcu. Interdum et malesuada fames ac ante ipsum primis in faucibus. \
1646 Aliquam sit amet placerat lorem.
1648 Cras a lacus vel ante posuere elementum. Nunc est leo, bibendum ut facilisis vel, bibendum at \
1649 mauris. Nullam adipiscing diam vel odio ornare, luctus adipiscing mi luctus. Nulla facilisi. \
1650 Mauris adipiscing bibendum neque, quis adipiscing lectus tempus et. Sed feugiat erat et nisl \
1651 lobortis pharetra. Donec vitae erat enim. Nullam sit amet felis et quam lacinia tincidunt. Aliquam \
1652 suscipit dapibus urna. Sed volutpat urna in magna pulvinar volutpat. Phasellus nec tellus ac diam \
1655 Nam lectus enim, dapibus non nisi tempor, consectetur convallis massa. Maecenas eleifend dictum \
1656 feugiat. Etiam quis mauris vel risus luctus mattis a a nunc. Nullam orci quam, imperdiet id \
1657 vehicula in, porttitor ut nibh. Duis sagittis adipiscing nisl vitae congue. Donec mollis risus eu \
1658 leo suscipit, varius porttitor nulla porta. Pellentesque ut sem nec nisi euismod vehicula. Nulla \
1659 malesuada sollicitudin quam eu fermentum.";
1660 let needle
= "english";
1663 assert
!(!haystack
.contains(needle
));
1668 fn bench_contains_bad_naive(b
: &mut Bencher
) {
1669 let haystack
= "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
1670 let needle
= "aaaaaaaab";
1673 assert
!(!haystack
.contains(needle
));
1678 fn bench_contains_equal(b
: &mut Bencher
) {
1679 let haystack
= "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
1680 let needle
= "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
1683 assert
!(haystack
.contains(needle
));
1687 macro_rules
! make_test_inner
{
1688 ($s
:ident
, $code
:expr
, $name
:ident
, $
str:expr
) => {
1690 fn $
name(bencher
: &mut Bencher
) {
1693 bencher
.iter(|| $code
);
1698 macro_rules
! make_test
{
1699 ($name
:ident
, $s
:ident
, $code
:expr
) => {
1702 use test
::black_box
;
1704 // Short strings: 65 bytes each
1705 make_test_inner
!($s
, $code
, short_ascii
,
1706 "Mary had a little lamb, Little lamb Mary had a littl lamb, lamb!");
1707 make_test_inner
!($s
, $code
, short_mixed
,
1708 "ศไทย中华Việt Nam; Mary had a little lamb, Little lam!");
1709 make_test_inner
!($s
, $code
, short_pile_of_poo
,
1710 "💩💩💩💩💩💩💩💩💩💩💩💩💩💩💩💩!");
1711 make_test_inner
!($s
, $code
, long_lorem_ipsum
,"\
1712 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse quis lorem sit amet dolor \
1713 ultricies condimentum. Praesent iaculis purus elit, ac malesuada quam malesuada in. Duis sed orci \
1714 eros. Suspendisse sit amet magna mollis, mollis nunc luctus, imperdiet mi. Integer fringilla non \
1715 sem ut lacinia. Fusce varius tortor a risus porttitor hendrerit. Morbi mauris dui, ultricies nec \
1716 tempus vel, gravida nec quam.
1718 In est dui, tincidunt sed tempus interdum, adipiscing laoreet ante. Etiam tempor, tellus quis \
1719 sagittis interdum, nulla purus mattis sem, quis auctor erat odio ac tellus. In nec nunc sit amet \
1720 diam volutpat molestie at sed ipsum. Vestibulum laoreet consequat vulputate. Integer accumsan \
1721 lorem ac dignissim placerat. Suspendisse convallis faucibus lorem. Aliquam erat volutpat. In vel \
1722 eleifend felis. Sed suscipit nulla lorem, sed mollis est sollicitudin et. Nam fermentum egestas \
1723 interdum. Curabitur ut nisi justo.
1725 Sed sollicitudin ipsum tellus, ut condimentum leo eleifend nec. Cras ut velit ante. Phasellus nec \
1726 mollis odio. Mauris molestie erat in arcu mattis, at aliquet dolor vehicula. Quisque malesuada \
1727 lectus sit amet nisi pretium, a condimentum ipsum porta. Morbi at dapibus diam. Praesent egestas \
1728 est sed risus elementum, eu rutrum metus ultrices. Etiam fermentum consectetur magna, id rutrum \
1729 felis accumsan a. Aliquam ut pellentesque libero. Sed mi nulla, lobortis eu tortor id, suscipit \
1730 ultricies neque. Morbi iaculis sit amet risus at iaculis. Praesent eget ligula quis turpis \
1731 feugiat suscipit vel non arcu. Interdum et malesuada fames ac ante ipsum primis in faucibus. \
1732 Aliquam sit amet placerat lorem.
1734 Cras a lacus vel ante posuere elementum. Nunc est leo, bibendum ut facilisis vel, bibendum at \
1735 mauris. Nullam adipiscing diam vel odio ornare, luctus adipiscing mi luctus. Nulla facilisi. \
1736 Mauris adipiscing bibendum neque, quis adipiscing lectus tempus et. Sed feugiat erat et nisl \
1737 lobortis pharetra. Donec vitae erat enim. Nullam sit amet felis et quam lacinia tincidunt. Aliquam \
1738 suscipit dapibus urna. Sed volutpat urna in magna pulvinar volutpat. Phasellus nec tellus ac diam \
1741 Nam lectus enim, dapibus non nisi tempor, consectetur convallis massa. Maecenas eleifend dictum \
1742 feugiat. Etiam quis mauris vel risus luctus mattis a a nunc. Nullam orci quam, imperdiet id \
1743 vehicula in, porttitor ut nibh. Duis sagittis adipiscing nisl vitae congue. Donec mollis risus eu \
1744 leo suscipit, varius porttitor nulla porta. Pellentesque ut sem nec nisi euismod vehicula. Nulla \
1745 malesuada sollicitudin quam eu fermentum!");
1750 make_test
!(chars_count
, s
, s
.chars().count());
1752 make_test
!(contains_bang_str
, s
, s
.contains("!"));
1753 make_test
!(contains_bang_char
, s
, s
.contains('
!'
));
1755 make_test
!(match_indices_a_str
, s
, s
.match_indices("a").count());
1757 make_test
!(split_a_str
, s
, s
.split("a").count());
1759 make_test
!(trim_ascii_char
, s
, {
1760 use std
::ascii
::AsciiExt
;
1761 s
.trim_matches(|c
: char| c
.is_ascii())
1763 make_test
!(trim_left_ascii_char
, s
, {
1764 use std
::ascii
::AsciiExt
;
1765 s
.trim_left_matches(|c
: char| c
.is_ascii())
1767 make_test
!(trim_right_ascii_char
, s
, {
1768 use std
::ascii
::AsciiExt
;
1769 s
.trim_right_matches(|c
: char| c
.is_ascii())
1772 make_test
!(find_underscore_char
, s
, s
.find('_'
));
1773 make_test
!(rfind_underscore_char
, s
, s
.rfind('_'
));
1774 make_test
!(find_underscore_str
, s
, s
.find("_"));
1776 make_test
!(find_zzz_char
, s
, s
.find('
\u{1F4A4}'
));
1777 make_test
!(rfind_zzz_char
, s
, s
.rfind('
\u{1F4A4}'
));
1778 make_test
!(find_zzz_str
, s
, s
.find("\u{1F4A4}"));
1780 make_test
!(split_space_char
, s
, s
.split(' '
).count());
1781 make_test
!(split_terminator_space_char
, s
, s
.split_terminator(' '
).count());
1783 make_test
!(splitn_space_char
, s
, s
.splitn(10, ' '
).count());
1784 make_test
!(rsplitn_space_char
, s
, s
.rsplitn(10, ' '
).count());
1786 make_test
!(split_space_str
, s
, s
.split(" ").count());
1787 make_test
!(split_ad_str
, s
, s
.split("ad").count());