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]
test_clone()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]
test_simple()20 fn test_simple() {
21 let x = Rc::new(5);
22 assert_eq!(*x, 5);
23 }
24
25 #[test]
test_simple_clone()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]
test_destructor()34 fn test_destructor() {
35 let x: Rc<Box<_>> = Rc::new(Box::new(5));
36 assert_eq!(**x, 5);
37 }
38
39 #[test]
test_live()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]
test_dead()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]
weak_self_cyclic()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]
is_unique()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]
test_strong_count()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]
test_weak_count()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]
weak_counts()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]
try_unwrap()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]
into_inner()155 fn into_inner() {
156 let x = Rc::new(3);
157 assert_eq!(Rc::into_inner(x), Some(3));
158
159 let x = Rc::new(4);
160 let y = Rc::clone(&x);
161 assert_eq!(Rc::into_inner(x), None);
162 assert_eq!(Rc::into_inner(y), Some(4));
163
164 let x = Rc::new(5);
165 let _w = Rc::downgrade(&x);
166 assert_eq!(Rc::into_inner(x), Some(5));
167 }
168
169 #[test]
into_from_raw()170 fn into_from_raw() {
171 let x = Rc::new(Box::new("hello"));
172 let y = x.clone();
173
174 let x_ptr = Rc::into_raw(x);
175 drop(y);
176 unsafe {
177 assert_eq!(**x_ptr, "hello");
178
179 let x = Rc::from_raw(x_ptr);
180 assert_eq!(**x, "hello");
181
182 assert_eq!(Rc::try_unwrap(x).map(|x| *x), Ok("hello"));
183 }
184 }
185
186 #[test]
test_into_from_raw_unsized()187 fn test_into_from_raw_unsized() {
188 use std::fmt::Display;
189 use std::string::ToString;
190
191 let rc: Rc<str> = Rc::from("foo");
192
193 let ptr = Rc::into_raw(rc.clone());
194 let rc2 = unsafe { Rc::from_raw(ptr) };
195
196 assert_eq!(unsafe { &*ptr }, "foo");
197 assert_eq!(rc, rc2);
198
199 let rc: Rc<dyn Display> = Rc::new(123);
200
201 let ptr = Rc::into_raw(rc.clone());
202 let rc2 = unsafe { Rc::from_raw(ptr) };
203
204 assert_eq!(unsafe { &*ptr }.to_string(), "123");
205 assert_eq!(rc2.to_string(), "123");
206 }
207
208 #[test]
into_from_weak_raw()209 fn into_from_weak_raw() {
210 let x = Rc::new(Box::new("hello"));
211 let y = Rc::downgrade(&x);
212
213 let y_ptr = Weak::into_raw(y);
214 unsafe {
215 assert_eq!(**y_ptr, "hello");
216
217 let y = Weak::from_raw(y_ptr);
218 let y_up = Weak::upgrade(&y).unwrap();
219 assert_eq!(**y_up, "hello");
220 drop(y_up);
221
222 assert_eq!(Rc::try_unwrap(x).map(|x| *x), Ok("hello"));
223 }
224 }
225
226 #[test]
test_into_from_weak_raw_unsized()227 fn test_into_from_weak_raw_unsized() {
228 use std::fmt::Display;
229 use std::string::ToString;
230
231 let arc: Rc<str> = Rc::from("foo");
232 let weak: Weak<str> = Rc::downgrade(&arc);
233
234 let ptr = Weak::into_raw(weak.clone());
235 let weak2 = unsafe { Weak::from_raw(ptr) };
236
237 assert_eq!(unsafe { &*ptr }, "foo");
238 assert!(weak.ptr_eq(&weak2));
239
240 let arc: Rc<dyn Display> = Rc::new(123);
241 let weak: Weak<dyn Display> = Rc::downgrade(&arc);
242
243 let ptr = Weak::into_raw(weak.clone());
244 let weak2 = unsafe { Weak::from_raw(ptr) };
245
246 assert_eq!(unsafe { &*ptr }.to_string(), "123");
247 assert!(weak.ptr_eq(&weak2));
248 }
249
250 #[test]
get_mut()251 fn get_mut() {
252 let mut x = Rc::new(3);
253 *Rc::get_mut(&mut x).unwrap() = 4;
254 assert_eq!(*x, 4);
255 let y = x.clone();
256 assert!(Rc::get_mut(&mut x).is_none());
257 drop(y);
258 assert!(Rc::get_mut(&mut x).is_some());
259 let _w = Rc::downgrade(&x);
260 assert!(Rc::get_mut(&mut x).is_none());
261 }
262
263 #[test]
test_cowrc_clone_make_unique()264 fn test_cowrc_clone_make_unique() {
265 let mut cow0 = Rc::new(75);
266 let mut cow1 = cow0.clone();
267 let mut cow2 = cow1.clone();
268
269 assert!(75 == *Rc::make_mut(&mut cow0));
270 assert!(75 == *Rc::make_mut(&mut cow1));
271 assert!(75 == *Rc::make_mut(&mut cow2));
272
273 *Rc::make_mut(&mut cow0) += 1;
274 *Rc::make_mut(&mut cow1) += 2;
275 *Rc::make_mut(&mut cow2) += 3;
276
277 assert!(76 == *cow0);
278 assert!(77 == *cow1);
279 assert!(78 == *cow2);
280
281 // none should point to the same backing memory
282 assert!(*cow0 != *cow1);
283 assert!(*cow0 != *cow2);
284 assert!(*cow1 != *cow2);
285 }
286
287 #[test]
test_cowrc_clone_unique2()288 fn test_cowrc_clone_unique2() {
289 let mut cow0 = Rc::new(75);
290 let cow1 = cow0.clone();
291 let cow2 = cow1.clone();
292
293 assert!(75 == *cow0);
294 assert!(75 == *cow1);
295 assert!(75 == *cow2);
296
297 *Rc::make_mut(&mut cow0) += 1;
298
299 assert!(76 == *cow0);
300 assert!(75 == *cow1);
301 assert!(75 == *cow2);
302
303 // cow1 and cow2 should share the same contents
304 // cow0 should have a unique reference
305 assert!(*cow0 != *cow1);
306 assert!(*cow0 != *cow2);
307 assert!(*cow1 == *cow2);
308 }
309
310 #[test]
test_cowrc_clone_weak()311 fn test_cowrc_clone_weak() {
312 let mut cow0 = Rc::new(75);
313 let cow1_weak = Rc::downgrade(&cow0);
314
315 assert!(75 == *cow0);
316 assert!(75 == *cow1_weak.upgrade().unwrap());
317
318 *Rc::make_mut(&mut cow0) += 1;
319
320 assert!(76 == *cow0);
321 assert!(cow1_weak.upgrade().is_none());
322 }
323
324 #[test]
test_show()325 fn test_show() {
326 let foo = Rc::new(75);
327 assert_eq!(format!("{foo:?}"), "75");
328 }
329
330 #[test]
test_unsized()331 fn test_unsized() {
332 let foo: Rc<[i32]> = Rc::new([1, 2, 3]);
333 assert_eq!(foo, foo.clone());
334 }
335
336 #[test]
test_maybe_thin_unsized()337 fn test_maybe_thin_unsized() {
338 // If/when custom thin DSTs exist, this test should be updated to use one
339 use std::ffi::{CStr, CString};
340
341 let x: Rc<CStr> = Rc::from(CString::new("swordfish").unwrap().into_boxed_c_str());
342 assert_eq!(format!("{x:?}"), "\"swordfish\"");
343 let y: Weak<CStr> = Rc::downgrade(&x);
344 drop(x);
345
346 // At this point, the weak points to a dropped DST
347 assert!(y.upgrade().is_none());
348 // But we still need to be able to get the alloc layout to drop.
349 // CStr has no drop glue, but custom DSTs might, and need to work.
350 drop(y);
351 }
352
353 #[test]
test_from_owned()354 fn test_from_owned() {
355 let foo = 123;
356 let foo_rc = Rc::from(foo);
357 assert!(123 == *foo_rc);
358 }
359
360 #[test]
test_new_weak()361 fn test_new_weak() {
362 let foo: Weak<usize> = Weak::new();
363 assert!(foo.upgrade().is_none());
364 }
365
366 #[test]
test_ptr_eq()367 fn test_ptr_eq() {
368 let five = Rc::new(5);
369 let same_five = five.clone();
370 let other_five = Rc::new(5);
371
372 assert!(Rc::ptr_eq(&five, &same_five));
373 assert!(!Rc::ptr_eq(&five, &other_five));
374 }
375
376 #[test]
test_from_str()377 fn test_from_str() {
378 let r: Rc<str> = Rc::from("foo");
379
380 assert_eq!(&r[..], "foo");
381 }
382
383 #[test]
test_copy_from_slice()384 fn test_copy_from_slice() {
385 let s: &[u32] = &[1, 2, 3];
386 let r: Rc<[u32]> = Rc::from(s);
387
388 assert_eq!(&r[..], [1, 2, 3]);
389 }
390
391 #[test]
test_clone_from_slice()392 fn test_clone_from_slice() {
393 #[derive(Clone, Debug, Eq, PartialEq)]
394 struct X(u32);
395
396 let s: &[X] = &[X(1), X(2), X(3)];
397 let r: Rc<[X]> = Rc::from(s);
398
399 assert_eq!(&r[..], s);
400 }
401
402 #[test]
403 #[should_panic]
test_clone_from_slice_panic()404 fn test_clone_from_slice_panic() {
405 use std::string::{String, ToString};
406
407 struct Fail(u32, String);
408
409 impl Clone for Fail {
410 fn clone(&self) -> Fail {
411 if self.0 == 2 {
412 panic!();
413 }
414 Fail(self.0, self.1.clone())
415 }
416 }
417
418 let s: &[Fail] =
419 &[Fail(0, "foo".to_string()), Fail(1, "bar".to_string()), Fail(2, "baz".to_string())];
420
421 // Should panic, but not cause memory corruption
422 let _r: Rc<[Fail]> = Rc::from(s);
423 }
424
425 #[test]
test_from_box()426 fn test_from_box() {
427 let b: Box<u32> = Box::new(123);
428 let r: Rc<u32> = Rc::from(b);
429
430 assert_eq!(*r, 123);
431 }
432
433 #[test]
test_from_box_str()434 fn test_from_box_str() {
435 use std::string::String;
436
437 let s = String::from("foo").into_boxed_str();
438 let r: Rc<str> = Rc::from(s);
439
440 assert_eq!(&r[..], "foo");
441 }
442
443 #[test]
test_from_box_slice()444 fn test_from_box_slice() {
445 let s = vec![1, 2, 3].into_boxed_slice();
446 let r: Rc<[u32]> = Rc::from(s);
447
448 assert_eq!(&r[..], [1, 2, 3]);
449 }
450
451 #[test]
test_from_box_trait()452 fn test_from_box_trait() {
453 use std::fmt::Display;
454 use std::string::ToString;
455
456 let b: Box<dyn Display> = Box::new(123);
457 let r: Rc<dyn Display> = Rc::from(b);
458
459 assert_eq!(r.to_string(), "123");
460 }
461
462 #[test]
test_from_box_trait_zero_sized()463 fn test_from_box_trait_zero_sized() {
464 use std::fmt::Debug;
465
466 let b: Box<dyn Debug> = Box::new(());
467 let r: Rc<dyn Debug> = Rc::from(b);
468
469 assert_eq!(format!("{r:?}"), "()");
470 }
471
472 #[test]
test_from_vec()473 fn test_from_vec() {
474 let v = vec![1, 2, 3];
475 let r: Rc<[u32]> = Rc::from(v);
476
477 assert_eq!(&r[..], [1, 2, 3]);
478 }
479
480 #[test]
test_downcast()481 fn test_downcast() {
482 use std::any::Any;
483
484 let r1: Rc<dyn Any> = Rc::new(i32::MAX);
485 let r2: Rc<dyn Any> = Rc::new("abc");
486
487 assert!(r1.clone().downcast::<u32>().is_err());
488
489 let r1i32 = r1.downcast::<i32>();
490 assert!(r1i32.is_ok());
491 assert_eq!(r1i32.unwrap(), Rc::new(i32::MAX));
492
493 assert!(r2.clone().downcast::<i32>().is_err());
494
495 let r2str = r2.downcast::<&'static str>();
496 assert!(r2str.is_ok());
497 assert_eq!(r2str.unwrap(), Rc::new("abc"));
498 }
499
500 #[test]
test_array_from_slice()501 fn test_array_from_slice() {
502 let v = vec![1, 2, 3];
503 let r: Rc<[u32]> = Rc::from(v);
504
505 let a: Result<Rc<[u32; 3]>, _> = r.clone().try_into();
506 assert!(a.is_ok());
507
508 let a: Result<Rc<[u32; 2]>, _> = r.clone().try_into();
509 assert!(a.is_err());
510 }
511
512 #[test]
test_rc_cyclic_with_zero_refs()513 fn test_rc_cyclic_with_zero_refs() {
514 struct ZeroRefs {
515 inner: Weak<ZeroRefs>,
516 }
517
518 let zero_refs = Rc::new_cyclic(|inner| {
519 assert_eq!(inner.strong_count(), 0);
520 assert!(inner.upgrade().is_none());
521 ZeroRefs { inner: Weak::new() }
522 });
523
524 assert_eq!(Rc::strong_count(&zero_refs), 1);
525 assert_eq!(Rc::weak_count(&zero_refs), 0);
526 assert_eq!(zero_refs.inner.strong_count(), 0);
527 assert_eq!(zero_refs.inner.weak_count(), 0);
528 }
529
530 #[test]
test_rc_cyclic_with_one_ref()531 fn test_rc_cyclic_with_one_ref() {
532 struct OneRef {
533 inner: Weak<OneRef>,
534 }
535
536 let one_ref = Rc::new_cyclic(|inner| {
537 assert_eq!(inner.strong_count(), 0);
538 assert!(inner.upgrade().is_none());
539 OneRef { inner: inner.clone() }
540 });
541
542 assert_eq!(Rc::strong_count(&one_ref), 1);
543 assert_eq!(Rc::weak_count(&one_ref), 1);
544
545 let one_ref2 = Weak::upgrade(&one_ref.inner).unwrap();
546 assert!(Rc::ptr_eq(&one_ref, &one_ref2));
547
548 assert_eq!(one_ref.inner.strong_count(), 2);
549 assert_eq!(one_ref.inner.weak_count(), 1);
550 }
551
552 #[test]
test_rc_cyclic_with_two_ref()553 fn test_rc_cyclic_with_two_ref() {
554 struct TwoRefs {
555 inner: Weak<TwoRefs>,
556 inner1: Weak<TwoRefs>,
557 }
558
559 let two_refs = Rc::new_cyclic(|inner| {
560 assert_eq!(inner.strong_count(), 0);
561 assert!(inner.upgrade().is_none());
562 TwoRefs { inner: inner.clone(), inner1: inner.clone() }
563 });
564
565 assert_eq!(Rc::strong_count(&two_refs), 1);
566 assert_eq!(Rc::weak_count(&two_refs), 2);
567
568 let two_ref3 = Weak::upgrade(&two_refs.inner).unwrap();
569 assert!(Rc::ptr_eq(&two_refs, &two_ref3));
570
571 let two_ref2 = Weak::upgrade(&two_refs.inner1).unwrap();
572 assert!(Rc::ptr_eq(&two_refs, &two_ref2));
573
574 assert_eq!(Rc::strong_count(&two_refs), 3);
575 assert_eq!(Rc::weak_count(&two_refs), 2);
576 }
577
578 #[test]
test_unique_rc_weak()579 fn test_unique_rc_weak() {
580 let rc = UniqueRc::new(42);
581 let weak = UniqueRc::downgrade(&rc);
582 assert!(weak.upgrade().is_none());
583
584 let _rc = UniqueRc::into_rc(rc);
585 assert_eq!(*weak.upgrade().unwrap(), 42);
586 }
587
588 #[test]
test_unique_rc_drop_weak()589 fn test_unique_rc_drop_weak() {
590 let rc = UniqueRc::new(42);
591 let weak = UniqueRc::downgrade(&rc);
592 mem::drop(weak);
593
594 let rc = UniqueRc::into_rc(rc);
595 assert_eq!(*rc, 42);
596 }
597
598 #[test]
test_unique_rc_drops_contents()599 fn test_unique_rc_drops_contents() {
600 let mut dropped = false;
601 struct DropMe<'a>(&'a mut bool);
602 impl Drop for DropMe<'_> {
603 fn drop(&mut self) {
604 *self.0 = true;
605 }
606 }
607 {
608 let rc = UniqueRc::new(DropMe(&mut dropped));
609 drop(rc);
610 }
611 assert!(dropped);
612 }
613
614 #[test]
test_unique_rc_weak_clone_holding_ref()615 fn test_unique_rc_weak_clone_holding_ref() {
616 let mut v = UniqueRc::new(0u8);
617 let w = UniqueRc::downgrade(&v);
618 let r = &mut *v;
619 let _ = w.clone(); // touch weak count
620 *r = 123;
621 }
622