~skinny.moey/drizzle/inno_replication_order

« back to all changes in this revision

Viewing changes to unittests/pthread_atomics_test.cc

  • Committer: Lee Bieber
  • Date: 2011-01-24 17:31:38 UTC
  • mfrom: (2108.1.3 build)
  • Revision ID: kalebral@gmail.com-20110124173138-s4k76qrhuewswttj
Merge Lee - 705699: rabbitmq.variables test blocks asking for sudo password
Merge Andrew - fix bug 703913: some support-files should be dropped
Merge Andrew - fix bug 619992: fix disabled unittests 
Merge Andrew - fix bug #667162: port unittests to boost::test 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2010 Stewart Smith
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
 
 
23
#define BOOST_TEST_DYN_LINK
 
24
#include <boost/test/unit_test.hpp>
 
25
 
 
26
# if defined(__SUNPRO_CC)
 
27
#  include <drizzled/atomic/sun_studio.h>
 
28
# endif
 
29
 
 
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
 
35
# endif
 
36
 
 
37
#include <pthread.h>
 
38
#include <drizzled/atomic/pthread_traits.h>
 
39
 
 
40
#include <drizzled/atomics.h>
 
41
 
 
42
using namespace drizzled;
 
43
 
 
44
template<typename T>
 
45
struct atomic_pthread {
 
46
};
 
47
 
 
48
#   define __DRIZZLE_DECL_ATOMIC_PTHREAD(T)                              \
 
49
  template<> struct atomic_pthread<T>                                   \
 
50
  : internal::atomic_impl<T,T, internal::pthread_traits<T,T> > {         \
 
51
    atomic_pthread<T>()                                                 \
 
52
      : internal::atomic_impl<T,T, internal::pthread_traits<T,T> >() {}  \
 
53
    T operator=( T rhs ) { return store_with_release(rhs); }            \
 
54
  };
 
55
 
 
56
__DRIZZLE_DECL_ATOMIC_PTHREAD(unsigned int)
 
57
 
 
58
BOOST_AUTO_TEST_SUITE(PthreadAtomicOperations)
 
59
BOOST_AUTO_TEST_CASE(fetch_and_store)
 
60
{
 
61
  atomic_pthread<uint32_t> u235;
 
62
 
 
63
  BOOST_REQUIRE_EQUAL(0, u235.fetch_and_store(1));
 
64
 
 
65
  u235.fetch_and_store(15);
 
66
 
 
67
  BOOST_REQUIRE_EQUAL(15, u235.fetch_and_store(100));
 
68
  BOOST_REQUIRE_EQUAL(100, u235);
 
69
}
 
70
 
 
71
BOOST_AUTO_TEST_CASE(fetch_and_increment)
 
72
{
 
73
  atomic_pthread<uint32_t> u235;
 
74
 
 
75
  BOOST_REQUIRE_EQUAL(0, u235.fetch_and_increment());
 
76
  BOOST_REQUIRE_EQUAL(1, u235);
 
77
}
 
78
 
 
79
BOOST_AUTO_TEST_CASE(fetch_and_add)
 
80
{
 
81
  atomic_pthread<uint32_t> u235;
 
82
 
 
83
  BOOST_REQUIRE_EQUAL(0, u235.fetch_and_add(2));
 
84
  BOOST_REQUIRE_EQUAL(2, u235);
 
85
}
 
86
 
 
87
BOOST_AUTO_TEST_CASE(add_and_fetch)
 
88
{
 
89
  atomic_pthread<uint32_t> u235;
 
90
 
 
91
  BOOST_REQUIRE_EQUAL(10, u235.add_and_fetch(10));
 
92
  BOOST_REQUIRE_EQUAL(10, u235);
 
93
}
 
94
 
 
95
BOOST_AUTO_TEST_CASE(fetch_and_decrement)
 
96
{
 
97
  atomic_pthread<uint32_t> u235;
 
98
 
 
99
  u235.fetch_and_store(15);
 
100
 
 
101
  BOOST_REQUIRE_EQUAL(15, u235.fetch_and_decrement());
 
102
  BOOST_REQUIRE_EQUAL(14, u235);
 
103
}
 
104
 
 
105
BOOST_AUTO_TEST_CASE(compare_and_swap)
 
106
{
 
107
  atomic_pthread<uint32_t> u235;
 
108
 
 
109
  u235.fetch_and_store(100);
 
110
 
 
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);
 
114
}
 
115
 
 
116
BOOST_AUTO_TEST_CASE(increment)
 
117
{
 
118
  atomic_pthread<uint32_t> u235;
 
119
  u235.fetch_and_store(200);
 
120
  BOOST_REQUIRE_EQUAL(201, u235.increment());
 
121
}
 
122
 
 
123
BOOST_AUTO_TEST_CASE(decrement)
 
124
{
 
125
  atomic_pthread<uint32_t> u235;
 
126
  u235.fetch_and_store(200);
 
127
 
 
128
  BOOST_REQUIRE_EQUAL(199, u235.decrement());
 
129
}
 
130
BOOST_AUTO_TEST_SUITE_END()