File tree Expand file tree Collapse file tree 1 file changed +32
-4
lines changed Expand file tree Collapse file tree 1 file changed +32
-4
lines changed Original file line number Diff line number Diff line change @@ -18,17 +18,18 @@ fn empty_cell<T>() -> Cell<T> {
18
18
impl < T > Cell < T > {
19
19
/// Yields the value, failing if the cell is empty.
20
20
fn take ( ) -> T {
21
- let mut value = none;
22
- value <-> self . value ;
23
- if value. is_none ( ) {
21
+ if self . is_empty ( ) {
24
22
fail ~"attempt to take an empty cell";
25
23
}
24
+
25
+ let mut value = none;
26
+ value <-> self . value ;
26
27
return option:: unwrap ( value) ;
27
28
}
28
29
29
30
/// Returns the value, failing if the cell is full.
30
31
fn put_back ( +value : T ) {
31
- if self . value . is_none ( ) {
32
+ if ! self . is_empty ( ) {
32
33
fail ~"attempt to put a value back into a full cell";
33
34
}
34
35
self . value = some ( move value) ;
@@ -39,3 +40,30 @@ impl<T> Cell<T> {
39
40
self . value . is_none ( )
40
41
}
41
42
}
43
+
44
+ #[ test]
45
+ fn test_basic ( ) {
46
+ let value_cell = Cell ( ~10 ) ;
47
+ assert !value_cell. is_empty ( ) ;
48
+ let value = value_cell. take ( ) ;
49
+ assert value == ~10 ;
50
+ assert value_cell. is_empty ( ) ;
51
+ value_cell. put_back ( value) ;
52
+ assert !value_cell. is_empty ( ) ;
53
+ }
54
+
55
+ #[ test]
56
+ #[ should_fail]
57
+ #[ ignore( cfg( windows) ) ]
58
+ fn test_take_empty ( ) {
59
+ let value_cell = empty_cell :: < ~int > ( ) ;
60
+ value_cell. take ( ) ;
61
+ }
62
+
63
+ #[ test]
64
+ #[ should_fail]
65
+ #[ ignore( cfg( windows) ) ]
66
+ fn test_put_back_non_empty ( ) {
67
+ let value_cell = Cell ( ~10 ) ;
68
+ value_cell. put_back ( ~20 ) ;
69
+ }
You can’t perform that action at this time.
0 commit comments