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 | |
11 | namespace dart { |
12 | |
13 | VM_UNIT_TEST_CASE(FetchAndIncrement) { |
14 | RelaxedAtomic<uintptr_t> v = 42; |
15 | EXPECT_EQ(static_cast<uintptr_t>(42), v.fetch_add(1)); |
16 | EXPECT_EQ(static_cast<uintptr_t>(43), v); |
17 | } |
18 | |
19 | VM_UNIT_TEST_CASE(FetchAndDecrement) { |
20 | RelaxedAtomic<uintptr_t> v = 42; |
21 | EXPECT_EQ(static_cast<uintptr_t>(42), v.fetch_sub(1)); |
22 | EXPECT_EQ(static_cast<uintptr_t>(41), v); |
23 | } |
24 | |
25 | VM_UNIT_TEST_CASE(FetchAndIncrementSigned) { |
26 | RelaxedAtomic<intptr_t> v = -42; |
27 | EXPECT_EQ(static_cast<intptr_t>(-42), v.fetch_add(1)); |
28 | EXPECT_EQ(static_cast<intptr_t>(-41), v); |
29 | } |
30 | |
31 | VM_UNIT_TEST_CASE(FetchAndDecrementSigned) { |
32 | RelaxedAtomic<intptr_t> v = -42; |
33 | EXPECT_EQ(static_cast<intptr_t>(-42), v.fetch_sub(1)); |
34 | EXPECT_EQ(static_cast<intptr_t>(-43), v); |
35 | } |
36 | |
37 | VM_UNIT_TEST_CASE(IncrementBy) { |
38 | RelaxedAtomic<intptr_t> v = 42; |
39 | v.fetch_add(100); |
40 | EXPECT_EQ(static_cast<intptr_t>(142), v); |
41 | } |
42 | |
43 | VM_UNIT_TEST_CASE(DecrementBy) { |
44 | RelaxedAtomic<intptr_t> v = 42; |
45 | v.fetch_sub(41); |
46 | EXPECT_EQ(static_cast<intptr_t>(1), v); |
47 | } |
48 | |
49 | VM_UNIT_TEST_CASE(FetchOrRelaxed) { |
50 | RelaxedAtomic<uint32_t> v = 42; |
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 | |
56 | VM_UNIT_TEST_CASE(FetchAndRelaxed) { |
57 | RelaxedAtomic<uint32_t> v = 42; |
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 | |
63 | VM_UNIT_TEST_CASE(LoadRelaxed) { |
64 | RelaxedAtomic<uword> v = 42; |
65 | EXPECT_EQ(static_cast<uword>(42), v.load()); |
66 | } |
67 | |
68 | TEST_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 | |
82 | TEST_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 |
97 | |