Skip to content

Commit ac4132b

Browse files
committed
std: Add tests for cell. Fix a logic error
1 parent 78d19d8 commit ac4132b

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

src/libstd/cell.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@ fn empty_cell<T>() -> Cell<T> {
1818
impl<T> Cell<T> {
1919
/// Yields the value, failing if the cell is empty.
2020
fn take() -> T {
21-
let mut value = none;
22-
value <-> self.value;
23-
if value.is_none() {
21+
if self.is_empty() {
2422
fail ~"attempt to take an empty cell";
2523
}
24+
25+
let mut value = none;
26+
value <-> self.value;
2627
return option::unwrap(value);
2728
}
2829

2930
/// Returns the value, failing if the cell is full.
3031
fn put_back(+value: T) {
31-
if self.value.is_none() {
32+
if !self.is_empty() {
3233
fail ~"attempt to put a value back into a full cell";
3334
}
3435
self.value = some(move value);
@@ -39,3 +40,30 @@ impl<T> Cell<T> {
3940
self.value.is_none()
4041
}
4142
}
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+
}

0 commit comments

Comments
 (0)