]> git.lizzy.rs Git - rust.git/blob - src/libcollectionstest/string.rs
Rollup merge of #39604 - est31:i128_tests, r=alexcrichton
[rust.git] / src / libcollectionstest / string.rs
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.
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
13 pub trait IntoCow<'a, B: ?Sized> where B: ToOwned {
14     fn into_cow(self) -> Cow<'a, B>;
15 }
16
17 impl<'a> IntoCow<'a, str> for String {
18     fn into_cow(self) -> Cow<'a, str> {
19         Cow::Owned(self)
20     }
21 }
22
23 impl<'a> IntoCow<'a, str> for &'a str {
24     fn into_cow(self) -> Cow<'a, str> {
25         Cow::Borrowed(self)
26     }
27 }
28
29 #[test]
30 fn test_from_str() {
31     let owned: Option<::std::string::String> = "string".parse().ok();
32     assert_eq!(owned.as_ref().map(|s| &**s), Some("string"));
33 }
34
35 #[test]
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");
39 }
40
41 #[test]
42 fn test_unsized_to_string() {
43     let s: &str = "abc";
44     let _: String = (*s).to_string();
45 }
46
47 #[test]
48 fn test_from_utf8() {
49     let xs = b"hello".to_vec();
50     assert_eq!(String::from_utf8(xs).unwrap(), String::from("hello"));
51
52     let xs = "ศไทย中华Việt Nam".as_bytes().to_vec();
53     assert_eq!(String::from_utf8(xs).unwrap(),
54                String::from("ศไทย中华Việt Nam"));
55
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());
59 }
60
61 #[test]
62 fn test_from_utf8_lossy() {
63     let xs = b"hello";
64     let ys: Cow<str> = "hello".into_cow();
65     assert_eq!(String::from_utf8_lossy(xs), ys);
66
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);
70
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());
74
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());
78
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());
82
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());
86
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());
90
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());
94
95     // surrogates
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());
99 }
100
101 #[test]
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]),
106
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,
110                        0xdc4d, 0x000a]),
111
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,
116                        0x000a]),
117
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])];
126
127     for p in &pairs {
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();
131
132         assert!(::std_unicode::str::is_utf16(&u));
133         assert_eq!(s_as_utf16, u);
134
135         assert_eq!(u_as_string, s);
136         assert_eq!(String::from_utf16_lossy(&u), s);
137
138         assert_eq!(String::from_utf16(&s_as_utf16).unwrap(), s);
139         assert_eq!(u_as_string.encode_utf16().collect::<Vec<u16>>(), u);
140     }
141 }
142
143 #[test]
144 fn test_utf16_invalid() {
145     // completely positive cases tested above.
146     // lead + eof
147     assert!(String::from_utf16(&[0xD800]).is_err());
148     // lead + lead
149     assert!(String::from_utf16(&[0xD800, 0xD800]).is_err());
150
151     // isolated trail
152     assert!(String::from_utf16(&[0x0061, 0xDC00]).is_err());
153
154     // general
155     assert!(String::from_utf16(&[0xD800, 0xd801, 0xdc8b, 0xD800]).is_err());
156 }
157
158 #[test]
159 fn test_from_utf16_lossy() {
160     // completely positive cases tested above.
161     // lead + eof
162     assert_eq!(String::from_utf16_lossy(&[0xD800]),
163                String::from("\u{FFFD}"));
164     // lead + lead
165     assert_eq!(String::from_utf16_lossy(&[0xD800, 0xD800]),
166                String::from("\u{FFFD}\u{FFFD}"));
167
168     // isolated trail
169     assert_eq!(String::from_utf16_lossy(&[0x0061, 0xDC00]),
170                String::from("a\u{FFFD}"));
171
172     // general
173     assert_eq!(String::from_utf16_lossy(&[0xD800, 0xd801, 0xdc8b, 0xD800]),
174                String::from("\u{FFFD}𐒋\u{FFFD}"));
175 }
176
177 #[test]
178 fn test_push_bytes() {
179     let mut s = String::from("ABC");
180     unsafe {
181         let mv = s.as_mut_vec();
182         mv.extend_from_slice(&[b'D']);
183     }
184     assert_eq!(s, "ABCD");
185 }
186
187 #[test]
188 fn test_push_str() {
189     let mut s = String::new();
190     s.push_str("");
191     assert_eq!(&s[0..], "");
192     s.push_str("abc");
193     assert_eq!(&s[0..], "abc");
194     s.push_str("ประเทศไทย中华Việt Nam");
195     assert_eq!(&s[0..], "abcประเทศไทย中华Việt Nam");
196 }
197
198 #[test]
199 fn test_add_assign() {
200     let mut s = String::new();
201     s += "";
202     assert_eq!(s.as_str(), "");
203     s += "abc";
204     assert_eq!(s.as_str(), "abc");
205     s += "ประเทศไทย中华Việt Nam";
206     assert_eq!(s.as_str(), "abcประเทศไทย中华Việt Nam");
207 }
208
209 #[test]
210 fn test_push() {
211     let mut data = String::from("ประเทศไทย中");
212     data.push('华');
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¢€𤭢");
218 }
219
220 #[test]
221 fn test_pop() {
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, "ประเทศไทย中");
229 }
230
231 #[test]
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());
237 }
238
239 #[test]
240 #[should_panic]
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);
245 }
246
247 #[test]
248 #[should_panic]
249 fn test_split_off_mid_char() {
250     let mut orig = String::from("山");
251     orig.split_off(1);
252 }
253
254 #[test]
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");
260 }
261
262 #[test]
263 fn test_split_off_unicode() {
264     let mut nihon = String::from("日本語");
265     let go = nihon.split_off("日本".len());
266     assert_eq!(nihon, "日本");
267     assert_eq!(go, "語");
268 }
269
270 #[test]
271 fn test_str_truncate() {
272     let mut s = String::from("12345");
273     s.truncate(5);
274     assert_eq!(s, "12345");
275     s.truncate(3);
276     assert_eq!(s, "123");
277     s.truncate(0);
278     assert_eq!(s, "");
279
280     let mut s = String::from("12345");
281     let p = s.as_ptr();
282     s.truncate(3);
283     s.push_str("6");
284     let p_ = s.as_ptr();
285     assert_eq!(p_, p);
286 }
287
288 #[test]
289 fn test_str_truncate_invalid_len() {
290     let mut s = String::from("12345");
291     s.truncate(6);
292     assert_eq!(s, "12345");
293 }
294
295 #[test]
296 #[should_panic]
297 fn test_str_truncate_split_codepoint() {
298     let mut s = String::from("\u{FC}"); // ü
299     s.truncate(1);
300 }
301
302 #[test]
303 fn test_str_clear() {
304     let mut s = String::from("12345");
305     s.clear();
306     assert_eq!(s.len(), 0);
307     assert_eq!(s, "");
308 }
309
310 #[test]
311 fn test_str_add() {
312     let a = String::from("12345");
313     let b = a + "2";
314     let b = b + "2";
315     assert_eq!(b.len(), 7);
316     assert_eq!(b, "1234522");
317 }
318
319 #[test]
320 fn remove() {
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");
327 }
328
329 #[test]
330 #[should_panic]
331 fn remove_bad() {
332     "ศ".to_string().remove(1);
333 }
334
335 #[test]
336 fn insert() {
337     let mut s = "foobar".to_string();
338     s.insert(0, 'ệ');
339     assert_eq!(s, "ệfoobar");
340     s.insert(6, 'ย');
341     assert_eq!(s, "ệfooยbar");
342 }
343
344 #[test]
345 #[should_panic]
346 fn insert_bad1() {
347     "".to_string().insert(1, 't');
348 }
349 #[test]
350 #[should_panic]
351 fn insert_bad2() {
352     "ệ".to_string().insert(1, 't');
353 }
354
355 #[test]
356 fn test_slicing() {
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]);
362 }
363
364 #[test]
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");
373 }
374
375 #[test]
376 fn test_vectors() {
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]]");
382 }
383
384 #[test]
385 fn test_from_iterator() {
386     let s = "ศไทย中华Việt Nam".to_string();
387     let t = "ศไทย中华";
388     let u = "Việt Nam";
389
390     let a: String = s.chars().collect();
391     assert_eq!(s, a);
392
393     let mut b = t.to_string();
394     b.extend(u.chars());
395     assert_eq!(s, b);
396
397     let c: String = vec![t, u].into_iter().collect();
398     assert_eq!(s, c);
399
400     let mut d = t.to_string();
401     d.extend(vec![u]);
402     assert_eq!(s, d);
403 }
404
405 #[test]
406 fn test_drain() {
407     let mut s = String::from("αβγ");
408     assert_eq!(s.drain(2..4).collect::<String>(), "β");
409     assert_eq!(s, "αγ");
410
411     let mut t = String::from("abcd");
412     t.drain(..0);
413     assert_eq!(t, "abcd");
414     t.drain(..1);
415     assert_eq!(t, "bcd");
416     t.drain(3..);
417     assert_eq!(t, "bcd");
418     t.drain(..);
419     assert_eq!(t, "");
420 }
421
422 #[test]
423 fn test_extend_ref() {
424     let mut a = "foo".to_string();
425     a.extend(&['b', 'a', 'r']);
426
427     assert_eq!(&a, "foobar");
428 }
429
430 #[test]
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");
435 }