]> git.proxmox.com Git - rustc.git/blob - src/liballoc/rc/tests.rs
New upstream version 1.46.0~beta.2+dfsg1
[rustc.git] / src / liballoc / rc / tests.rs
1 use super::*;
2
3 use std::boxed::Box;
4 use std::cell::RefCell;
5 use std::clone::Clone;
6 use std::convert::{From, TryInto};
7 use std::mem::drop;
8 use std::option::Option::{self, None, Some};
9 use std::result::Result::{Err, Ok};
10
11 #[test]
12 fn test_clone() {
13 let x = Rc::new(RefCell::new(5));
14 let y = x.clone();
15 *x.borrow_mut() = 20;
16 assert_eq!(*y.borrow(), 20);
17 }
18
19 #[test]
20 fn test_simple() {
21 let x = Rc::new(5);
22 assert_eq!(*x, 5);
23 }
24
25 #[test]
26 fn test_simple_clone() {
27 let x = Rc::new(5);
28 let y = x.clone();
29 assert_eq!(*x, 5);
30 assert_eq!(*y, 5);
31 }
32
33 #[test]
34 fn test_destructor() {
35 let x: Rc<Box<_>> = Rc::new(box 5);
36 assert_eq!(**x, 5);
37 }
38
39 #[test]
40 fn test_live() {
41 let x = Rc::new(5);
42 let y = Rc::downgrade(&x);
43 assert!(y.upgrade().is_some());
44 }
45
46 #[test]
47 fn test_dead() {
48 let x = Rc::new(5);
49 let y = Rc::downgrade(&x);
50 drop(x);
51 assert!(y.upgrade().is_none());
52 }
53
54 #[test]
55 fn weak_self_cyclic() {
56 struct Cycle {
57 x: RefCell<Option<Weak<Cycle>>>,
58 }
59
60 let a = Rc::new(Cycle { x: RefCell::new(None) });
61 let b = Rc::downgrade(&a.clone());
62 *a.x.borrow_mut() = Some(b);
63
64 // hopefully we don't double-free (or leak)...
65 }
66
67 #[test]
68 fn is_unique() {
69 let x = Rc::new(3);
70 assert!(Rc::is_unique(&x));
71 let y = x.clone();
72 assert!(!Rc::is_unique(&x));
73 drop(y);
74 assert!(Rc::is_unique(&x));
75 let w = Rc::downgrade(&x);
76 assert!(!Rc::is_unique(&x));
77 drop(w);
78 assert!(Rc::is_unique(&x));
79 }
80
81 #[test]
82 fn test_strong_count() {
83 let a = Rc::new(0);
84 assert!(Rc::strong_count(&a) == 1);
85 let w = Rc::downgrade(&a);
86 assert!(Rc::strong_count(&a) == 1);
87 let b = w.upgrade().expect("upgrade of live rc failed");
88 assert!(Rc::strong_count(&b) == 2);
89 assert!(Rc::strong_count(&a) == 2);
90 drop(w);
91 drop(a);
92 assert!(Rc::strong_count(&b) == 1);
93 let c = b.clone();
94 assert!(Rc::strong_count(&b) == 2);
95 assert!(Rc::strong_count(&c) == 2);
96 }
97
98 #[test]
99 fn test_weak_count() {
100 let a = Rc::new(0);
101 assert!(Rc::strong_count(&a) == 1);
102 assert!(Rc::weak_count(&a) == 0);
103 let w = Rc::downgrade(&a);
104 assert!(Rc::strong_count(&a) == 1);
105 assert!(Rc::weak_count(&a) == 1);
106 drop(w);
107 assert!(Rc::strong_count(&a) == 1);
108 assert!(Rc::weak_count(&a) == 0);
109 let c = a.clone();
110 assert!(Rc::strong_count(&a) == 2);
111 assert!(Rc::weak_count(&a) == 0);
112 drop(c);
113 }
114
115 #[test]
116 fn weak_counts() {
117 assert_eq!(Weak::weak_count(&Weak::<u64>::new()), 0);
118 assert_eq!(Weak::strong_count(&Weak::<u64>::new()), 0);
119
120 let a = Rc::new(0);
121 let w = Rc::downgrade(&a);
122 assert_eq!(Weak::strong_count(&w), 1);
123 assert_eq!(Weak::weak_count(&w), 1);
124 let w2 = w.clone();
125 assert_eq!(Weak::strong_count(&w), 1);
126 assert_eq!(Weak::weak_count(&w), 2);
127 assert_eq!(Weak::strong_count(&w2), 1);
128 assert_eq!(Weak::weak_count(&w2), 2);
129 drop(w);
130 assert_eq!(Weak::strong_count(&w2), 1);
131 assert_eq!(Weak::weak_count(&w2), 1);
132 let a2 = a.clone();
133 assert_eq!(Weak::strong_count(&w2), 2);
134 assert_eq!(Weak::weak_count(&w2), 1);
135 drop(a2);
136 drop(a);
137 assert_eq!(Weak::strong_count(&w2), 0);
138 assert_eq!(Weak::weak_count(&w2), 0);
139 drop(w2);
140 }
141
142 #[test]
143 fn try_unwrap() {
144 let x = Rc::new(3);
145 assert_eq!(Rc::try_unwrap(x), Ok(3));
146 let x = Rc::new(4);
147 let _y = x.clone();
148 assert_eq!(Rc::try_unwrap(x), Err(Rc::new(4)));
149 let x = Rc::new(5);
150 let _w = Rc::downgrade(&x);
151 assert_eq!(Rc::try_unwrap(x), Ok(5));
152 }
153
154 #[test]
155 fn into_from_raw() {
156 let x = Rc::new(box "hello");
157 let y = x.clone();
158
159 let x_ptr = Rc::into_raw(x);
160 drop(y);
161 unsafe {
162 assert_eq!(**x_ptr, "hello");
163
164 let x = Rc::from_raw(x_ptr);
165 assert_eq!(**x, "hello");
166
167 assert_eq!(Rc::try_unwrap(x).map(|x| *x), Ok("hello"));
168 }
169 }
170
171 #[test]
172 fn test_into_from_raw_unsized() {
173 use std::fmt::Display;
174 use std::string::ToString;
175
176 let rc: Rc<str> = Rc::from("foo");
177
178 let ptr = Rc::into_raw(rc.clone());
179 let rc2 = unsafe { Rc::from_raw(ptr) };
180
181 assert_eq!(unsafe { &*ptr }, "foo");
182 assert_eq!(rc, rc2);
183
184 let rc: Rc<dyn Display> = Rc::new(123);
185
186 let ptr = Rc::into_raw(rc.clone());
187 let rc2 = unsafe { Rc::from_raw(ptr) };
188
189 assert_eq!(unsafe { &*ptr }.to_string(), "123");
190 assert_eq!(rc2.to_string(), "123");
191 }
192
193 #[test]
194 fn get_mut() {
195 let mut x = Rc::new(3);
196 *Rc::get_mut(&mut x).unwrap() = 4;
197 assert_eq!(*x, 4);
198 let y = x.clone();
199 assert!(Rc::get_mut(&mut x).is_none());
200 drop(y);
201 assert!(Rc::get_mut(&mut x).is_some());
202 let _w = Rc::downgrade(&x);
203 assert!(Rc::get_mut(&mut x).is_none());
204 }
205
206 #[test]
207 fn test_cowrc_clone_make_unique() {
208 let mut cow0 = Rc::new(75);
209 let mut cow1 = cow0.clone();
210 let mut cow2 = cow1.clone();
211
212 assert!(75 == *Rc::make_mut(&mut cow0));
213 assert!(75 == *Rc::make_mut(&mut cow1));
214 assert!(75 == *Rc::make_mut(&mut cow2));
215
216 *Rc::make_mut(&mut cow0) += 1;
217 *Rc::make_mut(&mut cow1) += 2;
218 *Rc::make_mut(&mut cow2) += 3;
219
220 assert!(76 == *cow0);
221 assert!(77 == *cow1);
222 assert!(78 == *cow2);
223
224 // none should point to the same backing memory
225 assert!(*cow0 != *cow1);
226 assert!(*cow0 != *cow2);
227 assert!(*cow1 != *cow2);
228 }
229
230 #[test]
231 fn test_cowrc_clone_unique2() {
232 let mut cow0 = Rc::new(75);
233 let cow1 = cow0.clone();
234 let cow2 = cow1.clone();
235
236 assert!(75 == *cow0);
237 assert!(75 == *cow1);
238 assert!(75 == *cow2);
239
240 *Rc::make_mut(&mut cow0) += 1;
241
242 assert!(76 == *cow0);
243 assert!(75 == *cow1);
244 assert!(75 == *cow2);
245
246 // cow1 and cow2 should share the same contents
247 // cow0 should have a unique reference
248 assert!(*cow0 != *cow1);
249 assert!(*cow0 != *cow2);
250 assert!(*cow1 == *cow2);
251 }
252
253 #[test]
254 fn test_cowrc_clone_weak() {
255 let mut cow0 = Rc::new(75);
256 let cow1_weak = Rc::downgrade(&cow0);
257
258 assert!(75 == *cow0);
259 assert!(75 == *cow1_weak.upgrade().unwrap());
260
261 *Rc::make_mut(&mut cow0) += 1;
262
263 assert!(76 == *cow0);
264 assert!(cow1_weak.upgrade().is_none());
265 }
266
267 #[test]
268 fn test_show() {
269 let foo = Rc::new(75);
270 assert_eq!(format!("{:?}", foo), "75");
271 }
272
273 #[test]
274 fn test_unsized() {
275 let foo: Rc<[i32]> = Rc::new([1, 2, 3]);
276 assert_eq!(foo, foo.clone());
277 }
278
279 #[test]
280 fn test_from_owned() {
281 let foo = 123;
282 let foo_rc = Rc::from(foo);
283 assert!(123 == *foo_rc);
284 }
285
286 #[test]
287 fn test_new_weak() {
288 let foo: Weak<usize> = Weak::new();
289 assert!(foo.upgrade().is_none());
290 }
291
292 #[test]
293 fn test_ptr_eq() {
294 let five = Rc::new(5);
295 let same_five = five.clone();
296 let other_five = Rc::new(5);
297
298 assert!(Rc::ptr_eq(&five, &same_five));
299 assert!(!Rc::ptr_eq(&five, &other_five));
300 }
301
302 #[test]
303 fn test_from_str() {
304 let r: Rc<str> = Rc::from("foo");
305
306 assert_eq!(&r[..], "foo");
307 }
308
309 #[test]
310 fn test_copy_from_slice() {
311 let s: &[u32] = &[1, 2, 3];
312 let r: Rc<[u32]> = Rc::from(s);
313
314 assert_eq!(&r[..], [1, 2, 3]);
315 }
316
317 #[test]
318 fn test_clone_from_slice() {
319 #[derive(Clone, Debug, Eq, PartialEq)]
320 struct X(u32);
321
322 let s: &[X] = &[X(1), X(2), X(3)];
323 let r: Rc<[X]> = Rc::from(s);
324
325 assert_eq!(&r[..], s);
326 }
327
328 #[test]
329 #[should_panic]
330 fn test_clone_from_slice_panic() {
331 use std::string::{String, ToString};
332
333 struct Fail(u32, String);
334
335 impl Clone for Fail {
336 fn clone(&self) -> Fail {
337 if self.0 == 2 {
338 panic!();
339 }
340 Fail(self.0, self.1.clone())
341 }
342 }
343
344 let s: &[Fail] =
345 &[Fail(0, "foo".to_string()), Fail(1, "bar".to_string()), Fail(2, "baz".to_string())];
346
347 // Should panic, but not cause memory corruption
348 let _r: Rc<[Fail]> = Rc::from(s);
349 }
350
351 #[test]
352 fn test_from_box() {
353 let b: Box<u32> = box 123;
354 let r: Rc<u32> = Rc::from(b);
355
356 assert_eq!(*r, 123);
357 }
358
359 #[test]
360 fn test_from_box_str() {
361 use std::string::String;
362
363 let s = String::from("foo").into_boxed_str();
364 let r: Rc<str> = Rc::from(s);
365
366 assert_eq!(&r[..], "foo");
367 }
368
369 #[test]
370 fn test_from_box_slice() {
371 let s = vec![1, 2, 3].into_boxed_slice();
372 let r: Rc<[u32]> = Rc::from(s);
373
374 assert_eq!(&r[..], [1, 2, 3]);
375 }
376
377 #[test]
378 fn test_from_box_trait() {
379 use std::fmt::Display;
380 use std::string::ToString;
381
382 let b: Box<dyn Display> = box 123;
383 let r: Rc<dyn Display> = Rc::from(b);
384
385 assert_eq!(r.to_string(), "123");
386 }
387
388 #[test]
389 fn test_from_box_trait_zero_sized() {
390 use std::fmt::Debug;
391
392 let b: Box<dyn Debug> = box ();
393 let r: Rc<dyn Debug> = Rc::from(b);
394
395 assert_eq!(format!("{:?}", r), "()");
396 }
397
398 #[test]
399 fn test_from_vec() {
400 let v = vec![1, 2, 3];
401 let r: Rc<[u32]> = Rc::from(v);
402
403 assert_eq!(&r[..], [1, 2, 3]);
404 }
405
406 #[test]
407 fn test_downcast() {
408 use std::any::Any;
409
410 let r1: Rc<dyn Any> = Rc::new(i32::MAX);
411 let r2: Rc<dyn Any> = Rc::new("abc");
412
413 assert!(r1.clone().downcast::<u32>().is_err());
414
415 let r1i32 = r1.downcast::<i32>();
416 assert!(r1i32.is_ok());
417 assert_eq!(r1i32.unwrap(), Rc::new(i32::MAX));
418
419 assert!(r2.clone().downcast::<i32>().is_err());
420
421 let r2str = r2.downcast::<&'static str>();
422 assert!(r2str.is_ok());
423 assert_eq!(r2str.unwrap(), Rc::new("abc"));
424 }
425
426 #[test]
427 fn test_array_from_slice() {
428 let v = vec![1, 2, 3];
429 let r: Rc<[u32]> = Rc::from(v);
430
431 let a: Result<Rc<[u32; 3]>, _> = r.clone().try_into();
432 assert!(a.is_ok());
433
434 let a: Result<Rc<[u32; 2]>, _> = r.clone().try_into();
435 assert!(a.is_err());
436 }