|
17 | 17 | #include <type_traits>
|
18 | 18 |
|
19 | 19 | #include "atomic_helpers.h"
|
| 20 | +#include "test_helper.h" |
20 | 21 | #include "test_macros.h"
|
21 | 22 |
|
22 | 23 | template <typename T>
|
23 | 24 | struct TestExchange {
|
24 | 25 | void operator()() const {
|
25 |
| - T x(T(1)); |
26 |
| - std::atomic_ref<T> const a(x); |
| 26 | + { |
| 27 | + T x(T(1)); |
| 28 | + std::atomic_ref<T> const a(x); |
| 29 | + |
| 30 | + { |
| 31 | + std::same_as<T> decltype(auto) y = a.exchange(T(2)); |
| 32 | + assert(y == T(1)); |
| 33 | + ASSERT_NOEXCEPT(a.exchange(T(2))); |
| 34 | + } |
| 35 | + |
| 36 | + { |
| 37 | + std::same_as<T> decltype(auto) y = a.exchange(T(3), std::memory_order_seq_cst); |
| 38 | + assert(y == T(2)); |
| 39 | + ASSERT_NOEXCEPT(a.exchange(T(3), std::memory_order_seq_cst)); |
| 40 | + } |
| 41 | + } |
27 | 42 |
|
| 43 | + // memory_order::release |
28 | 44 | {
|
29 |
| - std::same_as<T> decltype(auto) y = a.exchange(T(2)); |
30 |
| - assert(y == T(1)); |
31 |
| - ASSERT_NOEXCEPT(a.exchange(T(2))); |
| 45 | + auto exchange = [](std::atomic_ref<T> const& x, T, T new_val) { |
| 46 | + x.exchange(new_val, std::memory_order::release); |
| 47 | + }; |
| 48 | + auto load = [](std::atomic_ref<T> const& x) { return x.load(std::memory_order::acquire); }; |
| 49 | + test_acquire_release<T>(exchange, load); |
32 | 50 | }
|
33 | 51 |
|
| 52 | + // memory_order::seq_cst |
34 | 53 | {
|
35 |
| - std::same_as<T> decltype(auto) y = a.exchange(T(3), std::memory_order_seq_cst); |
36 |
| - assert(y == T(2)); |
37 |
| - ASSERT_NOEXCEPT(a.exchange(T(3), std::memory_order_seq_cst)); |
| 54 | + auto exchange_no_arg = [](std::atomic_ref<T> const& x, T, T new_val) { x.exchange(new_val); }; |
| 55 | + auto exchange_with_order = [](std::atomic_ref<T> const& x, T, T new_val) { |
| 56 | + x.exchange(new_val, std::memory_order::seq_cst); |
| 57 | + }; |
| 58 | + auto load = [](std::atomic_ref<T> const& x) { return x.load(); }; |
| 59 | + test_seq_cst<T>(exchange_no_arg, load); |
| 60 | + test_seq_cst<T>(exchange_with_order, load); |
38 | 61 | }
|
39 | 62 | }
|
40 | 63 | };
|
|
0 commit comments