Skip to content

Commit 96d1f27

Browse files
committed
Add some unit tests for dangling Weak references
1 parent 391c174 commit 96d1f27

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

src/liballoc/tests/arc.rs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::any::Any;
12+
use std::sync::{Arc, Weak};
13+
14+
#[test]
15+
fn uninhabited() {
16+
enum Void {}
17+
let mut a = Weak::<Void>::new();
18+
a = a.clone();
19+
assert!(a.upgrade().is_none());
20+
21+
let mut a: Weak<Any> = a; // Unsizing
22+
a = a.clone();
23+
assert!(a.upgrade().is_none());
24+
}
25+
26+
#[test]
27+
fn slice() {
28+
let a: Arc<[u32; 3]> = Arc::new([3, 2, 1]);
29+
let a: Arc<[u32]> = a; // Unsizing
30+
let b: Arc<[u32]> = Arc::from(&[3, 2, 1][..]); // Conversion
31+
assert_eq!(a, b);
32+
33+
// Exercise is_dangling() with a DST
34+
let mut a = Arc::downgrade(&a);
35+
a = a.clone();
36+
assert!(a.upgrade().is_some());
37+
}
38+
39+
#[test]
40+
fn trait_object() {
41+
let a: Arc<u32> = Arc::new(4);
42+
let a: Arc<Any> = a; // Unsizing
43+
44+
// Exercise is_dangling() with a DST
45+
let mut a = Arc::downgrade(&a);
46+
a = a.clone();
47+
assert!(a.upgrade().is_some());
48+
49+
let mut b = Weak::<u32>::new();
50+
b = b.clone();
51+
assert!(b.upgrade().is_none());
52+
let mut b: Weak<Any> = b; // Unsizing
53+
b = b.clone();
54+
assert!(b.upgrade().is_none());
55+
}

src/liballoc/tests/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ extern crate rand;
3232
use std::hash::{Hash, Hasher};
3333
use std::collections::hash_map::DefaultHasher;
3434

35+
mod arc;
3536
mod binary_heap;
3637
mod btree;
3738
mod cow_str;
3839
mod fmt;
3940
mod heap;
4041
mod linked_list;
42+
mod rc;
4143
mod slice;
4244
mod str;
4345
mod string;

src/liballoc/tests/rc.rs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::any::Any;
12+
use std::rc::{Rc, Weak};
13+
14+
#[test]
15+
fn uninhabited() {
16+
enum Void {}
17+
let mut a = Weak::<Void>::new();
18+
a = a.clone();
19+
assert!(a.upgrade().is_none());
20+
21+
let mut a: Weak<Any> = a; // Unsizing
22+
a = a.clone();
23+
assert!(a.upgrade().is_none());
24+
}
25+
26+
#[test]
27+
fn slice() {
28+
let a: Rc<[u32; 3]> = Rc::new([3, 2, 1]);
29+
let a: Rc<[u32]> = a; // Unsizing
30+
let b: Rc<[u32]> = Rc::from(&[3, 2, 1][..]); // Conversion
31+
assert_eq!(a, b);
32+
33+
// Exercise is_dangling() with a DST
34+
let mut a = Rc::downgrade(&a);
35+
a = a.clone();
36+
assert!(a.upgrade().is_some());
37+
}
38+
39+
#[test]
40+
fn trait_object() {
41+
let a: Rc<u32> = Rc::new(4);
42+
let a: Rc<Any> = a; // Unsizing
43+
44+
// Exercise is_dangling() with a DST
45+
let mut a = Rc::downgrade(&a);
46+
a = a.clone();
47+
assert!(a.upgrade().is_some());
48+
49+
let mut b = Weak::<u32>::new();
50+
b = b.clone();
51+
assert!(b.upgrade().is_none());
52+
let mut b: Weak<Any> = b; // Unsizing
53+
b = b.clone();
54+
assert!(b.upgrade().is_none());
55+
}

0 commit comments

Comments
 (0)