Sierra Toolkit  Version of the Day
UnitTestTrace.cpp
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2010 Sandia Corporation. */
3 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
4 /* license for use of this work by or on behalf of the U.S. Government. */
5 /* Export of this program may require a license from the */
6 /* United States Government. */
7 /*------------------------------------------------------------------------*/
8 
9 #ifdef STK_MESH_TRACE_ENABLED
10 
11 #include <stk_util/unit_test_support/stk_utest_macros.hpp>
12 
13 #include <stk_mesh/base/Trace.hpp>
14 #include <stk_mesh/base/EntityKey.hpp>
15 
16 #include <sstream>
17 
19 
20 namespace {
21 
22 // Test globals
23 const std::string SHOULD_SEE_STR = "should_see";
24 const std::string SHOULD_NOT_SEE_STR = "should_not_see";
25 unsigned SHOULD_SEE_COUNTER = 0;
26 
27 std::string create_should_see_str(unsigned counter)
28 {
29  std::ostringstream oss;
30  oss << SHOULD_SEE_STR << counter;
31  return oss.str();
32 }
33 
34 const char* create_func(const std::string& func, bool should_see)
35 {
36  std::ostringstream& oss = *(new std::ostringstream());
37  if (should_see) {
38  oss << func << "::" << create_should_see_str(SHOULD_SEE_COUNTER++);
39  }
40  else {
41  oss << func << "::" << SHOULD_NOT_SEE_STR;
42  }
43  return oss.str().c_str();
44 }
45 
46 std::string create_string(bool should_see)
47 {
48  if (should_see) {
49  return create_should_see_str(SHOULD_SEE_COUNTER++);
50  }
51  else {
52  return SHOULD_NOT_SEE_STR;
53  }
54 }
55 
57 STKUNIT_UNIT_TEST(UnitTestTrace, testTrace)
59 {
60  // A simple unit-test for the stk_mesh's tracing infrastructure. Note that
61  // none of the Trace macros in the .cpp files in stk_mesh will be active
62  // because the will have already been compiled without the macros defined.
63  // This limits this unit test to very basic checks.
64 
65  // Local constants
66  const EntityKey watch_key(0, 1);
67  const EntityKey not_watch_key(0, 2);
68  const std::string tracing_func = "stk_classic::mesh::BulkData";
69  const std::string not_tracing_func = "stk_classic::mesh::MetaData";
70  const stk_classic::mesh::LogMask active_mask = stk_classic::mesh::LOG_ENTITY;
71  const stk_classic::mesh::LogMask inactive_mask = stk_classic::mesh::LOG_BUCKET;
72 
73  // Set up a dummy trace configuration. Here, we're telling the tracing
74  // system that we want to trace BulkData calls related to entities,
75  // specifically Node[1].
76  std::ostringstream trace_output;
77  stk_classic::mesh::setStream(trace_output);
78  meshlog.setPrintMask(active_mask | stk_classic::mesh::LOG_TRACE);
79  stk_classic::mesh::watch(watch_key);
81 
82  //
83  // Make calls to Trace API, some of which should generate trace output. We tag
84  // output that should / should-not be in the trace so we can validate later.
85  //
86 
87  {
88  bool should_see = false; // not tracing func
89  Trace_(create_func(not_tracing_func, should_see));
90  }
91 
92  {
93  bool should_see = true;
94  Trace_(create_func(tracing_func, should_see));
95  }
96 
97  {
98  bool should_see = false; // not tracing func
99  TraceIf(create_func(not_tracing_func, should_see), active_mask);
100  }
101 
102  {
103  bool should_see = false; // inactive mask
104  TraceIf(create_func(tracing_func, should_see), inactive_mask);
105  DiagIf(inactive_mask, create_string(should_see));
106  }
107 
108  {
109  bool should_see = true;
110  TraceIf(create_func(tracing_func, should_see), active_mask);
111  DiagIf(active_mask, create_string(should_see));
112  }
113 
114  {
115  bool should_see = false; // not tracing func
116  TraceIfWatching(create_func(not_tracing_func, should_see), active_mask, watch_key);
117  }
118 
119  {
120  bool should_see = false; // inactive mask
121  TraceIfWatching(create_func(tracing_func, should_see), inactive_mask, watch_key);
122  DiagIfWatching(inactive_mask, watch_key, create_string(should_see));
123  }
124 
125  {
126  bool should_see = false; // not watching key
127  TraceIfWatching(create_func(tracing_func, should_see), active_mask, not_watch_key);
128  DiagIfWatching(active_mask, not_watch_key, create_string(should_see));
129  }
130 
131  {
132  bool should_see = true;
133  TraceIfWatching(create_func(tracing_func, should_see), active_mask, watch_key);
134  DiagIfWatching(active_mask, watch_key, create_string(should_see));
135  }
136 
137  //
138  // Check validity of output
139  //
140 
141  const std::string trace_output_str = trace_output.str();
142 
143  // The not-trace tagged output should not be in the trace output
144  STKUNIT_ASSERT_EQUAL(trace_output_str.find(SHOULD_NOT_SEE_STR), std::string::npos);
145 
146  // Each occurance of should-see output should be in the trace output
147  for (unsigned i = 0; i < SHOULD_SEE_COUNTER; ++i) {
148  STKUNIT_ASSERT_NE(trace_output_str.find(create_should_see_str(i)), std::string::npos);
149  }
150 }
151 
152 
153 } // empty namespace
154 
155 #endif
static void addTraceFunction(const std::string &function_prefix)
Member function addTraceFunction adds a function prefix to the list of function prefixes search to en...
Integer type for the entity keys, which is an encoding of the entity type and entity identifier...