Skip to content

Commit

Permalink
tests: Add quickcheck tests for AtomicPtr
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Aug 31, 2024
1 parent 09a967b commit 284e8da
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 10 deletions.
24 changes: 16 additions & 8 deletions src/imp/interrupt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,10 @@ impl<T> AtomicPtr<T> {
any(target_arch = "riscv32", target_arch = "riscv64"),
any(
portable_atomic_force_amo,
target_feature = "zaamo",
portable_atomic_target_feature = "zaamo",
all(
any(target_feature = "zaamo", portable_atomic_target_feature = "zaamo"),
portable_atomic_unsafe_assume_single_core,
),
),
))]
{
Expand All @@ -187,8 +189,10 @@ impl<T> AtomicPtr<T> {
any(target_arch = "riscv32", target_arch = "riscv64"),
any(
portable_atomic_force_amo,
target_feature = "zaamo",
portable_atomic_target_feature = "zaamo",
all(
any(target_feature = "zaamo", portable_atomic_target_feature = "zaamo"),
portable_atomic_unsafe_assume_single_core,
),
),
)))]
// SAFETY: any data races are prevented by disabling interrupts (see
Expand Down Expand Up @@ -297,17 +301,21 @@ macro_rules! atomic_int {
any(target_arch = "riscv32", target_arch = "riscv64"),
any(
portable_atomic_force_amo,
target_feature = "zaamo",
portable_atomic_target_feature = "zaamo",
all(
any(target_feature = "zaamo", portable_atomic_target_feature = "zaamo"),
portable_atomic_unsafe_assume_single_core,
),
),
))]
atomic_int!(cas $([$kind])?, $atomic_type, $int_type);
#[cfg(not(all(
any(target_arch = "riscv32", target_arch = "riscv64"),
any(
portable_atomic_force_amo,
target_feature = "zaamo",
portable_atomic_target_feature = "zaamo",
all(
any(target_feature = "zaamo", portable_atomic_target_feature = "zaamo"),
portable_atomic_unsafe_assume_single_core,
),
),
)))]
atomic_int!(cas[emulate], $atomic_type, $int_type);
Expand Down
13 changes: 11 additions & 2 deletions src/imp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,19 @@ pub(crate) mod msp430;

// atomic load/store for RISC-V without A-extension
#[cfg(any(test, not(feature = "critical-section")))]
#[cfg_attr(portable_atomic_no_cfg_target_has_atomic, cfg(any(test, portable_atomic_no_atomic_cas)))]
#[cfg_attr(
portable_atomic_no_cfg_target_has_atomic,
cfg(any(
all(test, not(any(miri, portable_atomic_sanitize_thread))),
portable_atomic_no_atomic_cas,
))
)]
#[cfg_attr(
not(portable_atomic_no_cfg_target_has_atomic),
cfg(any(test, not(target_has_atomic = "ptr")))
cfg(any(
all(test, not(any(miri, portable_atomic_sanitize_thread))),
not(target_has_atomic = "ptr"),
))
)]
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
mod riscv;
Expand Down
30 changes: 30 additions & 0 deletions src/imp/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ macro_rules! atomic_ptr {
($([$($generics:tt)*])? $atomic_type:ident, $value_type:ty, $asm_suffix:tt) => {
atomic_load_store!($([$($generics)*])? $atomic_type, $value_type, $asm_suffix);
#[cfg(any(
test,
portable_atomic_force_amo,
target_feature = "zaamo",
portable_atomic_target_feature = "zaamo",
Expand Down Expand Up @@ -505,6 +506,34 @@ mod tests {
test_atomic_int_load_store!(isize);
test_atomic_int_load_store!(usize);

macro_rules! test_atomic_ptr_amo {
() => {
#[allow(
clippy::alloc_instead_of_core,
clippy::std_instead_of_alloc,
clippy::std_instead_of_core,
clippy::undocumented_unsafe_blocks
)]
mod test_atomic_ptr_amo {
use super::*;
test_atomic_ptr_amo!(AtomicPtr<u8>);
}
};
($atomic_type:ty) => {
::quickcheck::quickcheck! {
fn quickcheck_swap(x: usize, y: usize) -> bool {
let x = sptr::invalid_mut(x);
let y = sptr::invalid_mut(y);
for &order in &test_helper::SWAP_ORDERINGS {
let a = <$atomic_type>::new(x);
assert_eq!(a.swap(y, order), x);
assert_eq!(a.swap(x, order), y);
}
true
}
}
};
}
macro_rules! test_atomic_int_amo {
($int_type:ident) => {
paste::paste! {
Expand Down Expand Up @@ -793,6 +822,7 @@ mod tests {
}
};
}
test_atomic_ptr_amo!();
test_atomic_int_amo_sub_word!(i8);
test_atomic_int_amo_sub_word!(u8);
test_atomic_int_amo_sub_word!(i16);
Expand Down
31 changes: 31 additions & 0 deletions src/tests/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,37 @@ macro_rules! __test_atomic_ptr {
assert_eq!(a.load(Ordering::Relaxed), x as *mut _);
}
}
::quickcheck::quickcheck! {
fn quickcheck_swap(x: usize, y: usize) -> bool {
let x = sptr::invalid_mut(x);
let y = sptr::invalid_mut(y);
for &order in &test_helper::SWAP_ORDERINGS {
let a = <$atomic_type>::new(x);
assert_eq!(a.swap(y, order), x);
assert_eq!(a.swap(x, order), y);
}
true
}
fn quickcheck_compare_exchange(x: usize, y: usize) -> bool {
let z = loop {
let z = fastrand::usize(..);
if z != y {
break z;
}
};
let x = sptr::invalid_mut(x);
let y = sptr::invalid_mut(y);
let z = sptr::invalid_mut(z);
for &(success, failure) in &test_helper::COMPARE_EXCHANGE_ORDERINGS {
let a = <$atomic_type>::new(x);
assert_eq!(a.compare_exchange(x, y, success, failure).unwrap(), x);
assert_eq!(a.load(Ordering::Relaxed), y);
assert_eq!(a.compare_exchange(z, x, success, failure).unwrap_err(), y);
assert_eq!(a.load(Ordering::Relaxed), y);
}
true
}
}
};
($atomic_type:ty) => {
__test_atomic_ptr!($atomic_type, single_thread);
Expand Down

0 comments on commit 284e8da

Please sign in to comment.