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