]> git.lizzy.rs Git - rust.git/blob - library/core/tests/lazy.rs
update provenance test
[rust.git] / library / core / tests / lazy.rs
1 use core::{
2     cell::{Cell, LazyCell, OnceCell},
3     sync::atomic::{AtomicUsize, Ordering::SeqCst},
4 };
5
6 #[test]
7 fn once_cell() {
8     let c = OnceCell::new();
9     assert!(c.get().is_none());
10     c.get_or_init(|| 92);
11     assert_eq!(c.get(), Some(&92));
12
13     c.get_or_init(|| panic!("Kabom!"));
14     assert_eq!(c.get(), Some(&92));
15 }
16
17 #[test]
18 fn once_cell_get_mut() {
19     let mut c = OnceCell::new();
20     assert!(c.get_mut().is_none());
21     c.set(90).unwrap();
22     *c.get_mut().unwrap() += 2;
23     assert_eq!(c.get_mut(), Some(&mut 92));
24 }
25
26 #[test]
27 fn once_cell_drop() {
28     static DROP_CNT: AtomicUsize = AtomicUsize::new(0);
29     struct Dropper;
30     impl Drop for Dropper {
31         fn drop(&mut self) {
32             DROP_CNT.fetch_add(1, SeqCst);
33         }
34     }
35
36     let x = OnceCell::new();
37     x.get_or_init(|| Dropper);
38     assert_eq!(DROP_CNT.load(SeqCst), 0);
39     drop(x);
40     assert_eq!(DROP_CNT.load(SeqCst), 1);
41 }
42
43 #[test]
44 fn unsync_once_cell_drop_empty() {
45     let x = OnceCell::<&'static str>::new();
46     drop(x);
47 }
48
49 #[test]
50 const fn once_cell_const() {
51     let _once_cell: OnceCell<u32> = OnceCell::new();
52     let _once_cell: OnceCell<u32> = OnceCell::from(32);
53 }
54
55 #[test]
56 fn clone() {
57     let s = OnceCell::new();
58     let c = s.clone();
59     assert!(c.get().is_none());
60
61     s.set("hello").unwrap();
62     let c = s.clone();
63     assert_eq!(c.get().map(|c| *c), Some("hello"));
64 }
65
66 #[test]
67 fn from_impl() {
68     assert_eq!(OnceCell::from("value").get(), Some(&"value"));
69     assert_ne!(OnceCell::from("foo").get(), Some(&"bar"));
70 }
71
72 #[test]
73 fn partialeq_impl() {
74     assert!(OnceCell::from("value") == OnceCell::from("value"));
75     assert!(OnceCell::from("foo") != OnceCell::from("bar"));
76
77     assert!(OnceCell::<&'static str>::new() == OnceCell::new());
78     assert!(OnceCell::<&'static str>::new() != OnceCell::from("value"));
79 }
80
81 #[test]
82 fn into_inner() {
83     let cell: OnceCell<&'static str> = OnceCell::new();
84     assert_eq!(cell.into_inner(), None);
85     let cell = OnceCell::new();
86     cell.set("hello").unwrap();
87     assert_eq!(cell.into_inner(), Some("hello"));
88 }
89
90 #[test]
91 fn lazy_new() {
92     let called = Cell::new(0);
93     let x = LazyCell::new(|| {
94         called.set(called.get() + 1);
95         92
96     });
97
98     assert_eq!(called.get(), 0);
99
100     let y = *x - 30;
101     assert_eq!(y, 62);
102     assert_eq!(called.get(), 1);
103
104     let y = *x - 30;
105     assert_eq!(y, 62);
106     assert_eq!(called.get(), 1);
107 }
108
109 #[test]
110 fn aliasing_in_get() {
111     let x = OnceCell::new();
112     x.set(42).unwrap();
113     let at_x = x.get().unwrap(); // --- (shared) borrow of inner `Option<T>` --+
114     let _ = x.set(27); // <-- temporary (unique) borrow of inner `Option<T>`   |
115     println!("{at_x}"); // <------- up until here ---------------------------+
116 }
117
118 #[test]
119 #[should_panic(expected = "reentrant init")]
120 fn reentrant_init() {
121     let x: OnceCell<Box<i32>> = OnceCell::new();
122     let dangling_ref: Cell<Option<&i32>> = Cell::new(None);
123     x.get_or_init(|| {
124         let r = x.get_or_init(|| Box::new(92));
125         dangling_ref.set(Some(r));
126         Box::new(62)
127     });
128     eprintln!("use after free: {:?}", dangling_ref.get().unwrap());
129 }
130
131 #[test]
132 fn dropck() {
133     let cell = OnceCell::new();
134     {
135         let s = String::new();
136         cell.set(&s).unwrap();
137     }
138 }