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.
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 // check that Cow<'a, str> implements addition
15 fn check_cow_add_cow() {
16 let borrowed1 = Cow::Borrowed("Hello, ");
17 let borrowed2 = Cow::Borrowed("World!");
18 let borrow_empty = Cow::Borrowed("");
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());
24 assert_eq!("Hello, World!", borrowed1.clone() + borrowed2.clone());
25 assert_eq!("Hello, Rustaceans!", borrowed1.clone() + owned2.clone());
27 assert_eq!("Hi, World!", owned1.clone() + borrowed2.clone());
28 assert_eq!("Hi, Rustaceans!", owned1.clone() + owned2.clone());
30 if let Cow::Owned(_) = borrowed1.clone() + borrow_empty.clone() {
31 panic!("Adding empty strings to a borrow should note allocate");
33 if let Cow::Owned(_) = borrow_empty.clone() + borrowed1.clone() {
34 panic!("Adding empty strings to a borrow should note allocate");
36 if let Cow::Owned(_) = borrowed1.clone() + owned_empty.clone() {
37 panic!("Adding empty strings to a borrow should note allocate");
39 if let Cow::Owned(_) = owned_empty.clone() + borrowed1.clone() {
40 panic!("Adding empty strings to a borrow should note allocate");
45 fn check_cow_add_str() {
46 let borrowed = Cow::Borrowed("Hello, ");
47 let borrow_empty = Cow::Borrowed("");
49 let owned: Cow<str> = Cow::Owned(String::from("Hi, "));
50 let owned_empty: Cow<str> = Cow::Owned(String::new());
52 assert_eq!("Hello, World!", borrowed.clone() + "World!");
54 assert_eq!("Hi, World!", owned.clone() + "World!");
56 if let Cow::Owned(_) = borrowed.clone() + "" {
57 panic!("Adding empty strings to a borrow should note allocate");
59 if let Cow::Owned(_) = borrow_empty.clone() + "Hello, " {
60 panic!("Adding empty strings to a borrow should note allocate");
62 if let Cow::Owned(_) = owned_empty.clone() + "Hello, " {
63 panic!("Adding empty strings to a borrow should note allocate");
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("");
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());
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");
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");
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");
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");
105 assert_eq!("Hi, World!", owned1);
106 assert_eq!("Hello, Rustaceans!", borrowed1);
110 fn check_cow_add_assign_str() {
111 let mut borrowed = Cow::Borrowed("Hello, ");
112 let borrow_empty = Cow::Borrowed("");
114 let mut owned: Cow<str> = Cow::Owned(String::from("Hi, "));
115 let owned_empty: Cow<str> = Cow::Owned(String::new());
117 let mut s = borrowed.clone();
119 assert_eq!("Hello, ", s);
120 if let Cow::Owned(_) = s {
121 panic!("Adding empty strings to a borrow should note allocate");
123 let mut s = borrow_empty.clone();
125 assert_eq!("World!", s);
126 if let Cow::Owned(_) = s {
127 panic!("Adding empty strings to a borrow should note allocate");
129 let mut s = owned_empty.clone();
131 assert_eq!("World!", s);
132 if let Cow::Owned(_) = s {
133 panic!("Adding empty strings to a borrow should note allocate");
137 borrowed += "World!";
139 assert_eq!("Hi, World!", owned);
140 assert_eq!("Hello, World!", borrowed);
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);
150 assert!(c1.into_owned().capacity() >= 25);