Skip to content

Commit

Permalink
[eclipse-iceoryx#200] Refactor test to be more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Apr 29, 2024
1 parent 88c9ea7 commit 9ad018d
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions iceoryx2-pal/concurrency-sync/tests/iox_atomic_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,26 @@ mod ice_atomic {

#[test]
fn compare_exchange_failure_works<T: Req>() {
let n_outdated = T::generate_value();
let n_old = T::generate_value();
let n_new = T::generate_value();
let sut = IoxAtomic::<T>::new(n_old);

let result = sut.compare_exchange(n_new, n_old, Ordering::Relaxed, Ordering::Relaxed);
let result = sut.compare_exchange(n_outdated, n_new, Ordering::Relaxed, Ordering::Relaxed);

assert_that!(result, is_err);
assert_that!(result.err().unwrap(), eq n_old);
}

#[test]
fn compare_exchange_weak_failure_works<T: Req>() {
let n_outdated = T::generate_value();
let n_old = T::generate_value();
let n_new = T::generate_value();
let sut = IoxAtomic::<T>::new(n_old);

let result = sut.compare_exchange(n_new, n_old, Ordering::Relaxed, Ordering::Relaxed);
let result =
sut.compare_exchange_weak(n_outdated, n_new, Ordering::Relaxed, Ordering::Relaxed);

assert_that!(result, is_err);
assert_that!(result.err().unwrap(), eq n_old);
Expand Down Expand Up @@ -191,25 +194,33 @@ mod ice_atomic {
let n1 = T::generate_value();
let n2 = T::generate_value();

let sut = IoxAtomic::<T>::new(n1);
let sut_1 = IoxAtomic::<T>::new(n1);
let sut_2 = IoxAtomic::<T>::new(n2);

let result = sut.fetch_max(n2, Ordering::Relaxed);
let result_1 = sut_1.fetch_max(n2, Ordering::Relaxed);
let result_2 = sut_2.fetch_max(n1, Ordering::Relaxed);

assert_that!(result, eq n1);
assert_that!(sut.load(Ordering::Relaxed), eq n1.max(n2));
assert_that!(result_1, eq n1);
assert_that!(result_2, eq n2);
assert_that!(sut_1.load(Ordering::Relaxed), eq n1.max(n2));
assert_that!(sut_2.load(Ordering::Relaxed), eq n1.max(n2));
}

#[test]
fn fetch_min_works<T: Req>() {
let n1 = T::generate_value();
let n2 = T::generate_value();

let sut = IoxAtomic::<T>::new(n1);
let sut_1 = IoxAtomic::<T>::new(n1);
let sut_2 = IoxAtomic::<T>::new(n2);

let result = sut.fetch_min(n2, Ordering::Relaxed);
let result_1 = sut_1.fetch_min(n2, Ordering::Relaxed);
let result_2 = sut_2.fetch_min(n1, Ordering::Relaxed);

assert_that!(result, eq n1);
assert_that!(sut.load(Ordering::Relaxed), eq n1.min(n2));
assert_that!(result_1, eq n1);
assert_that!(result_2, eq n2);
assert_that!(sut_1.load(Ordering::Relaxed), eq n1.min(n2));
assert_that!(sut_2.load(Ordering::Relaxed), eq n1.min(n2));
}

#[test]
Expand Down

0 comments on commit 9ad018d

Please sign in to comment.