]> git.lizzy.rs Git - rust.git/blob - src/libcore/tests/cell.rs
Auto merge of #42612 - est31:master, r=nagisa
[rust.git] / src / libcore / tests / cell.rs
1 // Copyright 2014 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 core::cell::*;
12 use core::default::Default;
13 use std::mem::drop;
14
15 #[test]
16 fn smoketest_cell() {
17     let x = Cell::new(10);
18     assert!(x == Cell::new(10));
19     assert!(x.get() == 10);
20     x.set(20);
21     assert!(x == Cell::new(20));
22     assert!(x.get() == 20);
23
24     let y = Cell::new((30, 40));
25     assert!(y == Cell::new((30, 40)));
26     assert!(y.get() == (30, 40));
27 }
28
29 #[test]
30 fn cell_has_sensible_show() {
31     let x = Cell::new("foo bar");
32     assert!(format!("{:?}", x).contains(x.get()));
33
34     x.set("baz qux");
35     assert!(format!("{:?}", x).contains(x.get()));
36 }
37
38 #[test]
39 fn ref_and_refmut_have_sensible_show() {
40     let refcell = RefCell::new("foo");
41
42     let refcell_refmut = refcell.borrow_mut();
43     assert!(format!("{:?}", refcell_refmut).contains("foo"));
44     drop(refcell_refmut);
45
46     let refcell_ref = refcell.borrow();
47     assert!(format!("{:?}", refcell_ref).contains("foo"));
48     drop(refcell_ref);
49 }
50
51 #[test]
52 fn double_imm_borrow() {
53     let x = RefCell::new(0);
54     let _b1 = x.borrow();
55     x.borrow();
56 }
57
58 #[test]
59 fn no_mut_then_imm_borrow() {
60     let x = RefCell::new(0);
61     let _b1 = x.borrow_mut();
62     assert!(x.try_borrow().is_err());
63 }
64
65 #[test]
66 fn no_imm_then_borrow_mut() {
67     let x = RefCell::new(0);
68     let _b1 = x.borrow();
69     assert!(x.try_borrow_mut().is_err());
70 }
71
72 #[test]
73 fn no_double_borrow_mut() {
74     let x = RefCell::new(0);
75     assert!(x.try_borrow().is_ok());
76     let _b1 = x.borrow_mut();
77     assert!(x.try_borrow().is_err());
78 }
79
80 #[test]
81 fn imm_release_borrow_mut() {
82     let x = RefCell::new(0);
83     {
84         let _b1 = x.borrow();
85     }
86     x.borrow_mut();
87 }
88
89 #[test]
90 fn mut_release_borrow_mut() {
91     let x = RefCell::new(0);
92     {
93         let _b1 = x.borrow_mut();
94     }
95     x.borrow();
96 }
97
98 #[test]
99 fn double_borrow_single_release_no_borrow_mut() {
100     let x = RefCell::new(0);
101     let _b1 = x.borrow();
102     {
103         let _b2 = x.borrow();
104     }
105     assert!(x.try_borrow().is_ok());
106     assert!(x.try_borrow_mut().is_err());
107 }
108
109 #[test]
110 #[should_panic]
111 fn discard_doesnt_unborrow() {
112     let x = RefCell::new(0);
113     let _b = x.borrow();
114     let _ = _b;
115     let _b = x.borrow_mut();
116 }
117
118 #[test]
119 fn ref_clone_updates_flag() {
120     let x = RefCell::new(0);
121     {
122         let b1 = x.borrow();
123         assert!(x.try_borrow().is_ok());
124         assert!(x.try_borrow_mut().is_err());
125         {
126             let _b2 = Ref::clone(&b1);
127             assert!(x.try_borrow().is_ok());
128             assert!(x.try_borrow_mut().is_err());
129         }
130         assert!(x.try_borrow().is_ok());
131         assert!(x.try_borrow_mut().is_err());
132     }
133     assert!(x.try_borrow().is_ok());
134     assert!(x.try_borrow_mut().is_ok());
135 }
136
137 #[test]
138 fn ref_map_does_not_update_flag() {
139     let x = RefCell::new(Some(5));
140     {
141         let b1: Ref<Option<u32>> = x.borrow();
142         assert!(x.try_borrow().is_ok());
143         assert!(x.try_borrow_mut().is_err());
144         {
145             let b2: Ref<u32> = Ref::map(b1, |o| o.as_ref().unwrap());
146             assert_eq!(*b2, 5);
147             assert!(x.try_borrow().is_ok());
148             assert!(x.try_borrow_mut().is_err());
149         }
150         assert!(x.try_borrow().is_ok());
151         assert!(x.try_borrow_mut().is_ok());
152     }
153     assert!(x.try_borrow().is_ok());
154     assert!(x.try_borrow_mut().is_ok());
155 }
156
157 #[test]
158 fn ref_map_accessor() {
159     struct X(RefCell<(u32, char)>);
160     impl X {
161         fn accessor(&self) -> Ref<u32> {
162             Ref::map(self.0.borrow(), |tuple| &tuple.0)
163         }
164     }
165     let x = X(RefCell::new((7, 'z')));
166     let d: Ref<u32> = x.accessor();
167     assert_eq!(*d, 7);
168 }
169
170 #[test]
171 fn ref_mut_map_accessor() {
172     struct X(RefCell<(u32, char)>);
173     impl X {
174         fn accessor(&self) -> RefMut<u32> {
175             RefMut::map(self.0.borrow_mut(), |tuple| &mut tuple.0)
176         }
177     }
178     let x = X(RefCell::new((7, 'z')));
179     {
180         let mut d: RefMut<u32> = x.accessor();
181         assert_eq!(*d, 7);
182         *d += 1;
183     }
184     assert_eq!(*x.0.borrow(), (8, 'z'));
185 }
186
187 #[test]
188 fn as_ptr() {
189     let c1: Cell<usize> = Cell::new(0);
190     c1.set(1);
191     assert_eq!(1, unsafe { *c1.as_ptr() });
192
193     let c2: Cell<usize> = Cell::new(0);
194     unsafe { *c2.as_ptr() = 1; }
195     assert_eq!(1, c2.get());
196
197     let r1: RefCell<usize> = RefCell::new(0);
198     *r1.borrow_mut() = 1;
199     assert_eq!(1, unsafe { *r1.as_ptr() });
200
201     let r2: RefCell<usize> = RefCell::new(0);
202     unsafe { *r2.as_ptr() = 1; }
203     assert_eq!(1, *r2.borrow());
204 }
205
206 #[test]
207 fn cell_default() {
208     let cell: Cell<u32> = Default::default();
209     assert_eq!(0, cell.get());
210 }
211
212 #[test]
213 fn cell_set() {
214     let cell = Cell::new(10);
215     cell.set(20);
216     assert_eq!(20, cell.get());
217
218     let cell = Cell::new("Hello".to_owned());
219     cell.set("World".to_owned());
220     assert_eq!("World".to_owned(), cell.into_inner());
221 }
222
223 #[test]
224 fn cell_replace() {
225     let cell = Cell::new(10);
226     assert_eq!(10, cell.replace(20));
227     assert_eq!(20, cell.get());
228
229     let cell = Cell::new("Hello".to_owned());
230     assert_eq!("Hello".to_owned(), cell.replace("World".to_owned()));
231     assert_eq!("World".to_owned(), cell.into_inner());
232 }
233
234 #[test]
235 fn cell_into_inner() {
236     let cell = Cell::new(10);
237     assert_eq!(10, cell.into_inner());
238
239     let cell = Cell::new("Hello world".to_owned());
240     assert_eq!("Hello world".to_owned(), cell.into_inner());
241 }
242
243 #[test]
244 fn refcell_default() {
245     let cell: RefCell<u64> = Default::default();
246     assert_eq!(0, *cell.borrow());
247 }
248
249 #[test]
250 fn unsafe_cell_unsized() {
251     let cell: &UnsafeCell<[i32]> = &UnsafeCell::new([1, 2, 3]);
252     {
253         let val: &mut [i32] = unsafe { &mut *cell.get() };
254         val[0] = 4;
255         val[2] = 5;
256     }
257     let comp: &mut [i32] = &mut [4, 2, 5];
258     assert_eq!(unsafe { &mut *cell.get() }, comp);
259 }
260
261 #[test]
262 fn refcell_unsized() {
263     let cell: &RefCell<[i32]> = &RefCell::new([1, 2, 3]);
264     {
265         let b = &mut *cell.borrow_mut();
266         b[0] = 4;
267         b[2] = 5;
268     }
269     let comp: &mut [i32] = &mut [4, 2, 5];
270     assert_eq!(&*cell.borrow(), comp);
271 }
272
273 #[test]
274 fn refcell_ref_coercion() {
275     let cell: RefCell<[i32; 3]> = RefCell::new([1, 2, 3]);
276     {
277         let mut cellref: RefMut<[i32; 3]> = cell.borrow_mut();
278         cellref[0] = 4;
279         let mut coerced: RefMut<[i32]> = cellref;
280         coerced[2] = 5;
281     }
282     {
283         let comp: &mut [i32] = &mut [4, 2, 5];
284         let cellref: Ref<[i32; 3]> = cell.borrow();
285         assert_eq!(&*cellref, comp);
286         let coerced: Ref<[i32]> = cellref;
287         assert_eq!(&*coerced, comp);
288     }
289 }