]> git.lizzy.rs Git - rust.git/blob - src/liballoc/tests/cow_str.rs
Rollup merge of #46518 - partim:asref-borrow-doc, r=dtolnay
[rust.git] / src / liballoc / tests / cow_str.rs
1 // Copyright 2016 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 // check that Cow<'a, str> implements addition
14 #[test]
15 fn check_cow_add_cow() {
16     let borrowed1 = Cow::Borrowed("Hello, ");
17     let borrowed2 = Cow::Borrowed("World!");
18     let borrow_empty = Cow::Borrowed("");
19
20     let owned1: Cow<str> = Cow::Owned(String::from("Hi, "));
21     let owned2: Cow<str> = Cow::Owned(String::from("Rustaceans!"));
22     let owned_empty: Cow<str> = Cow::Owned(String::new());
23
24     assert_eq!("Hello, World!", borrowed1.clone() + borrowed2.clone());
25     assert_eq!("Hello, Rustaceans!", borrowed1.clone() + owned2.clone());
26
27     assert_eq!("Hi, World!", owned1.clone() + borrowed2.clone());
28     assert_eq!("Hi, Rustaceans!", owned1.clone() + owned2.clone());
29
30     if let Cow::Owned(_) = borrowed1.clone() + borrow_empty.clone() {
31         panic!("Adding empty strings to a borrow should note allocate");
32     }
33     if let Cow::Owned(_) = borrow_empty.clone() + borrowed1.clone() {
34         panic!("Adding empty strings to a borrow should note allocate");
35     }
36     if let Cow::Owned(_) = borrowed1.clone() + owned_empty.clone() {
37         panic!("Adding empty strings to a borrow should note allocate");
38     }
39     if let Cow::Owned(_) = owned_empty.clone() + borrowed1.clone() {
40         panic!("Adding empty strings to a borrow should note allocate");
41     }
42 }
43
44 #[test]
45 fn check_cow_add_str() {
46     let borrowed = Cow::Borrowed("Hello, ");
47     let borrow_empty = Cow::Borrowed("");
48
49     let owned: Cow<str> = Cow::Owned(String::from("Hi, "));
50     let owned_empty: Cow<str> = Cow::Owned(String::new());
51
52     assert_eq!("Hello, World!", borrowed.clone() + "World!");
53
54     assert_eq!("Hi, World!", owned.clone() + "World!");
55
56     if let Cow::Owned(_) = borrowed.clone() + "" {
57         panic!("Adding empty strings to a borrow should note allocate");
58     }
59     if let Cow::Owned(_) = borrow_empty.clone() + "Hello, " {
60         panic!("Adding empty strings to a borrow should note allocate");
61     }
62     if let Cow::Owned(_) = owned_empty.clone() + "Hello, " {
63         panic!("Adding empty strings to a borrow should note allocate");
64     }
65 }
66
67 #[test]
68 fn check_cow_add_assign_cow() {
69     let mut borrowed1 = Cow::Borrowed("Hello, ");
70     let borrowed2 = Cow::Borrowed("World!");
71     let borrow_empty = Cow::Borrowed("");
72
73     let mut owned1: Cow<str> = Cow::Owned(String::from("Hi, "));
74     let owned2: Cow<str> = Cow::Owned(String::from("Rustaceans!"));
75     let owned_empty: Cow<str> = Cow::Owned(String::new());
76
77     let mut s = borrowed1.clone();
78     s += borrow_empty.clone();
79     assert_eq!("Hello, ", s);
80     if let Cow::Owned(_) = s {
81         panic!("Adding empty strings to a borrow should note allocate");
82     }
83     let mut s = borrow_empty.clone();
84     s += borrowed1.clone();
85     assert_eq!("Hello, ", s);
86     if let Cow::Owned(_) = s {
87         panic!("Adding empty strings to a borrow should note allocate");
88     }
89     let mut s = borrowed1.clone();
90     s += owned_empty.clone();
91     assert_eq!("Hello, ", s);
92     if let Cow::Owned(_) = s {
93         panic!("Adding empty strings to a borrow should note allocate");
94     }
95     let mut s = owned_empty.clone();
96     s += borrowed1.clone();
97     assert_eq!("Hello, ", s);
98     if let Cow::Owned(_) = s {
99         panic!("Adding empty strings to a borrow should note allocate");
100     }
101
102     owned1 += borrowed2;
103     borrowed1 += owned2;
104
105     assert_eq!("Hi, World!", owned1);
106     assert_eq!("Hello, Rustaceans!", borrowed1);
107 }
108
109 #[test]
110 fn check_cow_add_assign_str() {
111     let mut borrowed = Cow::Borrowed("Hello, ");
112     let borrow_empty = Cow::Borrowed("");
113
114     let mut owned: Cow<str> = Cow::Owned(String::from("Hi, "));
115     let owned_empty: Cow<str> = Cow::Owned(String::new());
116
117     let mut s = borrowed.clone();
118     s += "";
119     assert_eq!("Hello, ", s);
120     if let Cow::Owned(_) = s {
121         panic!("Adding empty strings to a borrow should note allocate");
122     }
123     let mut s = borrow_empty.clone();
124     s += "World!";
125     assert_eq!("World!", s);
126     if let Cow::Owned(_) = s {
127         panic!("Adding empty strings to a borrow should note allocate");
128     }
129     let mut s = owned_empty.clone();
130     s += "World!";
131     assert_eq!("World!", s);
132     if let Cow::Owned(_) = s {
133         panic!("Adding empty strings to a borrow should note allocate");
134     }
135
136     owned += "World!";
137     borrowed += "World!";
138
139     assert_eq!("Hi, World!", owned);
140     assert_eq!("Hello, World!", borrowed);
141 }
142
143 #[test]
144 fn check_cow_clone_from() {
145     let mut c1: Cow<str> = Cow::Owned(String::with_capacity(25));
146     let s: String = "hi".to_string();
147     assert!(s.capacity() < 25);
148     let c2: Cow<str> = Cow::Owned(s);
149     c1.clone_from(&c2);
150     assert!(c1.into_owned().capacity() >= 25);
151 }