SimpleObjTbl_UnitTests.cpp

Go to the documentation of this file.
00001 #include "Teuchos_SimpleObjectTable.hpp"
00002 #include "Teuchos_RCP.hpp"
00003 
00004 #include "TestClasses.hpp"
00005 #include "Teuchos_UnitTestHarness.hpp"
00006 
00007 
00008 namespace {
00009 
00010 
00011 using Teuchos::null;
00012 using Teuchos::RCP;
00013 using Teuchos::rcp;
00014 using Teuchos::RangeError;
00015 using Teuchos::NullReferenceError;
00016 using Teuchos::m_bad_cast;
00017 using Teuchos::SimpleObjectTable;
00018 
00019 
00020 /* SimpleObjectTable::storeNew() */
00021 
00022 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( SimpleObjectTable, storeNew, T )
00023 {
00024   ECHO(SimpleObjectTable<T> sot);
00025   ECHO(int id = sot.storeNew(new T));
00026   TEST_EQUALITY_CONST(id, 0);
00027   TEST_EQUALITY_CONST(nonnull(sot.getRCP(id)), true);
00028   TEST_EQUALITY_CONST(is_null(sot.getRCP(id)), false);
00029 }
00030 
00031 TEUCHOS_UNIT_TEST( SimpleObjectTable, storeNewNull )
00032 {
00033   ECHO(SimpleObjectTable<A> sot);
00034   TEST_THROW(sot.storeNew(NULL), NullReferenceError); 
00035 }
00036 
00037 
00038 /* SimpleObjectTable::storeRCP() */
00039 
00040 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( SimpleObjectTable, storeRCP, T )
00041 {
00042   ECHO(SimpleObjectTable<T> sot);
00043   ECHO(RCP<T> rcpT = rcp(new T));
00044   TEST_EQUALITY_CONST(nonnull(rcpT), true);
00045   TEST_EQUALITY_CONST(is_null(rcpT), false);
00046   ECHO(int id = sot.storeRCP(rcpT));
00047   TEST_EQUALITY_CONST(id, 0);
00048   ECHO(RCP<T> rcpT2 = sot.getRCP(id));
00049   TEST_EQUALITY_CONST(nonnull(rcpT2), true);
00050   TEST_EQUALITY_CONST(is_null(rcpT2), false);
00051   TEST_EQUALITY(rcpT.get(), rcpT2.get());
00052 }
00053 
00054 TEUCHOS_UNIT_TEST( SimpleObjectTable, storeRCPNull1 )
00055 {
00056   ECHO(SimpleObjectTable<A> sot);
00057   ECHO(RCP<A> rcpA);
00058   TEST_THROW(sot.storeRCP(rcpA), NullReferenceError);
00059 }
00060 
00061 TEUCHOS_UNIT_TEST( SimpleObjectTable, storeRCPNull2 )
00062 {
00063   ECHO(SimpleObjectTable<A> sot);
00064   ECHO(A *a=NULL);
00065   TEST_THROW(sot.storeRCP(rcp(a)), NullReferenceError);
00066 }
00067 
00068 TEUCHOS_UNIT_TEST( SimpleObjectTable, storeRCPNull3 )
00069 {
00070   ECHO(SimpleObjectTable<A> sot);
00071   TEST_THROW(sot.storeRCP(Teuchos::null), NullReferenceError);
00072 }
00073 
00074 
00075 /* SimpleObjectTable::removeRCP() */
00076 
00077 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPFromNew )
00078 {
00079   ECHO(SimpleObjectTable<A> sot);
00080   ECHO(int id = sot.storeNew(new A));
00081   TEST_EQUALITY_CONST(id, 0);
00082   ECHO(sot.removeRCP(id));
00083   TEST_EQUALITY_CONST(id, -1);
00084 }
00085 
00086 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPFromRCP )
00087 {
00088   ECHO(SimpleObjectTable<A> sot);
00089   ECHO(RCP<A> rcpA = rcp(new A));
00090   ECHO(int id = sot.storeRCP(rcpA));
00091   TEST_EQUALITY_CONST(id, 0);
00092   ECHO(sot.removeRCP(id));
00093   TEST_EQUALITY_CONST(id, -1);
00094 }
00095 
00096 #ifdef TEUCHOS_DEBUG
00097 
00098 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPInvalid1 )
00099 {
00100   ECHO(SimpleObjectTable<A> sot);
00101   ECHO(int id = -1);
00102   TEST_THROW(sot.removeRCP(id), RangeError);
00103 }
00104 
00105 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPInvalid2 )
00106 {
00107   ECHO(SimpleObjectTable<A> sot);
00108   ECHO(int id = -2);
00109   TEST_THROW(sot.removeRCP(id), RangeError);
00110 }
00111 
00112 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPInvalid3 )
00113 {
00114   ECHO(SimpleObjectTable<A> sot);
00115   ECHO(int id = 0);
00116   TEST_THROW(sot.removeRCP(id), RangeError);
00117 }
00118 
00119 #endif /* TEUCHOS_DEBUG */
00120 
00121 
00122 /* SimpleObjectTable::getRCP() */
00123 
00124 #ifdef TEUCHOS_DEBUG
00125 
00126 TEUCHOS_UNIT_TEST( SimpleObjectTable, getRCPInvalid1 )
00127 {
00128   ECHO(SimpleObjectTable<A> sot);
00129   ECHO(int id = -1);
00130   TEST_THROW(sot.getRCP(id), RangeError);
00131 }
00132 
00133 TEUCHOS_UNIT_TEST( SimpleObjectTable, getRCPInvalid2 )
00134 {
00135   ECHO(SimpleObjectTable<A> sot);
00136   ECHO(int id = -2);
00137   TEST_THROW(sot.getRCP(id), RangeError);
00138 }
00139 
00140 TEUCHOS_UNIT_TEST( SimpleObjectTable, getRCPInvalid3 )
00141 {
00142   ECHO(SimpleObjectTable<A> sot);
00143   ECHO(int id = 0);
00144   TEST_THROW(sot.getRCP(id), RangeError);
00145 }
00146 
00147 #endif /* TEUCHOS_DEBUG */
00148 
00149 TEUCHOS_UNIT_TEST( SimpleObjectTable, getRCPInvalid4 )
00150 {
00151   ECHO(SimpleObjectTable<A> sot);
00152   ECHO(int id = sot.storeNew(new A));
00153   TEST_EQUALITY_CONST(id, 0);
00154   ECHO(int id2 = sot.storeNew(new A));
00155   TEST_EQUALITY_CONST(id2, 1);
00156   ECHO(int id3 = id);
00157   ECHO(sot.removeRCP(id));
00158   TEST_THROW(sot.getRCP(id3), RangeError);
00159 }
00160 
00161 
00162 /* SimpleObjectTable::storeCastedRCP() */
00163 
00164 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( SimpleObjectTable, storeCastedRCP, T1, T2 )
00165 {
00166   ECHO(SimpleObjectTable<T2> sot);
00167   ECHO(RCP<T1> rcpT1 = rcp(new T1));
00168   ECHO(T2 *pT2 = dynamic_cast<T2*>(rcpT1.get()));
00169   if (pT2 == NULL) {
00170     TEST_THROW(sot.storeCastedRCP(rcpT1), m_bad_cast);
00171   } else {
00172     ECHO(int id = sot.storeCastedRCP(rcpT1));
00173     TEST_EQUALITY_CONST(id, 0);
00174     TEST_EQUALITY_CONST(nonnull(sot.getRCP(id)), true);
00175     TEST_EQUALITY_CONST(rcpT1.shares_resource(sot.getRCP(id)), true);
00176   }
00177 }
00178 
00179 
00180 /* SimpleObjectTable::purge() */
00181 
00182 #ifdef TEUCHOS_DEBUG
00183 
00184 TEUCHOS_UNIT_TEST( SimpleObjectTable, purge )
00185 {
00186   ECHO(SimpleObjectTable<A> sot);
00187   ECHO(int id = sot.storeNew(new A));
00188   TEST_EQUALITY_CONST(nonnull(sot.getRCP(id)), true);
00189   ECHO(sot.purge());
00190   TEST_THROW(sot.getRCP(id), RangeError);
00191 }
00192 
00193 #endif /* TEUCHOS_DEBUG */
00194 
00195 
00196 /* SimpleObjectTable's freedIndices table */
00197 
00198 TEUCHOS_UNIT_TEST( SimpleObjectTable, recycleIndex1 )
00199 {
00200   ECHO(SimpleObjectTable<A> sot);
00201   ECHO(int id = sot.storeNew(new A));
00202   TEST_EQUALITY_CONST(id, 0);
00203   ECHO(sot.removeRCP(id));
00204   ECHO(int id2 = sot.storeNew(new A));
00205   TEST_EQUALITY_CONST(id2, 0);
00206 }
00207 
00208 TEUCHOS_UNIT_TEST( SimpleObjectTable, recycleIndex2 )
00209 {
00210   ECHO(SimpleObjectTable<A> sot);
00211   ECHO(int id = sot.storeNew(new A));
00212   TEST_EQUALITY_CONST(id, 0);
00213   ECHO(int id2 = sot.storeNew(new A));
00214   TEST_EQUALITY_CONST(id2, 1);
00215   ECHO(sot.removeRCP(id));
00216   ECHO(int id3 = sot.storeNew(new A));
00217   TEST_EQUALITY_CONST(id3, 0);
00218 }
00219 
00220 TEUCHOS_UNIT_TEST( SimpleObjectTable, recycleIndex3 )
00221 {
00222   ECHO(SimpleObjectTable<A> sot);
00223   ECHO(int id = sot.storeNew(new A));
00224   TEST_EQUALITY_CONST(id, 0);
00225   ECHO(int id2 = sot.storeNew(new A));
00226   TEST_EQUALITY_CONST(id2, 1);
00227   ECHO(sot.removeRCP(id2));
00228   ECHO(int id3 = sot.storeNew(new A));
00229   TEST_EQUALITY_CONST(id3, 1);
00230 }
00231 
00232 
00233 /* SimpleObjectTable's RCP counts */
00234 
00235 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpNewShared1 )
00236 {
00237   ECHO(SimpleObjectTable<A> sot);
00238   ECHO(A a);
00239   ECHO(int id = sot.storeNew(&a, false));
00240 
00241   ECHO(RCP<A> rcpA = sot.getRCP(id));
00242   TEST_EQUALITY(rcpA.get(), &a);
00243   TEST_EQUALITY_CONST(rcpA.has_ownership(), false);
00244   TEST_EQUALITY_CONST(rcpA.count(), 2);
00245 
00246   ECHO(rcpA = null);
00247   ECHO(int cnt = sot.removeRCP(id));
00248   TEST_EQUALITY_CONST(cnt, 0);
00249 }
00250 
00251 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpNewShared2 )
00252 {
00253   ECHO(SimpleObjectTable<A> sot);
00254   ECHO(A a);
00255   ECHO(int id = sot.storeNew(&a, false));
00256 
00257   ECHO(RCP<A> rcpA = sot.getRCP(id));
00258   TEST_EQUALITY(rcpA.get(), &a);
00259   TEST_EQUALITY_CONST(rcpA.has_ownership(), false);
00260   TEST_EQUALITY_CONST(rcpA.count(), 2);
00261 
00262   ECHO(int cnt = sot.removeRCP(id));
00263   TEST_EQUALITY_CONST(cnt, 1);
00264   TEST_EQUALITY_CONST(rcpA.count(), 1);
00265 }
00266 
00267 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpNewOwned1 )
00268 {
00269   ECHO(SimpleObjectTable<A> sot);
00270   ECHO(int id = sot.storeNew(new A));
00271 
00272   ECHO(RCP<A> rcpA = sot.getRCP(id));
00273   TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
00274   TEST_EQUALITY_CONST(rcpA.count(), 2);
00275 
00276   ECHO(rcpA = null);
00277   ECHO(int cnt = sot.removeRCP(id));
00278   TEST_EQUALITY_CONST(cnt, 0);
00279 }
00280 
00281 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpNewOwned2 )
00282 {
00283   ECHO(SimpleObjectTable<A> sot);
00284   ECHO(int id = sot.storeNew(new A));
00285 
00286   ECHO(RCP<A> rcpA = sot.getRCP(id));
00287   TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
00288   TEST_EQUALITY_CONST(rcpA.count(), 2);
00289 
00290   ECHO(int cnt = sot.removeRCP(id));
00291   TEST_EQUALITY_CONST(cnt, 1);
00292   TEST_EQUALITY_CONST(rcpA.count(), 1);
00293 }
00294 
00295 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpRCPOwned1 )
00296 {
00297   ECHO(SimpleObjectTable<A> sot);
00298   ECHO(RCP<A> rcpA = rcp(new A));
00299   TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
00300   TEST_EQUALITY_CONST(rcpA.count(), 1);
00301 
00302   ECHO(int id = sot.storeRCP(rcpA));
00303   TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
00304   TEST_EQUALITY_CONST(rcpA.count(), 2);
00305 
00306   ECHO(RCP<A> rcpA2 = sot.getRCP(id));
00307   TEST_EQUALITY(rcpA2.get(), rcpA.get());
00308   TEST_EQUALITY_CONST(rcpA2.has_ownership(), true);
00309   TEST_EQUALITY_CONST(rcpA2.count(), 3);
00310 }
00311 
00312 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpRCPOwned2 )
00313 {
00314   ECHO(SimpleObjectTable<A> sot);
00315   ECHO(RCP<A> rcpA = rcp(new A));
00316   TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
00317   TEST_EQUALITY_CONST(rcpA.count(), 1);
00318 
00319   ECHO(int id = sot.storeRCP(rcpA));
00320   TEST_EQUALITY_CONST(rcpA.count(), 2);
00321 
00322   ECHO(rcpA = null);
00323   ECHO(int cnt = sot.removeRCP(id));
00324   TEST_EQUALITY_CONST(cnt, 0);
00325 }
00326 
00327 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpRCPOwned3 )
00328 {
00329   ECHO(SimpleObjectTable<A> sot);
00330   ECHO(RCP<A> rcpA = rcp(new A));
00331   TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
00332   TEST_EQUALITY_CONST(rcpA.count(), 1);
00333 
00334   ECHO(int id = sot.storeRCP(rcpA));
00335   TEST_EQUALITY_CONST(rcpA.count(), 2);
00336 
00337   ECHO(int cnt = sot.removeRCP(id));
00338   TEST_EQUALITY_CONST(cnt, 1);
00339   TEST_EQUALITY_CONST(rcpA.count(), 1);
00340 }
00341 
00342 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpDestructTable )
00343 {
00344   ECHO(SimpleObjectTable<A> *psot = new SimpleObjectTable<A>);
00345   ECHO(A *pA = new A);
00346   ECHO(int id = psot->storeNew(pA));
00347 
00348   ECHO(RCP<A> rcpA = psot->getRCP(id));
00349   TEST_EQUALITY_CONST(rcpA.count(), 2);
00350   TEST_EQUALITY(rcpA.get(), pA);
00351 
00352   ECHO(delete psot);
00353   TEST_EQUALITY_CONST(rcpA.count(), 1);
00354   TEST_EQUALITY(rcpA.get(), pA);
00355 }
00356 
00357 
00358 //
00359 // Template Instantiations
00360 //
00361 
00362 
00363 #ifdef TEUCHOS_DEBUG
00364 
00365 #  define DEBUG_UNIT_TEST_GROUP( T ) \
00366 
00367 #else
00368 
00369 #  define DEBUG_UNIT_TEST_GROUP( T )
00370 
00371 #endif
00372 
00373 
00374 #define UNIT_TEST_GROUP( T ) \
00375   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SimpleObjectTable, storeNew, T ) \
00376   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SimpleObjectTable, storeRCP, T ) \
00377   DEBUG_UNIT_TEST_GROUP( T )
00378 
00379 #define UNIT_TEST_GROUP_PAIR( T1, T2 ) \
00380   TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( SimpleObjectTable, storeCastedRCP, T1, T2 )
00381 
00382 #define UNIT_TEST_GROUP_PAIR_SYM( T1, T2 ) \
00383   UNIT_TEST_GROUP_PAIR( T1, T2 ) \
00384   UNIT_TEST_GROUP_PAIR( T2, T1 )
00385 
00386 
00387 UNIT_TEST_GROUP(A)
00388 UNIT_TEST_GROUP(B1)
00389 UNIT_TEST_GROUP(B2)
00390 UNIT_TEST_GROUP(C)
00391 
00392 UNIT_TEST_GROUP_PAIR(A, A)
00393 UNIT_TEST_GROUP_PAIR(B1, B1)
00394 UNIT_TEST_GROUP_PAIR(B2, B2)
00395 UNIT_TEST_GROUP_PAIR(C, C)
00396 
00397 UNIT_TEST_GROUP_PAIR_SYM(A, B1)
00398 UNIT_TEST_GROUP_PAIR_SYM(A, B2)
00399 UNIT_TEST_GROUP_PAIR_SYM(A, C)
00400 UNIT_TEST_GROUP_PAIR_SYM(B1, B2)
00401 UNIT_TEST_GROUP_PAIR_SYM(B1, C)
00402 UNIT_TEST_GROUP_PAIR_SYM(B2, C)
00403 
00404 
00405 } // namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 09:57:28 2011 for Teuchos Package Browser (Single Doxygen Collection) by  doxygen 1.6.3