Skip to content

Commit fbd2365

Browse files
authored
[libc++] Improve coverage of std::atomic_ref<T>::exchange() (#121596)
Adapted from libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/exchange.pass.cpp as we did for testing other functionalities. Spotted that lapse in coverage when working on #121414.
1 parent 57b80e8 commit fbd2365

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

libcxx/test/std/atomics/atomics.ref/exchange.pass.cpp

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,47 @@
1717
#include <type_traits>
1818

1919
#include "atomic_helpers.h"
20+
#include "test_helper.h"
2021
#include "test_macros.h"
2122

2223
template <typename T>
2324
struct TestExchange {
2425
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+
}
2742

43+
// memory_order::release
2844
{
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);
3250
}
3351

52+
// memory_order::seq_cst
3453
{
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);
3861
}
3962
}
4063
};

0 commit comments

Comments
 (0)