]> git.lizzy.rs Git - rust.git/blob - src/libcoretest/cell.rs
Add a doctest for the std::string::as_string method.
[rust.git] / src / libcoretest / 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(10i);
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((30i, 40i));
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).as_slice().contains(x.get()));
33
34     x.set("baz qux");
35     assert!(format!("{}", x).as_slice().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).as_slice().contains("foo"));
44     drop(refcell_refmut);
45
46     let refcell_ref = refcell.borrow();
47     assert!(format!("{}", refcell_ref).as_slice().contains("foo"));
48     drop(refcell_ref);
49 }
50
51 #[test]
52 fn double_imm_borrow() {
53     let x = RefCell::new(0i);
54     let _b1 = x.borrow();
55     x.borrow();
56 }
57
58 #[test]
59 fn no_mut_then_imm_borrow() {
60     let x = RefCell::new(0i);
61     let _b1 = x.borrow_mut();
62     assert!(x.try_borrow().is_none());
63 }
64
65 #[test]
66 fn no_imm_then_borrow_mut() {
67     let x = RefCell::new(0i);
68     let _b1 = x.borrow();
69     assert!(x.try_borrow_mut().is_none());
70 }
71
72 #[test]
73 fn no_double_borrow_mut() {
74     let x = RefCell::new(0i);
75     let _b1 = x.borrow_mut();
76     assert!(x.try_borrow_mut().is_none());
77 }
78
79 #[test]
80 fn imm_release_borrow_mut() {
81     let x = RefCell::new(0i);
82     {
83         let _b1 = x.borrow();
84     }
85     x.borrow_mut();
86 }
87
88 #[test]
89 fn mut_release_borrow_mut() {
90     let x = RefCell::new(0i);
91     {
92         let _b1 = x.borrow_mut();
93     }
94     x.borrow();
95 }
96
97 #[test]
98 fn double_borrow_single_release_no_borrow_mut() {
99     let x = RefCell::new(0i);
100     let _b1 = x.borrow();
101     {
102         let _b2 = x.borrow();
103     }
104     assert!(x.try_borrow_mut().is_none());
105 }
106
107 #[test]
108 #[should_fail]
109 fn discard_doesnt_unborrow() {
110     let x = RefCell::new(0i);
111     let _b = x.borrow();
112     let _ = _b;
113     let _b = x.borrow_mut();
114 }
115
116 #[test]
117 #[allow(experimental)]
118 fn clone_ref_updates_flag() {
119     let x = RefCell::new(0i);
120     {
121         let b1 = x.borrow();
122         assert!(x.try_borrow_mut().is_none());
123         {
124             let _b2 = clone_ref(&b1);
125             assert!(x.try_borrow_mut().is_none());
126         }
127         assert!(x.try_borrow_mut().is_none());
128     }
129     assert!(x.try_borrow_mut().is_some());
130 }
131
132 #[test]
133 fn as_unsafe_cell() {
134     let c1: Cell<uint> = Cell::new(0u);
135     c1.set(1u);
136     assert_eq!(1u, unsafe { *c1.as_unsafe_cell().get() });
137
138     let c2: Cell<uint> = Cell::new(0u);
139     unsafe { *c2.as_unsafe_cell().get() = 1u; }
140     assert_eq!(1u, c2.get());
141
142     let r1: RefCell<uint> = RefCell::new(0u);
143     *r1.borrow_mut() = 1u;
144     assert_eq!(1u, unsafe { *r1.as_unsafe_cell().get() });
145
146     let r2: RefCell<uint> = RefCell::new(0u);
147     unsafe { *r2.as_unsafe_cell().get() = 1u; }
148     assert_eq!(1u, *r2.borrow());
149 }
150
151 #[test]
152 fn cell_default() {
153     let cell: Cell<u32> = Default::default();
154     assert_eq!(0, cell.get());
155 }
156
157 #[test]
158 fn refcell_default() {
159     let cell: RefCell<u64> = Default::default();
160     assert_eq!(0, *cell.borrow());
161 }