]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/29_atomics/atomic_ref/pointer.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 29_atomics / atomic_ref / pointer.cc
CommitLineData
99dee823 1// Copyright (C) 2019-2021 Free Software Foundation, Inc.
a90fe12c
JW
2//
3// This file is part of the GNU ISO C++ Library. This library is free
4// software; you can redistribute it and/or modify it under the
5// terms of the GNU General Public License as published by the
6// Free Software Foundation; either version 3, or (at your option)
7// any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License along
15// with this library; see the file COPYING3. If not see
16// <http://www.gnu.org/licenses/>.
17
18// { dg-options "-std=gnu++2a" }
19// { dg-do run { target c++2a } }
20// { dg-add-options libatomic }
21
22#include <atomic>
23#include <testsuite_hooks.h>
24
25void
26test01()
27{
28 long arr[10] = { };
29 long* value;
30
31 {
32 const auto mo = std::memory_order_relaxed;
33 std::atomic_ref<long*> a(value);
34 bool ok = a.is_lock_free();
35 if constexpr (std::atomic_ref<long*>::is_always_lock_free)
36 VERIFY( ok );
37 a = arr;
38 VERIFY( a.load() == arr );
39 VERIFY( a.load(mo) == arr );
40 a.store(arr+1);
41 VERIFY( a.load() == arr+1 );
42 auto v = a.exchange(arr+2);
43 VERIFY( a == arr+2 );
44 VERIFY( v == arr+1 );
45 v = a.exchange(arr+3, mo);
46 VERIFY( a == arr+3 );
47 VERIFY( v == arr+2 );
48
49 auto expected = a.load();
50 while (!a.compare_exchange_weak(expected, arr+4, mo, mo))
51 { /* weak form can fail spuriously */ }
52 VERIFY( a.load() == arr+4 );
53 VERIFY( expected == arr+3 );
54 expected = arr+1;
55 ok = a.compare_exchange_weak(expected, arr+5, mo, mo);
56 VERIFY( !ok && a.load() == arr+4 && expected == arr+4 );
57 ok = a.compare_exchange_strong(expected, arr+5, mo, mo);
58 VERIFY( ok && a.load() == arr+5 && expected == arr+4 );
59 expected = nullptr;
60 ok = a.compare_exchange_strong(expected, arr+3, mo, mo);
61 VERIFY( !ok && a.load() == arr+5 && expected == arr+5 );
62
63 while (!a.compare_exchange_weak(expected, arr+4))
64 { /* weak form can fail spuriously */ }
65 VERIFY( a.load() == arr+4 && expected == arr+5 );
66 expected = a.load();
67 while (!a.compare_exchange_weak(expected, arr+6, mo))
68 { /* weak form can fail spuriously */ }
69 VERIFY( a.load() == arr+6 && expected == arr+4 );
70 expected = a.load() + 1;
71 ok = a.compare_exchange_weak(expected, arr+8);
72 VERIFY( !ok && a.load() == arr+6 && expected == arr+6 );
73 expected = a.load() + 1;
74 ok = a.compare_exchange_weak(expected, arr+8, mo);
75 VERIFY( !ok && a.load() == arr+6 && expected == arr+6 );
76
77 ok = a.compare_exchange_strong(expected, arr+5);
78 VERIFY( ok && a.load() == arr+5 && expected == arr+6 );
79 expected = a.load();
80 ok = a.compare_exchange_strong(expected, arr+7, mo);
81 VERIFY( ok && a.load() == arr+7 && expected == arr+5 );
82 expected = a.load() + 1;
83 ok = a.compare_exchange_strong(expected, arr+2);
84 VERIFY( !ok && a.load() == arr+7 && expected == arr+7 );
85 expected = a.load() + 1;
86 ok = a.compare_exchange_strong(expected, arr+2, mo);
87 VERIFY( !ok && a.load() == arr+7 && expected == arr+7 );
88
89 v = a.fetch_add(2);
90 VERIFY( v == arr+7 );
91 VERIFY( a == arr+9 );
92 v = a.fetch_add(-3, mo);
93 VERIFY( v == arr+9 );
94 VERIFY( a == arr+6 );
95
96 v = a.fetch_sub(3);
97 VERIFY( v == arr+6 );
98 VERIFY( a == arr+3 );
99 v = a.fetch_sub(2, mo);
100 VERIFY( v == arr+3 );
101 VERIFY( a == arr+1 );
102
103 v = a += 5;
104 VERIFY( v == arr+6 );
105 VERIFY( a == arr+6 );
106
107 v = a -= 5;
108 VERIFY( v == arr+1 );
109 VERIFY( a == arr+1 );
110 }
111
112 VERIFY( value == arr+1 );
113}
114
115void
116test02()
117{
118 char arr[10] = { };
119 char* value;
120
121 {
122 const auto mo = std::memory_order_relaxed;
123 std::atomic_ref<char*> a(value);
124 bool ok = a.is_lock_free();
125 if constexpr (std::atomic_ref<char*>::is_always_lock_free)
126 VERIFY( ok );
127 a = arr;
128 VERIFY( a.load() == arr );
129 a.store(arr+3);
130 VERIFY( a.load(mo) == arr+3 );
131 a.store(arr+1, mo);
132 VERIFY( a.load() == arr+1 );
133 auto v = a.exchange(arr+2);
134 VERIFY( a == arr+2 );
135 VERIFY( v == arr+1 );
136 v = a.exchange(arr+3, mo);
137 VERIFY( a == arr+3 );
138 VERIFY( v == arr+2 );
139
140 auto expected = a.load();
141 while (!a.compare_exchange_weak(expected, arr+4, mo, mo))
142 { /* weak form can fail spuriously */ }
143 VERIFY( a.load() == arr+4 );
144 VERIFY( expected == arr+3 );
145 expected = arr+1;
146 ok = a.compare_exchange_weak(expected, arr+5, mo, mo);
147 VERIFY( !ok && a.load() == arr+4 && expected == arr+4 );
148 ok = a.compare_exchange_strong(expected, arr+5, mo, mo);
149 VERIFY( ok && a.load() == arr+5 && expected == arr+4 );
150 expected = nullptr;
151 ok = a.compare_exchange_strong(expected, arr+3, mo, mo);
152 VERIFY( !ok && a.load() == arr+5 && expected == arr+5 );
153
154 while (!a.compare_exchange_weak(expected, arr+4))
155 { /* weak form can fail spuriously */ }
156 VERIFY( a.load() == arr+4 && expected == arr+5 );
157 expected = a.load();
158 while (!a.compare_exchange_weak(expected, arr+6, mo))
159 { /* weak form can fail spuriously */ }
160 VERIFY( a.load() == arr+6 && expected == arr+4 );
161 expected = a.load() + 1;
162 ok = a.compare_exchange_weak(expected, arr+8);
163 VERIFY( !ok && a.load() == arr+6 && expected == arr+6 );
164 expected = a.load() + 1;
165 ok = a.compare_exchange_weak(expected, arr+8, mo);
166 VERIFY( !ok && a.load() == arr+6 && expected == arr+6 );
167
168 ok = a.compare_exchange_strong(expected, arr+5);
169 VERIFY( ok && a.load() == arr+5 && expected == arr+6 );
170 expected = a.load();
171 ok = a.compare_exchange_strong(expected, arr+7, mo);
172 VERIFY( ok && a.load() == arr+7 && expected == arr+5 );
173 expected = a.load() + 1;
174 ok = a.compare_exchange_strong(expected, arr+2);
175 VERIFY( !ok && a.load() == arr+7 && expected == arr+7 );
176 expected = a.load() + 1;
177 ok = a.compare_exchange_strong(expected, arr+2, mo);
178 VERIFY( !ok && a.load() == arr+7 && expected == arr+7 );
179
180 v = a.fetch_add(2);
181 VERIFY( v == arr+7 );
182 VERIFY( a == arr+9 );
183 v = a.fetch_add(-3, mo);
184 VERIFY( v == arr+9 );
185 VERIFY( a == arr+6 );
186
187 v = a.fetch_sub(3);
188 VERIFY( v == arr+6 );
189 VERIFY( a == arr+3 );
190 v = a.fetch_sub(2, mo);
191 VERIFY( v == arr+3 );
192 VERIFY( a == arr+1 );
193
194 v = a += 5;
195 VERIFY( v == arr+6 );
196 VERIFY( a == arr+6 );
197
198 v = a -= 5;
199 VERIFY( v == arr+1 );
200 VERIFY( a == arr+1 );
201 }
202
203 VERIFY( value == arr+1 );
204}
205
206void
207test03()
208{
209 int i = 0;
210 int* ptr = 0;
211 std::atomic_ref<int*> a0(ptr);
212 std::atomic_ref<int*> a1(ptr);
213 std::atomic_ref<int*> a2(a0);
214 a0 = &i;
215 VERIFY( a1 == &i );
216 VERIFY( a2 == &i );
217}
218
219int
220main()
221{
222 test01();
223 test02();
224 test03();
225}