Teuchos Package Browser (Single Doxygen Collection) Version of the Day
SimpleObjTbl_UnitTests.cpp
Go to the documentation of this file.
00001 /*
00002 // @HEADER
00003 // ***********************************************************************
00004 //
00005 //                    Teuchos: Common Tools Package
00006 //                 Copyright (2004) Sandia Corporation
00007 //
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 //
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA
00025 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00026 //
00027 // ***********************************************************************
00028 // @HEADER
00029 */
00030 
00031 #include "Teuchos_SimpleObjectTable.hpp"
00032 #include "Teuchos_RCP.hpp"
00033 
00034 #include "TestClasses.hpp"
00035 #include "Teuchos_UnitTestHarness.hpp"
00036 
00037 
00038 namespace {
00039 
00040 
00041 using Teuchos::null;
00042 using Teuchos::RCP;
00043 using Teuchos::rcp;
00044 using Teuchos::RangeError;
00045 using Teuchos::NullReferenceError;
00046 using Teuchos::m_bad_cast;
00047 using Teuchos::SimpleObjectTable;
00048 
00049 
00050 /* SimpleObjectTable::storeNew() */
00051 
00052 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( SimpleObjectTable, storeNew, T )
00053 {
00054   ECHO(SimpleObjectTable<T> sot);
00055   ECHO(int id = sot.storeNew(new T));
00056   TEST_EQUALITY_CONST(id, 0);
00057   TEST_EQUALITY_CONST(nonnull(sot.getRCP(id)), true);
00058   TEST_EQUALITY_CONST(is_null(sot.getRCP(id)), false);
00059 }
00060 
00061 TEUCHOS_UNIT_TEST( SimpleObjectTable, storeNewNull )
00062 {
00063   ECHO(SimpleObjectTable<A> sot);
00064   TEST_THROW(sot.storeNew(NULL), NullReferenceError); 
00065 }
00066 
00067 
00068 /* SimpleObjectTable::storeRCP() */
00069 
00070 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( SimpleObjectTable, storeRCP, T )
00071 {
00072   ECHO(SimpleObjectTable<T> sot);
00073   ECHO(RCP<T> rcpT = rcp(new T));
00074   TEST_EQUALITY_CONST(nonnull(rcpT), true);
00075   TEST_EQUALITY_CONST(is_null(rcpT), false);
00076   ECHO(int id = sot.storeRCP(rcpT));
00077   TEST_EQUALITY_CONST(id, 0);
00078   ECHO(RCP<T> rcpT2 = sot.getRCP(id));
00079   TEST_EQUALITY_CONST(nonnull(rcpT2), true);
00080   TEST_EQUALITY_CONST(is_null(rcpT2), false);
00081   TEST_EQUALITY(rcpT.get(), rcpT2.get());
00082 }
00083 
00084 TEUCHOS_UNIT_TEST( SimpleObjectTable, storeRCPNull1 )
00085 {
00086   ECHO(SimpleObjectTable<A> sot);
00087   ECHO(RCP<A> rcpA);
00088   TEST_THROW(sot.storeRCP(rcpA), NullReferenceError);
00089 }
00090 
00091 TEUCHOS_UNIT_TEST( SimpleObjectTable, storeRCPNull2 )
00092 {
00093   ECHO(SimpleObjectTable<A> sot);
00094   ECHO(A *a=NULL);
00095   TEST_THROW(sot.storeRCP(rcp(a)), NullReferenceError);
00096 }
00097 
00098 TEUCHOS_UNIT_TEST( SimpleObjectTable, storeRCPNull3 )
00099 {
00100   ECHO(SimpleObjectTable<A> sot);
00101   TEST_THROW(sot.storeRCP(Teuchos::null), NullReferenceError);
00102 }
00103 
00104 
00105 /* SimpleObjectTable::removeRCP() */
00106 
00107 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPFromNew )
00108 {
00109   ECHO(SimpleObjectTable<A> sot);
00110   ECHO(int id = sot.storeNew(new A));
00111   TEST_EQUALITY_CONST(id, 0);
00112   ECHO(sot.removeRCP(id));
00113   TEST_EQUALITY_CONST(id, -1);
00114 }
00115 
00116 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPFromRCP )
00117 {
00118   ECHO(SimpleObjectTable<A> sot);
00119   ECHO(RCP<A> rcpA = rcp(new A));
00120   ECHO(int id = sot.storeRCP(rcpA));
00121   TEST_EQUALITY_CONST(id, 0);
00122   ECHO(sot.removeRCP(id));
00123   TEST_EQUALITY_CONST(id, -1);
00124 }
00125 
00126 #ifdef TEUCHOS_DEBUG
00127 
00128 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPInvalid1 )
00129 {
00130   ECHO(SimpleObjectTable<A> sot);
00131   ECHO(int id = -1);
00132   TEST_THROW(sot.removeRCP(id), RangeError);
00133 }
00134 
00135 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPInvalid2 )
00136 {
00137   ECHO(SimpleObjectTable<A> sot);
00138   ECHO(int id = -2);
00139   TEST_THROW(sot.removeRCP(id), RangeError);
00140 }
00141 
00142 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPInvalid3 )
00143 {
00144   ECHO(SimpleObjectTable<A> sot);
00145   ECHO(int id = 0);
00146   TEST_THROW(sot.removeRCP(id), RangeError);
00147 }
00148 
00149 #endif /* TEUCHOS_DEBUG */
00150 
00151 
00152 /* SimpleObjectTable::getRCP() */
00153 
00154 #ifdef TEUCHOS_DEBUG
00155 
00156 TEUCHOS_UNIT_TEST( SimpleObjectTable, getRCPInvalid1 )
00157 {
00158   ECHO(SimpleObjectTable<A> sot);
00159   ECHO(int id = -1);
00160   TEST_THROW(sot.getRCP(id), RangeError);
00161 }
00162 
00163 TEUCHOS_UNIT_TEST( SimpleObjectTable, getRCPInvalid2 )
00164 {
00165   ECHO(SimpleObjectTable<A> sot);
00166   ECHO(int id = -2);
00167   TEST_THROW(sot.getRCP(id), RangeError);
00168 }
00169 
00170 TEUCHOS_UNIT_TEST( SimpleObjectTable, getRCPInvalid3 )
00171 {
00172   ECHO(SimpleObjectTable<A> sot);
00173   ECHO(int id = 0);
00174   TEST_THROW(sot.getRCP(id), RangeError);
00175 }
00176 
00177 #endif /* TEUCHOS_DEBUG */
00178 
00179 TEUCHOS_UNIT_TEST( SimpleObjectTable, getRCPInvalid4 )
00180 {
00181   ECHO(SimpleObjectTable<A> sot);
00182   ECHO(int id = sot.storeNew(new A));
00183   TEST_EQUALITY_CONST(id, 0);
00184   ECHO(int id2 = sot.storeNew(new A));
00185   TEST_EQUALITY_CONST(id2, 1);
00186   ECHO(int id3 = id);
00187   ECHO(sot.removeRCP(id));
00188   TEST_THROW(sot.getRCP(id3), RangeError);
00189 }
00190 
00191 
00192 /* SimpleObjectTable::storeCastedRCP() */
00193 
00194 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( SimpleObjectTable, storeCastedRCP, T1, T2 )
00195 {
00196   ECHO(SimpleObjectTable<T2> sot);
00197   ECHO(RCP<T1> rcpT1 = rcp(new T1));
00198   ECHO(T2 *pT2 = dynamic_cast<T2*>(rcpT1.get()));
00199   if (pT2 == NULL) {
00200     TEST_THROW(sot.storeCastedRCP(rcpT1), m_bad_cast);
00201   } else {
00202     ECHO(int id = sot.storeCastedRCP(rcpT1));
00203     TEST_EQUALITY_CONST(id, 0);
00204     TEST_EQUALITY_CONST(nonnull(sot.getRCP(id)), true);
00205     TEST_EQUALITY_CONST(rcpT1.shares_resource(sot.getRCP(id)), true);
00206   }
00207 }
00208 
00209 
00210 /* SimpleObjectTable::purge() */
00211 
00212 #ifdef TEUCHOS_DEBUG
00213 
00214 TEUCHOS_UNIT_TEST( SimpleObjectTable, purge )
00215 {
00216   ECHO(SimpleObjectTable<A> sot);
00217   ECHO(int id = sot.storeNew(new A));
00218   TEST_EQUALITY_CONST(nonnull(sot.getRCP(id)), true);
00219   ECHO(sot.purge());
00220   TEST_THROW(sot.getRCP(id), RangeError);
00221 }
00222 
00223 #endif /* TEUCHOS_DEBUG */
00224 
00225 
00226 /* SimpleObjectTable's freedIndices table */
00227 
00228 TEUCHOS_UNIT_TEST( SimpleObjectTable, recycleIndex1 )
00229 {
00230   ECHO(SimpleObjectTable<A> sot);
00231   ECHO(int id = sot.storeNew(new A));
00232   TEST_EQUALITY_CONST(id, 0);
00233   ECHO(sot.removeRCP(id));
00234   ECHO(int id2 = sot.storeNew(new A));
00235   TEST_EQUALITY_CONST(id2, 0);
00236 }
00237 
00238 TEUCHOS_UNIT_TEST( SimpleObjectTable, recycleIndex2 )
00239 {
00240   ECHO(SimpleObjectTable<A> sot);
00241   ECHO(int id = sot.storeNew(new A));
00242   TEST_EQUALITY_CONST(id, 0);
00243   ECHO(int id2 = sot.storeNew(new A));
00244   TEST_EQUALITY_CONST(id2, 1);
00245   ECHO(sot.removeRCP(id));
00246   ECHO(int id3 = sot.storeNew(new A));
00247   TEST_EQUALITY_CONST(id3, 0);
00248 }
00249 
00250 TEUCHOS_UNIT_TEST( SimpleObjectTable, recycleIndex3 )
00251 {
00252   ECHO(SimpleObjectTable<A> sot);
00253   ECHO(int id = sot.storeNew(new A));
00254   TEST_EQUALITY_CONST(id, 0);
00255   ECHO(int id2 = sot.storeNew(new A));
00256   TEST_EQUALITY_CONST(id2, 1);
00257   ECHO(sot.removeRCP(id2));
00258   ECHO(int id3 = sot.storeNew(new A));
00259   TEST_EQUALITY_CONST(id3, 1);
00260 }
00261 
00262 
00263 /* SimpleObjectTable's RCP counts */
00264 
00265 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpNewShared1 )
00266 {
00267   ECHO(SimpleObjectTable<A> sot);
00268   ECHO(A a);
00269   ECHO(int id = sot.storeNew(&a, false));
00270 
00271   ECHO(RCP<A> rcpA = sot.getRCP(id));
00272   TEST_EQUALITY(rcpA.get(), &a);
00273   TEST_EQUALITY_CONST(rcpA.has_ownership(), false);
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, rcpNewShared2 )
00282 {
00283   ECHO(SimpleObjectTable<A> sot);
00284   ECHO(A a);
00285   ECHO(int id = sot.storeNew(&a, false));
00286 
00287   ECHO(RCP<A> rcpA = sot.getRCP(id));
00288   TEST_EQUALITY(rcpA.get(), &a);
00289   TEST_EQUALITY_CONST(rcpA.has_ownership(), false);
00290   TEST_EQUALITY_CONST(rcpA.count(), 2);
00291 
00292   ECHO(int cnt = sot.removeRCP(id));
00293   TEST_EQUALITY_CONST(cnt, 1);
00294   TEST_EQUALITY_CONST(rcpA.count(), 1);
00295 }
00296 
00297 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpNewOwned1 )
00298 {
00299   ECHO(SimpleObjectTable<A> sot);
00300   ECHO(int id = sot.storeNew(new A));
00301 
00302   ECHO(RCP<A> rcpA = sot.getRCP(id));
00303   TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
00304   TEST_EQUALITY_CONST(rcpA.count(), 2);
00305 
00306   ECHO(rcpA = null);
00307   ECHO(int cnt = sot.removeRCP(id));
00308   TEST_EQUALITY_CONST(cnt, 0);
00309 }
00310 
00311 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpNewOwned2 )
00312 {
00313   ECHO(SimpleObjectTable<A> sot);
00314   ECHO(int id = sot.storeNew(new A));
00315 
00316   ECHO(RCP<A> rcpA = sot.getRCP(id));
00317   TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
00318   TEST_EQUALITY_CONST(rcpA.count(), 2);
00319 
00320   ECHO(int cnt = sot.removeRCP(id));
00321   TEST_EQUALITY_CONST(cnt, 1);
00322   TEST_EQUALITY_CONST(rcpA.count(), 1);
00323 }
00324 
00325 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpRCPOwned1 )
00326 {
00327   ECHO(SimpleObjectTable<A> sot);
00328   ECHO(RCP<A> rcpA = rcp(new A));
00329   TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
00330   TEST_EQUALITY_CONST(rcpA.count(), 1);
00331 
00332   ECHO(int id = sot.storeRCP(rcpA));
00333   TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
00334   TEST_EQUALITY_CONST(rcpA.count(), 2);
00335 
00336   ECHO(RCP<A> rcpA2 = sot.getRCP(id));
00337   TEST_EQUALITY(rcpA2.get(), rcpA.get());
00338   TEST_EQUALITY_CONST(rcpA2.has_ownership(), true);
00339   TEST_EQUALITY_CONST(rcpA2.count(), 3);
00340 }
00341 
00342 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpRCPOwned2 )
00343 {
00344   ECHO(SimpleObjectTable<A> sot);
00345   ECHO(RCP<A> rcpA = rcp(new A));
00346   TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
00347   TEST_EQUALITY_CONST(rcpA.count(), 1);
00348 
00349   ECHO(int id = sot.storeRCP(rcpA));
00350   TEST_EQUALITY_CONST(rcpA.count(), 2);
00351 
00352   ECHO(rcpA = null);
00353   ECHO(int cnt = sot.removeRCP(id));
00354   TEST_EQUALITY_CONST(cnt, 0);
00355 }
00356 
00357 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpRCPOwned3 )
00358 {
00359   ECHO(SimpleObjectTable<A> sot);
00360   ECHO(RCP<A> rcpA = rcp(new A));
00361   TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
00362   TEST_EQUALITY_CONST(rcpA.count(), 1);
00363 
00364   ECHO(int id = sot.storeRCP(rcpA));
00365   TEST_EQUALITY_CONST(rcpA.count(), 2);
00366 
00367   ECHO(int cnt = sot.removeRCP(id));
00368   TEST_EQUALITY_CONST(cnt, 1);
00369   TEST_EQUALITY_CONST(rcpA.count(), 1);
00370 }
00371 
00372 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpDestructTable )
00373 {
00374   ECHO(SimpleObjectTable<A> *psot = new SimpleObjectTable<A>);
00375   ECHO(A *pA = new A);
00376   ECHO(int id = psot->storeNew(pA));
00377 
00378   ECHO(RCP<A> rcpA = psot->getRCP(id));
00379   TEST_EQUALITY_CONST(rcpA.count(), 2);
00380   TEST_EQUALITY(rcpA.get(), pA);
00381 
00382   ECHO(delete psot);
00383   TEST_EQUALITY_CONST(rcpA.count(), 1);
00384   TEST_EQUALITY(rcpA.get(), pA);
00385 }
00386 
00387 
00388 //
00389 // Template Instantiations
00390 //
00391 
00392 
00393 #ifdef TEUCHOS_DEBUG
00394 
00395 #  define DEBUG_UNIT_TEST_GROUP( T ) \
00396 
00397 #else
00398 
00399 #  define DEBUG_UNIT_TEST_GROUP( T )
00400 
00401 #endif
00402 
00403 
00404 #define UNIT_TEST_GROUP( T ) \
00405   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SimpleObjectTable, storeNew, T ) \
00406   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SimpleObjectTable, storeRCP, T ) \
00407   DEBUG_UNIT_TEST_GROUP( T )
00408 
00409 #define UNIT_TEST_GROUP_PAIR( T1, T2 ) \
00410   TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( SimpleObjectTable, storeCastedRCP, T1, T2 )
00411 
00412 #define UNIT_TEST_GROUP_PAIR_SYM( T1, T2 ) \
00413   UNIT_TEST_GROUP_PAIR( T1, T2 ) \
00414   UNIT_TEST_GROUP_PAIR( T2, T1 )
00415 
00416 
00417 UNIT_TEST_GROUP(A)
00418 UNIT_TEST_GROUP(B1)
00419 UNIT_TEST_GROUP(B2)
00420 UNIT_TEST_GROUP(C)
00421 
00422 UNIT_TEST_GROUP_PAIR(A, A)
00423 UNIT_TEST_GROUP_PAIR(B1, B1)
00424 UNIT_TEST_GROUP_PAIR(B2, B2)
00425 UNIT_TEST_GROUP_PAIR(C, C)
00426 
00427 UNIT_TEST_GROUP_PAIR_SYM(A, B1)
00428 UNIT_TEST_GROUP_PAIR_SYM(A, B2)
00429 UNIT_TEST_GROUP_PAIR_SYM(A, C)
00430 UNIT_TEST_GROUP_PAIR_SYM(B1, B2)
00431 UNIT_TEST_GROUP_PAIR_SYM(B1, C)
00432 UNIT_TEST_GROUP_PAIR_SYM(B2, C)
00433 
00434 
00435 } // namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines