Skip to content

Commit

Permalink
Reorgonize test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad committed Jun 4, 2023
1 parent 61f8384 commit 14742ac
Show file tree
Hide file tree
Showing 8 changed files with 1,071 additions and 977 deletions.
977 changes: 0 additions & 977 deletions tests/it.rs

This file was deleted.

12 changes: 12 additions & 0 deletions tests/it/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
mod unsync_once_cell;
#[cfg(any(feature = "std", feature = "critical-section"))]
mod sync_once_cell;

mod unsync_lazy;
#[cfg(any(feature = "std", feature = "critical-section"))]
mod sync_lazy;

#[cfg(feature = "race")]
mod race;
#[cfg(all(feature = "race", feature = "alloc"))]
mod race_once_box;
132 changes: 132 additions & 0 deletions tests/it/race.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#[cfg(feature = "std")]
use std::sync::Barrier;
use std::{
num::NonZeroUsize,
sync::atomic::{AtomicUsize, Ordering::SeqCst},
};

use crossbeam_utils::thread::scope;

use once_cell::race::{OnceBool, OnceNonZeroUsize};

#[test]
fn once_non_zero_usize_smoke_test() {
let cnt = AtomicUsize::new(0);
let cell = OnceNonZeroUsize::new();
let val = NonZeroUsize::new(92).unwrap();
scope(|s| {
s.spawn(|_| {
assert_eq!(
cell.get_or_init(|| {
cnt.fetch_add(1, SeqCst);
val
}),
val
);
assert_eq!(cnt.load(SeqCst), 1);

assert_eq!(
cell.get_or_init(|| {
cnt.fetch_add(1, SeqCst);
val
}),
val
);
assert_eq!(cnt.load(SeqCst), 1);
});
})
.unwrap();
assert_eq!(cell.get(), Some(val));
assert_eq!(cnt.load(SeqCst), 1);
}

#[test]
fn once_non_zero_usize_set() {
let val1 = NonZeroUsize::new(92).unwrap();
let val2 = NonZeroUsize::new(62).unwrap();

let cell = OnceNonZeroUsize::new();

assert!(cell.set(val1).is_ok());
assert_eq!(cell.get(), Some(val1));

assert!(cell.set(val2).is_err());
assert_eq!(cell.get(), Some(val1));
}

#[cfg(feature = "std")]
#[test]
fn once_non_zero_usize_first_wins() {
let val1 = NonZeroUsize::new(92).unwrap();
let val2 = NonZeroUsize::new(62).unwrap();

let cell = OnceNonZeroUsize::new();

let b1 = Barrier::new(2);
let b2 = Barrier::new(2);
let b3 = Barrier::new(2);
scope(|s| {
s.spawn(|_| {
let r1 = cell.get_or_init(|| {
b1.wait();
b2.wait();
val1
});
assert_eq!(r1, val1);
b3.wait();
});
b1.wait();
s.spawn(|_| {
let r2 = cell.get_or_init(|| {
b2.wait();
b3.wait();
val2
});
assert_eq!(r2, val1);
});
})
.unwrap();

assert_eq!(cell.get(), Some(val1));
}

#[test]
fn once_bool_smoke_test() {
let cnt = AtomicUsize::new(0);
let cell = OnceBool::new();
scope(|s| {
s.spawn(|_| {
assert_eq!(
cell.get_or_init(|| {
cnt.fetch_add(1, SeqCst);
false
}),
false
);
assert_eq!(cnt.load(SeqCst), 1);

assert_eq!(
cell.get_or_init(|| {
cnt.fetch_add(1, SeqCst);
false
}),
false
);
assert_eq!(cnt.load(SeqCst), 1);
});
})
.unwrap();
assert_eq!(cell.get(), Some(false));
assert_eq!(cnt.load(SeqCst), 1);
}

#[test]
fn once_bool_set() {
let cell = OnceBool::new();

assert!(cell.set(false).is_ok());
assert_eq!(cell.get(), Some(false));

assert!(cell.set(true).is_err());
assert_eq!(cell.get(), Some(false));
}
146 changes: 146 additions & 0 deletions tests/it/race_once_box.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#[cfg(feature = "std")]
use std::sync::Barrier;
use std::sync::{
atomic::{AtomicUsize, Ordering::SeqCst},
Arc,
};

#[cfg(feature = "std")]
use crossbeam_utils::thread::scope;

use once_cell::race::OnceBox;

#[derive(Default)]
struct Heap {
total: Arc<AtomicUsize>,
}

#[derive(Debug)]
struct Pebble<T> {
val: T,
total: Arc<AtomicUsize>,
}

impl<T> Drop for Pebble<T> {
fn drop(&mut self) {
self.total.fetch_sub(1, SeqCst);
}
}

impl Heap {
fn total(&self) -> usize {
self.total.load(SeqCst)
}
fn new_pebble<T>(&self, val: T) -> Pebble<T> {
self.total.fetch_add(1, SeqCst);
Pebble { val, total: Arc::clone(&self.total) }
}
}

#[cfg(feature = "std")]
#[test]
fn once_box_smoke_test() {
let heap = Heap::default();
let global_cnt = AtomicUsize::new(0);
let cell = OnceBox::new();
let b = Barrier::new(128);
scope(|s| {
for _ in 0..128 {
s.spawn(|_| {
let local_cnt = AtomicUsize::new(0);
cell.get_or_init(|| {
global_cnt.fetch_add(1, SeqCst);
local_cnt.fetch_add(1, SeqCst);
b.wait();
Box::new(heap.new_pebble(()))
});
assert_eq!(local_cnt.load(SeqCst), 1);

cell.get_or_init(|| {
global_cnt.fetch_add(1, SeqCst);
local_cnt.fetch_add(1, SeqCst);
Box::new(heap.new_pebble(()))
});
assert_eq!(local_cnt.load(SeqCst), 1);
});
}
})
.unwrap();
assert!(cell.get().is_some());
assert!(global_cnt.load(SeqCst) > 10);

assert_eq!(heap.total(), 1);
drop(cell);
assert_eq!(heap.total(), 0);
}

#[test]
fn once_box_set() {
let heap = Heap::default();
let cell = OnceBox::new();
assert!(cell.get().is_none());

assert!(cell.set(Box::new(heap.new_pebble("hello"))).is_ok());
assert_eq!(cell.get().unwrap().val, "hello");
assert_eq!(heap.total(), 1);

assert!(cell.set(Box::new(heap.new_pebble("world"))).is_err());
assert_eq!(cell.get().unwrap().val, "hello");
assert_eq!(heap.total(), 1);

drop(cell);
assert_eq!(heap.total(), 0);
}

#[cfg(feature = "std")]
#[test]
fn once_box_first_wins() {
let cell = OnceBox::new();
let val1 = 92;
let val2 = 62;

let b1 = Barrier::new(2);
let b2 = Barrier::new(2);
let b3 = Barrier::new(2);
scope(|s| {
s.spawn(|_| {
let r1 = cell.get_or_init(|| {
b1.wait();
b2.wait();
Box::new(val1)
});
assert_eq!(*r1, val1);
b3.wait();
});
b1.wait();
s.spawn(|_| {
let r2 = cell.get_or_init(|| {
b2.wait();
b3.wait();
Box::new(val2)
});
assert_eq!(*r2, val1);
});
})
.unwrap();

assert_eq!(cell.get(), Some(&val1));
}

#[test]
fn once_box_reentrant() {
let cell = OnceBox::new();
let res = cell.get_or_init(|| {
cell.get_or_init(|| Box::new("hello".to_string()));
Box::new("world".to_string())
});
assert_eq!(res, "hello");
}

#[test]
fn once_box_default() {
struct Foo;

let cell: OnceBox<Foo> = Default::default();
assert!(cell.get().is_none());
}
Loading

0 comments on commit 14742ac

Please sign in to comment.