Sierra Toolkit Version of the Day
UnitTestTrace.cpp
00001 /*------------------------------------------------------------------------*/
00002 /*                 Copyright 2010 Sandia Corporation.                     */
00003 /*  Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive   */
00004 /*  license for use of this work by or on behalf of the U.S. Government.  */
00005 /*  Export of this program may require a license from the                 */
00006 /*  United States Government.                                             */
00007 /*------------------------------------------------------------------------*/
00008 
00009 #ifdef STK_MESH_TRACE_ENABLED
00010 
00011 #include <stk_util/unit_test_support/stk_utest_macros.hpp>
00012 
00013 #include <stk_mesh/base/Trace.hpp>
00014 #include <stk_mesh/base/EntityKey.hpp>
00015 
00016 #include <sstream>
00017 
00018 using stk::mesh::EntityKey;
00019 
00020 namespace {
00021 
00022 // Test globals
00023 const std::string SHOULD_SEE_STR = "should_see";
00024 const std::string SHOULD_NOT_SEE_STR = "should_not_see";
00025 unsigned SHOULD_SEE_COUNTER = 0;
00026 
00027 std::string create_should_see_str(unsigned counter)
00028 {
00029   std::ostringstream oss;
00030   oss << SHOULD_SEE_STR << counter;
00031   return oss.str();
00032 }
00033 
00034 const char* create_func(const std::string& func, bool should_see)
00035 {
00036   std::ostringstream& oss = *(new std::ostringstream());
00037   if (should_see) {
00038     oss << func << "::" << create_should_see_str(SHOULD_SEE_COUNTER++);
00039   }
00040   else {
00041     oss << func << "::" << SHOULD_NOT_SEE_STR;
00042   }
00043   return oss.str().c_str();
00044 }
00045 
00046 std::string create_string(bool should_see)
00047 {
00048   if (should_see) {
00049     return create_should_see_str(SHOULD_SEE_COUNTER++);
00050   }
00051   else {
00052     return SHOULD_NOT_SEE_STR;
00053   }
00054 }
00055 
00057 STKUNIT_UNIT_TEST(UnitTestTrace, testTrace)
00059 {
00060   // A simple unit-test for the stk_mesh's tracing infrastructure. Note that
00061   // none of the Trace macros in the .cpp files in stk_mesh will be active
00062   // because the will have already been compiled without the macros defined.
00063   // This limits this unit test to very basic checks.
00064 
00065   // Local constants
00066   const EntityKey watch_key(0, 1);
00067   const EntityKey not_watch_key(0, 2);
00068   const std::string tracing_func = "stk::mesh::BulkData";
00069   const std::string not_tracing_func = "stk::mesh::MetaData";
00070   const stk::mesh::LogMask active_mask = stk::mesh::LOG_ENTITY;
00071   const stk::mesh::LogMask inactive_mask = stk::mesh::LOG_BUCKET;
00072 
00073   // Set up a dummy trace configuration. Here, we're telling the tracing
00074   // system that we want to trace BulkData calls related to entities,
00075   // specifically Node[1].
00076   std::ostringstream trace_output;
00077   stk::mesh::setStream(trace_output);
00078   meshlog.setPrintMask(active_mask | stk::mesh::LOG_TRACE);
00079   stk::mesh::watch(watch_key);
00080   stk::diag::Trace::addTraceFunction(tracing_func);
00081 
00082   //
00083   // Make calls to Trace API, some of which should generate trace output. We tag
00084   // output that should / should-not be in the trace so we can validate later.
00085   //
00086 
00087   {
00088     bool should_see = false; // not tracing func
00089     Trace_(create_func(not_tracing_func, should_see));
00090   }
00091 
00092   {
00093     bool should_see = true;
00094     Trace_(create_func(tracing_func, should_see));
00095   }
00096 
00097   {
00098     bool should_see = false; // not tracing func
00099     TraceIf(create_func(not_tracing_func, should_see), active_mask);
00100   }
00101 
00102   {
00103     bool should_see = false; // inactive mask
00104     TraceIf(create_func(tracing_func, should_see), inactive_mask);
00105     DiagIf(inactive_mask, create_string(should_see));
00106   }
00107 
00108   {
00109     bool should_see = true;
00110     TraceIf(create_func(tracing_func, should_see), active_mask);
00111     DiagIf(active_mask, create_string(should_see));
00112   }
00113 
00114   {
00115     bool should_see = false; // not tracing func
00116     TraceIfWatching(create_func(not_tracing_func, should_see), active_mask, watch_key);
00117   }
00118 
00119   {
00120     bool should_see = false; // inactive mask
00121     TraceIfWatching(create_func(tracing_func, should_see), inactive_mask, watch_key);
00122     DiagIfWatching(inactive_mask, watch_key, create_string(should_see));
00123   }
00124 
00125   {
00126     bool should_see = false; // not watching key
00127     TraceIfWatching(create_func(tracing_func, should_see), active_mask, not_watch_key);
00128     DiagIfWatching(active_mask, not_watch_key, create_string(should_see));
00129   }
00130 
00131   {
00132     bool should_see = true;
00133     TraceIfWatching(create_func(tracing_func, should_see), active_mask, watch_key);
00134     DiagIfWatching(active_mask, watch_key, create_string(should_see));
00135   }
00136 
00137   //
00138   // Check validity of output
00139   //
00140 
00141   const std::string trace_output_str = trace_output.str();
00142 
00143   // The not-trace tagged output should not be in the trace output
00144   STKUNIT_ASSERT_EQUAL(trace_output_str.find(SHOULD_NOT_SEE_STR), std::string::npos);
00145 
00146   // Each occurance of should-see output should be in the trace output
00147   for (unsigned i = 0; i < SHOULD_SEE_COUNTER; ++i) {
00148     STKUNIT_ASSERT_NE(trace_output_str.find(create_should_see_str(i)), std::string::npos);
00149   }
00150 }
00151 
00152 
00153 } // empty namespace
00154 
00155 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends