Skip to content

Commit 45d0e15

Browse files
committed
[libc++][atomic_ref] Add tests for atomic_ref
1 parent 78e1371 commit 45d0e15

34 files changed

+1985
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
// REQUIRES: has-unix-headers
8+
// UNSUPPORTED: c++03, c++11, c++14, c++17
9+
// UNSUPPORTED: libcpp-hardening-mode=none
10+
// XFAIL: availability-verbose_abort-missing
11+
// ADDITIONAL_COMPILE_FLAGS: -Wno-user-defined-warnings
12+
13+
// <atomic>
14+
15+
// bool compare_exchange_strong(T& expected, T desired, memory_order success, memory_order failure) const noexcept;
16+
//
17+
// Preconditions: failure is memory_order::relaxed, memory_order::consume, memory_order::acquire, or memory_order::seq_cst.
18+
19+
#include <atomic>
20+
21+
#include "check_assertion.h"
22+
23+
template <typename T>
24+
void test_compare_exchange_strong_invalid_memory_order() {
25+
{
26+
T x(T(1));
27+
std::atomic_ref<T> a(x);
28+
T t(T(2));
29+
a.compare_exchange_strong(t, T(3), std::memory_order_relaxed, std::memory_order_relaxed);
30+
}
31+
32+
TEST_LIBCPP_ASSERT_FAILURE(
33+
([] {
34+
T x(T(1));
35+
std::atomic_ref<T> a(x);
36+
T t(T(2));
37+
a.compare_exchange_strong(t, T(3), std::memory_order_relaxed, std::memory_order_release);
38+
}()),
39+
"memory order argument to strong atomic compare-and-exchange operation is invalid");
40+
41+
TEST_LIBCPP_ASSERT_FAILURE(
42+
([] {
43+
T x(T(1));
44+
std::atomic_ref<T> a(x);
45+
T t(T(2));
46+
a.compare_exchange_strong(t, T(3), std::memory_order_relaxed, std::memory_order_acq_rel);
47+
}()),
48+
"memory order argument to strong atomic compare-and-exchange operation is invalid");
49+
}
50+
51+
int main(int, char**) {
52+
test_compare_exchange_strong_invalid_memory_order<int>();
53+
test_compare_exchange_strong_invalid_memory_order<float>();
54+
test_compare_exchange_strong_invalid_memory_order<int*>();
55+
struct X {
56+
int i;
57+
X(int ii) noexcept : i(ii) {}
58+
bool operator==(X o) const { return i == o.i; }
59+
};
60+
test_compare_exchange_strong_invalid_memory_order<X>();
61+
62+
return 0;
63+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
// REQUIRES: has-unix-headers
8+
// UNSUPPORTED: c++03, c++11, c++14, c++17
9+
// UNSUPPORTED: libcpp-hardening-mode=none
10+
// XFAIL: availability-verbose_abort-missing
11+
// ADDITIONAL_COMPILE_FLAGS: -Wno-user-defined-warnings
12+
13+
// <atomic>
14+
15+
// bool compare_exchange_weak(T& expected, T desired, memory_order success, memory_order failure) const noexcept;
16+
//
17+
// Preconditions: failure is memory_order::relaxed, memory_order::consume, memory_order::acquire, or memory_order::seq_cst.
18+
19+
#include <atomic>
20+
21+
#include "check_assertion.h"
22+
23+
template <typename T>
24+
void test_compare_exchange_weak_invalid_memory_order() {
25+
{
26+
T x(T(1));
27+
std::atomic_ref<T> a(x);
28+
T t(T(2));
29+
a.compare_exchange_weak(t, T(3), std::memory_order_relaxed, std::memory_order_relaxed);
30+
}
31+
32+
TEST_LIBCPP_ASSERT_FAILURE(
33+
([] {
34+
T x(T(1));
35+
std::atomic_ref<T> a(x);
36+
T t(T(2));
37+
a.compare_exchange_weak(t, T(3), std::memory_order_relaxed, std::memory_order_release);
38+
}()),
39+
"memory order argument to weak atomic compare-and-exchange operation is invalid");
40+
41+
TEST_LIBCPP_ASSERT_FAILURE(
42+
([] {
43+
T x(T(1));
44+
std::atomic_ref<T> a(x);
45+
T t(T(2));
46+
a.compare_exchange_weak(t, T(3), std::memory_order_relaxed, std::memory_order_acq_rel);
47+
}()),
48+
"memory order argument to weak atomic compare-and-exchange operation is invalid");
49+
}
50+
51+
int main(int, char**) {
52+
test_compare_exchange_weak_invalid_memory_order<int>();
53+
test_compare_exchange_weak_invalid_memory_order<float>();
54+
test_compare_exchange_weak_invalid_memory_order<int*>();
55+
struct X {
56+
int i;
57+
X(int ii) noexcept : i(ii) {}
58+
bool operator==(X o) const { return i == o.i; }
59+
};
60+
test_compare_exchange_weak_invalid_memory_order<X>();
61+
62+
return 0;
63+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
// REQUIRES: has-unix-headers
8+
// UNSUPPORTED: c++03, c++11, c++14, c++17
9+
// UNSUPPORTED: libcpp-hardening-mode=none
10+
// XFAIL: availability-verbose_abort-missing
11+
12+
// <atomic>
13+
14+
// atomic_ref(T& obj);
15+
//
16+
// Preconditions: The referenced object is aligned to required_alignment.
17+
18+
#include <atomic>
19+
20+
#include "check_assertion.h"
21+
22+
int main(int, char**) {
23+
{
24+
char c[8];
25+
float* f = new (c) float(3.14f);
26+
[[maybe_unused]] std::atomic_ref<float> r(*f);
27+
}
28+
29+
TEST_LIBCPP_ASSERT_FAILURE(
30+
([] {
31+
char c[8];
32+
float* f = new (c + 1) float(3.14f);
33+
[[maybe_unused]] std::atomic_ref<float> r(*f);
34+
}()),
35+
"atomic_ref ctor: referenced object must be aligned to required_alignment");
36+
37+
return 0;
38+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
// REQUIRES: has-unix-headers
8+
// UNSUPPORTED: c++03, c++11, c++14, c++17
9+
// UNSUPPORTED: libcpp-hardening-mode=none
10+
// XFAIL: availability-verbose_abort-missing
11+
// ADDITIONAL_COMPILE_FLAGS: -Wno-user-defined-warnings
12+
13+
// <atomic>
14+
15+
// T load(memory_order order = memory_order::seq_cst) const noexcept;
16+
//
17+
// Preconditions: order is memory_order::relaxed, memory_order::consume, memory_order::acquire, or memory_order::seq_cst.
18+
19+
#include <atomic>
20+
21+
#include "check_assertion.h"
22+
23+
template <typename T>
24+
void test_load_invalid_memory_order() {
25+
{
26+
T x(T(1));
27+
std::atomic_ref<T> a(x);
28+
(void)a.load(std::memory_order_relaxed);
29+
}
30+
31+
TEST_LIBCPP_ASSERT_FAILURE(
32+
([] {
33+
T x(T(1));
34+
std::atomic_ref<T> a(x);
35+
(void)a.load(std::memory_order_release);
36+
}()),
37+
"memory order argument to atomic load operation is invalid");
38+
39+
TEST_LIBCPP_ASSERT_FAILURE(
40+
([] {
41+
T x(T(1));
42+
std::atomic_ref<T> a(x);
43+
(void)a.load(std::memory_order_acq_rel);
44+
}()),
45+
"memory order argument to atomic load operation is invalid");
46+
}
47+
48+
int main(int, char**) {
49+
test_load_invalid_memory_order<int>();
50+
test_load_invalid_memory_order<float>();
51+
test_load_invalid_memory_order<int*>();
52+
struct X {
53+
int i;
54+
X(int ii) noexcept : i(ii) {}
55+
bool operator==(X o) const { return i == o.i; }
56+
};
57+
test_load_invalid_memory_order<X>();
58+
59+
return 0;
60+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
// REQUIRES: has-unix-headers
8+
// UNSUPPORTED: c++03, c++11, c++14, c++17
9+
// UNSUPPORTED: libcpp-hardening-mode=none
10+
// XFAIL: availability-verbose_abort-missing
11+
// ADDITIONAL_COMPILE_FLAGS: -Wno-user-defined-warnings
12+
13+
// <atomic>
14+
15+
// void store(T desired, memory_order order = memory_order::seq_cst) const noexcept;
16+
//
17+
// Preconditions: order is memory_order::relaxed, memory_order::release, or memory_order::seq_cst.
18+
19+
#include <atomic>
20+
21+
#include "check_assertion.h"
22+
23+
template <typename T>
24+
void test_store_invalid_memory_order() {
25+
{
26+
T x(T(1));
27+
std::atomic_ref<T> a(x);
28+
a.store(T(2), std::memory_order_relaxed);
29+
}
30+
31+
TEST_LIBCPP_ASSERT_FAILURE(
32+
([] {
33+
T x(T(1));
34+
std::atomic_ref<T> a(x);
35+
a.store(T(2), std::memory_order_consume);
36+
}()),
37+
"memory order argument to atomic store operation is invalid");
38+
39+
TEST_LIBCPP_ASSERT_FAILURE(
40+
([] {
41+
T x(T(1));
42+
std::atomic_ref<T> a(x);
43+
a.store(T(2), std::memory_order_acquire);
44+
}()),
45+
"memory order argument to atomic store operation is invalid");
46+
47+
TEST_LIBCPP_ASSERT_FAILURE(
48+
([] {
49+
T x(T(1));
50+
std::atomic_ref<T> a(x);
51+
a.store(T(2), std::memory_order_acq_rel);
52+
}()),
53+
"memory order argument to atomic store operation is invalid");
54+
}
55+
56+
int main(int, char**) {
57+
test_store_invalid_memory_order<int>();
58+
test_store_invalid_memory_order<float>();
59+
test_store_invalid_memory_order<int*>();
60+
struct X {
61+
int i;
62+
X(int ii) noexcept : i(ii) {}
63+
bool operator==(X o) const { return i == o.i; }
64+
};
65+
test_store_invalid_memory_order<X>();
66+
67+
return 0;
68+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
// REQUIRES: has-unix-headers
8+
// UNSUPPORTED: c++03, c++11, c++14, c++17
9+
// UNSUPPORTED: libcpp-hardening-mode=none
10+
// XFAIL: availability-verbose_abort-missing
11+
// ADDITIONAL_COMPILE_FLAGS: -Wno-user-defined-warnings
12+
13+
// <atomic>
14+
15+
// void wait(T old, memory_order order = memory_order::seq_cst) const noexcept;
16+
//
17+
// Preconditions: order is memory_order::relaxed, memory_order::consume, memory_order::acquire, or memory_order::seq_cst.
18+
19+
#include <atomic>
20+
21+
#include "check_assertion.h"
22+
23+
template <typename T>
24+
void test_wait_invalid_memory_order() {
25+
{
26+
T x(T(1));
27+
std::atomic_ref<T> a(x);
28+
a.wait(T(2), std::memory_order_relaxed);
29+
}
30+
31+
TEST_LIBCPP_ASSERT_FAILURE(
32+
([] {
33+
T x(T(1));
34+
std::atomic_ref<T> a(x);
35+
a.wait(T(2), std::memory_order_release);
36+
}()),
37+
"memory order argument to atomic wait operation is invalid");
38+
39+
TEST_LIBCPP_ASSERT_FAILURE(
40+
([] {
41+
T x(T(1));
42+
std::atomic_ref<T> a(x);
43+
a.wait(T(2), std::memory_order_acq_rel);
44+
}()),
45+
"memory order argument to atomic wait operation is invalid");
46+
}
47+
48+
int main(int, char**) {
49+
test_wait_invalid_memory_order<int>();
50+
test_wait_invalid_memory_order<float>();
51+
test_wait_invalid_memory_order<int*>();
52+
struct X {
53+
int i;
54+
X(int ii) noexcept : i(ii) {}
55+
bool operator==(X o) const { return i == o.i; }
56+
};
57+
test_wait_invalid_memory_order<X>();
58+
59+
return 0;
60+
}

0 commit comments

Comments
 (0)