RCPNodeHandle_UnitTests.cpp

Go to the documentation of this file.
00001 #include "Teuchos_UnitTestHarness.hpp"
00002 #include "Teuchos_RCPNode.hpp"
00003 #include "Teuchos_getConst.hpp"
00004 #include "Teuchos_TypeNameTraits.hpp"
00005 #include "TestClasses.hpp"
00006 
00007 
00008 namespace Teuchos {
00009 
00010 
00011 template<class T>
00012 class MockRCP {
00013 public:
00014   T* access_private_ptr() const
00015     {
00016       return (T*)(0x777777); // Just some bogus address printed to out
00017     }
00018 };
00019 
00020 
00021 template<class T>
00022 RCPNode* basicRCPNodeNoAlloc(T* p, const bool has_ownership)
00023 {
00024   RCPNodeTmpl<T,DeallocDelete<T> > *rcpNode = 
00025     new RCPNodeTmpl<T,DeallocDelete<T> >(p, DeallocDelete<T>(), has_ownership);
00026   return rcpNode;
00027 }
00028 
00029 
00030 template<class T>
00031 RCPNode* basicRCPNode(const bool has_ownership, T **p_out = 0)
00032 {
00033   T *p = new T;
00034   if (p_out)
00035     *p_out = p;
00036   RCPNode *rcpNode = basicRCPNodeNoAlloc<T>(p, has_ownership);
00037   return rcpNode;
00038 }
00039 
00040 
00041 void deleteRCPNode( RCPNode **node )
00042 {
00043   TEUCHOS_ASSERT(node);
00044   TEUCHOS_ASSERT(*node);
00045   (*node)->delete_obj();
00046   delete (*node);
00047   *node = 0;
00048 }
00049 
00050 
00051 template<class T>
00052 RCPNodeHandle basicRCPNodeHandle(const bool has_ownership, T **p_out = 0)
00053 {
00054   T *p = 0;
00055   RCPNode *rcpNode = basicRCPNode(has_ownership, &p);
00056   if (p_out)
00057     *p_out = p;
00058 #ifdef TEUCHOS_DEBUG
00059   return RCPNodeHandle(rcpNode, p, typeName(*p), concreteTypeName(*p), has_ownership);
00060 #else
00061   return RCPNodeHandle(rcpNode);
00062 #endif
00063 }
00064 
00065 
00066 TEUCHOS_STATIC_SETUP()
00067 {
00068 }
00069 
00070 
00071 //
00072 // Non-templated tests
00073 //
00074 
00075 
00076 TEUCHOS_UNIT_TEST( RCPNodeHandle, assignSelf )
00077 {
00078   RCPNodeHandle nodeRef;
00079   nodeRef = nodeRef;
00080 }
00081 
00082 
00083 TEUCHOS_UNIT_TEST( RCPNodeHandle, defaultConstruct)
00084 {
00085   RCPNodeHandle nodeRef;
00086   TEST_EQUALITY_CONST( nodeRef.count(), 0 );
00087   TEST_EQUALITY_CONST( nodeRef.has_ownership(), false );
00088   nodeRef.has_ownership(true);
00089   TEST_EQUALITY_CONST( nodeRef.has_ownership(), false );
00090 #ifdef TEUCHOS_DEBUG
00091   TEST_EQUALITY_CONST( nodeRef.get_base_obj_map_key_void_ptr(), static_cast<void*>(0) );
00092   TEST_THROW({nodeRef.set_extra_data(any(),"", PRE_DESTROY, true);},
00093     NullReferenceError);
00094   TEST_THROW({any &a = nodeRef.get_extra_data("int","blob"); (void)a;},
00095     NullReferenceError);
00096   TEST_THROW({const any &a = getConst(nodeRef).get_extra_data("int","blob"); (void)a;},
00097     NullReferenceError);
00098   TEST_THROW({any *a = nodeRef.get_optional_extra_data("int","blob"); (void)a;},
00099     NullReferenceError);
00100   TEST_THROW({const any *a = getConst(nodeRef).get_optional_extra_data("int","blob"); (void)a;},
00101     NullReferenceError);
00102 #endif // TEUCHOS_DEBUG
00103 }
00104 
00105 
00106 #ifdef TEUCHOS_DEBUG
00107 
00108 
00109 TEUCHOS_UNIT_TEST( RCPNodeHandle, add_New_RCPNode_basic )
00110 {
00111 
00112   typedef RCPNodeTracer::RCPNodeStatistics RCPNodeStatistics;
00113 
00114   SET_RCPNODE_TRACING();
00115 
00116   RCPNode *node = basicRCPNode<A>(true);
00117 
00118   const int numActiveNodesBase = RCPNodeTracer::numActiveRCPNodes();
00119   const RCPNodeStatistics rcpNodeStatisticsBefore = RCPNodeTracer::getRCPNodeStatistics();
00120 
00121   out << std::endl;
00122   ECHO(RCPNodeTracer::addNewRCPNode(node, "dummy"));
00123 
00124   out << std::endl;
00125   TEST_EQUALITY(RCPNodeTracer::numActiveRCPNodes(), numActiveNodesBase+1);
00126 
00127   out << std::endl;
00128   const RCPNodeStatistics rcpNodeStatistics1 = RCPNodeTracer::getRCPNodeStatistics();
00129   TEST_COMPARE(rcpNodeStatistics1.maxNumRCPNodes, >=,
00130     rcpNodeStatisticsBefore.maxNumRCPNodes);
00131   TEST_EQUALITY(rcpNodeStatistics1.totalNumRCPNodeAllocations,
00132     rcpNodeStatisticsBefore.totalNumRCPNodeAllocations+1);
00133   TEST_EQUALITY(rcpNodeStatistics1.totalNumRCPNodeDeletions,
00134     rcpNodeStatisticsBefore.totalNumRCPNodeDeletions);
00135 
00136   out << std::endl;
00137   ECHO(RCPNodeTracer::removeRCPNode(node));
00138 
00139   out << std::endl;
00140   TEST_EQUALITY(RCPNodeTracer::numActiveRCPNodes(), numActiveNodesBase);
00141 
00142   out << std::endl;
00143   const RCPNodeStatistics rcpNodeStatistics2 = RCPNodeTracer::getRCPNodeStatistics();
00144   TEST_COMPARE(rcpNodeStatistics2.maxNumRCPNodes, >=,
00145     rcpNodeStatisticsBefore.maxNumRCPNodes);
00146   TEST_EQUALITY(rcpNodeStatistics2.totalNumRCPNodeAllocations,
00147     rcpNodeStatisticsBefore.totalNumRCPNodeAllocations+1);
00148   TEST_EQUALITY(rcpNodeStatistics2.totalNumRCPNodeDeletions,
00149     rcpNodeStatisticsBefore.totalNumRCPNodeDeletions+1);
00150 
00151   out << std::endl;
00152   std::ostringstream statsOut_oss;
00153   RCPNodeTracer::printRCPNodeStatistics(rcpNodeStatistics2, statsOut_oss);
00154   std::ostringstream expectedStatsOut_oss;
00155   expectedStatsOut_oss
00156     << "\n***"
00157     << "\n*** RCPNode Tracing statistics:"
00158     << "\n**\n"
00159     << "\n    maxNumRCPNodes             = "<<rcpNodeStatistics2.maxNumRCPNodes
00160     << "\n    totalNumRCPNodeAllocations = "<<rcpNodeStatistics2.totalNumRCPNodeAllocations
00161     << "\n    totalNumRCPNodeDeletions   = "<<rcpNodeStatistics2.totalNumRCPNodeDeletions
00162     << "\n";
00163   TEST_EQUALITY(statsOut_oss.str(), expectedStatsOut_oss.str());
00164 
00165   deleteRCPNode(&node);
00166 
00167 }
00168 
00169 
00170 TEUCHOS_UNIT_TEST( RCPNodeHandle, add_New_RCPNode_add_owning_twice_error )
00171 {
00172   SET_RCPNODE_TRACING();
00173   A *a_ptr = new A;
00174   RCPNode *node1 = basicRCPNodeNoAlloc<A>(a_ptr, true);
00175   const int numActiveNodesBase = RCPNodeTracer::numActiveRCPNodes();
00176   ECHO(RCPNodeTracer::addNewRCPNode(node1, "dummy1"));
00177   TEST_EQUALITY(RCPNodeTracer::numActiveRCPNodes(), numActiveNodesBase+1);
00178   RCPNode *node2 = basicRCPNodeNoAlloc<A>(a_ptr, true);
00179   TEST_THROW(RCPNodeTracer::addNewRCPNode(node2, "dummy2"), DuplicateOwningRCPError);
00180   ECHO(RCPNodeTracer::removeRCPNode(node1));
00181   deleteRCPNode(&node1);
00182   TEST_EQUALITY(RCPNodeTracer::numActiveRCPNodes(), numActiveNodesBase);
00183   node2->has_ownership(false);
00184   deleteRCPNode(&node2);
00185 }
00186 
00187 
00188 TEUCHOS_UNIT_TEST( RCPNodeHandle, add_New_RCPNode_add_nonowning_twice_okay_1 )
00189 {
00190   SET_RCPNODE_TRACING();
00191   A *a_ptr = new A;
00192   RCPNode *node1 = basicRCPNodeNoAlloc<A>(a_ptr, true);
00193   const int numActiveNodesBase = RCPNodeTracer::numActiveRCPNodes();
00194   ECHO(RCPNodeTracer::addNewRCPNode(node1, "dummy1"));
00195   TEST_EQUALITY(RCPNodeTracer::numActiveRCPNodes(), numActiveNodesBase+1);
00196   RCPNode *node2 = basicRCPNodeNoAlloc<A>(a_ptr, false);
00197   ECHO(RCPNodeTracer::addNewRCPNode(node2, "dummy2"));
00198   TEST_EQUALITY(RCPNodeTracer::numActiveRCPNodes(), numActiveNodesBase+2);
00199   TEST_EQUALITY(RCPNodeTracer::getExistingRCPNode(a_ptr), node1);
00200   ECHO(RCPNodeTracer::removeRCPNode(node2));
00201   deleteRCPNode(&node2);
00202   TEST_EQUALITY(RCPNodeTracer::numActiveRCPNodes(), numActiveNodesBase+1);
00203   ECHO(RCPNodeTracer::removeRCPNode(node1));
00204   deleteRCPNode(&node1);
00205   TEST_EQUALITY(RCPNodeTracer::numActiveRCPNodes(), numActiveNodesBase);
00206 }
00207 
00208 
00209 TEUCHOS_UNIT_TEST( RCPNodeHandle, add_New_RCPNode_add_nonowning_twice_okay_2 )
00210 {
00211   SET_RCPNODE_TRACING();
00212   A *a_ptr = new A;
00213   RCPNode *node1 = basicRCPNodeNoAlloc<A>(a_ptr, false);
00214   const int numActiveNodesBase = RCPNodeTracer::numActiveRCPNodes();
00215   ECHO(RCPNodeTracer::addNewRCPNode(node1, "dummy1"));
00216   TEST_EQUALITY(RCPNodeTracer::numActiveRCPNodes(), numActiveNodesBase+1);
00217   RCPNode *node2 = basicRCPNodeNoAlloc<A>(a_ptr, true);
00218   ECHO(RCPNodeTracer::addNewRCPNode(node2, "dummy2"));
00219   TEST_EQUALITY(RCPNodeTracer::numActiveRCPNodes(), numActiveNodesBase+2);
00220   TEST_EQUALITY(RCPNodeTracer::getExistingRCPNode(a_ptr), node2);
00221   ECHO(RCPNodeTracer::removeRCPNode(node2));
00222   deleteRCPNode(&node2);
00223   TEST_EQUALITY(RCPNodeTracer::numActiveRCPNodes(), numActiveNodesBase+1);
00224   ECHO(RCPNodeTracer::removeRCPNode(node1));
00225   deleteRCPNode(&node1);
00226   TEST_EQUALITY(RCPNodeTracer::numActiveRCPNodes(), numActiveNodesBase);
00227 }
00228 
00229 
00230 TEUCHOS_UNIT_TEST( RCPNodeHandle, add_New_RCPNode_add_two_nodes_same_obj )
00231 {
00232   SET_RCPNODE_TRACING();
00233   ECHO(C *c_ptr = new C);
00234   ECHO(RCPNode *node_c = basicRCPNodeNoAlloc<C>(c_ptr, true));
00235   ECHO(RCPNode *node_b1 = basicRCPNodeNoAlloc<B1>(c_ptr, true));
00236   ECHO(const int numActiveNodesBase = RCPNodeTracer::numActiveRCPNodes());
00237   ECHO(RCPNodeTracer::addNewRCPNode(node_c, "dummy"));
00238   TEST_EQUALITY(RCPNodeTracer::numActiveRCPNodes(), numActiveNodesBase+1);
00239 #ifdef HAS_TEUCHOS_GET_BASE_OBJ_VOID_PTR
00240   // We can detect that these are the same object!
00241   TEST_THROW(RCPNodeTracer::addNewRCPNode(node_b1, "dummy"), DuplicateOwningRCPError);
00242 #else
00243   // We can not detect if these are the same object!
00244   ECHO(RCPNodeTracer::addNewRCPNode(node_b1, "dummy"));
00245   TEST_EQUALITY(RCPNodeTracer::numActiveRCPNodes(), numActiveNodesBase+2);
00246   ECHO(RCPNodeTracer::removeRCPNode(node_b1));
00247 #endif
00248   TEST_EQUALITY(RCPNodeTracer::numActiveRCPNodes(), numActiveNodesBase+1);
00249   ECHO(RCPNodeTracer::removeRCPNode(node_c));
00250   TEST_EQUALITY(RCPNodeTracer::numActiveRCPNodes(), numActiveNodesBase);
00251   ECHO(node_b1->has_ownership(false));
00252   ECHO(deleteRCPNode(&node_b1));
00253   ECHO(deleteRCPNode(&node_c));
00254 }
00255 
00256 
00257 #ifdef HAVE_TEUCHOS_DEBUG_RCP_NODE_TRACING
00258 TEUCHOS_UNIT_TEST( RCPNodeHandle, remove_RCPNode_missing_node )
00259 {
00260   SET_RCPNODE_TRACING();
00261   RCPNode *node = basicRCPNode<A>(true);
00262   TEST_THROW(RCPNodeTracer::removeRCPNode(node), std::logic_error);
00263   deleteRCPNode(&node);
00264 }
00265 #endif // HAVE_TEUCHOS_DEBUG_RCP_NODE_TRACING
00266 
00267 
00268 #endif // TEUCHOS_DEBUG
00269 
00270 
00271 //
00272 // Templated tests
00273 //
00274 
00275 
00276 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( RCPNodeHandle, basicConstruct_owns_mem, T )
00277 {
00278   T *p = 0;
00279   RCPNodeHandle nodeRef(basicRCPNodeHandle<T>(true, &p));
00280   TEST_EQUALITY_CONST( nodeRef.count(), 1 );
00281   TEST_EQUALITY_CONST( nodeRef.has_ownership(), true );
00282   nodeRef.has_ownership(false);
00283   TEST_EQUALITY_CONST( nodeRef.has_ownership(), false );
00284 #ifdef TEUCHOS_DEBUG
00285   TEST_INEQUALITY_CONST( nodeRef.get_base_obj_map_key_void_ptr(), static_cast<void*>(0) );
00286   TEST_EQUALITY( nodeRef.get_base_obj_map_key_void_ptr(), static_cast<void*>(p) );
00287   TEST_THROW({any &a = nodeRef.get_extra_data("int","blob"); (void)a;},
00288     std::invalid_argument);
00289   TEST_THROW({const any &a = getConst(nodeRef).get_extra_data("int","blob"); (void)a;},
00290     std::invalid_argument);
00291 #endif // TEUCHOS_DEBUG
00292   TEST_EQUALITY_CONST(nodeRef.get_optional_extra_data("int","blob"), 0);
00293   TEST_EQUALITY_CONST(getConst(nodeRef).get_optional_extra_data("int","blob"), 0);
00294   nodeRef.has_ownership(true);
00295 }
00296 
00297 
00298 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( RCPNodeHandle, basicConstruct_no_owns_mem, T )
00299 {
00300   RCPNodeHandle nodeRef(basicRCPNodeHandle<T>(false));
00301   TEST_EQUALITY_CONST( nodeRef.count(), 1 );
00302   TEST_EQUALITY_CONST( nodeRef.has_ownership(), false );
00303   nodeRef.has_ownership(true);
00304   TEST_EQUALITY_CONST( nodeRef.has_ownership(), true );
00305 }
00306 
00307 
00308 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( RCPNodeHandle, weakPtr_basic_1, T )
00309 {
00310 
00311   ECHO(RCPNodeHandle nodeRef1(basicRCPNodeHandle<T>(true)));
00312   TEST_EQUALITY_CONST( nodeRef1.strength(), RCP_STRONG );
00313 
00314   ECHO(RCPNodeHandle nodeRef2 = nodeRef1.create_weak());
00315 
00316   TEST_EQUALITY_CONST( nodeRef2.strength(), RCP_WEAK );
00317   TEST_EQUALITY_CONST( nodeRef1.strong_count(), 1 );
00318   TEST_EQUALITY_CONST( nodeRef1.weak_count(), 1 );
00319   TEST_EQUALITY_CONST( nodeRef2.strong_count(), 1 );
00320   TEST_EQUALITY_CONST( nodeRef2.weak_count(), 1 );
00321 
00322   ECHO(RCPNodeHandle nodeRef3 = nodeRef2.create_strong());
00323 
00324   TEST_EQUALITY_CONST( nodeRef3.strength(), RCP_STRONG );
00325   TEST_EQUALITY_CONST( nodeRef1.strong_count(), 2 );
00326   TEST_EQUALITY_CONST( nodeRef1.weak_count(), 1 );
00327   TEST_EQUALITY_CONST( nodeRef2.strong_count(), 2 );
00328   TEST_EQUALITY_CONST( nodeRef2.weak_count(), 1 );
00329   
00330   MockRCP<T> mockRCP;
00331   ECHO(nodeRef2.debug_assert_valid_ptr(mockRCP)); // Should not throw!
00332 
00333   // This will make the underlying object T get deleted!
00334   ECHO(nodeRef1 = null);
00335   ECHO(nodeRef3 = null);
00336 
00337   TEST_EQUALITY_CONST( nodeRef1.node_ptr()==0, true );
00338   TEST_EQUALITY_CONST( nodeRef1.is_node_null(), true );
00339   TEST_EQUALITY_CONST( nodeRef1.is_valid_ptr(), true );
00340 
00341   TEST_EQUALITY_CONST( nodeRef2.node_ptr()!=0, true );
00342   TEST_EQUALITY_CONST( nodeRef2.is_node_null(), false );
00343   TEST_EQUALITY_CONST( nodeRef2.is_valid_ptr(), false );
00344 
00345 #ifdef TEUCHOS_DEBUG
00346   TEST_THROW( nodeRef2.debug_assert_valid_ptr(mockRCP),
00347     DanglingReferenceError );
00348 #endif
00349 
00350   ECHO(nodeRef2 = null); // Should make the underlying node go away
00351 
00352 }
00353 
00354 
00355 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( RCPNodeHandle, weakPtr_basic_2, T )
00356 {
00357 
00358   ECHO(RCPNodeHandle nodeRef1(basicRCPNodeHandle<T>(true)));
00359   TEST_EQUALITY_CONST( nodeRef1.strength(), RCP_STRONG );
00360 
00361   ECHO(RCPNodeHandle nodeRef2 = nodeRef1.create_weak());
00362   TEST_EQUALITY_CONST( nodeRef2.strength(), RCP_WEAK );
00363   TEST_EQUALITY_CONST( nodeRef1.strong_count(), 1 );
00364   TEST_EQUALITY_CONST( nodeRef1.weak_count(), 1 );
00365   TEST_EQUALITY_CONST( nodeRef2.strong_count(), 1 );
00366   TEST_EQUALITY_CONST( nodeRef2.weak_count(), 1 );
00367   
00368   MockRCP<T> mockRCP;
00369 
00370   ECHO(nodeRef2.debug_assert_valid_ptr(mockRCP)); // Should not throw!
00371 
00372   ECHO(nodeRef2 = null); // The underlying object stays alive!
00373 
00374   TEST_EQUALITY_CONST( nodeRef2.node_ptr()==0, true );
00375   TEST_EQUALITY_CONST( nodeRef2.is_node_null(), true );
00376   TEST_EQUALITY_CONST( nodeRef2.is_valid_ptr(), true );
00377 
00378   TEST_EQUALITY_CONST( nodeRef1.node_ptr()!=0, true );
00379   TEST_EQUALITY_CONST( nodeRef1.is_node_null(), false );
00380   TEST_EQUALITY_CONST( nodeRef1.is_valid_ptr(), true );
00381 
00382   nodeRef1.debug_assert_valid_ptr(mockRCP); // Should not throw!
00383 
00384 }
00385 
00386 //
00387 // Test behavior of RCP node tracing but only if it is off by default
00388 //
00389 // NOTE: If node tracing is on by default then we can't control how many nodes
00390 // get created in other code not in the unit test.
00391 //
00392 
00393 #if defined(TEUCHOS_DEBUG) && !defined(HAVE_TEUCHOS_DEBUG_RCP_NODE_TRACING)
00394 #  define DO_RCPNODE_TRACING_TESTS 1
00395 #endif
00396 
00397 
00398 #ifdef DO_RCPNODE_TRACING_TESTS
00399 
00400 
00401 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( RCPNodeHandle, debugWithNodeTracingPrint, T )
00402 {
00403 
00404   TEST_EQUALITY_CONST(RCPNodeTracer::isTracingActiveRCPNodes(), false);
00405   RCPNodeTracer::setTracingActiveRCPNodes(true);
00406   TEST_EQUALITY_CONST(RCPNodeTracer::isTracingActiveRCPNodes(), true);
00407 
00408   {
00409 
00410     T *p = new T; // Never do this in production code!
00411     const std::string T_name = "T_name";
00412     const std::string concreateT_name = "concreateT_name";
00413     const bool has_ownership = true;
00414     RCPNode *node = new RCPNodeTmpl<T,DeallocDelete<T> >(
00415       p, DeallocDelete<T>(), has_ownership);
00416 
00417     RCPNodeHandle nodeRef(node, p, T_name, concreateT_name, has_ownership);
00418 
00419     TEST_EQUALITY_CONST(RCPNodeTracer::numActiveRCPNodes(), 1);
00420 
00421     out << "\nMake sure output is printed when there is an active node with tracing ...\n";
00422 
00423     const void* rcpNodeKey = RCPNodeTracer::getRCPNodeBaseObjMapKeyVoidPtr(p);
00424 
00425     std::ostringstream expendedOutput_oss;
00426     expendedOutput_oss
00427         << RCPNodeTracer::getActiveRCPNodeHeaderString()
00428         << "\n"
00429         << "  0: RCPNode (map_key_void_ptr=" << rcpNodeKey << ")\n"
00430         << "       Information = {T="<<T_name<<", ConcreteT="<<concreateT_name<<", p="<<p<<", has_ownership="<<has_ownership<<"}\n"
00431         << "       RCPNode address = " << node << "\n"
00432         << "       insertionNumber = " << node->insertion_number()
00433         << "\n\n"
00434         << RCPNodeTracer::getCommonDebugNotesString();
00435 
00436     std::ostringstream printActiveRCPNodes_out;
00437     RCPNodeTracer::printActiveRCPNodes(printActiveRCPNodes_out);
00438     TEST_EQUALITY( printActiveRCPNodes_out.str(), expendedOutput_oss.str() );
00439 
00440     // NOTE: The above test basically copied and pasted the ouptut stream code
00441     // from printActiveRCPNodes(...) and will need to be maintained
00442     // with this code.  However, this is a good test because it ensures that
00443     // the arguments node, p, T_name, and concreateT_name are passed, stored,
00444     // and retrieved correctly.  It is also a good test because it ensures
00445     // that output is printed when node tracing is turned on.
00446     //
00447     // This is the very definition of a "white box" test but that is just fine
00448     // for a unit test.
00449 
00450   }
00451 
00452   TEST_EQUALITY_CONST(RCPNodeTracer::numActiveRCPNodes(), 0);
00453 
00454   out << "\nMake sure no output is printed when there are no active nodes ...\n";
00455   const std::string expendedOutput = "";
00456   std::ostringstream printActiveRCPNodes_out;
00457   RCPNodeTracer::printActiveRCPNodes(printActiveRCPNodes_out);
00458   TEST_EQUALITY( printActiveRCPNodes_out.str(), expendedOutput );
00459 
00460   RCPNodeTracer::setTracingActiveRCPNodes(false);
00461   TEST_EQUALITY_CONST(RCPNodeTracer::isTracingActiveRCPNodes(), false);
00462 
00463 }
00464 
00465 
00466 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( RCPNodeHandle, debugWithoutNodeTracingPrint, T )
00467 {
00468 
00469   TEST_EQUALITY_CONST(RCPNodeTracer::isTracingActiveRCPNodes(), false);
00470   RCPNodeTracer::setTracingActiveRCPNodes(false);
00471   TEST_EQUALITY_CONST(RCPNodeTracer::isTracingActiveRCPNodes(), false);
00472 
00473   T *p = new T; // Never do this in production code!
00474   const std::string T_name = "T_name";
00475   const std::string concreateT_name = "concreateT_name";
00476   const bool has_ownership = true;
00477   RCPNode *node = new RCPNodeTmpl<T,DeallocDelete<T> >(
00478     p, DeallocDelete<T>(), has_ownership);
00479   
00480   RCPNodeHandle nodeRef(node, p, T_name, concreateT_name, has_ownership);
00481 
00482   TEST_EQUALITY_CONST(RCPNodeTracer::numActiveRCPNodes(), 0);
00483   
00484   out << "\nMake sure no output is printed when there are no active nodes without tracing ...\n";
00485   const std::string expendedOutput = "";
00486   std::ostringstream printActiveRCPNodes_out;
00487   RCPNodeTracer::printActiveRCPNodes(printActiveRCPNodes_out);
00488   TEST_EQUALITY( printActiveRCPNodes_out.str(), expendedOutput );
00489 
00490 }
00491 
00492 
00493 #endif // DO_RCPNODE_TRACING_TESTS
00494 
00495 
00496 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( RCPNodeHandle, copyConstruct, T )
00497 {
00498   RCPNodeHandle nodeRef1(basicRCPNodeHandle<T>(true));
00499   RCPNodeHandle nodeRef2(nodeRef1);
00500   TEST_EQUALITY_CONST( nodeRef1.count(), 2 );
00501   TEST_EQUALITY_CONST( nodeRef2.count(), 2 );
00502   TEST_EQUALITY_CONST( nodeRef1.has_ownership(), true );
00503   TEST_EQUALITY_CONST( nodeRef2.has_ownership(), true );
00504 }
00505 
00506 
00507 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( RCPNodeHandle, assignmentOperator, T )
00508 {
00509   RCPNodeHandle nodeRef1(basicRCPNodeHandle<T>(true));
00510   RCPNodeHandle nodeRef2;
00511   nodeRef2 = nodeRef1;
00512   TEST_EQUALITY_CONST( nodeRef1.count(), 2 );
00513   TEST_EQUALITY_CONST( nodeRef2.count(), 2 );
00514   TEST_EQUALITY_CONST( nodeRef1.has_ownership(), true );
00515   TEST_EQUALITY_CONST( nodeRef2.has_ownership(), true );
00516 }
00517 
00518 
00519 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( RCPNodeHandle, extraData_basic, T )
00520 {
00521 
00522   RCPNodeHandle nodeRef(basicRCPNodeHandle<T>(true));
00523 
00524   const int v1 = 2;
00525   const any a1(v1);
00526   nodeRef.set_extra_data(a1, "a1", PRE_DESTROY, true); 
00527 
00528   any &a2 = nodeRef.get_extra_data(a1.typeName(), "a1");
00529   TEST_EQUALITY_CONST( a1.same(a2), true );
00530   TEST_EQUALITY( any_cast<int>(a2), v1 );
00531 
00532   any *a3 = nodeRef.get_optional_extra_data(a1.typeName(), "a1");
00533   TEST_EQUALITY_CONST( a3!=0, true );
00534   TEST_EQUALITY( &a2, a3 );
00535   TEST_EQUALITY_CONST( a3->same(a1), true );
00536 
00537   RCPNodeHandle nodeRef2 = nodeRef;
00538 
00539   const int v2 = 3;
00540   a2 = v2;
00541   TEST_EQUALITY( any_cast<int>(a1), v1 );
00542   TEST_EQUALITY( any_cast<int>(*a3), v2 );
00543 
00544   any &a4 = nodeRef2.get_extra_data(a1.typeName(), "a1");
00545   TEST_EQUALITY( &a4, &a2 );
00546   TEST_EQUALITY( &a4, a3 );
00547   TEST_EQUALITY( any_cast<int>(a4), v2 );
00548   
00549 }
00550 
00551 
00552 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( RCPNodeHandle, extraData_basic_const, T )
00553 {
00554 
00555   RCPNodeHandle nodeRef(basicRCPNodeHandle<T>(true));
00556 
00557   const int v1 = 2;
00558   const any a1(v1);
00559   nodeRef.set_extra_data(a1, "a1", PRE_DESTROY, true); 
00560   
00561   const RCPNodeHandle nodeRef2 = nodeRef;
00562 
00563   const any &a2 = nodeRef2.get_extra_data(a1.typeName(), "a1");
00564   TEST_EQUALITY_CONST( a1.same(a2), true );
00565   TEST_EQUALITY( any_cast<int>(a2), v1 );
00566 
00567   const any *a3 = nodeRef2.get_optional_extra_data(a1.typeName(), "a1");
00568   TEST_EQUALITY_CONST( a3!=0, true );
00569   TEST_EQUALITY( &a2, a3 );
00570   TEST_EQUALITY_CONST( a3->same(a1), true );
00571   
00572 }
00573 
00574 
00575 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( RCPNodeHandle, extraData_failed, T )
00576 {
00577 
00578   RCPNodeHandle nodeRef(basicRCPNodeHandle<T>(true));
00579 
00580   const int v1 = 2;
00581   const any a1(v1);
00582   nodeRef.set_extra_data(a1, "a1", PRE_DESTROY, true); 
00583 
00584 #ifdef TEUCHOS_DEBUG
00585 
00586   TEST_THROW({nodeRef.get_extra_data("wrong type", "a1");},
00587     std::invalid_argument);
00588 
00589   TEST_THROW({nodeRef.get_extra_data(a1.typeName(), "wrong name");},
00590     std::invalid_argument);
00591 
00592 #endif // TEUCHOS_DEBUG
00593 
00594   any *a2 = nodeRef.get_optional_extra_data("wrong type", "a1");
00595   TEST_EQUALITY_CONST( a2, 0 );
00596 
00597   any *a3 = nodeRef.get_optional_extra_data(a1.typeName(), "wrong name");
00598   TEST_EQUALITY_CONST( a3, 0 );
00599   
00600 }
00601 
00602 
00603 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( RCPNodeHandle, extraData_failed_const, T )
00604 {
00605 
00606   RCPNodeHandle nodeRef(basicRCPNodeHandle<T>(true));
00607 
00608   const int v1 = 2;
00609   const any a1(v1);
00610   nodeRef.set_extra_data(a1, "a1", PRE_DESTROY, true); 
00611   
00612   const RCPNodeHandle nodeRef2 = nodeRef;
00613 
00614 #ifdef TEUCHOS_DEBUG
00615 
00616   TEST_THROW({nodeRef2.get_extra_data("wrong type", "a1");},
00617     std::invalid_argument);
00618 
00619   TEST_THROW({nodeRef2.get_extra_data(a1.typeName(), "wrong name");},
00620     std::invalid_argument);
00621 
00622 #endif // TEUCHOS_DEBUG
00623 
00624   const any *a2 = nodeRef2.get_optional_extra_data("wrong type", "a1");
00625   TEST_EQUALITY_CONST( a2, 0 );
00626 
00627   const any *a3 = nodeRef2.get_optional_extra_data(a1.typeName(), "wrong name");
00628   TEST_EQUALITY_CONST( a3, 0 );
00629   
00630 }
00631 
00632 
00633 //
00634 // Instantiations
00635 //
00636 
00637 
00638 #ifdef DO_RCPNODE_TRACING_TESTS
00639 
00640 #  define DEBUG_UNIT_TEST_GROUP( T ) \
00641     TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( RCPNodeHandle, debugWithNodeTracingPrint, T ) \
00642     TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( RCPNodeHandle, debugWithoutNodeTracingPrint, T )
00643 
00644 #else
00645 
00646 #  define DEBUG_UNIT_TEST_GROUP( T )
00647 
00648 #endif
00649 
00650 
00651 #define UNIT_TEST_GROUP( T ) \
00652   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( RCPNodeHandle, basicConstruct_owns_mem, T ) \
00653   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( RCPNodeHandle, basicConstruct_no_owns_mem, T ) \
00654   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( RCPNodeHandle, weakPtr_basic_1, T ) \
00655   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( RCPNodeHandle, weakPtr_basic_2, T ) \
00656   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( RCPNodeHandle, copyConstruct, T ) \
00657   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( RCPNodeHandle, assignmentOperator, T ) \
00658   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( RCPNodeHandle, extraData_basic, T ) \
00659   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( RCPNodeHandle, extraData_basic_const, T ) \
00660   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( RCPNodeHandle, extraData_failed, T ) \
00661   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( RCPNodeHandle, extraData_failed_const, T ) \
00662   DEBUG_UNIT_TEST_GROUP(T)
00663 
00664 
00665 UNIT_TEST_GROUP(A)
00666 //UNIT_TEST_GROUP(B1)
00667 //UNIT_TEST_GROUP(B2)
00668 UNIT_TEST_GROUP(C)
00669 //UNIT_TEST_GROUP(D)
00670 UNIT_TEST_GROUP(E)
00671 
00672 // 2008/09/22: rabartl: Above: We don't need to test with all of these classes
00673 // in order to test this functionality.
00674 
00675 
00676 } // namespace Teuchos 
 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