Debian Codegraph @

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

raw
1 //
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 //
6 //===----------------------------------------------------------------------===//
7
8 // UNSUPPORTED: c++03, c++11, c++14, c++17
9 // XFAIL: !has-64-bit-atomics
10
11 // integral-type fetch_sub(integral-type, memory_order = memory_order::seq_cst) const noexcept;
12 // floating-point-type fetch_sub(floating-point-type, memory_order = memory_order::seq_cst) const noexcept;
13 // T* fetch_sub(difference_type, memory_order = memory_order::seq_cst) const noexcept;
14
15 #include <atomic>
16 #include <cassert>
17 #include <concepts>
18 #include <type_traits>
19
20 #include "atomic_helpers.h"
21 #include "test_helper.h"
22 #include "test_macros.h"
23
24 template <typename T>
25 concept has_fetch_sub = requires {
26 std::declval<T const>().fetch_sub(std::declval<T>());
27 std::declval<T const>().fetch_sub(std::declval<T>(), std::declval<std::memory_order>());
28 };
29
30 template <typename T>
31 struct TestDoesNotHaveFetchSub {
32 void operator()() const { static_assert(!has_fetch_sub<std::atomic_ref<T>>); }
33 };
34
35 template <typename T>
36 struct TestFetchSub {
37 void operator()() const {
38 if constexpr (std::is_arithmetic_v<T>) {
39 T x(T(7));
40 std::atomic_ref<T> const a(x);
41
42 {
43 std::same_as<T> decltype(auto) y = a.fetch_sub(T(4));
44 assert(y == T(7));
45 assert(x == T(3));
46 ASSERT_NOEXCEPT(a.fetch_sub(T(0)));
47 }
48
49 {
50 std::same_as<T> decltype(auto) y = a.fetch_sub(T(2), std::memory_order_relaxed);
51 assert(y == T(3));
52 assert(x == T(1));
53 ASSERT_NOEXCEPT(a.fetch_sub(T(0), std::memory_order_relaxed));
54 }
55 } else if constexpr (std::is_pointer_v<T>) {
56 using U = std::remove_pointer_t<T>;
57 U t[9] = {};
58 T p{&t[7]};
59 std::atomic_ref<T> const a(p);
60
61 {
62 std::same_as<T> decltype(auto) y = a.fetch_sub(4);
63 assert(y == &t[7]);
64 assert(a == &t[3]);
65 ASSERT_NOEXCEPT(a.fetch_sub(0));
66 }
67
68 {
69 std::same_as<T> decltype(auto) y = a.fetch_sub(2, std::memory_order_relaxed);
70 assert(y == &t[3]);
71 assert(a == &t[1]);
72 ASSERT_NOEXCEPT(a.fetch_sub(0, std::memory_order_relaxed));
73 }
74 } else {
75 static_assert(std::is_void_v<T>);
76 }
77
78 // memory_order::release
79 {
80 auto fetch_sub = [](std::atomic_ref<T> const& x, T old_val, T new_val) {
81 x.fetch_sub(old_val - new_val, std::memory_order::release);
82 };
83 auto load = [](std::atomic_ref<T> const& x) { return x.load(std::memory_order::acquire); };
84 test_acquire_release<T>(fetch_sub, load);
85 }
86
87 // memory_order::seq_cst
88 {
89 auto fetch_sub_no_arg = [](std::atomic_ref<T> const& x, T old_val, T new_val) { x.fetch_sub(old_val - new_val); };
90 auto fetch_sub_with_order = [](std::atomic_ref<T> const& x, T old_val, T new_val) {
91 x.fetch_sub(old_val - new_val, std::memory_order::seq_cst);
92 };
93 auto load = [](std::atomic_ref<T> const& x) { return x.load(); };
94 test_seq_cst<T>(fetch_sub_no_arg, load);
95 test_seq_cst<T>(fetch_sub_with_order, load);
96 }
97 }
98 };
99
100 int main(int, char**) {
101 TestEachIntegralType<TestFetchSub>()();
102
103 TestFetchSub<float>()();
104 TestFetchSub<double>()();
105
106 TestEachPointerType<TestFetchSub>()();
107
108 TestDoesNotHaveFetchSub<bool>()();
109 TestDoesNotHaveFetchSub<UserAtomicType>()();
110 TestDoesNotHaveFetchSub<LargeUserAtomicType>()();
111
112 return 0;
113 }