libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
atomic_counter.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2011 by J.M.McGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 #include "stdafx.h"
20 
21 #define BOOST_TEST_MODULE libjmmcg_tests
22 #include <boost/test/included/unit_test.hpp>
23 
24 #include <boost/mpl/list.hpp>
25 
26 #include "core/thread_api_traits.hpp"
27 
28 using namespace libjmmcg;
29 using namespace ppd;
30 
31 typedef boost::mpl::list<
32  api_lock_traits<platform_api, sequential_mode>::atomic_counter_type<long>,
33  api_lock_traits<platform_api, heavyweight_threading>::atomic_counter_type<long>,
34  api_lock_traits<platform_api, heavyweight_threading>::atomic_counter_type<double>
35 > ctr_types;
36 
37 BOOST_AUTO_TEST_SUITE(atomic_counter_tests)
38 
39 BOOST_AUTO_TEST_CASE_TEMPLATE(default_ctor, ctr_t, ctr_types)
40 {
41  ctr_t a;
42  BOOST_CHECK_EQUAL(a.get(), 0);
43  BOOST_CHECK(!a);
44  BOOST_CHECK(a==0);
45  BOOST_CHECK(a!=1);
46  BOOST_CHECK(a<1);
47  BOOST_CHECK(a<=1);
48  BOOST_CHECK(a<=0);
49  BOOST_CHECK(a>-1);
50  BOOST_CHECK(a>=-1);
51  BOOST_CHECK(a>=0);
52 }
53 
54 BOOST_AUTO_TEST_CASE_TEMPLATE(ctor, ctr_t, ctr_types)
55 {
56  ctr_t a(1);
57  BOOST_CHECK_EQUAL(a.get(), 1);
58 }
59 
60 BOOST_AUTO_TEST_CASE_TEMPLATE(cctor, ctr_t, ctr_types)
61 {
62  ctr_t a(1);
63  ctr_t b(a);
64  BOOST_CHECK_EQUAL(a.get(), b.get());
65  BOOST_CHECK(a==b);
66  BOOST_CHECK(!(a!=b));
67  BOOST_CHECK(!(a<b));
68  BOOST_CHECK(a<=b);
69  BOOST_CHECK(!(a>b));
70  BOOST_CHECK(a>=b);
71 }
72 
73 BOOST_AUTO_TEST_CASE_TEMPLATE(assign, ctr_t, ctr_types)
74 {
75  ctr_t a(1), b;
76  b=a;
77  BOOST_CHECK_EQUAL(a.get(), b.get());
78  BOOST_CHECK(a==b);
79 }
80 
81 BOOST_AUTO_TEST_CASE_TEMPLATE(increment, ctr_t, ctr_types)
82 {
83  ctr_t a(1);
84  ++a;
85  BOOST_CHECK_EQUAL(a.get(), 2);
86  BOOST_CHECK(a==2);
87 }
88 
89 BOOST_AUTO_TEST_CASE_TEMPLATE(postincrement, ctr_t, ctr_types)
90 {
91  ctr_t a(1);
92  ctr_t b(a++);
93  BOOST_CHECK_EQUAL(a.get(), 2);
94  BOOST_CHECK_EQUAL(b.get(), 1);
95 }
96 
97 BOOST_AUTO_TEST_CASE_TEMPLATE(decrement, ctr_t, ctr_types)
98 {
99  ctr_t a(1);
100  --a;
101  BOOST_CHECK_EQUAL(a.get(), 0);
102  BOOST_CHECK(a==0);
103 }
104 
105 BOOST_AUTO_TEST_CASE_TEMPLATE(postdecrement, ctr_t, ctr_types)
106 {
107  ctr_t a(1);
108  ctr_t b(a--);
109  BOOST_CHECK_EQUAL(a.get(), 0);
110  BOOST_CHECK_EQUAL(b.get(), 1);
111 }
112 
113 BOOST_AUTO_TEST_CASE_TEMPLATE(add_one, ctr_t, ctr_types)
114 {
115  ctr_t a(1);
116  a+=1;
117  BOOST_CHECK_EQUAL(a.get(), 2);
118 }
119 
120 BOOST_AUTO_TEST_CASE_TEMPLATE(subtract_one, ctr_t, ctr_types)
121 {
122  ctr_t a(1);
123  a-=1;
124  BOOST_CHECK_EQUAL(a.get(), 0);
125 }
126 
127 BOOST_AUTO_TEST_CASE_TEMPLATE(apply_plus, ctr_t, ctr_types)
128 {
129  ctr_t a(1);
130  a.apply(1, std::plus<typename ctr_t::value_type>());
131  BOOST_CHECK_EQUAL(a.get(), 2);
132 }
133 
134 BOOST_AUTO_TEST_CASE_TEMPLATE(apply_minus, ctr_t, ctr_types)
135 {
136  ctr_t a(1);
137  a.apply(1, std::minus<typename ctr_t::value_type>());
138  BOOST_CHECK_EQUAL(a.get(), 0);
139 }
140 
141 BOOST_AUTO_TEST_CASE_TEMPLATE(apply_multiply, ctr_t, ctr_types)
142 {
143  ctr_t a(6);
144  a.apply(7, std::multiplies<typename ctr_t::value_type>());
145  BOOST_CHECK_EQUAL(a.get(), 42);
146 }
147 
148 BOOST_AUTO_TEST_SUITE_END()