Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SimpleObjTbl_UnitTests.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
11 #include "Teuchos_RCP.hpp"
12 
13 #include "TestClasses.hpp"
15 
16 
17 namespace {
18 
19 
20 using Teuchos::null;
21 using Teuchos::RCP;
22 using Teuchos::rcp;
27 
28 
29 /* SimpleObjectTable::storeNew() */
30 
31 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( SimpleObjectTable, storeNew, T )
32 {
33  ECHO(SimpleObjectTable<T> sot);
34  ECHO(int id = sot.storeNew(new T));
35  TEST_EQUALITY_CONST(id, 0);
36  TEST_EQUALITY_CONST(nonnull(sot.getRCP(id)), true);
37  TEST_EQUALITY_CONST(is_null(sot.getRCP(id)), false);
38 }
39 
40 TEUCHOS_UNIT_TEST( SimpleObjectTable, storeNewNull )
41 {
42  ECHO(SimpleObjectTable<A> sot);
43  TEST_THROW(sot.storeNew(NULL), NullReferenceError);
44 }
45 
46 
47 /* SimpleObjectTable::storeRCP() */
48 
49 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( SimpleObjectTable, storeRCP, T )
50 {
51  ECHO(SimpleObjectTable<T> sot);
52  ECHO(RCP<T> rcpT = rcp(new T));
53  TEST_EQUALITY_CONST(nonnull(rcpT), true);
54  TEST_EQUALITY_CONST(is_null(rcpT), false);
55  ECHO(int id = sot.storeRCP(rcpT));
56  TEST_EQUALITY_CONST(id, 0);
57  ECHO(RCP<T> rcpT2 = sot.getRCP(id));
58  TEST_EQUALITY_CONST(nonnull(rcpT2), true);
59  TEST_EQUALITY_CONST(is_null(rcpT2), false);
60  TEST_EQUALITY(rcpT.get(), rcpT2.get());
61 }
62 
63 TEUCHOS_UNIT_TEST( SimpleObjectTable, storeRCPNull1 )
64 {
65  ECHO(SimpleObjectTable<A> sot);
66  ECHO(RCP<A> rcpA);
67  TEST_THROW(sot.storeRCP(rcpA), NullReferenceError);
68 }
69 
70 TEUCHOS_UNIT_TEST( SimpleObjectTable, storeRCPNull2 )
71 {
72  ECHO(SimpleObjectTable<A> sot);
73  ECHO(A *a=NULL);
74  TEST_THROW(sot.storeRCP(rcp(a)), NullReferenceError);
75 }
76 
77 TEUCHOS_UNIT_TEST( SimpleObjectTable, storeRCPNull3 )
78 {
79  ECHO(SimpleObjectTable<A> sot);
80  TEST_THROW(sot.storeRCP(Teuchos::null), NullReferenceError);
81 }
82 
83 
84 /* SimpleObjectTable::removeRCP() */
85 
86 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPFromNew )
87 {
88  ECHO(SimpleObjectTable<A> sot);
89  ECHO(int id = sot.storeNew(new A));
90  TEST_EQUALITY_CONST(id, 0);
91  ECHO(sot.removeRCP(id));
92  TEST_EQUALITY_CONST(id, -1);
93 }
94 
95 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPFromRCP )
96 {
97  ECHO(SimpleObjectTable<A> sot);
98  ECHO(RCP<A> rcpA = rcp(new A));
99  ECHO(int id = sot.storeRCP(rcpA));
100  TEST_EQUALITY_CONST(id, 0);
101  ECHO(sot.removeRCP(id));
102  TEST_EQUALITY_CONST(id, -1);
103 }
104 
105 #ifdef TEUCHOS_DEBUG
106 
107 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPInvalid1 )
108 {
109  ECHO(SimpleObjectTable<A> sot);
110  ECHO(int id = -1);
111  TEST_THROW(sot.removeRCP(id), RangeError);
112 }
113 
114 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPInvalid2 )
115 {
116  ECHO(SimpleObjectTable<A> sot);
117  ECHO(int id = -2);
118  TEST_THROW(sot.removeRCP(id), RangeError);
119 }
120 
121 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPInvalid3 )
122 {
123  ECHO(SimpleObjectTable<A> sot);
124  ECHO(int id = 0);
125  TEST_THROW(sot.removeRCP(id), RangeError);
126 }
127 
128 #endif /* TEUCHOS_DEBUG */
129 
130 
131 /* SimpleObjectTable::getRCP() */
132 
133 #ifdef TEUCHOS_DEBUG
134 
135 TEUCHOS_UNIT_TEST( SimpleObjectTable, getRCPInvalid1 )
136 {
137  ECHO(SimpleObjectTable<A> sot);
138  ECHO(int id = -1);
139  TEST_THROW(sot.getRCP(id), RangeError);
140 }
141 
142 TEUCHOS_UNIT_TEST( SimpleObjectTable, getRCPInvalid2 )
143 {
144  ECHO(SimpleObjectTable<A> sot);
145  ECHO(int id = -2);
146  TEST_THROW(sot.getRCP(id), RangeError);
147 }
148 
149 TEUCHOS_UNIT_TEST( SimpleObjectTable, getRCPInvalid3 )
150 {
151  ECHO(SimpleObjectTable<A> sot);
152  ECHO(int id = 0);
153  TEST_THROW(sot.getRCP(id), RangeError);
154 }
155 
156 #endif /* TEUCHOS_DEBUG */
157 
158 TEUCHOS_UNIT_TEST( SimpleObjectTable, getRCPInvalid4 )
159 {
160  ECHO(SimpleObjectTable<A> sot);
161  ECHO(int id = sot.storeNew(new A));
162  TEST_EQUALITY_CONST(id, 0);
163  ECHO(int id2 = sot.storeNew(new A));
164  TEST_EQUALITY_CONST(id2, 1);
165  ECHO(int id3 = id);
166  ECHO(sot.removeRCP(id));
167  TEST_THROW(sot.getRCP(id3), RangeError);
168 }
169 
170 
171 /* SimpleObjectTable::storeCastedRCP() */
172 
173 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( SimpleObjectTable, storeCastedRCP, T1, T2 )
174 {
175  ECHO(SimpleObjectTable<T2> sot);
176  ECHO(RCP<T1> rcpT1 = rcp(new T1));
177  ECHO(T2 *pT2 = dynamic_cast<T2*>(rcpT1.get()));
178  if (pT2 == NULL) {
179  TEST_THROW(sot.storeCastedRCP(rcpT1), m_bad_cast);
180  } else {
181  ECHO(int id = sot.storeCastedRCP(rcpT1));
182  TEST_EQUALITY_CONST(id, 0);
183  TEST_EQUALITY_CONST(nonnull(sot.getRCP(id)), true);
184  TEST_EQUALITY_CONST(rcpT1.shares_resource(sot.getRCP(id)), true);
185  }
186 }
187 
188 
189 /* SimpleObjectTable::purge() */
190 
191 #ifdef TEUCHOS_DEBUG
192 
193 TEUCHOS_UNIT_TEST( SimpleObjectTable, purge )
194 {
195  ECHO(SimpleObjectTable<A> sot);
196  ECHO(int id = sot.storeNew(new A));
197  TEST_EQUALITY_CONST(nonnull(sot.getRCP(id)), true);
198  ECHO(sot.purge());
199  TEST_THROW(sot.getRCP(id), RangeError);
200 }
201 
202 #endif /* TEUCHOS_DEBUG */
203 
204 
205 /* SimpleObjectTable's freedIndices table */
206 
207 TEUCHOS_UNIT_TEST( SimpleObjectTable, recycleIndex1 )
208 {
209  ECHO(SimpleObjectTable<A> sot);
210  ECHO(int id = sot.storeNew(new A));
211  TEST_EQUALITY_CONST(id, 0);
212  ECHO(sot.removeRCP(id));
213  ECHO(int id2 = sot.storeNew(new A));
214  TEST_EQUALITY_CONST(id2, 0);
215 }
216 
217 TEUCHOS_UNIT_TEST( SimpleObjectTable, recycleIndex2 )
218 {
219  ECHO(SimpleObjectTable<A> sot);
220  ECHO(int id = sot.storeNew(new A));
221  TEST_EQUALITY_CONST(id, 0);
222  ECHO(int id2 = sot.storeNew(new A));
223  TEST_EQUALITY_CONST(id2, 1);
224  ECHO(sot.removeRCP(id));
225  ECHO(int id3 = sot.storeNew(new A));
226  TEST_EQUALITY_CONST(id3, 0);
227 }
228 
229 TEUCHOS_UNIT_TEST( SimpleObjectTable, recycleIndex3 )
230 {
231  ECHO(SimpleObjectTable<A> sot);
232  ECHO(int id = sot.storeNew(new A));
233  TEST_EQUALITY_CONST(id, 0);
234  ECHO(int id2 = sot.storeNew(new A));
235  TEST_EQUALITY_CONST(id2, 1);
236  ECHO(sot.removeRCP(id2));
237  ECHO(int id3 = sot.storeNew(new A));
238  TEST_EQUALITY_CONST(id3, 1);
239 }
240 
241 
242 /* SimpleObjectTable's RCP counts */
243 
244 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpNewShared1 )
245 {
246  ECHO(SimpleObjectTable<A> sot);
247  ECHO(A a);
248  ECHO(int id = sot.storeNew(&a, false));
249 
250  ECHO(RCP<A> rcpA = sot.getRCP(id));
251  TEST_EQUALITY(rcpA.get(), &a);
252  TEST_EQUALITY_CONST(rcpA.has_ownership(), false);
253  TEST_EQUALITY_CONST(rcpA.strong_count(), 2);
254 
255  ECHO(rcpA = null);
256  ECHO(int cnt = sot.removeRCP(id));
257  TEST_EQUALITY_CONST(cnt, 0);
258 }
259 
260 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpNewShared2 )
261 {
262  ECHO(SimpleObjectTable<A> sot);
263  ECHO(A a);
264  ECHO(int id = sot.storeNew(&a, false));
265 
266  ECHO(RCP<A> rcpA = sot.getRCP(id));
267  TEST_EQUALITY(rcpA.get(), &a);
268  TEST_EQUALITY_CONST(rcpA.has_ownership(), false);
269  TEST_EQUALITY_CONST(rcpA.strong_count(), 2);
270 
271  ECHO(int cnt = sot.removeRCP(id));
272  TEST_EQUALITY_CONST(cnt, 1);
273  TEST_EQUALITY_CONST(rcpA.strong_count(), 1);
274 }
275 
276 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpNewOwned1 )
277 {
278  ECHO(SimpleObjectTable<A> sot);
279  ECHO(int id = sot.storeNew(new A));
280 
281  ECHO(RCP<A> rcpA = sot.getRCP(id));
282  TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
283  TEST_EQUALITY_CONST(rcpA.strong_count(), 2);
284 
285  ECHO(rcpA = null);
286  ECHO(int cnt = sot.removeRCP(id));
287  TEST_EQUALITY_CONST(cnt, 0);
288 }
289 
290 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpNewOwned2 )
291 {
292  ECHO(SimpleObjectTable<A> sot);
293  ECHO(int id = sot.storeNew(new A));
294 
295  ECHO(RCP<A> rcpA = sot.getRCP(id));
296  TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
297  TEST_EQUALITY_CONST(rcpA.strong_count(), 2);
298 
299  ECHO(int cnt = sot.removeRCP(id));
300  TEST_EQUALITY_CONST(cnt, 1);
301  TEST_EQUALITY_CONST(rcpA.strong_count(), 1);
302 }
303 
304 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpRCPOwned1 )
305 {
306  ECHO(SimpleObjectTable<A> sot);
307  ECHO(RCP<A> rcpA = rcp(new A));
308  TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
309  TEST_EQUALITY_CONST(rcpA.strong_count(), 1);
310 
311  ECHO(int id = sot.storeRCP(rcpA));
312  TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
313  TEST_EQUALITY_CONST(rcpA.strong_count(), 2);
314 
315  ECHO(RCP<A> rcpA2 = sot.getRCP(id));
316  TEST_EQUALITY(rcpA2.get(), rcpA.get());
317  TEST_EQUALITY_CONST(rcpA2.has_ownership(), true);
318  TEST_EQUALITY_CONST(rcpA2.strong_count(), 3);
319 }
320 
321 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpRCPOwned2 )
322 {
323  ECHO(SimpleObjectTable<A> sot);
324  ECHO(RCP<A> rcpA = rcp(new A));
325  TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
326  TEST_EQUALITY_CONST(rcpA.strong_count(), 1);
327 
328  ECHO(int id = sot.storeRCP(rcpA));
329  TEST_EQUALITY_CONST(rcpA.strong_count(), 2);
330 
331  ECHO(rcpA = null);
332  ECHO(int cnt = sot.removeRCP(id));
333  TEST_EQUALITY_CONST(cnt, 0);
334 }
335 
336 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpRCPOwned3 )
337 {
338  ECHO(SimpleObjectTable<A> sot);
339  ECHO(RCP<A> rcpA = rcp(new A));
340  TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
341  TEST_EQUALITY_CONST(rcpA.strong_count(), 1);
342 
343  ECHO(int id = sot.storeRCP(rcpA));
344  TEST_EQUALITY_CONST(rcpA.strong_count(), 2);
345 
346  ECHO(int cnt = sot.removeRCP(id));
347  TEST_EQUALITY_CONST(cnt, 1);
348  TEST_EQUALITY_CONST(rcpA.strong_count(), 1);
349 }
350 
351 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpDestructTable )
352 {
353  ECHO(SimpleObjectTable<A> *psot = new SimpleObjectTable<A>);
354  ECHO(A *pA = new A);
355  ECHO(int id = psot->storeNew(pA));
356 
357  ECHO(RCP<A> rcpA = psot->getRCP(id));
358  TEST_EQUALITY_CONST(rcpA.strong_count(), 2);
359  TEST_EQUALITY(rcpA.get(), pA);
360 
361  ECHO(delete psot);
362  TEST_EQUALITY_CONST(rcpA.strong_count(), 1);
363  TEST_EQUALITY(rcpA.get(), pA);
364 }
365 
366 
367 //
368 // Template Instantiations
369 //
370 
371 
372 #ifdef TEUCHOS_DEBUG
373 
374 # define DEBUG_UNIT_TEST_GROUP( T ) \
375 
376 #else
377 
378 # define DEBUG_UNIT_TEST_GROUP( T )
379 
380 #endif
381 
382 
383 #define UNIT_TEST_GROUP( T ) \
384  TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SimpleObjectTable, storeNew, T ) \
385  TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SimpleObjectTable, storeRCP, T ) \
386  DEBUG_UNIT_TEST_GROUP( T )
387 
388 #define UNIT_TEST_GROUP_PAIR( T1, T2 ) \
389  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( SimpleObjectTable, storeCastedRCP, T1, T2 )
390 
391 #define UNIT_TEST_GROUP_PAIR_SYM( T1, T2 ) \
392  UNIT_TEST_GROUP_PAIR( T1, T2 ) \
393  UNIT_TEST_GROUP_PAIR( T2, T1 )
394 
395 
400 
405 
412 
413 
414 } // namespace
Null reference error exception class.
RCP< T > rcp(const boost::shared_ptr< T > &sptr)
Conversion function that takes in a boost::shared_ptr object and spits out a Teuchos::RCP object...
#define ECHO(statement)
Echo the given statement before it is executed.
This class provides a central place to store objects.
#define TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(TEST_GROUP, TEST_NAME, TYPE)
Macro for defining a templated unit test with one template parameter.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#define TEST_THROW(code, ExceptType)
Assert that the statement &#39;code&#39; throws the exception &#39;ExceptType&#39; (otherwise the test fails)...
#define TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL(TEST_GROUP, TEST_NAME, TYPE1, TYPE2)
Macro for defining a templated unit test with two template parameters.
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
bool is_null(const ArrayRCP< T > &p)
Returns true if p.get()==NULL.
#define UNIT_TEST_GROUP_PAIR_SYM(T1, T2)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Unit testing support.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
Exception class for bad cast.
#define UNIT_TEST_GROUP(T)
Smart reference counting pointer class for automatic garbage collection.
#define UNIT_TEST_GROUP_PAIR(T1, T2)
Range error exception class.
Reference-counted pointer class and non-member templated function implementations.
A simple object table class for Teuchos.
bool nonnull(const ArrayRCP< T > &p)
Returns true if p.get()!=NULL.