COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-ptr-benchmark.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2013 von Karman Institute for Fluid Dynamics, Belgium
2 //
3 // This software is distributed under the terms of the
4 // GNU Lesser General Public License version 3 (LGPLv3).
5 // See doc/lgpl.txt and doc/gpl.txt for the license text.
6 
7 #define BOOST_TEST_DYN_LINK
8 #define BOOST_TEST_MODULE "Test module for Map component"
9 
10 #include <iostream>
11 
12 #include <boost/test/unit_test.hpp>
13 #include <boost/lexical_cast.hpp>
14 #include <boost/shared_ptr.hpp>
15 #include <boost/weak_ptr.hpp>
16 
17 #include "common/CF.hpp"
18 #include "common/Handle.hpp"
19 
21 
22 
23 
25 
26 using namespace cf3;
27 using namespace cf3::common;
28 
30 
32 {
33  typedef boost::shared_ptr<Uint> SharedT;
34  typedef boost::weak_ptr<Uint> WeakT;
36  typedef Uint* RawT;
37 
38  SharedT* shared_vec()
39  {
40  static SharedT* v = 0;
41  if(!v)
42  v = new SharedT[vec_size];
43  return v;
44  }
45 
46  WeakT* weak_vec()
47  {
48  static WeakT* v = 0;
49  if(!v)
50  v = new WeakT[vec_size];
51  return v;
52  }
53 
54  HandleT* handle_vec()
55  {
56  static HandleT* v = 0;
57  if(!v)
58  v = new HandleT[vec_size];
59  return v;
60  }
61 
62  RawT* raw_vec()
63  {
64  static RawT* v = 0;
65  if(!v)
66  v = new RawT[vec_size*2];
67  return v;
68  }
69 
70  static const Uint vec_size = 1000000;
71  static const Uint deref_repeats = 100;
72 };
73 
75 
76 BOOST_FIXTURE_TEST_SUITE( BuildOptions, PtrFixture )
77 
78 
80 BOOST_AUTO_TEST_CASE ( FillShared )
81 {
82  std::cout << "size of boost::shared_ptr: " << sizeof(SharedT) << std::endl;
83  std::cout << "size of boost::weak_ptr: " << sizeof(WeakT) << std::endl;
84  std::cout << "size of raw pointer: " << sizeof(RawT) << std::endl;
85 
86  SharedT* vec = shared_vec();
87  for(Uint i = 0; i != vec_size; ++i)
88  vec[i] = boost::shared_ptr<Uint>(new Uint(i));
89 }
90 
92 {
93  SharedT* s_vec = shared_vec();
94  WeakT* w_vec = weak_vec();
95  for(Uint i = 0; i != vec_size; ++i)
96  w_vec[i] = s_vec[i];
97 }
98 
99 BOOST_AUTO_TEST_CASE ( FillHandle )
100 {
101  SharedT* s_vec = shared_vec();
102  HandleT* w_vec = handle_vec();
103  for(Uint i = 0; i != vec_size; ++i)
104  w_vec[i] = HandleT(s_vec[i]);
105 }
106 
108 {
109  RawT* vec = raw_vec();
110  for(Uint i = 0; i != 2*vec_size; ++i)
111  vec[i] = new Uint(i/2);
112 }
113 
114 BOOST_AUTO_TEST_CASE ( CopyShared )
115 {
116  SharedT* new_vec = new SharedT[vec_size];
117  SharedT* old_vec = shared_vec();
118  for(Uint i = 0; i != vec_size; ++i)
119  new_vec[i] = old_vec[i];
120 }
121 
123 {
124  WeakT* new_vec = new WeakT[vec_size];
125  WeakT* old_vec = weak_vec();
126  for(Uint i = 0; i != vec_size; ++i)
127  new_vec[i] = old_vec[i];
128 }
129 
130 BOOST_AUTO_TEST_CASE ( CopyHandle )
131 {
132  HandleT* new_vec = new HandleT[vec_size];
133  HandleT* old_vec = handle_vec();
134  for(Uint i = 0; i != vec_size; ++i)
135  new_vec[i] = old_vec[i];
136 }
137 
139 {
140  RawT* new_vec = new RawT[vec_size*2];
141  RawT* old_vec = raw_vec();
142  for(Uint i = 0; i != vec_size; ++i)
143  new_vec[2*i] = old_vec[2*i];
144 }
145 
146 BOOST_AUTO_TEST_CASE ( CheckShared )
147 {
148  SharedT* vec = shared_vec();
149  Uint result = 0;
150  for(Uint i = 0; i != vec_size; ++i)
151  result += is_not_null(vec[i].get());
152  BOOST_CHECK(vec_size == result);
153 }
154 
156 {
157  WeakT* vec = weak_vec();
158  Uint result = 0;
159  for(Uint i = 0; i != vec_size; ++i)
160  result += !vec[i].expired();
161  BOOST_CHECK(vec_size == result);
162 }
163 
164 BOOST_AUTO_TEST_CASE ( CheckHandle )
165 {
166  HandleT* vec = handle_vec();
167  Uint result = 0;
168  for(Uint i = 0; i != vec_size; ++i)
169  result += is_not_null(vec[i]);
170  BOOST_CHECK(vec_size == result);
171 }
172 
174 {
175  RawT* vec = raw_vec();
176  Uint result = 0;
177  for(Uint i = 0; i != vec_size; ++i)
178  result += is_not_null(vec[2*i]);
179  BOOST_CHECK(vec_size == result);
180 }
181 
182 BOOST_AUTO_TEST_CASE ( DerefShared )
183 {
184  SharedT* vec = shared_vec();
185  Uint result = 0;
186  for(Uint r = 0; r != deref_repeats; ++r)
187  {
188  for(Uint i = 0; i != vec_size; ++i)
189  {
190  result += *vec[i];
191  }
192  }
193  BOOST_CHECK(result);
194 }
195 
197 {
198  WeakT* vec = weak_vec();
199  Uint result = 0;
200  for(Uint r = 0; r != deref_repeats; ++r)
201  {
202  for(Uint i = 0; i != vec_size; ++i)
203  {
204  result += *vec[i].lock();
205  }
206  }
207  BOOST_CHECK(result);
208 }
209 
210 BOOST_AUTO_TEST_CASE ( DerefHandle )
211 {
212  HandleT* vec = handle_vec();
213  Uint result = 0;
214  for(Uint r = 0; r != deref_repeats; ++r)
215  {
216  for(Uint i = 0; i != vec_size; ++i)
217  {
218  result += *vec[i];
219  }
220  }
221  BOOST_CHECK(result);
222 }
223 
225 {
226  RawT* vec = raw_vec();
227  Uint result = 0;
228  for(Uint r = 0; r != deref_repeats; ++r)
229  {
230  for(Uint i = 0; i != vec_size; ++i)
231  {
232  result += *vec[2*i];
233  }
234  }
235  BOOST_CHECK(result);
236 }
237 
238 BOOST_AUTO_TEST_SUITE_END()
239 
240 
boost::shared_ptr< Uint > SharedT
Safe pointer to an object. This is the supported method for referring to components.
Definition: Handle.hpp:39
Any test using this fixture (or a derivative) will be timed.
Top-level namespace for coolfluid.
Definition: Action.cpp:18
boost::weak_ptr< Uint > WeakT
HandleT * handle_vec()
SharedT * shared_vec()
unsigned int Uint
typedef for unsigned int
Definition: CF.hpp:90
Handle< Uint > HandleT
coolfluid3 header, included almost everywhere
BOOST_AUTO_TEST_CASE(FillShared)
Most basic kernel library.
Definition: Action.cpp:19
bool is_not_null(T ptr)
predicate for comparison to nullptr
Definition: CF.hpp:147
Send comments to:
COOLFluiD Web Admin