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