Skip to content

Commit

Permalink
Add a few tests for UnsafeCell
Browse files Browse the repository at this point in the history
  • Loading branch information
inquisitivecrystal committed Aug 31, 2021
1 parent 753dac1 commit 227e004
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions library/core/tests/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,38 @@ use core::cell::*;
use core::default::Default;
use std::mem::drop;

#[test]
fn smoketest_unsafe_cell() {
let mut x = UnsafeCell::new(10);
let ref_mut = &mut x;
unsafe {
// The asserts are repeated in order to ensure that `get()`
// is non-mutating.
assert_eq!(*ref_mut.get(), 10);
assert_eq!(*ref_mut.get(), 10);
*ref_mut.get_mut() += 5;
assert_eq!(*ref_mut.get(), 15);
assert_eq!(*ref_mut.get(), 15);
assert_eq!(x.into_inner(), 15);
}
}

#[test]
fn unsafe_cell_raw_get() {
let x = UnsafeCell::new(10);
let ptr = &x as *const UnsafeCell<i32>;
unsafe {
// The asserts are repeated in order to ensure that `raw_get()`
// is non-mutating.
assert_eq!(*UnsafeCell::raw_get(ptr), 10);
assert_eq!(*UnsafeCell::raw_get(ptr), 10);
*UnsafeCell::raw_get(ptr) += 5;
assert_eq!(*UnsafeCell::raw_get(ptr), 15);
assert_eq!(*UnsafeCell::raw_get(ptr), 15);
assert_eq!(x.into_inner(), 15);
}
}

#[test]
fn smoketest_cell() {
let x = Cell::new(10);
Expand Down

0 comments on commit 227e004

Please sign in to comment.