Skip to content

Commit 19557a4

Browse files
authored
[libc++] Fix bug in tests for std::atomic_ref<T*> increment and decrement operators (#122271)
The implementation is fine and has the proper increment/decrement operators defined, but the tests were wrong: - a typo (`T` instead of `std::atomic_ref<T>`) when ensuring that increment/decrement operators are not defined in the primary template and specialization for floating point types, and - the specialization for pointer types was miscategorized.
1 parent 9248428 commit 19557a4

File tree

1 file changed

+68
-33
lines changed

1 file changed

+68
-33
lines changed

libcxx/test/std/atomics/atomics.ref/increment_decrement.pass.cpp

Lines changed: 68 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,43 +42,78 @@ constexpr bool does_not_have_increment_nor_decrement_operators() {
4242

4343
template <typename T>
4444
struct TestDoesNotHaveIncrementDecrement {
45-
void operator()() const { static_assert(does_not_have_increment_nor_decrement_operators<T>()); }
45+
void operator()() const { static_assert(does_not_have_increment_nor_decrement_operators<std::atomic_ref<T>>()); }
4646
};
4747

4848
template <typename T>
4949
struct TestIncrementDecrement {
5050
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--);
51+
if constexpr (std::is_integral_v<T>) {
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+
} else if constexpr (std::is_pointer_v<T>) {
83+
using U = std::remove_pointer_t<T>;
84+
U t[9] = {};
85+
T p{&t[1]};
86+
std::atomic_ref<T> const a(p);
87+
88+
{
89+
std::same_as<T> decltype(auto) y = ++a;
90+
assert(y == &t[2]);
91+
assert(p == &t[2]);
92+
ASSERT_NOEXCEPT(++a);
93+
}
94+
95+
{
96+
std::same_as<T> decltype(auto) y = --a;
97+
assert(y == &t[1]);
98+
assert(p == &t[1]);
99+
ASSERT_NOEXCEPT(--a);
100+
}
101+
102+
{
103+
std::same_as<T> decltype(auto) y = a++;
104+
assert(y == &t[1]);
105+
assert(p == &t[2]);
106+
ASSERT_NOEXCEPT(a++);
107+
}
108+
109+
{
110+
std::same_as<T> decltype(auto) y = a--;
111+
assert(y == &t[2]);
112+
assert(p == &t[1]);
113+
ASSERT_NOEXCEPT(a--);
114+
}
115+
} else {
116+
static_assert(std::is_void_v<T>);
82117
}
83118
}
84119
};
@@ -88,7 +123,7 @@ int main(int, char**) {
88123

89124
TestEachFloatingPointType<TestDoesNotHaveIncrementDecrement>()();
90125

91-
TestEachPointerType<TestDoesNotHaveIncrementDecrement>()();
126+
TestEachPointerType<TestIncrementDecrement>()();
92127

93128
TestDoesNotHaveIncrementDecrement<bool>()();
94129
TestDoesNotHaveIncrementDecrement<UserAtomicType>()();

0 commit comments

Comments
 (0)