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 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00039 //
00040 // ***********************************************************************
00041 // @HEADER
00042 */
00043 
00044 #include "Teuchos_SimpleObjectTable.hpp"
00045 #include "Teuchos_RCP.hpp"
00046 
00047 #include "TestClasses.hpp"
00048 #include "Teuchos_UnitTestHarness.hpp"
00049 
00050 
00051 namespace {
00052 
00053 
00054 using Teuchos::null;
00055 using Teuchos::RCP;
00056 using Teuchos::rcp;
00057 using Teuchos::RangeError;
00058 using Teuchos::NullReferenceError;
00059 using Teuchos::m_bad_cast;
00060 using Teuchos::SimpleObjectTable;
00061 
00062 
00063 /* SimpleObjectTable::storeNew() */
00064 
00065 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( SimpleObjectTable, storeNew, T )
00066 {
00067   ECHO(SimpleObjectTable<T> sot);
00068   ECHO(int id = sot.storeNew(new T));
00069   TEST_EQUALITY_CONST(id, 0);
00070   TEST_EQUALITY_CONST(nonnull(sot.getRCP(id)), true);
00071   TEST_EQUALITY_CONST(is_null(sot.getRCP(id)), false);
00072 }
00073 
00074 TEUCHOS_UNIT_TEST( SimpleObjectTable, storeNewNull )
00075 {
00076   ECHO(SimpleObjectTable<A> sot);
00077   TEST_THROW(sot.storeNew(NULL), NullReferenceError); 
00078 }
00079 
00080 
00081 /* SimpleObjectTable::storeRCP() */
00082 
00083 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( SimpleObjectTable, storeRCP, T )
00084 {
00085   ECHO(SimpleObjectTable<T> sot);
00086   ECHO(RCP<T> rcpT = rcp(new T));
00087   TEST_EQUALITY_CONST(nonnull(rcpT), true);
00088   TEST_EQUALITY_CONST(is_null(rcpT), false);
00089   ECHO(int id = sot.storeRCP(rcpT));
00090   TEST_EQUALITY_CONST(id, 0);
00091   ECHO(RCP<T> rcpT2 = sot.getRCP(id));
00092   TEST_EQUALITY_CONST(nonnull(rcpT2), true);
00093   TEST_EQUALITY_CONST(is_null(rcpT2), false);
00094   TEST_EQUALITY(rcpT.get(), rcpT2.get());
00095 }
00096 
00097 TEUCHOS_UNIT_TEST( SimpleObjectTable, storeRCPNull1 )
00098 {
00099   ECHO(SimpleObjectTable<A> sot);
00100   ECHO(RCP<A> rcpA);
00101   TEST_THROW(sot.storeRCP(rcpA), NullReferenceError);
00102 }
00103 
00104 TEUCHOS_UNIT_TEST( SimpleObjectTable, storeRCPNull2 )
00105 {
00106   ECHO(SimpleObjectTable<A> sot);
00107   ECHO(A *a=NULL);
00108   TEST_THROW(sot.storeRCP(rcp(a)), NullReferenceError);
00109 }
00110 
00111 TEUCHOS_UNIT_TEST( SimpleObjectTable, storeRCPNull3 )
00112 {
00113   ECHO(SimpleObjectTable<A> sot);
00114   TEST_THROW(sot.storeRCP(Teuchos::null), NullReferenceError);
00115 }
00116 
00117 
00118 /* SimpleObjectTable::removeRCP() */
00119 
00120 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPFromNew )
00121 {
00122   ECHO(SimpleObjectTable<A> sot);
00123   ECHO(int id = sot.storeNew(new A));
00124   TEST_EQUALITY_CONST(id, 0);
00125   ECHO(sot.removeRCP(id));
00126   TEST_EQUALITY_CONST(id, -1);
00127 }
00128 
00129 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPFromRCP )
00130 {
00131   ECHO(SimpleObjectTable<A> sot);
00132   ECHO(RCP<A> rcpA = rcp(new A));
00133   ECHO(int id = sot.storeRCP(rcpA));
00134   TEST_EQUALITY_CONST(id, 0);
00135   ECHO(sot.removeRCP(id));
00136   TEST_EQUALITY_CONST(id, -1);
00137 }
00138 
00139 #ifdef TEUCHOS_DEBUG
00140 
00141 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPInvalid1 )
00142 {
00143   ECHO(SimpleObjectTable<A> sot);
00144   ECHO(int id = -1);
00145   TEST_THROW(sot.removeRCP(id), RangeError);
00146 }
00147 
00148 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPInvalid2 )
00149 {
00150   ECHO(SimpleObjectTable<A> sot);
00151   ECHO(int id = -2);
00152   TEST_THROW(sot.removeRCP(id), RangeError);
00153 }
00154 
00155 TEUCHOS_UNIT_TEST( SimpleObjectTable, removeRCPInvalid3 )
00156 {
00157   ECHO(SimpleObjectTable<A> sot);
00158   ECHO(int id = 0);
00159   TEST_THROW(sot.removeRCP(id), RangeError);
00160 }
00161 
00162 #endif /* TEUCHOS_DEBUG */
00163 
00164 
00165 /* SimpleObjectTable::getRCP() */
00166 
00167 #ifdef TEUCHOS_DEBUG
00168 
00169 TEUCHOS_UNIT_TEST( SimpleObjectTable, getRCPInvalid1 )
00170 {
00171   ECHO(SimpleObjectTable<A> sot);
00172   ECHO(int id = -1);
00173   TEST_THROW(sot.getRCP(id), RangeError);
00174 }
00175 
00176 TEUCHOS_UNIT_TEST( SimpleObjectTable, getRCPInvalid2 )
00177 {
00178   ECHO(SimpleObjectTable<A> sot);
00179   ECHO(int id = -2);
00180   TEST_THROW(sot.getRCP(id), RangeError);
00181 }
00182 
00183 TEUCHOS_UNIT_TEST( SimpleObjectTable, getRCPInvalid3 )
00184 {
00185   ECHO(SimpleObjectTable<A> sot);
00186   ECHO(int id = 0);
00187   TEST_THROW(sot.getRCP(id), RangeError);
00188 }
00189 
00190 #endif /* TEUCHOS_DEBUG */
00191 
00192 TEUCHOS_UNIT_TEST( SimpleObjectTable, getRCPInvalid4 )
00193 {
00194   ECHO(SimpleObjectTable<A> sot);
00195   ECHO(int id = sot.storeNew(new A));
00196   TEST_EQUALITY_CONST(id, 0);
00197   ECHO(int id2 = sot.storeNew(new A));
00198   TEST_EQUALITY_CONST(id2, 1);
00199   ECHO(int id3 = id);
00200   ECHO(sot.removeRCP(id));
00201   TEST_THROW(sot.getRCP(id3), RangeError);
00202 }
00203 
00204 
00205 /* SimpleObjectTable::storeCastedRCP() */
00206 
00207 TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL( SimpleObjectTable, storeCastedRCP, T1, T2 )
00208 {
00209   ECHO(SimpleObjectTable<T2> sot);
00210   ECHO(RCP<T1> rcpT1 = rcp(new T1));
00211   ECHO(T2 *pT2 = dynamic_cast<T2*>(rcpT1.get()));
00212   if (pT2 == NULL) {
00213     TEST_THROW(sot.storeCastedRCP(rcpT1), m_bad_cast);
00214   } else {
00215     ECHO(int id = sot.storeCastedRCP(rcpT1));
00216     TEST_EQUALITY_CONST(id, 0);
00217     TEST_EQUALITY_CONST(nonnull(sot.getRCP(id)), true);
00218     TEST_EQUALITY_CONST(rcpT1.shares_resource(sot.getRCP(id)), true);
00219   }
00220 }
00221 
00222 
00223 /* SimpleObjectTable::purge() */
00224 
00225 #ifdef TEUCHOS_DEBUG
00226 
00227 TEUCHOS_UNIT_TEST( SimpleObjectTable, purge )
00228 {
00229   ECHO(SimpleObjectTable<A> sot);
00230   ECHO(int id = sot.storeNew(new A));
00231   TEST_EQUALITY_CONST(nonnull(sot.getRCP(id)), true);
00232   ECHO(sot.purge());
00233   TEST_THROW(sot.getRCP(id), RangeError);
00234 }
00235 
00236 #endif /* TEUCHOS_DEBUG */
00237 
00238 
00239 /* SimpleObjectTable's freedIndices table */
00240 
00241 TEUCHOS_UNIT_TEST( SimpleObjectTable, recycleIndex1 )
00242 {
00243   ECHO(SimpleObjectTable<A> sot);
00244   ECHO(int id = sot.storeNew(new A));
00245   TEST_EQUALITY_CONST(id, 0);
00246   ECHO(sot.removeRCP(id));
00247   ECHO(int id2 = sot.storeNew(new A));
00248   TEST_EQUALITY_CONST(id2, 0);
00249 }
00250 
00251 TEUCHOS_UNIT_TEST( SimpleObjectTable, recycleIndex2 )
00252 {
00253   ECHO(SimpleObjectTable<A> sot);
00254   ECHO(int id = sot.storeNew(new A));
00255   TEST_EQUALITY_CONST(id, 0);
00256   ECHO(int id2 = sot.storeNew(new A));
00257   TEST_EQUALITY_CONST(id2, 1);
00258   ECHO(sot.removeRCP(id));
00259   ECHO(int id3 = sot.storeNew(new A));
00260   TEST_EQUALITY_CONST(id3, 0);
00261 }
00262 
00263 TEUCHOS_UNIT_TEST( SimpleObjectTable, recycleIndex3 )
00264 {
00265   ECHO(SimpleObjectTable<A> sot);
00266   ECHO(int id = sot.storeNew(new A));
00267   TEST_EQUALITY_CONST(id, 0);
00268   ECHO(int id2 = sot.storeNew(new A));
00269   TEST_EQUALITY_CONST(id2, 1);
00270   ECHO(sot.removeRCP(id2));
00271   ECHO(int id3 = sot.storeNew(new A));
00272   TEST_EQUALITY_CONST(id3, 1);
00273 }
00274 
00275 
00276 /* SimpleObjectTable's RCP counts */
00277 
00278 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpNewShared1 )
00279 {
00280   ECHO(SimpleObjectTable<A> sot);
00281   ECHO(A a);
00282   ECHO(int id = sot.storeNew(&a, false));
00283 
00284   ECHO(RCP<A> rcpA = sot.getRCP(id));
00285   TEST_EQUALITY(rcpA.get(), &a);
00286   TEST_EQUALITY_CONST(rcpA.has_ownership(), false);
00287   TEST_EQUALITY_CONST(rcpA.count(), 2);
00288 
00289   ECHO(rcpA = null);
00290   ECHO(int cnt = sot.removeRCP(id));
00291   TEST_EQUALITY_CONST(cnt, 0);
00292 }
00293 
00294 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpNewShared2 )
00295 {
00296   ECHO(SimpleObjectTable<A> sot);
00297   ECHO(A a);
00298   ECHO(int id = sot.storeNew(&a, false));
00299 
00300   ECHO(RCP<A> rcpA = sot.getRCP(id));
00301   TEST_EQUALITY(rcpA.get(), &a);
00302   TEST_EQUALITY_CONST(rcpA.has_ownership(), false);
00303   TEST_EQUALITY_CONST(rcpA.count(), 2);
00304 
00305   ECHO(int cnt = sot.removeRCP(id));
00306   TEST_EQUALITY_CONST(cnt, 1);
00307   TEST_EQUALITY_CONST(rcpA.count(), 1);
00308 }
00309 
00310 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpNewOwned1 )
00311 {
00312   ECHO(SimpleObjectTable<A> sot);
00313   ECHO(int id = sot.storeNew(new A));
00314 
00315   ECHO(RCP<A> rcpA = sot.getRCP(id));
00316   TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
00317   TEST_EQUALITY_CONST(rcpA.count(), 2);
00318 
00319   ECHO(rcpA = null);
00320   ECHO(int cnt = sot.removeRCP(id));
00321   TEST_EQUALITY_CONST(cnt, 0);
00322 }
00323 
00324 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpNewOwned2 )
00325 {
00326   ECHO(SimpleObjectTable<A> sot);
00327   ECHO(int id = sot.storeNew(new A));
00328 
00329   ECHO(RCP<A> rcpA = sot.getRCP(id));
00330   TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
00331   TEST_EQUALITY_CONST(rcpA.count(), 2);
00332 
00333   ECHO(int cnt = sot.removeRCP(id));
00334   TEST_EQUALITY_CONST(cnt, 1);
00335   TEST_EQUALITY_CONST(rcpA.count(), 1);
00336 }
00337 
00338 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpRCPOwned1 )
00339 {
00340   ECHO(SimpleObjectTable<A> sot);
00341   ECHO(RCP<A> rcpA = rcp(new A));
00342   TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
00343   TEST_EQUALITY_CONST(rcpA.count(), 1);
00344 
00345   ECHO(int id = sot.storeRCP(rcpA));
00346   TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
00347   TEST_EQUALITY_CONST(rcpA.count(), 2);
00348 
00349   ECHO(RCP<A> rcpA2 = sot.getRCP(id));
00350   TEST_EQUALITY(rcpA2.get(), rcpA.get());
00351   TEST_EQUALITY_CONST(rcpA2.has_ownership(), true);
00352   TEST_EQUALITY_CONST(rcpA2.count(), 3);
00353 }
00354 
00355 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpRCPOwned2 )
00356 {
00357   ECHO(SimpleObjectTable<A> sot);
00358   ECHO(RCP<A> rcpA = rcp(new A));
00359   TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
00360   TEST_EQUALITY_CONST(rcpA.count(), 1);
00361 
00362   ECHO(int id = sot.storeRCP(rcpA));
00363   TEST_EQUALITY_CONST(rcpA.count(), 2);
00364 
00365   ECHO(rcpA = null);
00366   ECHO(int cnt = sot.removeRCP(id));
00367   TEST_EQUALITY_CONST(cnt, 0);
00368 }
00369 
00370 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpRCPOwned3 )
00371 {
00372   ECHO(SimpleObjectTable<A> sot);
00373   ECHO(RCP<A> rcpA = rcp(new A));
00374   TEST_EQUALITY_CONST(rcpA.has_ownership(), true);
00375   TEST_EQUALITY_CONST(rcpA.count(), 1);
00376 
00377   ECHO(int id = sot.storeRCP(rcpA));
00378   TEST_EQUALITY_CONST(rcpA.count(), 2);
00379 
00380   ECHO(int cnt = sot.removeRCP(id));
00381   TEST_EQUALITY_CONST(cnt, 1);
00382   TEST_EQUALITY_CONST(rcpA.count(), 1);
00383 }
00384 
00385 TEUCHOS_UNIT_TEST( SimpleObjectTable, rcpDestructTable )
00386 {
00387   ECHO(SimpleObjectTable<A> *psot = new SimpleObjectTable<A>);
00388   ECHO(A *pA = new A);
00389   ECHO(int id = psot->storeNew(pA));
00390 
00391   ECHO(RCP<A> rcpA = psot->getRCP(id));
00392   TEST_EQUALITY_CONST(rcpA.count(), 2);
00393   TEST_EQUALITY(rcpA.get(), pA);
00394 
00395   ECHO(delete psot);
00396   TEST_EQUALITY_CONST(rcpA.count(), 1);
00397   TEST_EQUALITY(rcpA.get(), pA);
00398 }
00399 
00400 
00401 //
00402 // Template Instantiations
00403 //
00404 
00405 
00406 #ifdef TEUCHOS_DEBUG
00407 
00408 #  define DEBUG_UNIT_TEST_GROUP( T ) \
00409 
00410 #else
00411 
00412 #  define DEBUG_UNIT_TEST_GROUP( T )
00413 
00414 #endif
00415 
00416 
00417 #define UNIT_TEST_GROUP( T ) \
00418   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SimpleObjectTable, storeNew, T ) \
00419   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( SimpleObjectTable, storeRCP, T ) \
00420   DEBUG_UNIT_TEST_GROUP( T )
00421 
00422 #define UNIT_TEST_GROUP_PAIR( T1, T2 ) \
00423   TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT( SimpleObjectTable, storeCastedRCP, T1, T2 )
00424 
00425 #define UNIT_TEST_GROUP_PAIR_SYM( T1, T2 ) \
00426   UNIT_TEST_GROUP_PAIR( T1, T2 ) \
00427   UNIT_TEST_GROUP_PAIR( T2, T1 )
00428 
00429 
00430 UNIT_TEST_GROUP(A)
00431 UNIT_TEST_GROUP(B1)
00432 UNIT_TEST_GROUP(B2)
00433 UNIT_TEST_GROUP(C)
00434 
00435 UNIT_TEST_GROUP_PAIR(A, A)
00436 UNIT_TEST_GROUP_PAIR(B1, B1)
00437 UNIT_TEST_GROUP_PAIR(B2, B2)
00438 UNIT_TEST_GROUP_PAIR(C, C)
00439 
00440 UNIT_TEST_GROUP_PAIR_SYM(A, B1)
00441 UNIT_TEST_GROUP_PAIR_SYM(A, B2)
00442 UNIT_TEST_GROUP_PAIR_SYM(A, C)
00443 UNIT_TEST_GROUP_PAIR_SYM(B1, B2)
00444 UNIT_TEST_GROUP_PAIR_SYM(B1, C)
00445 UNIT_TEST_GROUP_PAIR_SYM(B2, C)
00446 
00447 
00448 } // namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines