~drizzle-trunk/drizzle/development

1491.6.8 by Stewart Smith
add unit test for pthread based atomics. It fails. Why? because the implementation is broken.
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
#include <gtest/gtest.h>
24
25
# if defined(__SUNPRO_CC)
26
#  include <drizzled/atomic/sun_studio.h>
27
# endif
28
29
# if !defined(__ICC) && (defined(HAVE_GCC_ATOMIC_BUILTINS) || defined(__SUNPRO_CC))
30
#  include <drizzled/atomic/gcc_traits.h>
31
#  define ATOMIC_TRAITS internal::gcc_traits
32
# else  /* use pthread impl */
33
#  define ATOMIC_TRAITS internal::pthread_traits
34
# endif
35
36
#include <pthread.h>
37
#include <drizzled/atomic/pthread_traits.h>
38
39
#include <drizzled/atomics.h>
40
41
using namespace drizzled;
42
43
template<typename T>
44
struct atomic_pthread {
45
};
46
47
#   define __DRIZZLE_DECL_ATOMIC_PTHREAD(T)                              \
48
  template<> struct atomic_pthread<T>                                   \
49
  : internal::atomic_impl<T,T, internal::pthread_traits<T,T> > {         \
50
    atomic_pthread<T>()                                                 \
51
      : internal::atomic_impl<T,T, internal::pthread_traits<T,T> >() {}  \
52
    T operator=( T rhs ) { return store_with_release(rhs); }            \
53
  };
54
55
__DRIZZLE_DECL_ATOMIC_PTHREAD(unsigned int)
56
57
TEST(pthread_atomic_operations, fetch_and_store)
58
{
59
  atomic_pthread<uint32_t> u235;
60
61
  EXPECT_EQ(0, u235.fetch_and_store(1));
62
63
  u235.fetch_and_store(15);
64
65
  EXPECT_EQ(15, u235.fetch_and_store(100));
66
  EXPECT_EQ(100, u235);
67
}
68
69
TEST(pthread_atomic_operations, fetch_and_increment)
70
{
71
  atomic_pthread<uint32_t> u235;
72
73
  EXPECT_EQ(0, u235.fetch_and_increment());
74
  EXPECT_EQ(1, u235);
75
}
76
77
TEST(pthread_atomic_operations, fetch_and_add)
78
{
79
  atomic_pthread<uint32_t> u235;
80
81
  EXPECT_EQ(0, u235.fetch_and_add(2));
82
  EXPECT_EQ(2, u235);
83
}
84
85
TEST(pthread_atomic_operations, add_and_fetch)
86
{
87
  atomic_pthread<uint32_t> u235;
88
89
  EXPECT_EQ(10, u235.add_and_fetch(10));
90
  EXPECT_EQ(10, u235);
91
}
92
93
TEST(pthread_atomic_operations, fetch_and_decrement)
94
{
95
  atomic_pthread<uint32_t> u235;
96
97
  u235.fetch_and_store(15);
98
99
  EXPECT_EQ(15, u235.fetch_and_decrement());
100
  EXPECT_EQ(14, u235);
101
}
102
103
TEST(pthread_atomic_operations, compare_and_swap)
104
{
105
  atomic_pthread<uint32_t> u235;
106
107
  u235.fetch_and_store(100);
108
109
  EXPECT_EQ(false, u235.compare_and_swap(42, 200));
110
  EXPECT_EQ(true, u235.compare_and_swap(200, 100));
111
  EXPECT_EQ(200, u235);
112
}
113
114
TEST(pthread_atomic_operations, increment)
115
{
116
  atomic_pthread<uint32_t> u235;
117
  u235.fetch_and_store(200);
118
  EXPECT_EQ(201, u235.increment());
119
}
120
121
TEST(pthread_atomic_operations, decrement)
122
{
123
  atomic_pthread<uint32_t> u235;
124
  u235.fetch_and_store(200);
125
126
  EXPECT_EQ(199, u235.decrement());
127
}
128
129
// these don't build for whatever reason
130
//  EXPECT_EQ(242, u235+=42);
131
//  EXPECT_EQ(200, u235-=42);
132
133
134