70 lines · cpp
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-exception5//6//===----------------------------------------------------------------------===//7 8// UNSUPPORTED: c++03, c++11, c++14, c++179// XFAIL: !has-64-bit-atomics10 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 22template <typename T>23concept 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 28template <typename T>29struct TestDoesNotHaveFetchOr {30 void operator()() const { static_assert(!has_fetch_or<std::atomic_ref<T>>); }31};32 33template <typename T>34struct 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 57int 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