summaryrefslogtreecommitdiff
path: root/source/test/unittests/atomic.test.c
blob: 9f1a10b6a0382beb043270c5d89b463fe01cccaf (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
#include "../../kit/atomic.h"

#define KIT_TEST_FILE atomic
#include "../../kit_test/test.h"

TEST("atomic store and load") {
  ATOMIC(int) value;
  atomic_store_explicit(&value, 20, memory_order_relaxed);
  REQUIRE(atomic_load_explicit(&value, memory_order_relaxed) == 20);
}

TEST("atomic exchange") {
  ATOMIC(int) value;
  atomic_store_explicit(&value, 20, memory_order_relaxed);
  REQUIRE(atomic_exchange_explicit(&value, 42,
                                   memory_order_relaxed) == 20);
  REQUIRE(atomic_load_explicit(&value, memory_order_relaxed) == 42);
}

TEST("atomic fetch add") {
  ATOMIC(int) value;
  atomic_store_explicit(&value, 20, memory_order_relaxed);
  REQUIRE(atomic_fetch_add_explicit(&value, 22,
                                    memory_order_relaxed) == 20);
  REQUIRE(atomic_load_explicit(&value, memory_order_relaxed) == 42);
}