organize std tests a bit better

This commit is contained in:
Ralf Jung 2018-11-08 08:58:03 +01:00
parent a94e197105
commit 224d03dbdc
3 changed files with 38 additions and 37 deletions

View File

@ -1,13 +1,33 @@
use std::cell::RefCell;
use std::cell::{Cell, RefCell};
use std::rc::Rc;
use std::sync::Arc;
fn rc_refcell() {
let r = Rc::new(RefCell::new(42));
let r2 = r.clone();
*r.borrow_mut() += 10;
let x = *r.borrow();
let x = *r2.borrow();
assert_eq!(x, 52);
}
fn rc_cell() {
let r = Rc::new(Cell::new(42));
let r2 = r.clone();
let x = r.get();
r2.set(x + x);
assert_eq!(r.get(), 84);
}
fn rc_refcell2() {
let r = Rc::new(RefCell::new(42));
let r2 = r.clone();
*r.borrow_mut() += 10;
let x = r2.borrow();
let r3 = r.clone();
let y = r3.borrow();
assert_eq!((*x + *y)/2, 52);
}
fn rc_raw() {
let r = Rc::new(0);
let r2 = Rc::into_raw(r.clone());
@ -17,6 +37,14 @@ fn rc_raw() {
assert!(Rc::try_unwrap(r2).is_ok());
}
fn arc() {
fn test() -> Arc<i32> {
let a = Arc::new(42);
a
}
assert_eq!(*test(), 42);
}
// Make sure this Rc doesn't fall apart when touched
fn check_unique_rc<T: ?Sized>(mut r: Rc<T>) {
let r2 = r.clone();
@ -34,6 +62,9 @@ fn rc_from() {
fn main() {
rc_refcell();
rc_refcell2();
rc_cell();
rc_raw();
rc_from();
arc();
}

View File

@ -1,6 +1,6 @@
use std::cell::RefCell;
fn main() {
fn lots_of_funny_borrows() {
let c = RefCell::new(42);
{
let s1 = c.borrow();
@ -31,3 +31,7 @@ fn main() {
let _y: i32 = *s2;
}
}
fn main() {
lots_of_funny_borrows();
}

View File

@ -1,34 +0,0 @@
use std::cell::{Cell, RefCell};
use std::rc::Rc;
use std::sync::Arc;
fn rc_cell() -> Rc<Cell<i32>> {
let r = Rc::new(Cell::new(42));
let x = r.get();
r.set(x + x);
r
}
fn rc_refcell() -> i32 {
let r = Rc::new(RefCell::new(42));
*r.borrow_mut() += 10;
let x = r.borrow();
let y = r.borrow();
(*x + *y)/2
}
fn arc() -> Arc<i32> {
let a = Arc::new(42);
a
}
fn true_assert() {
assert_eq!(1, 1);
}
fn main() {
assert_eq!(*arc(), 42);
assert_eq!(rc_cell().get(), 84);
assert_eq!(rc_refcell(), 52);
true_assert();
}