Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI to unit test for those using Cell/RefCell/UnsafeCell #76454

Merged
merged 14 commits into from
Sep 28, 2020
24 changes: 24 additions & 0 deletions library/core/tests/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,30 @@ fn cell_into_inner() {
assert_eq!("Hello world".to_owned(), cell.into_inner());
}

#[test]
fn cell_exterior() {
#[derive(Copy, Clone)]
#[allow(dead_code)]
struct Point {
x: isize,
y: isize,
z: isize,
}

fn f(p: &Cell<Point>) {
assert_eq!(p.get().z, 12);
p.set(Point { x: 10, y: 11, z: 13 });
assert_eq!(p.get().z, 13);
}

let a = Point { x: 10, y: 11, z: 12 };
let b = &Cell::new(a);
assert_eq!(b.get().z, 12);
f(b);
assert_eq!(a.z, 12);
assert_eq!(b.get().z, 13);
}

#[test]
fn refcell_default() {
let cell: RefCell<u64> = Default::default();
Expand Down
31 changes: 31 additions & 0 deletions library/core/tests/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,34 @@ fn option_const() {
const IS_NONE: bool = OPTION.is_none();
assert!(!IS_NONE);
}

#[test]
fn test_unwrap_drop() {
use std::cell::Cell;
poliorcetics marked this conversation as resolved.
Show resolved Hide resolved

struct Dtor<'a> {
x: &'a Cell<isize>,
}

impl<'a> std::ops::Drop for Dtor<'a> {
fn drop(&mut self) {
self.x.set(self.x.get() - 1);
}
}

fn unwrap<T>(o: Option<T>) -> T {
match o {
Some(v) => v,
None => panic!(),
}
}

let x = &Cell::new(1);

{
let b = Some(Dtor { x });
let _c = unwrap(b);
}

assert_eq!(x.get(), 0);
}
24 changes: 0 additions & 24 deletions src/test/ui/exterior.rs

This file was deleted.

32 changes: 0 additions & 32 deletions src/test/ui/option-unwrap.rs

This file was deleted.