Debian Codegraph @

libcxx/test/std/atomics/atomics.ref/fetch_and.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_and(integral-type, memory_order = memory_order::seq_cst) const noexcept;
12
13 #include <atomic>
14 #include <cassert>
15 #include <concepts>
16 #include <type_traits>
17
18 #include "atomic_helpers.h"
19 #include "test_macros.h"
20
21 template <typename T>
22 concept has_fetch_and = requires {
23 std::declval<T const>().fetch_and(std::declval<T>());
24 std::declval<T const>().fetch_and(std::declval<T>(), std::declval<std::memory_order>());
25 };
26
27 template <typename T>
28 struct TestDoesNotHaveFetchAnd {
29 void operator()() const { static_assert(!has_fetch_and<std::atomic_ref<T>>); }
30 };
31
32 template <typename T>
33 struct TestFetchAnd {
34 void operator()() const {
35 static_assert(std::is_integral_v<T>);
36
37 T x(T(1));
38 std::atomic_ref<T> const a(x);
39
40 {
41 std::same_as<T> decltype(auto) y = a.fetch_and(T(2));
42 assert(y == T(1));
43 assert(x == T(0));
44 ASSERT_NOEXCEPT(a.fetch_and(T(0)));
45 }
46
47 x = T(1);
48
49 {
50 std::same_as<T> decltype(auto) y = a.fetch_and(T(2), std::memory_order_relaxed);
51 assert(y == T(1));
52 assert(x == T(0));
53 ASSERT_NOEXCEPT(a.fetch_and(T(0), std::memory_order_relaxed));
54 }
55 }
56 };
57
58 int main(int, char**) {
59 TestEachIntegralType<TestFetchAnd>()();
60
61 TestEachFloatingPointType<TestDoesNotHaveFetchAnd>()();
62
63 TestEachPointerType<TestDoesNotHaveFetchAnd>()();
64
65 TestDoesNotHaveFetchAnd<bool>()();
66 TestDoesNotHaveFetchAnd<UserAtomicType>()();
67 TestDoesNotHaveFetchAnd<LargeUserAtomicType>()();
68 return 0;
69 }