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