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
25
#include <gtest/gtest.h>
27
# if defined(__SUNPRO_CC)
28
# include <drizzled/atomic/sun_studio.h>
31
# if !defined(__ICC) && (defined(HAVE_GCC_ATOMIC_BUILTINS) || defined(__SUNPRO_CC))
32
# include <drizzled/atomic/gcc_traits.h>
33
# define ATOMIC_TRAITS internal::gcc_traits
34
# else /* use pthread impl */
35
# define ATOMIC_TRAITS internal::pthread_traits
39
#include <drizzled/atomic/pthread_traits.h>
41
#include <drizzled/atomics.h>
43
using namespace drizzled;
46
struct atomic_pthread {
49
# define __DRIZZLE_DECL_ATOMIC_PTHREAD(T) \
50
template<> struct atomic_pthread<T> \
51
: internal::atomic_impl<T,T, internal::pthread_traits<T,T> > { \
53
: internal::atomic_impl<T,T, internal::pthread_traits<T,T> >() {} \
54
T operator=( T rhs ) { return store_with_release(rhs); } \
57
__DRIZZLE_DECL_ATOMIC_PTHREAD(unsigned int)
59
TEST(pthread_atomic_operations, fetch_and_store)
61
atomic_pthread<uint32_t> u235;
63
EXPECT_EQ(0, u235.fetch_and_store(1));
65
u235.fetch_and_store(15);
67
EXPECT_EQ(15, u235.fetch_and_store(100));
71
TEST(pthread_atomic_operations, fetch_and_increment)
73
atomic_pthread<uint32_t> u235;
75
EXPECT_EQ(0, u235.fetch_and_increment());
79
TEST(pthread_atomic_operations, fetch_and_add)
81
atomic_pthread<uint32_t> u235;
83
EXPECT_EQ(0, u235.fetch_and_add(2));
87
TEST(pthread_atomic_operations, add_and_fetch)
89
atomic_pthread<uint32_t> u235;
91
EXPECT_EQ(10, u235.add_and_fetch(10));
95
TEST(pthread_atomic_operations, fetch_and_decrement)
97
atomic_pthread<uint32_t> u235;
99
u235.fetch_and_store(15);
101
EXPECT_EQ(15, u235.fetch_and_decrement());
105
TEST(pthread_atomic_operations, compare_and_swap)
107
atomic_pthread<uint32_t> u235;
109
u235.fetch_and_store(100);
111
EXPECT_EQ(false, u235.compare_and_swap(42, 200));
112
EXPECT_EQ(true, u235.compare_and_swap(200, 100));
113
EXPECT_EQ(200, u235);
116
TEST(pthread_atomic_operations, increment)
118
atomic_pthread<uint32_t> u235;
119
u235.fetch_and_store(200);
120
EXPECT_EQ(201, u235.increment());
123
TEST(pthread_atomic_operations, decrement)
125
atomic_pthread<uint32_t> u235;
126
u235.fetch_and_store(200);
128
EXPECT_EQ(199, u235.decrement());
131
// these don't build for whatever reason
132
// EXPECT_EQ(242, u235+=42);
133
// EXPECT_EQ(200, u235-=42);
137
int main(int argc, char **argv)
139
::testing::InitGoogleTest(&argc, argv);
140
return RUN_ALL_TESTS();