Debian Codegraph @

libcxx/test/std/atomics/atomics.ref/increment_decrement.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 operator++(int) const noexcept;
12 // integral-type operator--(int) const noexcept;
13 // integral-type operator++() const noexcept;
14 // integral-type operator--() const noexcept;
15
16 #include <atomic>
17 #include <cassert>
18 #include <concepts>
19 #include <type_traits>
20
21 #include "atomic_helpers.h"
22 #include "test_macros.h"
23
24 template <typename T>
25 concept has_pre_increment_operator = requires { ++std::declval<T const>(); };
26
27 template <typename T>
28 concept has_post_increment_operator = requires { std::declval<T const>()++; };
29
30 template <typename T>
31 concept has_pre_decrement_operator = requires { --std::declval<T const>(); };
32
33 template <typename T>
34 concept has_post_decrement_operator = requires { std::declval<T const>()--; };
35
36 template <typename T>
37 constexpr bool does_not_have_increment_nor_decrement_operators() {
38 return !has_pre_increment_operator<T> && !has_pre_decrement_operator<T> && !has_post_increment_operator<T> &&
39 !has_post_decrement_operator<T>;
40 }
41
42 template <typename T>
43 struct TestDoesNotHaveIncrementDecrement {
44 void operator()() const { static_assert(does_not_have_increment_nor_decrement_operators<T>()); }
45 };
46
47 template <typename T>
48 struct TestIncrementDecrement {
49 void operator()() const {
50 static_assert(std::is_integral_v<T>);
51
52 T x(T(1));
53 std::atomic_ref<T> const a(x);
54
55 {
56 std::same_as<T> decltype(auto) y = ++a;
57 assert(y == T(2));
58 assert(x == T(2));
59 ASSERT_NOEXCEPT(++a);
60 }
61
62 {
63 std::same_as<T> decltype(auto) y = --a;
64 assert(y == T(1));
65 assert(x == T(1));
66 ASSERT_NOEXCEPT(--a);
67 }
68
69 {
70 std::same_as<T> decltype(auto) y = a++;
71 assert(y == T(1));
72 assert(x == T(2));
73 ASSERT_NOEXCEPT(a++);
74 }
75
76 {
77 std::same_as<T> decltype(auto) y = a--;
78 assert(y == T(2));
79 assert(x == T(1));
80 ASSERT_NOEXCEPT(a--);
81 }
82 }
83 };
84
85 int main(int, char**) {
86 TestEachIntegralType<TestIncrementDecrement>()();
87
88 TestEachFloatingPointType<TestDoesNotHaveIncrementDecrement>()();
89
90 TestEachPointerType<TestDoesNotHaveIncrementDecrement>()();
91
92 TestDoesNotHaveIncrementDecrement<bool>()();
93 TestDoesNotHaveIncrementDecrement<UserAtomicType>()();
94 TestDoesNotHaveIncrementDecrement<LargeUserAtomicType>()();
95
96 return 0;
97 }