]> git.lizzy.rs Git - rust.git/blob - src/liballoc/tests/arc.rs
Rollup merge of #61453 - lzutao:nouse-featuregate-integer_atomics, r=sfackler
[rust.git] / src / liballoc / tests / arc.rs
1 use std::any::Any;
2 use std::sync::{Arc, Weak};
3 use std::cell::RefCell;
4 use std::cmp::PartialEq;
5
6 #[test]
7 fn uninhabited() {
8     enum Void {}
9     let mut a = Weak::<Void>::new();
10     a = a.clone();
11     assert!(a.upgrade().is_none());
12
13     let mut a: Weak<dyn Any> = a;  // Unsizing
14     a = a.clone();
15     assert!(a.upgrade().is_none());
16 }
17
18 #[test]
19 fn slice() {
20     let a: Arc<[u32; 3]> = Arc::new([3, 2, 1]);
21     let a: Arc<[u32]> = a;  // Unsizing
22     let b: Arc<[u32]> = Arc::from(&[3, 2, 1][..]);  // Conversion
23     assert_eq!(a, b);
24
25     // Exercise is_dangling() with a DST
26     let mut a = Arc::downgrade(&a);
27     a = a.clone();
28     assert!(a.upgrade().is_some());
29 }
30
31 #[test]
32 fn trait_object() {
33     let a: Arc<u32> = Arc::new(4);
34     let a: Arc<dyn Any> = a;  // Unsizing
35
36     // Exercise is_dangling() with a DST
37     let mut a = Arc::downgrade(&a);
38     a = a.clone();
39     assert!(a.upgrade().is_some());
40
41     let mut b = Weak::<u32>::new();
42     b = b.clone();
43     assert!(b.upgrade().is_none());
44     let mut b: Weak<dyn Any> = b;  // Unsizing
45     b = b.clone();
46     assert!(b.upgrade().is_none());
47 }
48
49 #[test]
50 fn float_nan_ne() {
51     let x = Arc::new(std::f32::NAN);
52     assert!(x != x);
53     assert!(!(x == x));
54 }
55
56 #[test]
57 fn partial_eq() {
58     struct TestPEq (RefCell<usize>);
59     impl PartialEq for TestPEq {
60         fn eq(&self, other: &TestPEq) -> bool {
61             *self.0.borrow_mut() += 1;
62             *other.0.borrow_mut() += 1;
63             true
64         }
65     }
66     let x = Arc::new(TestPEq(RefCell::new(0)));
67     assert!(x == x);
68     assert!(!(x != x));
69     assert_eq!(*x.0.borrow(), 4);
70 }
71
72 #[test]
73 fn eq() {
74     #[derive(Eq)]
75     struct TestEq (RefCell<usize>);
76     impl PartialEq for TestEq {
77         fn eq(&self, other: &TestEq) -> bool {
78             *self.0.borrow_mut() += 1;
79             *other.0.borrow_mut() += 1;
80             true
81         }
82     }
83     let x = Arc::new(TestEq(RefCell::new(0)));
84     assert!(x == x);
85     assert!(!(x != x));
86     assert_eq!(*x.0.borrow(), 0);
87 }