@@ -434,3 +434,69 @@ fn test_array_from_slice() {
434
434
let a: Result < Rc < [ u32 ; 2 ] > , _ > = r. clone ( ) . try_into ( ) ;
435
435
assert ! ( a. is_err( ) ) ;
436
436
}
437
+
438
+ #[ test]
439
+ fn test_rc_cyclic_with_zero_refs ( ) {
440
+ struct ZeroRefs {
441
+ inner : Weak < ZeroRefs > ,
442
+ }
443
+
444
+ let zero_refs = Rc :: new_cyclic ( |inner| {
445
+ assert_eq ! ( inner. strong_count( ) , 0 ) ;
446
+ assert ! ( inner. upgrade( ) . is_none( ) ) ;
447
+ ZeroRefs { inner : Weak :: new ( ) }
448
+ } ) ;
449
+
450
+ assert_eq ! ( Rc :: strong_count( & zero_refs) , 1 ) ;
451
+ assert_eq ! ( Rc :: weak_count( & zero_refs) , 0 ) ;
452
+ assert_eq ! ( zero_refs. inner. strong_count( ) , 0 ) ;
453
+ assert_eq ! ( zero_refs. inner. weak_count( ) , 0 ) ;
454
+ }
455
+
456
+ #[ test]
457
+ fn test_rc_cyclic_with_one_ref ( ) {
458
+ struct OneRef {
459
+ inner : Weak < OneRef > ,
460
+ }
461
+
462
+ let one_ref = Rc :: new_cyclic ( |inner| {
463
+ assert_eq ! ( inner. strong_count( ) , 0 ) ;
464
+ assert ! ( inner. upgrade( ) . is_none( ) ) ;
465
+ OneRef { inner : inner. clone ( ) }
466
+ } ) ;
467
+
468
+ assert_eq ! ( Rc :: strong_count( & one_ref) , 1 ) ;
469
+ assert_eq ! ( Rc :: weak_count( & one_ref) , 1 ) ;
470
+
471
+ let one_ref2 = Weak :: upgrade ( & one_ref. inner ) . unwrap ( ) ;
472
+ assert ! ( Rc :: ptr_eq( & one_ref, & one_ref2) ) ;
473
+
474
+ assert_eq ! ( one_ref. inner. strong_count( ) , 2 ) ;
475
+ assert_eq ! ( one_ref. inner. weak_count( ) , 1 ) ;
476
+ }
477
+
478
+ #[ test]
479
+ fn test_rc_cyclic_with_two_ref ( ) {
480
+ struct TwoRefs {
481
+ inner : Weak < TwoRefs > ,
482
+ inner1 : Weak < TwoRefs > ,
483
+ }
484
+
485
+ let two_refs = Rc :: new_cyclic ( |inner| {
486
+ assert_eq ! ( inner. strong_count( ) , 0 ) ;
487
+ assert ! ( inner. upgrade( ) . is_none( ) ) ;
488
+ TwoRefs { inner : inner. clone ( ) , inner1 : inner. clone ( ) }
489
+ } ) ;
490
+
491
+ assert_eq ! ( Rc :: strong_count( & two_refs) , 1 ) ;
492
+ assert_eq ! ( Rc :: weak_count( & two_refs) , 2 ) ;
493
+
494
+ let two_ref3 = Weak :: upgrade ( & two_refs. inner ) . unwrap ( ) ;
495
+ assert ! ( Rc :: ptr_eq( & two_refs, & two_ref3) ) ;
496
+
497
+ let two_ref2 = Weak :: upgrade ( & two_refs. inner1 ) . unwrap ( ) ;
498
+ assert ! ( Rc :: ptr_eq( & two_refs, & two_ref2) ) ;
499
+
500
+ assert_eq ! ( Rc :: strong_count( & two_refs) , 3 ) ;
501
+ assert_eq ! ( Rc :: weak_count( & two_refs) , 2 ) ;
502
+ }
0 commit comments