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 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Teuchos: Common Tools Package
6 // Copyright (2004) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
45 #include "Teuchos_RCP.hpp"
46 
47 #include "TestClasses.hpp"
49 
50 
51 namespace {
52 
53 
54 using Teuchos::null;
55 using Teuchos::RCP;
56 using Teuchos::rcp;
61 
62 
63 /* SimpleObjectTable::storeNew() */
64 
65 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( SimpleObjectTable, storeNew, T )
66 {
67  ECHO(SimpleObjectTable<T> sot);
68  ECHO(int id = sot.storeNew(new T));
69  TEST_EQUALITY_CONST(id, 0);
70  TEST_EQUALITY_CONST(nonnull(sot.getRCP(id)), true);
71  TEST_EQUALITY_CONST(is_null(sot.getRCP(id)), false);
72 }
73 
74 TEUCHOS_UNIT_TEST( SimpleObjectTable, storeNewNull )
75 {
76  ECHO(SimpleObjectTable<A> sot);
77  TEST_THROW(sot.storeNew(NULL), NullReferenceError);
78 }
79 
80 
81 /* SimpleObjectTable::storeRCP() */
82 
83 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( SimpleObjectTable, storeRCP, T )
84 {
85  ECHO(SimpleObjectTable<T> sot);
86  ECHO(RCP<T> rcpT = rcp(new T));
87  TEST_EQUALITY_CONST(nonnull(rcpT), true);
88  TEST_EQUALITY_CONST(is_null(rcpT), false);
89  ECHO(int id = sot.storeRCP(rcpT));
90  TEST_EQUALITY_CONST(id, 0);
91  ECHO(RCP<T> rcpT2 = sot.getRCP(id));
92  TEST_EQUALITY_CONST(nonnull(rcpT2), true);
93  TEST_EQUALITY_CONST(is_null(rcpT2), false);
94  TEST_EQUALITY(rcpT.get(), rcpT2.get());
95 }
96 
97 TEUCHOS_UNIT_TEST( SimpleObjectTable, storeRCPNull1 )
98 {
99  ECHO(SimpleObjectTable<A> sot);
100  ECHO(RCP<A> rcpA);
101  TEST_THROW(sot.storeRCP(rcpA), NullReferenceError);
102 }
103 
104 TEUCHOS_UNIT_TEST( SimpleObjectTable, storeRCPNull2 )
105 {
106  ECHO(SimpleObjectTable<A> sot);
107  ECHO(A *a=NULL);
108  TEST_THROW(sot.storeRCP(rcp(a)), NullReferenceError);
109 }
110 
111 TEUCHOS_UNIT_TEST( SimpleObjectTable, storeRCPNull3 )
112 {
113  ECHO(SimpleObjectTable<A> sot);
114  TEST_THROW(sot.storeRCP(Teuchos::null), NullReferenceError);
115 }
116 
117 
118 /* SimpleObjectTable::removeRCP() */
119 
120 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPFromNew )
121 {
122  ECHO(SimpleObjectTable<A> sot);
123  ECHO(int id = sot.storeNew(new A));
124  TEST_EQUALITY_CONST(id, 0);
125  ECHO(sot.removeRCP(id));
126  TEST_EQUALITY_CONST(id, -1);
127 }
128 
129 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPFromRCP )
130 {
131  ECHO(SimpleObjectTable<A> sot);
132  ECHO(RCP<A> rcpA = rcp(new A));
133  ECHO(int id = sot.storeRCP(rcpA));
134  TEST_EQUALITY_CONST(id, 0);
135  ECHO(sot.removeRCP(id));
136  TEST_EQUALITY_CONST(id, -1);
137 }
138 
139 #ifdef TEUCHOS_DEBUG
140 
141 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPInvalid1 )
142 {
143  ECHO(SimpleObjectTable<A> sot);
144  ECHO(int id = -1);
145  TEST_THROW(sot.removeRCP(id), RangeError);
146 }
147 
148 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPInvalid2 )
149 {
150  ECHO(SimpleObjectTable<A> sot);
151  ECHO(int id = -2);
152  TEST_THROW(sot.removeRCP(id), RangeError);
153 }
154 
155 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPInvalid3 )
156 {
157  ECHO(SimpleObjectTable<A> sot);
158  ECHO(int id = 0);
159  TEST_THROW(sot.removeRCP(id), RangeError);
160 }
161 
162 #endif /* TEUCHOS_DEBUG */
163 
164 
165 /* SimpleObjectTable::getRCP() */
166 
167 #ifdef TEUCHOS_DEBUG
168 
169 TEUCHOS_UNIT_TEST( SimpleObjectTable, getRCPInvalid1 )
170 {
171  ECHO(SimpleObjectTable<A> sot);
172  ECHO(int id = -1);
173  TEST_THROW(sot.getRCP(id), RangeError);
174 }
175 
176 TEUCHOS_UNIT_TEST( SimpleObjectTable, getRCPInvalid2 )
177 {
178  ECHO(SimpleObjectTable<A> sot);
179  ECHO(int id = -2);
180  TEST_THROW(sot.getRCP(id), RangeError);
181 }
182 
183 TEUCHOS_UNIT_TEST( SimpleObjectTable, getRCPInvalid3 )
184 {
185  ECHO(SimpleObjectTable<A> sot);
186  ECHO(int id = 0);
187  TEST_THROW(sot.getRCP(id), RangeError);
188 }
189 
190 #endif /* TEUCHOS_DEBUG */
191 
192 TEUCHOS_UNIT_TEST( SimpleObjectTable, getRCPInvalid4 )
193 {
194  ECHO(SimpleObjectTable<A> sot);
195  ECHO(int id = sot.storeNew(new A));
196  TEST_EQUALITY_CONST(id, 0);
197  ECHO(int id2 = sot.storeNew(new A));
198  TEST_EQUALITY_CONST(id2, 1);
199  ECHO(int id3 = id);
200  ECHO(sot.removeRCP(id));
201  TEST_THROW(sot.getRCP(id3), RangeError);
202 }
203 
204 
205 /* SimpleObjectTable::storeCastedRCP() */
206 
207 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( SimpleObjectTable, storeCastedRCP, T1, T2 )
208 {
209  ECHO(SimpleObjectTable<T2> sot);
210  ECHO(RCP<T1> rcpT1 = rcp(new T1));
211  ECHO(T2 *pT2 = dynamic_cast<T2*>(rcpT1.get()));
212  if (pT2 == NULL) {
213  TEST_THROW(sot.storeCastedRCP(rcpT1), m_bad_cast);
214  } else {
215  ECHO(int id = sot.storeCastedRCP(rcpT1));
216  TEST_EQUALITY_CONST(id, 0);
217  TEST_EQUALITY_CONST(nonnull(sot.getRCP(id)), true);
218  TEST_EQUALITY_CONST(rcpT1.shares_resource(sot.getRCP(id)), true);
219  }
220 }
221 
222 
223 /* SimpleObjectTable::purge() */
224 
225 #ifdef TEUCHOS_DEBUG
226 
227 TEUCHOS_UNIT_TEST( SimpleObjectTable, purge )
228 {
229  ECHO(SimpleObjectTable<A> sot);
230  ECHO(int id = sot.storeNew(new A));
231  TEST_EQUALITY_CONST(nonnull(sot.getRCP(id)), true);
232  ECHO(sot.purge());
233  TEST_THROW(sot.getRCP(id), RangeError);
234 }
235 
236 #endif /* TEUCHOS_DEBUG */
237 
238 
239 /* SimpleObjectTable's freedIndices table */
240 
241 TEUCHOS_UNIT_TEST( SimpleObjectTable, recycleIndex1 )
242 {
243  ECHO(SimpleObjectTable<A> sot);
244  ECHO(int id = sot.storeNew(new A));
245  TEST_EQUALITY_CONST(id, 0);
246  ECHO(sot.removeRCP(id));
247  ECHO(int id2 = sot.storeNew(new A));
248  TEST_EQUALITY_CONST(id2, 0);
249 }
250 
251 TEUCHOS_UNIT_TEST( SimpleObjectTable, recycleIndex2 )
252 {
253  ECHO(SimpleObjectTable<A> sot);
254  ECHO(int id = sot.storeNew(new A));
255  TEST_EQUALITY_CONST(id, 0);
256  ECHO(int id2 = sot.storeNew(new A));
257  TEST_EQUALITY_CONST(id2, 1);
258  ECHO(sot.removeRCP(id));
259  ECHO(int id3 = sot.storeNew(new A));
260  TEST_EQUALITY_CONST(id3, 0);
261 }
262 
263 TEUCHOS_UNIT_TEST( SimpleObjectTable, recycleIndex3 )
264 {
265  ECHO(SimpleObjectTable<A> sot);
266  ECHO(int id = sot.storeNew(new A));
267  TEST_EQUALITY_CONST(id, 0);
268  ECHO(int id2 = sot.storeNew(new A));
269  TEST_EQUALITY_CONST(id2, 1);
270  ECHO(sot.removeRCP(id2));
271  ECHO(int id3 = sot.storeNew(new A));
272  TEST_EQUALITY_CONST(id3, 1);
273 }
274 
275 
276 /* SimpleObjectTable's RCP counts */
277 
278 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpNewShared1 )
279 {
280  ECHO(SimpleObjectTable<A> sot);
281  ECHO(A a);
282  ECHO(int id = sot.storeNew(&a, false));
283 
284  ECHO(RCP<A> rcpA = sot.getRCP(id));
285  TEST_EQUALITY(rcpA.get(), &a);
286  TEST_EQUALITY_CONST(rcpA.has_ownership(), false);
287  TEST_EQUALITY_CONST(rcpA.strong_count(), 2);
288 
289  ECHO(rcpA = null);
290  ECHO(int cnt = sot.removeRCP(id));
291  TEST_EQUALITY_CONST(cnt, 0);
292 }
293 
294 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpNewShared2 )
295 {
296  ECHO(SimpleObjectTable<A> sot);
297  ECHO(A a);
298  ECHO(int id = sot.storeNew(&a, false));
299 
300  ECHO(RCP<A> rcpA = sot.getRCP(id));
301  TEST_EQUALITY(rcpA.get(), &a);
302  TEST_EQUALITY_CONST(rcpA.has_ownership(), false);
303  TEST_EQUALITY_CONST(rcpA.strong_count(), 2);
304 
305  ECHO(int cnt = sot.removeRCP(id));
306  TEST_EQUALITY_CONST(cnt, 1);
307  TEST_EQUALITY_CONST(rcpA.strong_count(), 1);
308 }
309 
310 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpNewOwned1 )
311 {
312  ECHO(SimpleObjectTable<A> sot);
313  ECHO(int id = sot.storeNew(new A));
314 
315  ECHO(RCP<A> rcpA = sot.getRCP(id));
316  TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
317  TEST_EQUALITY_CONST(rcpA.strong_count(), 2);
318 
319  ECHO(rcpA = null);
320  ECHO(int cnt = sot.removeRCP(id));
321  TEST_EQUALITY_CONST(cnt, 0);
322 }
323 
324 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpNewOwned2 )
325 {
326  ECHO(SimpleObjectTable<A> sot);
327  ECHO(int id = sot.storeNew(new A));
328 
329  ECHO(RCP<A> rcpA = sot.getRCP(id));
330  TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
331  TEST_EQUALITY_CONST(rcpA.strong_count(), 2);
332 
333  ECHO(int cnt = sot.removeRCP(id));
334  TEST_EQUALITY_CONST(cnt, 1);
335  TEST_EQUALITY_CONST(rcpA.strong_count(), 1);
336 }
337 
338 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpRCPOwned1 )
339 {
340  ECHO(SimpleObjectTable<A> sot);
341  ECHO(RCP<A> rcpA = rcp(new A));
342  TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
343  TEST_EQUALITY_CONST(rcpA.strong_count(), 1);
344 
345  ECHO(int id = sot.storeRCP(rcpA));
346  TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
347  TEST_EQUALITY_CONST(rcpA.strong_count(), 2);
348 
349  ECHO(RCP<A> rcpA2 = sot.getRCP(id));
350  TEST_EQUALITY(rcpA2.get(), rcpA.get());
351  TEST_EQUALITY_CONST(rcpA2.has_ownership(), true);
352  TEST_EQUALITY_CONST(rcpA2.strong_count(), 3);
353 }
354 
355 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpRCPOwned2 )
356 {
357  ECHO(SimpleObjectTable<A> sot);
358  ECHO(RCP<A> rcpA = rcp(new A));
359  TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
360  TEST_EQUALITY_CONST(rcpA.strong_count(), 1);
361 
362  ECHO(int id = sot.storeRCP(rcpA));
363  TEST_EQUALITY_CONST(rcpA.strong_count(), 2);
364 
365  ECHO(rcpA = null);
366  ECHO(int cnt = sot.removeRCP(id));
367  TEST_EQUALITY_CONST(cnt, 0);
368 }
369 
370 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpRCPOwned3 )
371 {
372  ECHO(SimpleObjectTable<A> sot);
373  ECHO(RCP<A> rcpA = rcp(new A));
374  TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
375  TEST_EQUALITY_CONST(rcpA.strong_count(), 1);
376 
377  ECHO(int id = sot.storeRCP(rcpA));
378  TEST_EQUALITY_CONST(rcpA.strong_count(), 2);
379 
380  ECHO(int cnt = sot.removeRCP(id));
381  TEST_EQUALITY_CONST(cnt, 1);
382  TEST_EQUALITY_CONST(rcpA.strong_count(), 1);
383 }
384 
385 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpDestructTable )
386 {
387  ECHO(SimpleObjectTable<A> *psot = new SimpleObjectTable<A>);
388  ECHO(A *pA = new A);
389  ECHO(int id = psot->storeNew(pA));
390 
391  ECHO(RCP<A> rcpA = psot->getRCP(id));
392  TEST_EQUALITY_CONST(rcpA.strong_count(), 2);
393  TEST_EQUALITY(rcpA.get(), pA);
394 
395  ECHO(delete psot);
396  TEST_EQUALITY_CONST(rcpA.strong_count(), 1);
397  TEST_EQUALITY(rcpA.get(), pA);
398 }
399 
400 
401 //
402 // Template Instantiations
403 //
404 
405 
406 #ifdef TEUCHOS_DEBUG
407 
408 # define DEBUG_UNIT_TEST_GROUP( T ) \
409 
410 #else
411 
412 # define DEBUG_UNIT_TEST_GROUP( T )
413 
414 #endif
415 
416 
417 #define UNIT_TEST_GROUP( T ) \
418  TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SimpleObjectTable, storeNew, T ) \
419  TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SimpleObjectTable, storeRCP, T ) \
420  DEBUG_UNIT_TEST_GROUP( T )
421 
422 #define UNIT_TEST_GROUP_PAIR( T1, T2 ) \
423  TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( SimpleObjectTable, storeCastedRCP, T1, T2 )
424 
425 #define UNIT_TEST_GROUP_PAIR_SYM( T1, T2 ) \
426  UNIT_TEST_GROUP_PAIR( T1, T2 ) \
427  UNIT_TEST_GROUP_PAIR( T2, T1 )
428 
429 
434 
439 
446 
447 
448 } // 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.