2014-06-28 13:57:36 -07:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use core::cell::*;
|
2014-11-14 22:22:42 +03:00
|
|
|
use core::default::Default;
|
2014-06-28 13:57:36 -07:00
|
|
|
use std::mem::drop;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn smoketest_cell() {
|
2015-01-25 22:05:03 +01:00
|
|
|
let x = Cell::new(10);
|
2014-06-28 13:57:36 -07:00
|
|
|
assert!(x == Cell::new(10));
|
|
|
|
assert!(x.get() == 10);
|
|
|
|
x.set(20);
|
|
|
|
assert!(x == Cell::new(20));
|
|
|
|
assert!(x.get() == 20);
|
|
|
|
|
2015-01-25 22:05:03 +01:00
|
|
|
let y = Cell::new((30, 40));
|
2014-06-28 13:57:36 -07:00
|
|
|
assert!(y == Cell::new((30, 40)));
|
|
|
|
assert!(y.get() == (30, 40));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cell_has_sensible_show() {
|
|
|
|
let x = Cell::new("foo bar");
|
2014-12-20 00:09:35 -08:00
|
|
|
assert!(format!("{:?}", x).contains(x.get()));
|
2014-06-28 13:57:36 -07:00
|
|
|
|
|
|
|
x.set("baz qux");
|
2014-12-20 00:09:35 -08:00
|
|
|
assert!(format!("{:?}", x).contains(x.get()));
|
2014-06-28 13:57:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ref_and_refmut_have_sensible_show() {
|
|
|
|
let refcell = RefCell::new("foo");
|
|
|
|
|
|
|
|
let refcell_refmut = refcell.borrow_mut();
|
2014-12-20 00:09:35 -08:00
|
|
|
assert!(format!("{:?}", refcell_refmut).contains("foo"));
|
2014-06-28 13:57:36 -07:00
|
|
|
drop(refcell_refmut);
|
|
|
|
|
|
|
|
let refcell_ref = refcell.borrow();
|
2014-12-20 00:09:35 -08:00
|
|
|
assert!(format!("{:?}", refcell_ref).contains("foo"));
|
2014-06-28 13:57:36 -07:00
|
|
|
drop(refcell_ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn double_imm_borrow() {
|
2015-01-25 22:05:03 +01:00
|
|
|
let x = RefCell::new(0);
|
2014-06-28 13:57:36 -07:00
|
|
|
let _b1 = x.borrow();
|
|
|
|
x.borrow();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_mut_then_imm_borrow() {
|
2015-01-25 22:05:03 +01:00
|
|
|
let x = RefCell::new(0);
|
2014-06-28 13:57:36 -07:00
|
|
|
let _b1 = x.borrow_mut();
|
2015-02-01 18:53:47 -08:00
|
|
|
assert_eq!(x.borrow_state(), BorrowState::Writing);
|
2014-06-28 13:57:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_imm_then_borrow_mut() {
|
2015-01-25 22:05:03 +01:00
|
|
|
let x = RefCell::new(0);
|
2014-06-28 13:57:36 -07:00
|
|
|
let _b1 = x.borrow();
|
2015-02-01 18:53:47 -08:00
|
|
|
assert_eq!(x.borrow_state(), BorrowState::Reading);
|
2014-06-28 13:57:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_double_borrow_mut() {
|
2015-01-25 22:05:03 +01:00
|
|
|
let x = RefCell::new(0);
|
2015-02-01 18:53:47 -08:00
|
|
|
assert_eq!(x.borrow_state(), BorrowState::Unused);
|
2014-06-28 13:57:36 -07:00
|
|
|
let _b1 = x.borrow_mut();
|
2015-02-01 18:53:47 -08:00
|
|
|
assert_eq!(x.borrow_state(), BorrowState::Writing);
|
2014-06-28 13:57:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn imm_release_borrow_mut() {
|
2015-01-25 22:05:03 +01:00
|
|
|
let x = RefCell::new(0);
|
2014-06-28 13:57:36 -07:00
|
|
|
{
|
|
|
|
let _b1 = x.borrow();
|
|
|
|
}
|
|
|
|
x.borrow_mut();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mut_release_borrow_mut() {
|
2015-01-25 22:05:03 +01:00
|
|
|
let x = RefCell::new(0);
|
2014-06-28 13:57:36 -07:00
|
|
|
{
|
|
|
|
let _b1 = x.borrow_mut();
|
|
|
|
}
|
|
|
|
x.borrow();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn double_borrow_single_release_no_borrow_mut() {
|
2015-01-25 22:05:03 +01:00
|
|
|
let x = RefCell::new(0);
|
2014-06-28 13:57:36 -07:00
|
|
|
let _b1 = x.borrow();
|
|
|
|
{
|
|
|
|
let _b2 = x.borrow();
|
|
|
|
}
|
2015-03-30 11:00:05 -07:00
|
|
|
assert_eq!(x.borrow_state(), BorrowState::Reading);
|
2014-06-28 13:57:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-01-31 15:08:25 -08:00
|
|
|
#[should_panic]
|
2014-06-28 13:57:36 -07:00
|
|
|
fn discard_doesnt_unborrow() {
|
2015-01-25 22:05:03 +01:00
|
|
|
let x = RefCell::new(0);
|
2014-06-28 13:57:36 -07:00
|
|
|
let _b = x.borrow();
|
|
|
|
let _ = _b;
|
|
|
|
let _b = x.borrow_mut();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn clone_ref_updates_flag() {
|
2015-01-25 22:05:03 +01:00
|
|
|
let x = RefCell::new(0);
|
2014-06-28 13:57:36 -07:00
|
|
|
{
|
|
|
|
let b1 = x.borrow();
|
2015-03-30 11:00:05 -07:00
|
|
|
assert_eq!(x.borrow_state(), BorrowState::Reading);
|
2014-06-28 13:57:36 -07:00
|
|
|
{
|
|
|
|
let _b2 = clone_ref(&b1);
|
2015-03-30 11:00:05 -07:00
|
|
|
assert_eq!(x.borrow_state(), BorrowState::Reading);
|
2014-06-28 13:57:36 -07:00
|
|
|
}
|
2015-03-30 11:00:05 -07:00
|
|
|
assert_eq!(x.borrow_state(), BorrowState::Reading);
|
2014-06-28 13:57:36 -07:00
|
|
|
}
|
2015-03-30 11:00:05 -07:00
|
|
|
assert_eq!(x.borrow_state(), BorrowState::Unused);
|
2014-06-28 13:57:36 -07:00
|
|
|
}
|
2014-10-21 12:59:21 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn as_unsafe_cell() {
|
2015-03-25 17:06:52 -07:00
|
|
|
let c1: Cell<usize> = Cell::new(0);
|
2015-02-03 13:50:52 +00:00
|
|
|
c1.set(1);
|
|
|
|
assert_eq!(1, unsafe { *c1.as_unsafe_cell().get() });
|
2014-10-21 12:59:21 -07:00
|
|
|
|
2015-03-25 17:06:52 -07:00
|
|
|
let c2: Cell<usize> = Cell::new(0);
|
2015-02-03 13:50:52 +00:00
|
|
|
unsafe { *c2.as_unsafe_cell().get() = 1; }
|
|
|
|
assert_eq!(1, c2.get());
|
2014-10-21 12:59:21 -07:00
|
|
|
|
2015-03-25 17:06:52 -07:00
|
|
|
let r1: RefCell<usize> = RefCell::new(0);
|
2015-02-03 13:50:52 +00:00
|
|
|
*r1.borrow_mut() = 1;
|
|
|
|
assert_eq!(1, unsafe { *r1.as_unsafe_cell().get() });
|
2014-10-21 12:59:21 -07:00
|
|
|
|
2015-03-25 17:06:52 -07:00
|
|
|
let r2: RefCell<usize> = RefCell::new(0);
|
2015-02-03 13:50:52 +00:00
|
|
|
unsafe { *r2.as_unsafe_cell().get() = 1; }
|
|
|
|
assert_eq!(1, *r2.borrow());
|
2014-10-21 12:59:21 -07:00
|
|
|
}
|
2014-11-14 22:22:42 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cell_default() {
|
|
|
|
let cell: Cell<u32> = Default::default();
|
|
|
|
assert_eq!(0, cell.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn refcell_default() {
|
|
|
|
let cell: RefCell<u64> = Default::default();
|
|
|
|
assert_eq!(0, *cell.borrow());
|
|
|
|
}
|
2015-04-23 22:53:54 +12:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unsafe_cell_unsized() {
|
|
|
|
let cell: &UnsafeCell<[i32]> = &UnsafeCell::new([1, 2, 3]);
|
|
|
|
{
|
|
|
|
let val: &mut [i32] = unsafe { &mut *cell.get() };
|
|
|
|
val[0] = 4;
|
|
|
|
val[2] = 5;
|
|
|
|
}
|
|
|
|
let comp: &mut [i32] = &mut [4, 2, 5];
|
|
|
|
assert_eq!(unsafe { &mut *cell.get() }, comp);
|
|
|
|
}
|
|
|
|
|
2015-05-12 14:41:08 +12:00
|
|
|
// FIXME(#25351) needs deeply nested coercions of DST structs.
|
|
|
|
// #[test]
|
|
|
|
// fn refcell_unsized() {
|
|
|
|
// let cell: &RefCell<[i32]> = &RefCell::new([1, 2, 3]);
|
|
|
|
// {
|
|
|
|
// let b = &mut *cell.borrow_mut();
|
|
|
|
// b[0] = 4;
|
|
|
|
// b[2] = 5;
|
|
|
|
// }
|
|
|
|
// let comp: &mut [i32] = &mut [4, 2, 5];
|
|
|
|
// assert_eq!(&*cell.borrow(), comp);
|
|
|
|
// }
|