summaryrefslogtreecommitdiff
path: root/mfbt/tests/TestAtomics.cpp
blob: 113392f08b814c97447ff1e8e7ebedde8f78bfd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/Assertions.h"
#include "mozilla/Atomics.h"

#include <stdint.h>

using mozilla::Atomic;
using mozilla::MemoryOrdering;
using mozilla::Relaxed;
using mozilla::ReleaseAcquire;
using mozilla::SequentiallyConsistent;

#define A(a,b)  MOZ_RELEASE_ASSERT(a,b)

template <typename T, MemoryOrdering Order>
static void
TestTypeWithOrdering()
{
  Atomic<T, Order> atomic(5);
  A(atomic == 5, "Atomic variable did not initialize");

  // Test atomic increment
  A(++atomic == T(6), "Atomic increment did not work");
  A(atomic++ == T(6), "Atomic post-increment did not work");
  A(atomic == T(7), "Atomic post-increment did not work");

  // Test atomic decrement
  A(--atomic == 6, "Atomic decrement did not work");
  A(atomic-- == 6, "Atomic post-decrement did not work");
  A(atomic == 5, "Atomic post-decrement did not work");

  // Test other arithmetic.
  T result;
  result = (atomic += T(5));
  A(atomic == T(10), "Atomic += did not work");
  A(result == T(10), "Atomic += returned the wrong value");
  result = (atomic -= T(3));
  A(atomic == T(7), "Atomic -= did not work");
  A(result == T(7), "Atomic -= returned the wrong value");

  // Test assignment
  result = (atomic = T(5));
  A(atomic == T(5), "Atomic assignment failed");
  A(result == T(5), "Atomic assignment returned the wrong value");

  // Test logical operations.
  result = (atomic ^= T(2));
  A(atomic == T(7), "Atomic ^= did not work");
  A(result == T(7), "Atomic ^= returned the wrong value");
  result = (atomic ^= T(4));
  A(atomic == T(3), "Atomic ^= did not work");
  A(result == T(3), "Atomic ^= returned the wrong value");
  result = (atomic |= T(8));
  A(atomic == T(11), "Atomic |= did not work");
  A(result == T(11), "Atomic |= returned the wrong value");
  result = (atomic |= T(8));
  A(atomic == T(11), "Atomic |= did not work");
  A(result == T(11), "Atomic |= returned the wrong value");
  result = (atomic &= T(12));
  A(atomic == T(8), "Atomic &= did not work");
  A(result == T(8), "Atomic &= returned the wrong value");

  // Test exchange.
  atomic = T(30);
  result = atomic.exchange(42);
  A(atomic == T(42), "Atomic exchange did not work");
  A(result == T(30), "Atomic exchange returned the wrong value");

  // Test CAS.
  atomic = T(1);
  bool boolResult = atomic.compareExchange(0, 2);
  A(!boolResult, "CAS should have returned false.");
  A(atomic == T(1), "CAS shouldn't have done anything.");

  boolResult = atomic.compareExchange(1, 42);
  A(boolResult, "CAS should have succeeded.");
  A(atomic == T(42), "CAS should have changed atomic's value.");
}

template<typename T, MemoryOrdering Order>
static void
TestPointerWithOrdering()
{
  T array1[10];
  Atomic<T*, Order> atomic(array1);
  A(atomic == array1, "Atomic variable did not initialize");

  // Test atomic increment
  A(++atomic == array1 + 1, "Atomic increment did not work");
  A(atomic++ == array1 + 1, "Atomic post-increment did not work");
  A(atomic == array1 + 2, "Atomic post-increment did not work");

  // Test atomic decrement
  A(--atomic == array1 + 1, "Atomic decrement did not work");
  A(atomic-- == array1 + 1, "Atomic post-decrement did not work");
  A(atomic == array1, "Atomic post-decrement did not work");

  // Test other arithmetic operations
  T* result;
  result = (atomic += 2);
  A(atomic == array1 + 2, "Atomic += did not work");
  A(result == array1 + 2, "Atomic += returned the wrong value");
  result = (atomic -= 1);
  A(atomic == array1 + 1, "Atomic -= did not work");
  A(result == array1 + 1, "Atomic -= returned the wrong value");

  // Test stores
  result = (atomic = array1);
  A(atomic == array1, "Atomic assignment did not work");
  A(result == array1, "Atomic assignment returned the wrong value");

  // Test exchange
  atomic = array1 + 2;
  result = atomic.exchange(array1);
  A(atomic == array1, "Atomic exchange did not work");
  A(result == array1 + 2, "Atomic exchange returned the wrong value");

  atomic = array1;
  bool boolResult = atomic.compareExchange(array1 + 1, array1 + 2);
  A(!boolResult, "CAS should have returned false.");
  A(atomic == array1, "CAS shouldn't have done anything.");

  boolResult = atomic.compareExchange(array1, array1 + 3);
  A(boolResult, "CAS should have succeeded.");
  A(atomic == array1 + 3, "CAS should have changed atomic's value.");
}

enum EnumType
{
  EnumType_0 = 0,
  EnumType_1 = 1,
  EnumType_2 = 2,
  EnumType_3 = 3
};

template<MemoryOrdering Order>
static void
TestEnumWithOrdering()
{
  Atomic<EnumType, Order> atomic(EnumType_2);
  A(atomic == EnumType_2, "Atomic variable did not initialize");

  // Test assignment
  EnumType result;
  result = (atomic = EnumType_3);
  A(atomic == EnumType_3, "Atomic assignment failed");
  A(result == EnumType_3, "Atomic assignment returned the wrong value");

  // Test exchange.
  atomic = EnumType_1;
  result = atomic.exchange(EnumType_2);
  A(atomic == EnumType_2, "Atomic exchange did not work");
  A(result == EnumType_1, "Atomic exchange returned the wrong value");

  // Test CAS.
  atomic = EnumType_1;
  bool boolResult = atomic.compareExchange(EnumType_0, EnumType_2);
  A(!boolResult, "CAS should have returned false.");
  A(atomic == EnumType_1, "CAS shouldn't have done anything.");

  boolResult = atomic.compareExchange(EnumType_1, EnumType_3);
  A(boolResult, "CAS should have succeeded.");
  A(atomic == EnumType_3, "CAS should have changed atomic's value.");
}

enum class EnumClass : uint32_t
{
  Value0 = 0,
  Value1 = 1,
  Value2 = 2,
  Value3 = 3
};

template<MemoryOrdering Order>
static void
TestEnumClassWithOrdering()
{
  Atomic<EnumClass, Order> atomic(EnumClass::Value2);
  A(atomic == EnumClass::Value2, "Atomic variable did not initialize");

  // Test assignment
  EnumClass result;
  result = (atomic = EnumClass::Value3);
  A(atomic == EnumClass::Value3, "Atomic assignment failed");
  A(result == EnumClass::Value3, "Atomic assignment returned the wrong value");

  // Test exchange.
  atomic = EnumClass::Value1;
  result = atomic.exchange(EnumClass::Value2);
  A(atomic == EnumClass::Value2, "Atomic exchange did not work");
  A(result == EnumClass::Value1, "Atomic exchange returned the wrong value");

  // Test CAS.
  atomic = EnumClass::Value1;
  bool boolResult = atomic.compareExchange(EnumClass::Value0, EnumClass::Value2);
  A(!boolResult, "CAS should have returned false.");
  A(atomic == EnumClass::Value1, "CAS shouldn't have done anything.");

  boolResult = atomic.compareExchange(EnumClass::Value1, EnumClass::Value3);
  A(boolResult, "CAS should have succeeded.");
  A(atomic == EnumClass::Value3, "CAS should have changed atomic's value.");
}

template <MemoryOrdering Order>
static void
TestBoolWithOrdering()
{
  Atomic<bool, Order> atomic(false);
  A(atomic == false, "Atomic variable did not initialize");

  // Test assignment
  bool result;
  result = (atomic = true);
  A(atomic == true, "Atomic assignment failed");
  A(result == true, "Atomic assignment returned the wrong value");

  // Test exchange.
  atomic = false;
  result = atomic.exchange(true);
  A(atomic == true, "Atomic exchange did not work");
  A(result == false, "Atomic exchange returned the wrong value");

  // Test CAS.
  atomic = false;
  bool boolResult = atomic.compareExchange(true, false);
  A(!boolResult, "CAS should have returned false.");
  A(atomic == false, "CAS shouldn't have done anything.");

  boolResult = atomic.compareExchange(false, true);
  A(boolResult, "CAS should have succeeded.");
  A(atomic == true, "CAS should have changed atomic's value.");
}

template <typename T>
static void
TestType()
{
  TestTypeWithOrdering<T, SequentiallyConsistent>();
  TestTypeWithOrdering<T, ReleaseAcquire>();
  TestTypeWithOrdering<T, Relaxed>();
}

template<typename T>
static void
TestPointer()
{
  TestPointerWithOrdering<T, SequentiallyConsistent>();
  TestPointerWithOrdering<T, ReleaseAcquire>();
  TestPointerWithOrdering<T, Relaxed>();
}

static void
TestEnum()
{
  TestEnumWithOrdering<SequentiallyConsistent>();
  TestEnumWithOrdering<ReleaseAcquire>();
  TestEnumWithOrdering<Relaxed>();

  TestEnumClassWithOrdering<SequentiallyConsistent>();
  TestEnumClassWithOrdering<ReleaseAcquire>();
  TestEnumClassWithOrdering<Relaxed>();
}

static void
TestBool()
{
  TestBoolWithOrdering<SequentiallyConsistent>();
  TestBoolWithOrdering<ReleaseAcquire>();
  TestBoolWithOrdering<Relaxed>();
}

#undef A

int
main()
{
  TestType<uint32_t>();
  TestType<int32_t>();
  TestType<uint64_t>();
  TestType<int64_t>();
  TestType<intptr_t>();
  TestType<uintptr_t>();
  TestPointer<int>();
  TestPointer<float>();
  TestPointer<uint16_t*>();
  TestPointer<uint32_t*>();
  TestEnum();
  TestBool();
  return 0;
}