]> git.lizzy.rs Git - rust.git/blob - library/core/tests/lazy.rs
Add a dedicated length-prefixing method to `Hasher`
[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 const fn once_cell_const() {
52     let _once_cell: OnceCell<u32> = OnceCell::new();
53     let _once_cell: OnceCell<u32> = OnceCell::from(32);
54 }
55
56 #[test]
57 fn clone() {
58     let s = OnceCell::new();
59     let c = s.clone();
60     assert!(c.get().is_none());
61
62     s.set("hello").unwrap();
63     let c = s.clone();
64     assert_eq!(c.get().map(|c| *c), Some("hello"));
65 }
66
67 #[test]
68 fn from_impl() {
69     assert_eq!(OnceCell::from("value").get(), Some(&"value"));
70     assert_ne!(OnceCell::from("foo").get(), Some(&"bar"));
71 }
72
73 #[test]
74 fn partialeq_impl() {
75     assert!(OnceCell::from("value") == OnceCell::from("value"));
76     assert!(OnceCell::from("foo") != OnceCell::from("bar"));
77
78     assert!(OnceCell::<&'static str>::new() == OnceCell::new());
79     assert!(OnceCell::<&'static str>::new() != OnceCell::from("value"));
80 }
81
82 #[test]
83 fn into_inner() {
84     let cell: OnceCell<&'static str> = OnceCell::new();
85     assert_eq!(cell.into_inner(), None);
86     let cell = OnceCell::new();
87     cell.set("hello").unwrap();
88     assert_eq!(cell.into_inner(), Some("hello"));
89 }
90
91 #[test]
92 fn lazy_new() {
93     let called = Cell::new(0);
94     let x = Lazy::new(|| {
95         called.set(called.get() + 1);
96         92
97     });
98
99     assert_eq!(called.get(), 0);
100
101     let y = *x - 30;
102     assert_eq!(y, 62);
103     assert_eq!(called.get(), 1);
104
105     let y = *x - 30;
106     assert_eq!(y, 62);
107     assert_eq!(called.get(), 1);
108 }
109
110 #[test]
111 fn aliasing_in_get() {
112     let x = OnceCell::new();
113     x.set(42).unwrap();
114     let at_x = x.get().unwrap(); // --- (shared) borrow of inner `Option<T>` --+
115     let _ = x.set(27); // <-- temporary (unique) borrow of inner `Option<T>`   |
116     println!("{at_x}"); // <------- up until here ---------------------------+
117 }
118
119 #[test]
120 #[should_panic(expected = "reentrant init")]
121 fn reentrant_init() {
122     let x: OnceCell<Box<i32>> = OnceCell::new();
123     let dangling_ref: Cell<Option<&i32>> = Cell::new(None);
124     x.get_or_init(|| {
125         let r = x.get_or_init(|| Box::new(92));
126         dangling_ref.set(Some(r));
127         Box::new(62)
128     });
129     eprintln!("use after free: {:?}", dangling_ref.get().unwrap());
130 }
131
132 #[test]
133 fn dropck() {
134     let cell = OnceCell::new();
135     {
136         let s = String::new();
137         cell.set(&s).unwrap();
138     }
139 }