21
21
#include "config.h"
23
#include <gtest/gtest.h>
22
#define BOOST_TEST_DYN_LINK
23
#include <boost/test/unit_test.hpp>
25
25
#include <drizzled/atomics.h>
27
27
using namespace drizzled;
29
TEST(atomic_operations, fetch_and_store)
31
atomic<uint32_t> u235;
33
EXPECT_EQ(0, u235.fetch_and_store(1));
35
u235.fetch_and_store(15);
37
EXPECT_EQ(15, u235.fetch_and_store(100));
41
TEST(atomic_operations, fetch_and_increment)
43
atomic<uint32_t> u235;
45
EXPECT_EQ(0, u235.fetch_and_increment());
49
TEST(atomic_operations, fetch_and_add)
51
atomic<uint32_t> u235;
53
EXPECT_EQ(0, u235.fetch_and_add(2));
57
TEST(atomic_operations, add_and_fetch)
59
atomic<uint32_t> u235;
61
EXPECT_EQ(10, u235.add_and_fetch(10));
65
TEST(atomic_operations, fetch_and_decrement)
67
atomic<uint32_t> u235;
69
u235.fetch_and_store(15);
71
EXPECT_EQ(15, u235.fetch_and_decrement());
75
TEST(atomic_operations, compare_and_swap)
29
BOOST_AUTO_TEST_SUITE(AtomicOperations)
30
BOOST_AUTO_TEST_CASE(fetch_and_store)
32
atomic<uint32_t> u235;
34
BOOST_REQUIRE_EQUAL(0, u235.fetch_and_store(1));
36
u235.fetch_and_store(15);
38
BOOST_REQUIRE_EQUAL(15, u235.fetch_and_store(100));
39
BOOST_REQUIRE_EQUAL(100, u235);
42
BOOST_AUTO_TEST_CASE(fetch_and_increment)
44
atomic<uint32_t> u235;
46
BOOST_REQUIRE_EQUAL(0, u235.fetch_and_increment());
47
BOOST_REQUIRE_EQUAL(1, u235);
50
BOOST_AUTO_TEST_CASE(fetch_and_add)
52
atomic<uint32_t> u235;
54
BOOST_REQUIRE_EQUAL(0, u235.fetch_and_add(2));
55
BOOST_REQUIRE_EQUAL(2, u235);
58
BOOST_AUTO_TEST_CASE(add_and_fetch)
60
atomic<uint32_t> u235;
62
BOOST_REQUIRE_EQUAL(10, u235.add_and_fetch(10));
63
BOOST_REQUIRE_EQUAL(10, u235);
66
BOOST_AUTO_TEST_CASE(fetch_and_decrement)
68
atomic<uint32_t> u235;
70
u235.fetch_and_store(15);
72
BOOST_REQUIRE_EQUAL(15, u235.fetch_and_decrement());
73
BOOST_REQUIRE_EQUAL(14, u235);
76
BOOST_AUTO_TEST_CASE(compare_and_swap)
77
78
atomic<uint32_t> u235;
79
80
u235.fetch_and_store(100);
81
ASSERT_FALSE(u235.compare_and_swap(42, 200));
82
ASSERT_TRUE(u235.compare_and_swap(200, 100));
86
TEST(atomic_operations, increment)
88
atomic<uint32_t> u235;
89
u235.fetch_and_store(200);
90
EXPECT_EQ(201, u235.increment());
93
TEST(atomic_operations, decrement)
95
atomic<uint32_t> u235;
96
u235.fetch_and_store(200);
98
EXPECT_EQ(199, u235.decrement());
102
TEST(atomic_operations, increment_assign)
104
atomic<uint32_t> u235;
105
u235.fetch_and_store(200);
107
EXPECT_EQ(242, u235+=42);
110
TEST(atomic_operations, decrement_assign)
112
atomic<uint32_t> u235;
113
u235.fetch_and_store(200);
115
EXPECT_EQ(158, u235-=42);
82
BOOST_REQUIRE(not u235.compare_and_swap(42, 200));
83
BOOST_REQUIRE(u235.compare_and_swap(200, 100));
84
BOOST_REQUIRE_EQUAL(200, u235);
87
BOOST_AUTO_TEST_CASE(increment)
89
atomic<uint32_t> u235;
90
u235.fetch_and_store(200);
91
BOOST_REQUIRE_EQUAL(201, u235.increment());
94
BOOST_AUTO_TEST_CASE(decrement)
96
atomic<uint32_t> u235;
97
u235.fetch_and_store(200);
99
BOOST_REQUIRE_EQUAL(199, u235.decrement());
102
BOOST_AUTO_TEST_SUITE_END()
104
/* TODO: add tests for += and -= when supported */