1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
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.
12 fn test_bool_from_str() {
13 assert_eq!("true".parse(), Some(true));
14 assert_eq!("false".parse(), Some(false));
15 assert_eq!("not even a boolean".parse::<bool>(), None);
18 fn check_contains_all_substrings(s: &str) {
19 assert!(s.contains(""));
20 for i in range(0, s.len()) {
21 for j in range(i+1, s.len() + 1) {
22 assert!(s.contains(s.slice(i, j)));
28 fn strslice_issue_16589() {
29 assert!("bananas".contains("nana"));
31 // prior to the fix for #16589, x.contains("abcdabcd") returned false
32 // test all substrings for good measure
33 check_contains_all_substrings("012345678901234567890123456789bcdabcdabcd");
37 fn strslice_issue_16878() {
38 assert!(!"1234567ah012345678901ah".contains("hah"));
39 assert!(!"00abc01234567890123456789abc".contains("bcabc"));
44 fn test_strslice_contains() {
45 let x = "There are moments, Jeeves, when one asks oneself, 'Do trousers matter?'";
46 check_contains_all_substrings(x);
50 fn test_rsplitn_char_iterator() {
51 let data = "\nMäry häd ä little lämb\nLittle lämb\n";
53 let mut split: Vec<&str> = data.rsplitn(3, ' ').collect();
55 assert_eq!(split, vec!["\nMäry häd ä", "little", "lämb\nLittle", "lämb\n"]);
57 let mut split: Vec<&str> = data.rsplitn(3, |&: c: char| c == ' ').collect();
59 assert_eq!(split, vec!["\nMäry häd ä", "little", "lämb\nLittle", "lämb\n"]);
62 let mut split: Vec<&str> = data.rsplitn(3, 'ä').collect();
64 assert_eq!(split, vec!["\nMäry häd ", " little l", "mb\nLittle l", "mb\n"]);
66 let mut split: Vec<&str> = data.rsplitn(3, |&: c: char| c == 'ä').collect();
68 assert_eq!(split, vec!["\nMäry häd ", " little l", "mb\nLittle l", "mb\n"]);
72 fn test_split_char_iterator() {
73 let data = "\nMäry häd ä little lämb\nLittle lämb\n";
75 let split: Vec<&str> = data.split(' ').collect();
76 assert_eq!( split, vec!["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
78 let mut rsplit: Vec<&str> = data.split(' ').rev().collect();
80 assert_eq!(rsplit, vec!["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
82 let split: Vec<&str> = data.split(|&: c: char| c == ' ').collect();
83 assert_eq!( split, vec!["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
85 let mut rsplit: Vec<&str> = data.split(|&: c: char| c == ' ').rev().collect();
87 assert_eq!(rsplit, vec!["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
90 let split: Vec<&str> = data.split('ä').collect();
91 assert_eq!( split, vec!["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
93 let mut rsplit: Vec<&str> = data.split('ä').rev().collect();
95 assert_eq!(rsplit, vec!["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
97 let split: Vec<&str> = data.split(|&: c: char| c == 'ä').collect();
98 assert_eq!( split, vec!["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
100 let mut rsplit: Vec<&str> = data.split(|&: c: char| c == 'ä').rev().collect();
102 assert_eq!(rsplit, vec!["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
106 fn test_rev_split_char_iterator_no_trailing() {
107 let data = "\nMäry häd ä little lämb\nLittle lämb\n";
109 let mut split: Vec<&str> = data.split('\n').rev().collect();
111 assert_eq!(split, vec!["", "Märy häd ä little lämb", "Little lämb", ""]);
113 let mut split: Vec<&str> = data.split_terminator('\n').rev().collect();
115 assert_eq!(split, vec!["", "Märy häd ä little lämb", "Little lämb"]);
119 fn test_utf16_code_units() {
120 use unicode::str::Utf16Encoder;
121 assert_eq!(Utf16Encoder::new(vec!['é', '\u{1F4A9}'].into_iter()).collect::<Vec<u16>>(),
122 vec![0xE9, 0xD83D, 0xDCA9])