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