Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TabularOutputter_UnitTests.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
12 
13 
14 namespace {
15 
16 
17 using Teuchos::null;
18 using Teuchos::RCP;
19 using Teuchos::rcp;
21 
22 
23 TEUCHOS_UNIT_TEST( TabularOutputter, basic1 )
24 {
25 
26  typedef Teuchos::TabularOutputter TO;
27 
28  std::stringstream sout;
29  sout << "\n";
30 
31  TabularOutputter outputter(sout);
32 
33  outputter.pushFieldSpec("very long col name", TO::INT);
34  outputter.pushFieldSpec("col b", TO::DOUBLE);
35  outputter.pushFieldSpec("col cc", TO::STRING, TO::LEFT, TO::GENERAL, 6);
36  outputter.pushFieldSpec("col d", TO::DOUBLE);
37  outputter.pushFieldSpec("col e", TO::STRING);
38 
39  outputter.outputHeader();
40 
41  outputter.outputField(1);
42  outputter.outputField(1.2);
43  outputter.outputField("s13");
44  outputter.outputField(1.4);
45  outputter.outputField("s15");
46  outputter.nextRow();
47 
48  outputter.outputField(2);
49  outputter.outputField(2.2);
50  outputter.outputField("s23");
51  outputter.outputField(2.4);
52  outputter.outputField("s25");
53  outputter.nextRow();
54 
55  outputter.outputField(3);
56  outputter.outputField(3.2);
57  outputter.outputField("s33");
58  outputter.outputField(3.4);
59  outputter.outputField("s35");
60  outputter.nextRow();
61 
62  std::stringstream expectedOutput;
63  expectedOutput
64  << "\n"
65  << " very long col name col b col cc col d col e\n"
66  << " ------------------ ------------ ------ ------------ -----\n"
67  << " 1 1.2000e+00 s13 1.4000e+00 s15\n"
68  << " 2 2.2000e+00 s23 2.4000e+00 s25\n"
69  << " 3 3.2000e+00 s33 3.4000e+00 s35\n"
70  ;
71 
72  TEST_EQUALITY_CONST( sout.str(), expectedOutput.str() );
73 
74  // 2008/11/12: rabartl: Note: The above test may not be portable because it
75  // requires the numeric formatting of the doubles to be the same. To make
76  // this more portable, I may have to do some work.
77 
78 }
79 
80 
81 TEUCHOS_UNIT_TEST( TabularOutputter, basic2 )
82 {
83 
84  typedef Teuchos::TabularOutputter TO;
85 
86  std::stringstream sout;
87  sout << "\n";
88 
89  TabularOutputter outputter(Teuchos::rcpFromRef(sout));
90 
91  outputter.setFieldTypePrecision(TO::DOUBLE, 8);
92  outputter.setFieldTypePrecision(TO::INT, 4);
93  outputter.setFieldTypePrecision(TO::STRING, 5);
94 
95  outputter.pushFieldSpec("col a", TO::INT);
96  outputter.pushFieldSpec("col b", TO::DOUBLE);
97  outputter.pushFieldSpec("col cc", TO::STRING, TO::LEFT, TO::GENERAL, 6);
98  outputter.pushFieldSpec("col d", TO::DOUBLE);
99  outputter.pushFieldSpec("col e", TO::STRING);
100 
101  outputter.outputHeader();
102 
103  outputter.outputField(1);
104  outputter.outputField(1.2);
105  outputter.outputField("s13");
106  outputter.outputField(1.4);
107  outputter.outputField("s15");
108  outputter.nextRow();
109 
110  outputter.outputField(2);
111  outputter.outputField(2.2);
112  outputter.outputField("s23");
113  outputter.outputField(2.4);
114  outputter.outputField("s25");
115  outputter.nextRow();
116 
117  outputter.outputField(3);
118  outputter.outputField(3.2);
119  outputter.outputField("s33");
120  outputter.outputField(3.4);
121  outputter.outputField("s35");
122  outputter.nextRow();
123 
124  std::stringstream expectedOutput;
125  expectedOutput
126  << "\n"
127  << " col a col b col cc col d col e\n"
128  << " ----- ---------------- ------ ---------------- -----\n"
129  << " 1 1.20000000e+00 s13 1.40000000e+00 s15\n"
130  << " 2 2.20000000e+00 s23 2.40000000e+00 s25\n"
131  << " 3 3.20000000e+00 s33 3.40000000e+00 s35\n"
132  ;
133 
134  TEST_EQUALITY_CONST( sout.str(), expectedOutput.str() );
135 
136  // 2008/11/12: rabartl: Note: See the comment in the basic1 test above!
137 
138 }
139 
140 
141 TEUCHOS_UNIT_TEST( TabularOutputter, perfTiming )
142 {
143 
144  typedef Teuchos::TabularOutputter TO;
145 
146  std::stringstream sout;
147  sout << "\n";
148 
149  TabularOutputter outputter(sout);
150 
151  outputter.pushFieldSpec("num loops", TO::INT);
152  outputter.pushFieldSpec("vecTime", TO::DOUBLE);
153  outputter.pushFieldSpec("dequeTime", TO::DOUBLE);
154 
155  outputter.outputHeader();
156 
157  const int numLoops = 15;
158 
159  // num loops
160  outputter.outputField(numLoops);
161 
162  // vecTime
163  TEUCHOS_START_PERF_OUTPUT_TIMER(outputter, numLoops)
164  {
165  std::vector<int> a(numLoops);
166  std::vector<int> b(numLoops);
167  a = b;
168  }
169  TEUCHOS_END_PERF_OUTPUT_TIMER(outputter, vecTime);
170  TEST_INEQUALITY_CONST(vecTime, 0);
171 
172  // dequeTime
173  TEUCHOS_START_PERF_OUTPUT_TIMER(outputter, numLoops)
174  {
175  std::deque<int> a(numLoops);
176  std::deque<int> b(numLoops);
177  a = b;
178  }
179  TEUCHOS_END_PERF_OUTPUT_TIMER(outputter, dequeTime);
180  TEST_INEQUALITY_CONST(dequeTime, 0);
181 
182  outputter.nextRow();
183 
184  std::stringstream expectedOutput;
185  expectedOutput
186  << "\n"
187  << "Nothing\n"
188  ;
189 
190  TEST_INEQUALITY_CONST( sout.str(), expectedOutput.str() );
191 
192  // 2008/11/12: rabartl: Above, this is not the greatest test but it would be
193  // hard to produce the exact same formatted output since it involves timing
194  // results.
195 
196 }
197 
198 
199 #ifdef TEUCHOS_DEBUG
200 
201 
202 TEUCHOS_UNIT_TEST( TabularOutputter, nullOStream )
203 {
204  //typedef Teuchos::TabularOutputter TO; // unused
205 
206  TabularOutputter outputter(out);
207 
208  TEST_THROW(
209  outputter.setOStream(Teuchos::null),
211  );
212 }
213 
214 
215 TEUCHOS_UNIT_TEST( TabularOutputter, invalidFieldSpecError )
216 {
217 
218  typedef Teuchos::TabularOutputter TO;
219 
220  TabularOutputter outputter(out);
221 
222  outputter.setFieldTypePrecision(TO::DOUBLE, 8);
223  outputter.setFieldTypePrecision(TO::INT, 4);
224  outputter.setFieldTypePrecision(TO::STRING, 3);
225 
226  outputter.pushFieldSpec("col d", TO::DOUBLE);
227 
228  TEST_THROW(
229  outputter.pushFieldSpec(
230  "very long field name", TO::INT, TO::LEFT, TO::GENERAL, 4),
231  TO::InvalidFieldSpecError
232  );
233 
234 }
235 
236 
237 TEUCHOS_UNIT_TEST( TabularOutputter, missingHeaderError )
238 {
239 
240  typedef Teuchos::TabularOutputter TO;
241 
242  TabularOutputter outputter(out);
243 
244  outputter.pushFieldSpec("col a", TO::INT);
245  outputter.pushFieldSpec("col b", TO::DOUBLE);
246  outputter.pushFieldSpec("col c", TO::STRING);
247  outputter.pushFieldSpec("col d", TO::DOUBLE);
248 
249  TEST_THROW(outputter.outputField(1), TO::MissingHeaderError);
250 
251 }
252 
253 
254 TEUCHOS_UNIT_TEST( TabularOutputter, missingNextRowError )
255 {
256 
257  typedef Teuchos::TabularOutputter TO;
258 
259  TabularOutputter outputter(out);
260 
261  outputter.pushFieldSpec("col a", TO::INT);
262  outputter.pushFieldSpec("col b", TO::DOUBLE);
263  outputter.pushFieldSpec("col c", TO::STRING);
264  outputter.pushFieldSpec("col d", TO::DOUBLE);
265 
266  outputter.outputHeader();
267 
268  outputter.outputField(1);
269  outputter.outputField(1.2);
270  outputter.outputField("s13");
271  outputter.outputField(1.4);
272 
273  // Missing nextRow()!
274 
275  TEST_THROW(outputter.outputField(2), TO::InvalidFieldOutputError);
276 
277 }
278 
279 
280 TEUCHOS_UNIT_TEST( TabularOutputter, missingFieldOutputError )
281 {
282 
283  typedef Teuchos::TabularOutputter TO;
284 
285  TabularOutputter outputter(out);
286 
287  outputter.pushFieldSpec("col a", TO::INT);
288  outputter.pushFieldSpec("col b", TO::DOUBLE);
289  outputter.pushFieldSpec("col c", TO::STRING);
290  outputter.pushFieldSpec("col d", TO::DOUBLE);
291 
292  outputter.outputHeader();
293 
294  outputter.outputField(1);
295  outputter.outputField(1.2);
296  outputter.outputField("s13");
297 
298  // Missing a call to outputField(...);
299 
300  out << "\n\n";
301 
302  TEST_THROW(outputter.nextRow(), TO::InvalidFieldOutputError);
303 
304 }
305 
306 
307 TEUCHOS_UNIT_TEST( TabularOutputter, missingFieldOutputOkay )
308 {
309 
310  typedef Teuchos::TabularOutputter TO;
311 
312  TabularOutputter outputter(out);
313 
314  outputter.pushFieldSpec("col a", TO::INT);
315  outputter.pushFieldSpec("col b", TO::DOUBLE);
316  outputter.pushFieldSpec("col c", TO::STRING);
317  outputter.pushFieldSpec("col d", TO::DOUBLE);
318 
319  outputter.outputHeader();
320 
321  outputter.outputField(1);
322  outputter.outputField(1.2);
323  outputter.outputField("s13");
324 
325  // Missing a call to outputField(...);
326 
327  outputter.nextRow(true); // Just fine!
328 
329 }
330 
331 
332 TEUCHOS_UNIT_TEST( TabularOutputter, missingFields )
333 {
334 
335  typedef Teuchos::TabularOutputter TO;
336 
337  std::ostringstream sout;
338  TabularOutputter outputter(sout);
339 
340  TEST_THROW(outputter.outputHeader(), TO::MissingFieldsError);
341 
342 }
343 
344 
345 #endif // TEUCHOS_DEBUG
346 
347 
348 } // namespace
Null reference error exception class.
#define TEST_INEQUALITY_CONST(v1, v2)
Assert the inequality of v1 and constant v2.
#define TEST_THROW(code, ExceptType)
Assert that the statement &#39;code&#39; throws the exception &#39;ExceptType&#39; (otherwise the test fails)...
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
Utility class that makes it easy to create formatted tables of output.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Unit testing support.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
#define TEUCHOS_END_PERF_OUTPUT_TIMER(OUTPUTTER, VARNAME)
End a timer block, output the time field to a TabularOutputter object, and set a variable with the ti...
Smart reference counting pointer class for automatic garbage collection.
#define TEUCHOS_START_PERF_OUTPUT_TIMER(OUTPUTTER, NUMLOOPS)
Start a timer block using a TabularOutputter object .