Flutter Engine
The Flutter Engine
atomic_test.cc
Go to the documentation of this file.
1// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "platform/atomic.h"
6#include "platform/assert.h"
7#include "platform/utils.h"
8#include "vm/globals.h"
9#include "vm/unit_test.h"
10
11namespace dart {
12
13VM_UNIT_TEST_CASE(FetchAndIncrement) {
15 EXPECT_EQ(static_cast<uintptr_t>(42), v.fetch_add(1));
16 EXPECT_EQ(static_cast<uintptr_t>(43), v);
17}
18
19VM_UNIT_TEST_CASE(FetchAndDecrement) {
21 EXPECT_EQ(static_cast<uintptr_t>(42), v.fetch_sub(1));
22 EXPECT_EQ(static_cast<uintptr_t>(41), v);
23}
24
25VM_UNIT_TEST_CASE(FetchAndIncrementSigned) {
27 EXPECT_EQ(static_cast<intptr_t>(-42), v.fetch_add(1));
28 EXPECT_EQ(static_cast<intptr_t>(-41), v);
29}
30
31VM_UNIT_TEST_CASE(FetchAndDecrementSigned) {
33 EXPECT_EQ(static_cast<intptr_t>(-42), v.fetch_sub(1));
34 EXPECT_EQ(static_cast<intptr_t>(-43), v);
35}
36
37VM_UNIT_TEST_CASE(IncrementBy) {
39 v.fetch_add(100);
40 EXPECT_EQ(static_cast<intptr_t>(142), v);
41}
42
43VM_UNIT_TEST_CASE(DecrementBy) {
45 v.fetch_sub(41);
46 EXPECT_EQ(static_cast<intptr_t>(1), v);
47}
48
49VM_UNIT_TEST_CASE(FetchOrRelaxed) {
51 uint32_t previous = v.fetch_or(3);
52 EXPECT_EQ(static_cast<uint32_t>(42), previous);
53 EXPECT_EQ(static_cast<uint32_t>(43), v);
54}
55
56VM_UNIT_TEST_CASE(FetchAndRelaxed) {
58 uint32_t previous = v.fetch_and(3);
59 EXPECT_EQ(static_cast<uint32_t>(42), previous);
60 EXPECT_EQ(static_cast<uint32_t>(2), v);
61}
62
65 EXPECT_EQ(static_cast<uword>(42), v.load());
66}
67
68TEST_CASE(CompareAndSwapWord) {
69 uword old_value = 42;
70 RelaxedAtomic<uword> variable = {old_value};
71 uword new_value = 100;
72 bool success = variable.compare_exchange_strong(old_value, new_value);
73 EXPECT_EQ(true, success);
74 EXPECT_EQ(static_cast<uword>(42), old_value);
75
76 old_value = 50;
77 success = variable.compare_exchange_strong(old_value, new_value);
78 EXPECT_EQ(false, success);
79 EXPECT_EQ(static_cast<uword>(100), old_value);
80}
81
82TEST_CASE(CompareAndSwapUint32) {
83 uint32_t old_value = 42;
84 RelaxedAtomic<uint32_t> variable = {old_value};
85 uint32_t new_value = 100;
86 bool success = variable.compare_exchange_strong(old_value, new_value);
87 EXPECT_EQ(true, success);
88 EXPECT_EQ(static_cast<uword>(42), old_value);
89
90 old_value = 50;
91 success = variable.compare_exchange_strong(old_value, new_value);
92 EXPECT_EQ(false, success);
93 EXPECT_EQ(static_cast<uword>(100), old_value);
94}
95
96} // namespace dart
T load(std::memory_order order=std::memory_order_relaxed) const
Definition: atomic.h:21
bool compare_exchange_strong(T &expected, T desired, std::memory_order order=std::memory_order_relaxed)
Definition: atomic.h:64
T fetch_add(T arg, std::memory_order order=std::memory_order_relaxed)
Definition: atomic.h:35
T fetch_and(T arg, std::memory_order order=std::memory_order_relaxed)
Definition: atomic.h:44
T fetch_or(T arg, std::memory_order order=std::memory_order_relaxed)
Definition: atomic.h:41
T fetch_sub(T arg, std::memory_order order=std::memory_order_relaxed)
Definition: atomic.h:38
Definition: dart_vm.cc:33
static T LoadRelaxed(const T *ptr)
Definition: atomic.h:150
uintptr_t uword
Definition: globals.h:501
TEST_CASE(DirectoryCurrent)
VM_UNIT_TEST_CASE(DirectoryCurrentNoScope)