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.
13 pub trait IntoCow<'a, B: ?Sized> where B: ToOwned {
14 fn into_cow(self) -> Cow<'a, B>;
17 impl<'a> IntoCow<'a, str> for String {
18 fn into_cow(self) -> Cow<'a, str> {
23 impl<'a> IntoCow<'a, str> for &'a str {
24 fn into_cow(self) -> Cow<'a, str> {
31 let owned: Option<::std::string::String> = "string".parse().ok();
32 assert_eq!(owned.as_ref().map(|s| &**s), Some("string"));
36 fn test_from_cow_str() {
37 assert_eq!(String::from(Cow::Borrowed("string")), "string");
38 assert_eq!(String::from(Cow::Owned(String::from("string"))), "string");
42 fn test_unsized_to_string() {
44 let _: String = (*s).to_string();
49 let xs = b"hello".to_vec();
50 assert_eq!(String::from_utf8(xs).unwrap(), String::from("hello"));
52 let xs = "ศไทย中华Việt Nam".as_bytes().to_vec();
53 assert_eq!(String::from_utf8(xs).unwrap(),
54 String::from("ศไทย中华Việt Nam"));
56 let xs = b"hello\xFF".to_vec();
57 let err = String::from_utf8(xs).unwrap_err();
58 assert_eq!(err.into_bytes(), b"hello\xff".to_vec());
62 fn test_from_utf8_lossy() {
64 let ys: Cow<str> = "hello".into_cow();
65 assert_eq!(String::from_utf8_lossy(xs), ys);
67 let xs = "ศไทย中华Việt Nam".as_bytes();
68 let ys: Cow<str> = "ศไทย中华Việt Nam".into_cow();
69 assert_eq!(String::from_utf8_lossy(xs), ys);
71 let xs = b"Hello\xC2 There\xFF Goodbye";
72 assert_eq!(String::from_utf8_lossy(xs),
73 String::from("Hello\u{FFFD} There\u{FFFD} Goodbye").into_cow());
75 let xs = b"Hello\xC0\x80 There\xE6\x83 Goodbye";
76 assert_eq!(String::from_utf8_lossy(xs),
77 String::from("Hello\u{FFFD}\u{FFFD} There\u{FFFD} Goodbye").into_cow());
79 let xs = b"\xF5foo\xF5\x80bar";
80 assert_eq!(String::from_utf8_lossy(xs),
81 String::from("\u{FFFD}foo\u{FFFD}\u{FFFD}bar").into_cow());
83 let xs = b"\xF1foo\xF1\x80bar\xF1\x80\x80baz";
84 assert_eq!(String::from_utf8_lossy(xs),
85 String::from("\u{FFFD}foo\u{FFFD}bar\u{FFFD}baz").into_cow());
87 let xs = b"\xF4foo\xF4\x80bar\xF4\xBFbaz";
88 assert_eq!(String::from_utf8_lossy(xs),
89 String::from("\u{FFFD}foo\u{FFFD}bar\u{FFFD}\u{FFFD}baz").into_cow());
91 let xs = b"\xF0\x80\x80\x80foo\xF0\x90\x80\x80bar";
92 assert_eq!(String::from_utf8_lossy(xs),
93 String::from("\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}foo\u{10000}bar").into_cow());
96 let xs = b"\xED\xA0\x80foo\xED\xBF\xBFbar";
97 assert_eq!(String::from_utf8_lossy(xs),
98 String::from("\u{FFFD}\u{FFFD}\u{FFFD}foo\u{FFFD}\u{FFFD}\u{FFFD}bar").into_cow());
102 fn test_from_utf16() {
103 let pairs = [(String::from("𐍅𐌿𐌻𐍆𐌹𐌻𐌰\n"),
104 vec![0xd800, 0xdf45, 0xd800, 0xdf3f, 0xd800, 0xdf3b, 0xd800, 0xdf46, 0xd800,
105 0xdf39, 0xd800, 0xdf3b, 0xd800, 0xdf30, 0x000a]),
107 (String::from("𐐒𐑉𐐮𐑀𐐲𐑋 𐐏𐐲𐑍\n"),
108 vec![0xd801, 0xdc12, 0xd801, 0xdc49, 0xd801, 0xdc2e, 0xd801, 0xdc40, 0xd801,
109 0xdc32, 0xd801, 0xdc4b, 0x0020, 0xd801, 0xdc0f, 0xd801, 0xdc32, 0xd801,
112 (String::from("𐌀𐌖𐌋𐌄𐌑𐌉·𐌌𐌄𐌕𐌄𐌋𐌉𐌑\n"),
113 vec![0xd800, 0xdf00, 0xd800, 0xdf16, 0xd800, 0xdf0b, 0xd800, 0xdf04, 0xd800,
114 0xdf11, 0xd800, 0xdf09, 0x00b7, 0xd800, 0xdf0c, 0xd800, 0xdf04, 0xd800,
115 0xdf15, 0xd800, 0xdf04, 0xd800, 0xdf0b, 0xd800, 0xdf09, 0xd800, 0xdf11,
118 (String::from("𐒋𐒘𐒈𐒑𐒛𐒒 𐒕𐒓 𐒈𐒚𐒍 𐒏𐒜𐒒𐒖𐒆 𐒕𐒆\n"),
119 vec![0xd801, 0xdc8b, 0xd801, 0xdc98, 0xd801, 0xdc88, 0xd801, 0xdc91, 0xd801,
120 0xdc9b, 0xd801, 0xdc92, 0x0020, 0xd801, 0xdc95, 0xd801, 0xdc93, 0x0020,
121 0xd801, 0xdc88, 0xd801, 0xdc9a, 0xd801, 0xdc8d, 0x0020, 0xd801, 0xdc8f,
122 0xd801, 0xdc9c, 0xd801, 0xdc92, 0xd801, 0xdc96, 0xd801, 0xdc86, 0x0020,
123 0xd801, 0xdc95, 0xd801, 0xdc86, 0x000a]),
124 // Issue #12318, even-numbered non-BMP planes
125 (String::from("\u{20000}"), vec![0xD840, 0xDC00])];
128 let (s, u) = (*p).clone();
129 let s_as_utf16 = s.encode_utf16().collect::<Vec<u16>>();
130 let u_as_string = String::from_utf16(&u).unwrap();
132 assert!(::std_unicode::char::decode_utf16(u.iter().cloned()).all(|r| r.is_ok()));
133 assert_eq!(s_as_utf16, u);
135 assert_eq!(u_as_string, s);
136 assert_eq!(String::from_utf16_lossy(&u), s);
138 assert_eq!(String::from_utf16(&s_as_utf16).unwrap(), s);
139 assert_eq!(u_as_string.encode_utf16().collect::<Vec<u16>>(), u);
144 fn test_utf16_invalid() {
145 // completely positive cases tested above.
147 assert!(String::from_utf16(&[0xD800]).is_err());
149 assert!(String::from_utf16(&[0xD800, 0xD800]).is_err());
152 assert!(String::from_utf16(&[0x0061, 0xDC00]).is_err());
155 assert!(String::from_utf16(&[0xD800, 0xd801, 0xdc8b, 0xD800]).is_err());
159 fn test_from_utf16_lossy() {
160 // completely positive cases tested above.
162 assert_eq!(String::from_utf16_lossy(&[0xD800]),
163 String::from("\u{FFFD}"));
165 assert_eq!(String::from_utf16_lossy(&[0xD800, 0xD800]),
166 String::from("\u{FFFD}\u{FFFD}"));
169 assert_eq!(String::from_utf16_lossy(&[0x0061, 0xDC00]),
170 String::from("a\u{FFFD}"));
173 assert_eq!(String::from_utf16_lossy(&[0xD800, 0xd801, 0xdc8b, 0xD800]),
174 String::from("\u{FFFD}𐒋\u{FFFD}"));
178 fn test_push_bytes() {
179 let mut s = String::from("ABC");
181 let mv = s.as_mut_vec();
182 mv.extend_from_slice(&[b'D']);
184 assert_eq!(s, "ABCD");
189 let mut s = String::new();
191 assert_eq!(&s[0..], "");
193 assert_eq!(&s[0..], "abc");
194 s.push_str("ประเทศไทย中华Việt Nam");
195 assert_eq!(&s[0..], "abcประเทศไทย中华Việt Nam");
199 fn test_add_assign() {
200 let mut s = String::new();
202 assert_eq!(s.as_str(), "");
204 assert_eq!(s.as_str(), "abc");
205 s += "ประเทศไทย中华Việt Nam";
206 assert_eq!(s.as_str(), "abcประเทศไทย中华Việt Nam");
211 let mut data = String::from("ประเทศไทย中");
213 data.push('b'); // 1 byte
214 data.push('¢'); // 2 byte
215 data.push('€'); // 3 byte
216 data.push('𤭢'); // 4 byte
217 assert_eq!(data, "ประเทศไทย中华b¢€𤭢");
222 let mut data = String::from("ประเทศไทย中华b¢€𤭢");
223 assert_eq!(data.pop().unwrap(), '𤭢'); // 4 bytes
224 assert_eq!(data.pop().unwrap(), '€'); // 3 bytes
225 assert_eq!(data.pop().unwrap(), '¢'); // 2 bytes
226 assert_eq!(data.pop().unwrap(), 'b'); // 1 bytes
227 assert_eq!(data.pop().unwrap(), '华');
228 assert_eq!(data, "ประเทศไทย中");
232 fn test_split_off_empty() {
233 let orig = "Hello, world!";
234 let mut split = String::from(orig);
235 let empty: String = split.split_off(orig.len());
236 assert!(empty.is_empty());
241 fn test_split_off_past_end() {
242 let orig = "Hello, world!";
243 let mut split = String::from(orig);
244 split.split_off(orig.len() + 1);
249 fn test_split_off_mid_char() {
250 let mut orig = String::from("山");
255 fn test_split_off_ascii() {
256 let mut ab = String::from("ABCD");
257 let cd = ab.split_off(2);
258 assert_eq!(ab, "AB");
259 assert_eq!(cd, "CD");
263 fn test_split_off_unicode() {
264 let mut nihon = String::from("日本語");
265 let go = nihon.split_off("日本".len());
266 assert_eq!(nihon, "日本");
271 fn test_str_truncate() {
272 let mut s = String::from("12345");
274 assert_eq!(s, "12345");
276 assert_eq!(s, "123");
280 let mut s = String::from("12345");
289 fn test_str_truncate_invalid_len() {
290 let mut s = String::from("12345");
292 assert_eq!(s, "12345");
297 fn test_str_truncate_split_codepoint() {
298 let mut s = String::from("\u{FC}"); // ü
303 fn test_str_clear() {
304 let mut s = String::from("12345");
306 assert_eq!(s.len(), 0);
312 let a = String::from("12345");
315 assert_eq!(b.len(), 7);
316 assert_eq!(b, "1234522");
321 let mut s = "ศไทย中华Việt Nam; foobar".to_string();
322 assert_eq!(s.remove(0), 'ศ');
323 assert_eq!(s.len(), 33);
324 assert_eq!(s, "ไทย中华Việt Nam; foobar");
325 assert_eq!(s.remove(17), 'ệ');
326 assert_eq!(s, "ไทย中华Vit Nam; foobar");
332 "ศ".to_string().remove(1);
337 let mut s = "foobar".to_string();
339 assert_eq!(s, "ệfoobar");
341 assert_eq!(s, "ệfooยbar");
347 "".to_string().insert(1, 't');
352 "ệ".to_string().insert(1, 't');
357 let s = "foobar".to_string();
358 assert_eq!("foobar", &s[..]);
359 assert_eq!("foo", &s[..3]);
360 assert_eq!("bar", &s[3..]);
361 assert_eq!("oob", &s[1..4]);
365 fn test_simple_types() {
366 assert_eq!(1.to_string(), "1");
367 assert_eq!((-1).to_string(), "-1");
368 assert_eq!(200.to_string(), "200");
369 assert_eq!(2.to_string(), "2");
370 assert_eq!(true.to_string(), "true");
371 assert_eq!(false.to_string(), "false");
372 assert_eq!(("hi".to_string()).to_string(), "hi");
377 let x: Vec<i32> = vec![];
378 assert_eq!(format!("{:?}", x), "[]");
379 assert_eq!(format!("{:?}", vec![1]), "[1]");
380 assert_eq!(format!("{:?}", vec![1, 2, 3]), "[1, 2, 3]");
381 assert!(format!("{:?}", vec![vec![], vec![1], vec![1, 1]]) == "[[], [1], [1, 1]]");
385 fn test_from_iterator() {
386 let s = "ศไทย中华Việt Nam".to_string();
390 let a: String = s.chars().collect();
393 let mut b = t.to_string();
397 let c: String = vec![t, u].into_iter().collect();
400 let mut d = t.to_string();
407 let mut s = String::from("αβγ");
408 assert_eq!(s.drain(2..4).collect::<String>(), "β");
411 let mut t = String::from("abcd");
413 assert_eq!(t, "abcd");
415 assert_eq!(t, "bcd");
417 assert_eq!(t, "bcd");
423 fn test_extend_ref() {
424 let mut a = "foo".to_string();
425 a.extend(&['b', 'a', 'r']);
427 assert_eq!(&a, "foobar");
431 fn test_into_boxed_str() {
432 let xs = String::from("hello my name is bob");
433 let ys = xs.into_boxed_str();
434 assert_eq!(&*ys, "hello my name is bob");