Skip to content

Commit

Permalink
tests: Turn ordering helpers in no-std tests into constants
Browse files Browse the repository at this point in the history
Same as 8e9dfb9, but for no-std tests.
  • Loading branch information
taiki-e committed Nov 4, 2024
1 parent 293154c commit 15adb28
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 144 deletions.
64 changes: 26 additions & 38 deletions tests/avr/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ macro_rules! __test_atomic {
unsafe {
static VAR: AtomicMaybeUninit<$int_type> =
AtomicMaybeUninit::<$int_type>::const_new(MaybeUninit::new(10));
for (load_order, store_order) in load_orderings().into_iter().zip(store_orderings())
{
for (load_order, store_order) in LOAD_ORDERINGS.into_iter().zip(STORE_ORDERINGS) {
assert_eq!(VAR.load(load_order).assume_init(), 10);
VAR.store(MaybeUninit::new(5), store_order);
assert_eq!(VAR.load(load_order).assume_init(), 5);
Expand All @@ -40,7 +39,7 @@ macro_rules! __test_atomic {
swap();
fn swap() {
unsafe {
for order in swap_orderings() {
for order in SWAP_ORDERINGS {
let a = AtomicMaybeUninit::<$int_type>::new(MaybeUninit::new(5));
assert_eq!(a.swap(MaybeUninit::new(10), order).assume_init(), 5);
assert_eq!(a.swap(MaybeUninit::uninit(), order).assume_init(), 10);
Expand All @@ -51,11 +50,10 @@ macro_rules! __test_atomic {
}
}
}
cfg_has_atomic_cas! {
compare_exchange();
fn compare_exchange() {
unsafe {
for (success, failure) in compare_exchange_orderings() {
for (success, failure) in COMPARE_EXCHANGE_ORDERINGS {
let a = AtomicMaybeUninit::<$int_type>::new(MaybeUninit::new(5));
assert_eq!(
a.compare_exchange(
Expand Down Expand Up @@ -87,7 +85,7 @@ macro_rules! __test_atomic {
compare_exchange_weak();
fn compare_exchange_weak() {
unsafe {
for (success, failure) in compare_exchange_orderings() {
for (success, failure) in COMPARE_EXCHANGE_ORDERINGS {
let a = AtomicMaybeUninit::<$int_type>::new(MaybeUninit::new(4));
assert_eq!(
a.compare_exchange_weak(
Expand Down Expand Up @@ -115,7 +113,7 @@ macro_rules! __test_atomic {
fetch_update();
fn fetch_update() {
unsafe {
for (success, failure) in compare_exchange_orderings() {
for (success, failure) in COMPARE_EXCHANGE_ORDERINGS {
let a = AtomicMaybeUninit::<$int_type>::new(MaybeUninit::new(7));
assert_eq!(
a.fetch_update(success, failure, |_| None).unwrap_err().assume_init(),
Expand All @@ -141,7 +139,6 @@ macro_rules! __test_atomic {
}
}
}
}
};
}

Expand Down Expand Up @@ -185,36 +182,27 @@ fn main() -> ! {
semihosting::exit(0)
}

fn load_orderings() -> [Ordering; 3] {
[Ordering::Relaxed, Ordering::Acquire, Ordering::SeqCst]
}
fn store_orderings() -> [Ordering; 3] {
[Ordering::Relaxed, Ordering::Release, Ordering::SeqCst]
}
fn swap_orderings() -> [Ordering; 5] {
[Ordering::Relaxed, Ordering::Release, Ordering::Acquire, Ordering::AcqRel, Ordering::SeqCst]
}
cfg_has_atomic_cas! {
fn compare_exchange_orderings() -> [(Ordering, Ordering); 15] {
[
(Ordering::Relaxed, Ordering::Relaxed),
(Ordering::Relaxed, Ordering::Acquire),
(Ordering::Relaxed, Ordering::SeqCst),
(Ordering::Acquire, Ordering::Relaxed),
(Ordering::Acquire, Ordering::Acquire),
(Ordering::Acquire, Ordering::SeqCst),
(Ordering::Release, Ordering::Relaxed),
(Ordering::Release, Ordering::Acquire),
(Ordering::Release, Ordering::SeqCst),
(Ordering::AcqRel, Ordering::Relaxed),
(Ordering::AcqRel, Ordering::Acquire),
(Ordering::AcqRel, Ordering::SeqCst),
(Ordering::SeqCst, Ordering::Relaxed),
(Ordering::SeqCst, Ordering::Acquire),
(Ordering::SeqCst, Ordering::SeqCst),
]
}
}
const LOAD_ORDERINGS: [Ordering; 3] = [Ordering::Relaxed, Ordering::Acquire, Ordering::SeqCst];
const STORE_ORDERINGS: [Ordering; 3] = [Ordering::Relaxed, Ordering::Release, Ordering::SeqCst];
const SWAP_ORDERINGS: [Ordering; 5] =
[Ordering::Relaxed, Ordering::Release, Ordering::Acquire, Ordering::AcqRel, Ordering::SeqCst];
const COMPARE_EXCHANGE_ORDERINGS: [(Ordering, Ordering); 15] = [
(Ordering::Relaxed, Ordering::Relaxed),
(Ordering::Relaxed, Ordering::Acquire),
(Ordering::Relaxed, Ordering::SeqCst),
(Ordering::Acquire, Ordering::Relaxed),
(Ordering::Acquire, Ordering::Acquire),
(Ordering::Acquire, Ordering::SeqCst),
(Ordering::Release, Ordering::Relaxed),
(Ordering::Release, Ordering::Acquire),
(Ordering::Release, Ordering::SeqCst),
(Ordering::AcqRel, Ordering::Relaxed),
(Ordering::AcqRel, Ordering::Acquire),
(Ordering::AcqRel, Ordering::SeqCst),
(Ordering::SeqCst, Ordering::Relaxed),
(Ordering::SeqCst, Ordering::Acquire),
(Ordering::SeqCst, Ordering::SeqCst),
];

#[inline(never)]
#[panic_handler]
Expand Down
64 changes: 26 additions & 38 deletions tests/msp430/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ macro_rules! __test_atomic {
unsafe {
static VAR: AtomicMaybeUninit<$int_type> =
AtomicMaybeUninit::<$int_type>::const_new(MaybeUninit::new(10));
for (load_order, store_order) in load_orderings().into_iter().zip(store_orderings())
{
for (load_order, store_order) in LOAD_ORDERINGS.into_iter().zip(STORE_ORDERINGS) {
assert_eq!(VAR.load(load_order).assume_init(), 10);
VAR.store(MaybeUninit::new(5), store_order);
assert_eq!(VAR.load(load_order).assume_init(), 5);
Expand All @@ -41,7 +40,7 @@ macro_rules! __test_atomic {
swap();
fn swap() {
unsafe {
for order in swap_orderings() {
for order in SWAP_ORDERINGS {
let a = AtomicMaybeUninit::<$int_type>::new(MaybeUninit::new(5));
assert_eq!(a.swap(MaybeUninit::new(10), order).assume_init(), 5);
assert_eq!(a.swap(MaybeUninit::uninit(), order).assume_init(), 10);
Expand All @@ -52,11 +51,10 @@ macro_rules! __test_atomic {
}
}
}
cfg_has_atomic_cas! {
compare_exchange();
fn compare_exchange() {
unsafe {
for (success, failure) in compare_exchange_orderings() {
for (success, failure) in COMPARE_EXCHANGE_ORDERINGS {
let a = AtomicMaybeUninit::<$int_type>::new(MaybeUninit::new(5));
assert_eq!(
a.compare_exchange(
Expand Down Expand Up @@ -88,7 +86,7 @@ macro_rules! __test_atomic {
compare_exchange_weak();
fn compare_exchange_weak() {
unsafe {
for (success, failure) in compare_exchange_orderings() {
for (success, failure) in COMPARE_EXCHANGE_ORDERINGS {
let a = AtomicMaybeUninit::<$int_type>::new(MaybeUninit::new(4));
assert_eq!(
a.compare_exchange_weak(
Expand Down Expand Up @@ -116,7 +114,7 @@ macro_rules! __test_atomic {
fetch_update();
fn fetch_update() {
unsafe {
for (success, failure) in compare_exchange_orderings() {
for (success, failure) in COMPARE_EXCHANGE_ORDERINGS {
let a = AtomicMaybeUninit::<$int_type>::new(MaybeUninit::new(7));
assert_eq!(
a.fetch_update(success, failure, |_| None).unwrap_err().assume_init(),
Expand All @@ -142,7 +140,6 @@ macro_rules! __test_atomic {
}
}
}
}
};
}

Expand Down Expand Up @@ -185,36 +182,27 @@ fn main() -> ! {
loop {}
}

fn load_orderings() -> [Ordering; 3] {
[Ordering::Relaxed, Ordering::Acquire, Ordering::SeqCst]
}
fn store_orderings() -> [Ordering; 3] {
[Ordering::Relaxed, Ordering::Release, Ordering::SeqCst]
}
fn swap_orderings() -> [Ordering; 5] {
[Ordering::Relaxed, Ordering::Release, Ordering::Acquire, Ordering::AcqRel, Ordering::SeqCst]
}
cfg_has_atomic_cas! {
fn compare_exchange_orderings() -> [(Ordering, Ordering); 15] {
[
(Ordering::Relaxed, Ordering::Relaxed),
(Ordering::Relaxed, Ordering::Acquire),
(Ordering::Relaxed, Ordering::SeqCst),
(Ordering::Acquire, Ordering::Relaxed),
(Ordering::Acquire, Ordering::Acquire),
(Ordering::Acquire, Ordering::SeqCst),
(Ordering::Release, Ordering::Relaxed),
(Ordering::Release, Ordering::Acquire),
(Ordering::Release, Ordering::SeqCst),
(Ordering::AcqRel, Ordering::Relaxed),
(Ordering::AcqRel, Ordering::Acquire),
(Ordering::AcqRel, Ordering::SeqCst),
(Ordering::SeqCst, Ordering::Relaxed),
(Ordering::SeqCst, Ordering::Acquire),
(Ordering::SeqCst, Ordering::SeqCst),
]
}
}
const LOAD_ORDERINGS: [Ordering; 3] = [Ordering::Relaxed, Ordering::Acquire, Ordering::SeqCst];
const STORE_ORDERINGS: [Ordering; 3] = [Ordering::Relaxed, Ordering::Release, Ordering::SeqCst];
const SWAP_ORDERINGS: [Ordering; 5] =
[Ordering::Relaxed, Ordering::Release, Ordering::Acquire, Ordering::AcqRel, Ordering::SeqCst];
const COMPARE_EXCHANGE_ORDERINGS: [(Ordering, Ordering); 15] = [
(Ordering::Relaxed, Ordering::Relaxed),
(Ordering::Relaxed, Ordering::Acquire),
(Ordering::Relaxed, Ordering::SeqCst),
(Ordering::Acquire, Ordering::Relaxed),
(Ordering::Acquire, Ordering::Acquire),
(Ordering::Acquire, Ordering::SeqCst),
(Ordering::Release, Ordering::Relaxed),
(Ordering::Release, Ordering::Acquire),
(Ordering::Release, Ordering::SeqCst),
(Ordering::AcqRel, Ordering::Relaxed),
(Ordering::AcqRel, Ordering::Acquire),
(Ordering::AcqRel, Ordering::SeqCst),
(Ordering::SeqCst, Ordering::Relaxed),
(Ordering::SeqCst, Ordering::Acquire),
(Ordering::SeqCst, Ordering::SeqCst),
];

#[inline(never)]
#[panic_handler]
Expand Down
60 changes: 26 additions & 34 deletions tests/no-std-qemu/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ macro_rules! __test_atomic {
unsafe {
static VAR: AtomicMaybeUninit<$int_type> =
AtomicMaybeUninit::<$int_type>::const_new(MaybeUninit::new(10));
for (load_order, store_order) in load_orderings().into_iter().zip(store_orderings())
{
for (load_order, store_order) in LOAD_ORDERINGS.into_iter().zip(STORE_ORDERINGS) {
assert_eq!(VAR.load(load_order).assume_init(), 10);
VAR.store(MaybeUninit::new(5), store_order);
assert_eq!(VAR.load(load_order).assume_init(), 5);
Expand All @@ -42,7 +41,7 @@ macro_rules! __test_atomic {
swap();
fn swap() {
unsafe {
for order in swap_orderings() {
for order in SWAP_ORDERINGS {
let a = AtomicMaybeUninit::<$int_type>::new(MaybeUninit::new(5));
assert_eq!(a.swap(MaybeUninit::new(10), order).assume_init(), 5);
assert_eq!(a.swap(MaybeUninit::uninit(), order).assume_init(), 10);
Expand All @@ -56,7 +55,7 @@ macro_rules! __test_atomic {
compare_exchange();
fn compare_exchange() {
unsafe {
for (success, failure) in compare_exchange_orderings() {
for (success, failure) in COMPARE_EXCHANGE_ORDERINGS {
let a = AtomicMaybeUninit::<$int_type>::new(MaybeUninit::new(5));
assert_eq!(
a.compare_exchange(
Expand Down Expand Up @@ -88,7 +87,7 @@ macro_rules! __test_atomic {
compare_exchange_weak();
fn compare_exchange_weak() {
unsafe {
for (success, failure) in compare_exchange_orderings() {
for (success, failure) in COMPARE_EXCHANGE_ORDERINGS {
let a = AtomicMaybeUninit::<$int_type>::new(MaybeUninit::new(4));
assert_eq!(
a.compare_exchange_weak(
Expand Down Expand Up @@ -116,7 +115,7 @@ macro_rules! __test_atomic {
fetch_update();
fn fetch_update() {
unsafe {
for (success, failure) in compare_exchange_orderings() {
for (success, failure) in COMPARE_EXCHANGE_ORDERINGS {
let a = AtomicMaybeUninit::<$int_type>::new(MaybeUninit::new(7));
assert_eq!(
a.fetch_update(success, failure, |_| None).unwrap_err().assume_init(),
Expand Down Expand Up @@ -181,33 +180,26 @@ fn run() {
}
}

fn load_orderings() -> [Ordering; 3] {
[Ordering::Relaxed, Ordering::Acquire, Ordering::SeqCst]
}
fn store_orderings() -> [Ordering; 3] {
[Ordering::Relaxed, Ordering::Release, Ordering::SeqCst]
}
const LOAD_ORDERINGS: [Ordering; 3] = [Ordering::Relaxed, Ordering::Acquire, Ordering::SeqCst];
const STORE_ORDERINGS: [Ordering; 3] = [Ordering::Relaxed, Ordering::Release, Ordering::SeqCst];
cfg_has_atomic_cas! {
fn swap_orderings() -> [Ordering; 5] {
[Ordering::Relaxed, Ordering::Release, Ordering::Acquire, Ordering::AcqRel, Ordering::SeqCst]
}
fn compare_exchange_orderings() -> [(Ordering, Ordering); 15] {
[
(Ordering::Relaxed, Ordering::Relaxed),
(Ordering::Relaxed, Ordering::Acquire),
(Ordering::Relaxed, Ordering::SeqCst),
(Ordering::Acquire, Ordering::Relaxed),
(Ordering::Acquire, Ordering::Acquire),
(Ordering::Acquire, Ordering::SeqCst),
(Ordering::Release, Ordering::Relaxed),
(Ordering::Release, Ordering::Acquire),
(Ordering::Release, Ordering::SeqCst),
(Ordering::AcqRel, Ordering::Relaxed),
(Ordering::AcqRel, Ordering::Acquire),
(Ordering::AcqRel, Ordering::SeqCst),
(Ordering::SeqCst, Ordering::Relaxed),
(Ordering::SeqCst, Ordering::Acquire),
(Ordering::SeqCst, Ordering::SeqCst),
]
}
const SWAP_ORDERINGS: [Ordering; 5] =
[Ordering::Relaxed, Ordering::Release, Ordering::Acquire, Ordering::AcqRel, Ordering::SeqCst];
const COMPARE_EXCHANGE_ORDERINGS: [(Ordering, Ordering); 15] = [
(Ordering::Relaxed, Ordering::Relaxed),
(Ordering::Relaxed, Ordering::Acquire),
(Ordering::Relaxed, Ordering::SeqCst),
(Ordering::Acquire, Ordering::Relaxed),
(Ordering::Acquire, Ordering::Acquire),
(Ordering::Acquire, Ordering::SeqCst),
(Ordering::Release, Ordering::Relaxed),
(Ordering::Release, Ordering::Acquire),
(Ordering::Release, Ordering::SeqCst),
(Ordering::AcqRel, Ordering::Relaxed),
(Ordering::AcqRel, Ordering::Acquire),
(Ordering::AcqRel, Ordering::SeqCst),
(Ordering::SeqCst, Ordering::Relaxed),
(Ordering::SeqCst, Ordering::Acquire),
(Ordering::SeqCst, Ordering::SeqCst),
];
}
Loading

0 comments on commit 15adb28

Please sign in to comment.