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