diff options
| author | Brian Anderson <banderson@mozilla.com> | 2012-08-12 16:36:07 -0700 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2012-08-12 16:36:07 -0700 |
| commit | ac4132b7fda1e2bd356a5af8d89badf3e05f7a1a (patch) | |
| tree | b9f5f06695bc64badd558f48acebd96eb83d389c /src/libstd | |
| parent | 78d19d8f1bdb64317ebdb072c25ee1240a562095 (diff) | |
| download | rust-ac4132b7fda1e2bd356a5af8d89badf3e05f7a1a.tar.gz rust-ac4132b7fda1e2bd356a5af8d89badf3e05f7a1a.zip | |
std: Add tests for cell. Fix a logic error
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/cell.rs | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/src/libstd/cell.rs b/src/libstd/cell.rs index 734eb0af8fe..02cae69eca5 100644 --- a/src/libstd/cell.rs +++ b/src/libstd/cell.rs @@ -18,17 +18,18 @@ fn empty_cell<T>() -> Cell<T> { impl<T> Cell<T> { /// Yields the value, failing if the cell is empty. fn take() -> T { - let mut value = none; - value <-> self.value; - if value.is_none() { + if self.is_empty() { fail ~"attempt to take an empty cell"; } + + let mut value = none; + value <-> self.value; return option::unwrap(value); } /// Returns the value, failing if the cell is full. fn put_back(+value: T) { - if self.value.is_none() { + if !self.is_empty() { fail ~"attempt to put a value back into a full cell"; } self.value = some(move value); @@ -39,3 +40,30 @@ impl<T> Cell<T> { self.value.is_none() } } + +#[test] +fn test_basic() { + let value_cell = Cell(~10); + assert !value_cell.is_empty(); + let value = value_cell.take(); + assert value == ~10; + assert value_cell.is_empty(); + value_cell.put_back(value); + assert !value_cell.is_empty(); +} + +#[test] +#[should_fail] +#[ignore(cfg(windows))] +fn test_take_empty() { + let value_cell = empty_cell::<~int>(); + value_cell.take(); +} + +#[test] +#[should_fail] +#[ignore(cfg(windows))] +fn test_put_back_non_empty() { + let value_cell = Cell(~10); + value_cell.put_back(~20); +} \ No newline at end of file |
