1
/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2010 Stewart Smith
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
#define BOOST_TEST_DYN_LINK
24
#include <boost/test/unit_test.hpp>
26
# if defined(__SUNPRO_CC)
27
# include <drizzled/atomic/sun_studio.h>
30
# if !defined(__ICC) && (defined(HAVE_GCC_ATOMIC_BUILTINS) || defined(__SUNPRO_CC))
31
# include <drizzled/atomic/gcc_traits.h>
32
# define ATOMIC_TRAITS internal::gcc_traits
33
# else /* use pthread impl */
34
# define ATOMIC_TRAITS internal::pthread_traits
38
#include <drizzled/atomic/pthread_traits.h>
40
#include <drizzled/atomics.h>
42
using namespace drizzled;
45
struct atomic_pthread {
48
# define __DRIZZLE_DECL_ATOMIC_PTHREAD(T) \
49
template<> struct atomic_pthread<T> \
50
: internal::atomic_impl<T,T, internal::pthread_traits<T,T> > { \
52
: internal::atomic_impl<T,T, internal::pthread_traits<T,T> >() {} \
53
T operator=( T rhs ) { return store_with_release(rhs); } \
56
__DRIZZLE_DECL_ATOMIC_PTHREAD(unsigned int)
58
BOOST_AUTO_TEST_SUITE(PthreadAtomicOperations)
59
BOOST_AUTO_TEST_CASE(fetch_and_store)
61
atomic_pthread<uint32_t> u235;
63
BOOST_REQUIRE_EQUAL(0, u235.fetch_and_store(1));
65
u235.fetch_and_store(15);
67
BOOST_REQUIRE_EQUAL(15, u235.fetch_and_store(100));
68
BOOST_REQUIRE_EQUAL(100, u235);
71
BOOST_AUTO_TEST_CASE(fetch_and_increment)
73
atomic_pthread<uint32_t> u235;
75
BOOST_REQUIRE_EQUAL(0, u235.fetch_and_increment());
76
BOOST_REQUIRE_EQUAL(1, u235);
79
BOOST_AUTO_TEST_CASE(fetch_and_add)
81
atomic_pthread<uint32_t> u235;
83
BOOST_REQUIRE_EQUAL(0, u235.fetch_and_add(2));
84
BOOST_REQUIRE_EQUAL(2, u235);
87
BOOST_AUTO_TEST_CASE(add_and_fetch)
89
atomic_pthread<uint32_t> u235;
91
BOOST_REQUIRE_EQUAL(10, u235.add_and_fetch(10));
92
BOOST_REQUIRE_EQUAL(10, u235);
95
BOOST_AUTO_TEST_CASE(fetch_and_decrement)
97
atomic_pthread<uint32_t> u235;
99
u235.fetch_and_store(15);
101
BOOST_REQUIRE_EQUAL(15, u235.fetch_and_decrement());
102
BOOST_REQUIRE_EQUAL(14, u235);
105
BOOST_AUTO_TEST_CASE(compare_and_swap)
107
atomic_pthread<uint32_t> u235;
109
u235.fetch_and_store(100);
111
BOOST_REQUIRE(not u235.compare_and_swap(42, 200));
112
BOOST_REQUIRE(u235.compare_and_swap(200, 100));
113
BOOST_REQUIRE_EQUAL(200, u235);
116
BOOST_AUTO_TEST_CASE(increment)
118
atomic_pthread<uint32_t> u235;
119
u235.fetch_and_store(200);
120
BOOST_REQUIRE_EQUAL(201, u235.increment());
123
BOOST_AUTO_TEST_CASE(decrement)
125
atomic_pthread<uint32_t> u235;
126
u235.fetch_and_store(200);
128
BOOST_REQUIRE_EQUAL(199, u235.decrement());
130
BOOST_AUTO_TEST_SUITE_END()