Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed atomic bitflags to be handled by generic wrapper
- Loading branch information
Showing
6 changed files
with
202 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod all; | ||
mod atomic; | ||
mod bits; | ||
mod complement; | ||
mod contains; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use core::{sync::atomic::Ordering, fmt::Debug}; | ||
|
||
use super::*; | ||
|
||
use crate::{Atomic, Flags, HasAtomic}; | ||
|
||
#[test] | ||
fn cases() { | ||
case( | ||
TestFlags::ABC, | ||
&[ | ||
(&|a| a.load(Ordering::Relaxed),TestFlags::ABC), | ||
], | ||
TestFlags::ABC | ||
); | ||
case( | ||
TestFlags::A, | ||
&[ | ||
(&|a| a.swap(TestFlags::B, Ordering::Relaxed),TestFlags::A), | ||
(&|a| a.swap(TestFlags::C, Ordering::Relaxed),TestFlags::B), | ||
], | ||
TestFlags::C | ||
); | ||
case( | ||
TestFlags::empty(), | ||
&[ | ||
(&|a| a.fetch_insert(TestFlags::A, Ordering::Relaxed), TestFlags::empty()), | ||
(&|a| a.fetch_toggle(TestFlags::ABC, Ordering::Relaxed), TestFlags::A), | ||
(&|a| a.fetch_remove(TestFlags::C, Ordering::Relaxed), TestFlags::B | TestFlags::C), | ||
], | ||
TestFlags::B | ||
); | ||
} | ||
|
||
#[track_caller] | ||
fn case<F: Flags>( | ||
init: F, | ||
ops: &[(&dyn Fn(&Atomic<F>) -> F, F)], | ||
final_res: F | ||
) | ||
where | ||
F: Flags + Debug + Copy + PartialEq, | ||
<F as Flags>::Bits: HasAtomic, | ||
{ | ||
let atomic = Atomic::from(init); | ||
|
||
for (op, op_res) in ops { | ||
let op_val = op(&atomic); | ||
assert_eq!(&op_val, op_res, "expected={op_res:?} got={op_val:?}"); | ||
} | ||
|
||
let final_val = atomic.load(Ordering::Relaxed); | ||
assert_eq!(final_val, final_res, "expected={final_res:?} got={final_val:?}"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.