]> git.lizzy.rs Git - rust.git/blob - library/alloc/src/rc/tests.rs
d7c28f8063337f3d4ac78b67f2aa99050e32c3e0
[rust.git] / library / alloc / src / 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 into_from_weak_raw() {
195     let x = Rc::new(box "hello");
196     let y = Rc::downgrade(&x);
197
198     let y_ptr = Weak::into_raw(y);
199     unsafe {
200         assert_eq!(**y_ptr, "hello");
201
202         let y = Weak::from_raw(y_ptr);
203         let y_up = Weak::upgrade(&y).unwrap();
204         assert_eq!(**y_up, "hello");
205         drop(y_up);
206
207         assert_eq!(Rc::try_unwrap(x).map(|x| *x), Ok("hello"));
208     }
209 }
210
211 #[test]
212 fn test_into_from_weak_raw_unsized() {
213     use std::fmt::Display;
214     use std::string::ToString;
215
216     let arc: Rc<str> = Rc::from("foo");
217     let weak: Weak<str> = Rc::downgrade(&arc);
218
219     let ptr = Weak::into_raw(weak.clone());
220     let weak2 = unsafe { Weak::from_raw(ptr) };
221
222     assert_eq!(unsafe { &*ptr }, "foo");
223     assert!(weak.ptr_eq(&weak2));
224
225     let arc: Rc<dyn Display> = Rc::new(123);
226     let weak: Weak<dyn Display> = Rc::downgrade(&arc);
227
228     let ptr = Weak::into_raw(weak.clone());
229     let weak2 = unsafe { Weak::from_raw(ptr) };
230
231     assert_eq!(unsafe { &*ptr }.to_string(), "123");
232     assert!(weak.ptr_eq(&weak2));
233 }
234
235 #[test]
236 fn get_mut() {
237     let mut x = Rc::new(3);
238     *Rc::get_mut(&mut x).unwrap() = 4;
239     assert_eq!(*x, 4);
240     let y = x.clone();
241     assert!(Rc::get_mut(&mut x).is_none());
242     drop(y);
243     assert!(Rc::get_mut(&mut x).is_some());
244     let _w = Rc::downgrade(&x);
245     assert!(Rc::get_mut(&mut x).is_none());
246 }
247
248 #[test]
249 fn test_cowrc_clone_make_unique() {
250     let mut cow0 = Rc::new(75);
251     let mut cow1 = cow0.clone();
252     let mut cow2 = cow1.clone();
253
254     assert!(75 == *Rc::make_mut(&mut cow0));
255     assert!(75 == *Rc::make_mut(&mut cow1));
256     assert!(75 == *Rc::make_mut(&mut cow2));
257
258     *Rc::make_mut(&mut cow0) += 1;
259     *Rc::make_mut(&mut cow1) += 2;
260     *Rc::make_mut(&mut cow2) += 3;
261
262     assert!(76 == *cow0);
263     assert!(77 == *cow1);
264     assert!(78 == *cow2);
265
266     // none should point to the same backing memory
267     assert!(*cow0 != *cow1);
268     assert!(*cow0 != *cow2);
269     assert!(*cow1 != *cow2);
270 }
271
272 #[test]
273 fn test_cowrc_clone_unique2() {
274     let mut cow0 = Rc::new(75);
275     let cow1 = cow0.clone();
276     let cow2 = cow1.clone();
277
278     assert!(75 == *cow0);
279     assert!(75 == *cow1);
280     assert!(75 == *cow2);
281
282     *Rc::make_mut(&mut cow0) += 1;
283
284     assert!(76 == *cow0);
285     assert!(75 == *cow1);
286     assert!(75 == *cow2);
287
288     // cow1 and cow2 should share the same contents
289     // cow0 should have a unique reference
290     assert!(*cow0 != *cow1);
291     assert!(*cow0 != *cow2);
292     assert!(*cow1 == *cow2);
293 }
294
295 #[test]
296 fn test_cowrc_clone_weak() {
297     let mut cow0 = Rc::new(75);
298     let cow1_weak = Rc::downgrade(&cow0);
299
300     assert!(75 == *cow0);
301     assert!(75 == *cow1_weak.upgrade().unwrap());
302
303     *Rc::make_mut(&mut cow0) += 1;
304
305     assert!(76 == *cow0);
306     assert!(cow1_weak.upgrade().is_none());
307 }
308
309 #[test]
310 fn test_show() {
311     let foo = Rc::new(75);
312     assert_eq!(format!("{foo:?}"), "75");
313 }
314
315 #[test]
316 fn test_unsized() {
317     let foo: Rc<[i32]> = Rc::new([1, 2, 3]);
318     assert_eq!(foo, foo.clone());
319 }
320
321 #[test]
322 fn test_maybe_thin_unsized() {
323     // If/when custom thin DSTs exist, this test should be updated to use one
324     use std::ffi::{CStr, CString};
325
326     let x: Rc<CStr> = Rc::from(CString::new("swordfish").unwrap().into_boxed_c_str());
327     assert_eq!(format!("{x:?}"), "\"swordfish\"");
328     let y: Weak<CStr> = Rc::downgrade(&x);
329     drop(x);
330
331     // At this point, the weak points to a dropped DST
332     assert!(y.upgrade().is_none());
333     // But we still need to be able to get the alloc layout to drop.
334     // CStr has no drop glue, but custom DSTs might, and need to work.
335     drop(y);
336 }
337
338 #[test]
339 fn test_from_owned() {
340     let foo = 123;
341     let foo_rc = Rc::from(foo);
342     assert!(123 == *foo_rc);
343 }
344
345 #[test]
346 fn test_new_weak() {
347     let foo: Weak<usize> = Weak::new();
348     assert!(foo.upgrade().is_none());
349 }
350
351 #[test]
352 fn test_ptr_eq() {
353     let five = Rc::new(5);
354     let same_five = five.clone();
355     let other_five = Rc::new(5);
356
357     assert!(Rc::ptr_eq(&five, &same_five));
358     assert!(!Rc::ptr_eq(&five, &other_five));
359 }
360
361 #[test]
362 fn test_from_str() {
363     let r: Rc<str> = Rc::from("foo");
364
365     assert_eq!(&r[..], "foo");
366 }
367
368 #[test]
369 fn test_copy_from_slice() {
370     let s: &[u32] = &[1, 2, 3];
371     let r: Rc<[u32]> = Rc::from(s);
372
373     assert_eq!(&r[..], [1, 2, 3]);
374 }
375
376 #[test]
377 fn test_clone_from_slice() {
378     #[derive(Clone, Debug, Eq, PartialEq)]
379     struct X(u32);
380
381     let s: &[X] = &[X(1), X(2), X(3)];
382     let r: Rc<[X]> = Rc::from(s);
383
384     assert_eq!(&r[..], s);
385 }
386
387 #[test]
388 #[should_panic]
389 fn test_clone_from_slice_panic() {
390     use std::string::{String, ToString};
391
392     struct Fail(u32, String);
393
394     impl Clone for Fail {
395         fn clone(&self) -> Fail {
396             if self.0 == 2 {
397                 panic!();
398             }
399             Fail(self.0, self.1.clone())
400         }
401     }
402
403     let s: &[Fail] =
404         &[Fail(0, "foo".to_string()), Fail(1, "bar".to_string()), Fail(2, "baz".to_string())];
405
406     // Should panic, but not cause memory corruption
407     let _r: Rc<[Fail]> = Rc::from(s);
408 }
409
410 #[test]
411 fn test_from_box() {
412     let b: Box<u32> = box 123;
413     let r: Rc<u32> = Rc::from(b);
414
415     assert_eq!(*r, 123);
416 }
417
418 #[test]
419 fn test_from_box_str() {
420     use std::string::String;
421
422     let s = String::from("foo").into_boxed_str();
423     let r: Rc<str> = Rc::from(s);
424
425     assert_eq!(&r[..], "foo");
426 }
427
428 #[test]
429 fn test_from_box_slice() {
430     let s = vec![1, 2, 3].into_boxed_slice();
431     let r: Rc<[u32]> = Rc::from(s);
432
433     assert_eq!(&r[..], [1, 2, 3]);
434 }
435
436 #[test]
437 fn test_from_box_trait() {
438     use std::fmt::Display;
439     use std::string::ToString;
440
441     let b: Box<dyn Display> = box 123;
442     let r: Rc<dyn Display> = Rc::from(b);
443
444     assert_eq!(r.to_string(), "123");
445 }
446
447 #[test]
448 fn test_from_box_trait_zero_sized() {
449     use std::fmt::Debug;
450
451     let b: Box<dyn Debug> = box ();
452     let r: Rc<dyn Debug> = Rc::from(b);
453
454     assert_eq!(format!("{r:?}"), "()");
455 }
456
457 #[test]
458 fn test_from_vec() {
459     let v = vec![1, 2, 3];
460     let r: Rc<[u32]> = Rc::from(v);
461
462     assert_eq!(&r[..], [1, 2, 3]);
463 }
464
465 #[test]
466 fn test_downcast() {
467     use std::any::Any;
468
469     let r1: Rc<dyn Any> = Rc::new(i32::MAX);
470     let r2: Rc<dyn Any> = Rc::new("abc");
471
472     assert!(r1.clone().downcast::<u32>().is_err());
473
474     let r1i32 = r1.downcast::<i32>();
475     assert!(r1i32.is_ok());
476     assert_eq!(r1i32.unwrap(), Rc::new(i32::MAX));
477
478     assert!(r2.clone().downcast::<i32>().is_err());
479
480     let r2str = r2.downcast::<&'static str>();
481     assert!(r2str.is_ok());
482     assert_eq!(r2str.unwrap(), Rc::new("abc"));
483 }
484
485 #[test]
486 fn test_array_from_slice() {
487     let v = vec![1, 2, 3];
488     let r: Rc<[u32]> = Rc::from(v);
489
490     let a: Result<Rc<[u32; 3]>, _> = r.clone().try_into();
491     assert!(a.is_ok());
492
493     let a: Result<Rc<[u32; 2]>, _> = r.clone().try_into();
494     assert!(a.is_err());
495 }
496
497 #[test]
498 fn test_rc_cyclic_with_zero_refs() {
499     struct ZeroRefs {
500         inner: Weak<ZeroRefs>,
501     }
502
503     let zero_refs = Rc::new_cyclic(|inner| {
504         assert_eq!(inner.strong_count(), 0);
505         assert!(inner.upgrade().is_none());
506         ZeroRefs { inner: Weak::new() }
507     });
508
509     assert_eq!(Rc::strong_count(&zero_refs), 1);
510     assert_eq!(Rc::weak_count(&zero_refs), 0);
511     assert_eq!(zero_refs.inner.strong_count(), 0);
512     assert_eq!(zero_refs.inner.weak_count(), 0);
513 }
514
515 #[test]
516 fn test_rc_cyclic_with_one_ref() {
517     struct OneRef {
518         inner: Weak<OneRef>,
519     }
520
521     let one_ref = Rc::new_cyclic(|inner| {
522         assert_eq!(inner.strong_count(), 0);
523         assert!(inner.upgrade().is_none());
524         OneRef { inner: inner.clone() }
525     });
526
527     assert_eq!(Rc::strong_count(&one_ref), 1);
528     assert_eq!(Rc::weak_count(&one_ref), 1);
529
530     let one_ref2 = Weak::upgrade(&one_ref.inner).unwrap();
531     assert!(Rc::ptr_eq(&one_ref, &one_ref2));
532
533     assert_eq!(one_ref.inner.strong_count(), 2);
534     assert_eq!(one_ref.inner.weak_count(), 1);
535 }
536
537 #[test]
538 fn test_rc_cyclic_with_two_ref() {
539     struct TwoRefs {
540         inner: Weak<TwoRefs>,
541         inner1: Weak<TwoRefs>,
542     }
543
544     let two_refs = Rc::new_cyclic(|inner| {
545         assert_eq!(inner.strong_count(), 0);
546         assert!(inner.upgrade().is_none());
547         TwoRefs { inner: inner.clone(), inner1: inner.clone() }
548     });
549
550     assert_eq!(Rc::strong_count(&two_refs), 1);
551     assert_eq!(Rc::weak_count(&two_refs), 2);
552
553     let two_ref3 = Weak::upgrade(&two_refs.inner).unwrap();
554     assert!(Rc::ptr_eq(&two_refs, &two_ref3));
555
556     let two_ref2 = Weak::upgrade(&two_refs.inner1).unwrap();
557     assert!(Rc::ptr_eq(&two_refs, &two_ref2));
558
559     assert_eq!(Rc::strong_count(&two_refs), 3);
560     assert_eq!(Rc::weak_count(&two_refs), 2);
561 }