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 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Teuchos: Common Tools Package
6 // Copyright (2004) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
46 
47 
48 namespace {
49 
50 
51 using Teuchos::null;
52 using Teuchos::RCP;
53 using Teuchos::rcp;
55 
56 
57 TEUCHOS_UNIT_TEST( TabularOutputter, basic1 )
58 {
59 
60  typedef Teuchos::TabularOutputter TO;
61 
62  std::stringstream sout;
63  sout << "\n";
64 
65  TabularOutputter outputter(sout);
66 
67  outputter.pushFieldSpec("very long col name", TO::INT);
68  outputter.pushFieldSpec("col b", TO::DOUBLE);
69  outputter.pushFieldSpec("col cc", TO::STRING, TO::LEFT, TO::GENERAL, 6);
70  outputter.pushFieldSpec("col d", TO::DOUBLE);
71  outputter.pushFieldSpec("col e", TO::STRING);
72 
73  outputter.outputHeader();
74 
75  outputter.outputField(1);
76  outputter.outputField(1.2);
77  outputter.outputField("s13");
78  outputter.outputField(1.4);
79  outputter.outputField("s15");
80  outputter.nextRow();
81 
82  outputter.outputField(2);
83  outputter.outputField(2.2);
84  outputter.outputField("s23");
85  outputter.outputField(2.4);
86  outputter.outputField("s25");
87  outputter.nextRow();
88 
89  outputter.outputField(3);
90  outputter.outputField(3.2);
91  outputter.outputField("s33");
92  outputter.outputField(3.4);
93  outputter.outputField("s35");
94  outputter.nextRow();
95 
96  std::stringstream expectedOutput;
97  expectedOutput
98  << "\n"
99  << " very long col name col b col cc col d col e\n"
100  << " ------------------ ------------ ------ ------------ -----\n"
101  << " 1 1.2000e+00 s13 1.4000e+00 s15\n"
102  << " 2 2.2000e+00 s23 2.4000e+00 s25\n"
103  << " 3 3.2000e+00 s33 3.4000e+00 s35\n"
104  ;
105 
106  TEST_EQUALITY_CONST( sout.str(), expectedOutput.str() );
107 
108  // 2008/11/12: rabartl: Note: The above test may not be portable because it
109  // requires the numeric formatting of the doubles to be the same. To make
110  // this more portable, I may have to do some work.
111 
112 }
113 
114 
115 TEUCHOS_UNIT_TEST( TabularOutputter, basic2 )
116 {
117 
118  typedef Teuchos::TabularOutputter TO;
119 
120  std::stringstream sout;
121  sout << "\n";
122 
123  TabularOutputter outputter(Teuchos::rcpFromRef(sout));
124 
125  outputter.setFieldTypePrecision(TO::DOUBLE, 8);
126  outputter.setFieldTypePrecision(TO::INT, 4);
127  outputter.setFieldTypePrecision(TO::STRING, 5);
128 
129  outputter.pushFieldSpec("col a", TO::INT);
130  outputter.pushFieldSpec("col b", TO::DOUBLE);
131  outputter.pushFieldSpec("col cc", TO::STRING, TO::LEFT, TO::GENERAL, 6);
132  outputter.pushFieldSpec("col d", TO::DOUBLE);
133  outputter.pushFieldSpec("col e", TO::STRING);
134 
135  outputter.outputHeader();
136 
137  outputter.outputField(1);
138  outputter.outputField(1.2);
139  outputter.outputField("s13");
140  outputter.outputField(1.4);
141  outputter.outputField("s15");
142  outputter.nextRow();
143 
144  outputter.outputField(2);
145  outputter.outputField(2.2);
146  outputter.outputField("s23");
147  outputter.outputField(2.4);
148  outputter.outputField("s25");
149  outputter.nextRow();
150 
151  outputter.outputField(3);
152  outputter.outputField(3.2);
153  outputter.outputField("s33");
154  outputter.outputField(3.4);
155  outputter.outputField("s35");
156  outputter.nextRow();
157 
158  std::stringstream expectedOutput;
159  expectedOutput
160  << "\n"
161  << " col a col b col cc col d col e\n"
162  << " ----- ---------------- ------ ---------------- -----\n"
163  << " 1 1.20000000e+00 s13 1.40000000e+00 s15\n"
164  << " 2 2.20000000e+00 s23 2.40000000e+00 s25\n"
165  << " 3 3.20000000e+00 s33 3.40000000e+00 s35\n"
166  ;
167 
168  TEST_EQUALITY_CONST( sout.str(), expectedOutput.str() );
169 
170  // 2008/11/12: rabartl: Note: See the comment in the basic1 test above!
171 
172 }
173 
174 
175 TEUCHOS_UNIT_TEST( TabularOutputter, perfTiming )
176 {
177 
178  typedef Teuchos::TabularOutputter TO;
179 
180  std::stringstream sout;
181  sout << "\n";
182 
183  TabularOutputter outputter(sout);
184 
185  outputter.pushFieldSpec("num loops", TO::INT);
186  outputter.pushFieldSpec("vecTime", TO::DOUBLE);
187  outputter.pushFieldSpec("dequeTime", TO::DOUBLE);
188 
189  outputter.outputHeader();
190 
191  const int numLoops = 15;
192 
193  // num loops
194  outputter.outputField(numLoops);
195 
196  // vecTime
197  TEUCHOS_START_PERF_OUTPUT_TIMER(outputter, numLoops)
198  {
199  std::vector<int> a(numLoops);
200  std::vector<int> b(numLoops);
201  a = b;
202  }
203  TEUCHOS_END_PERF_OUTPUT_TIMER(outputter, vecTime);
204  TEST_INEQUALITY_CONST(vecTime, 0);
205 
206  // dequeTime
207  TEUCHOS_START_PERF_OUTPUT_TIMER(outputter, numLoops)
208  {
209  std::deque<int> a(numLoops);
210  std::deque<int> b(numLoops);
211  a = b;
212  }
213  TEUCHOS_END_PERF_OUTPUT_TIMER(outputter, dequeTime);
214  TEST_INEQUALITY_CONST(dequeTime, 0);
215 
216  outputter.nextRow();
217 
218  std::stringstream expectedOutput;
219  expectedOutput
220  << "\n"
221  << "Nothing\n"
222  ;
223 
224  TEST_INEQUALITY_CONST( sout.str(), expectedOutput.str() );
225 
226  // 2008/11/12: rabartl: Above, this is not the greatest test but it would be
227  // hard to produce the exact same formatted output since it involves timing
228  // results.
229 
230 }
231 
232 
233 #ifdef TEUCHOS_DEBUG
234 
235 
236 TEUCHOS_UNIT_TEST( TabularOutputter, nullOStream )
237 {
238  //typedef Teuchos::TabularOutputter TO; // unused
239 
240  TabularOutputter outputter(out);
241 
242  TEST_THROW(
243  outputter.setOStream(Teuchos::null),
245  );
246 }
247 
248 
249 TEUCHOS_UNIT_TEST( TabularOutputter, invalidFieldSpecError )
250 {
251 
252  typedef Teuchos::TabularOutputter TO;
253 
254  TabularOutputter outputter(out);
255 
256  outputter.setFieldTypePrecision(TO::DOUBLE, 8);
257  outputter.setFieldTypePrecision(TO::INT, 4);
258  outputter.setFieldTypePrecision(TO::STRING, 3);
259 
260  outputter.pushFieldSpec("col d", TO::DOUBLE);
261 
262  TEST_THROW(
263  outputter.pushFieldSpec(
264  "very long field name", TO::INT, TO::LEFT, TO::GENERAL, 4),
265  TO::InvalidFieldSpecError
266  );
267 
268 }
269 
270 
271 TEUCHOS_UNIT_TEST( TabularOutputter, missingHeaderError )
272 {
273 
274  typedef Teuchos::TabularOutputter TO;
275 
276  TabularOutputter outputter(out);
277 
278  outputter.pushFieldSpec("col a", TO::INT);
279  outputter.pushFieldSpec("col b", TO::DOUBLE);
280  outputter.pushFieldSpec("col c", TO::STRING);
281  outputter.pushFieldSpec("col d", TO::DOUBLE);
282 
283  TEST_THROW(outputter.outputField(1), TO::MissingHeaderError);
284 
285 }
286 
287 
288 TEUCHOS_UNIT_TEST( TabularOutputter, missingNextRowError )
289 {
290 
291  typedef Teuchos::TabularOutputter TO;
292 
293  TabularOutputter outputter(out);
294 
295  outputter.pushFieldSpec("col a", TO::INT);
296  outputter.pushFieldSpec("col b", TO::DOUBLE);
297  outputter.pushFieldSpec("col c", TO::STRING);
298  outputter.pushFieldSpec("col d", TO::DOUBLE);
299 
300  outputter.outputHeader();
301 
302  outputter.outputField(1);
303  outputter.outputField(1.2);
304  outputter.outputField("s13");
305  outputter.outputField(1.4);
306 
307  // Missing nextRow()!
308 
309  TEST_THROW(outputter.outputField(2), TO::InvalidFieldOutputError);
310 
311 }
312 
313 
314 TEUCHOS_UNIT_TEST( TabularOutputter, missingFieldOutputError )
315 {
316 
317  typedef Teuchos::TabularOutputter TO;
318 
319  TabularOutputter outputter(out);
320 
321  outputter.pushFieldSpec("col a", TO::INT);
322  outputter.pushFieldSpec("col b", TO::DOUBLE);
323  outputter.pushFieldSpec("col c", TO::STRING);
324  outputter.pushFieldSpec("col d", TO::DOUBLE);
325 
326  outputter.outputHeader();
327 
328  outputter.outputField(1);
329  outputter.outputField(1.2);
330  outputter.outputField("s13");
331 
332  // Missing a call to outputField(...);
333 
334  out << "\n\n";
335 
336  TEST_THROW(outputter.nextRow(), TO::InvalidFieldOutputError);
337 
338 }
339 
340 
341 TEUCHOS_UNIT_TEST( TabularOutputter, missingFieldOutputOkay )
342 {
343 
344  typedef Teuchos::TabularOutputter TO;
345 
346  TabularOutputter outputter(out);
347 
348  outputter.pushFieldSpec("col a", TO::INT);
349  outputter.pushFieldSpec("col b", TO::DOUBLE);
350  outputter.pushFieldSpec("col c", TO::STRING);
351  outputter.pushFieldSpec("col d", TO::DOUBLE);
352 
353  outputter.outputHeader();
354 
355  outputter.outputField(1);
356  outputter.outputField(1.2);
357  outputter.outputField("s13");
358 
359  // Missing a call to outputField(...);
360 
361  outputter.nextRow(true); // Just fine!
362 
363 }
364 
365 
366 TEUCHOS_UNIT_TEST( TabularOutputter, missingFields )
367 {
368 
369  typedef Teuchos::TabularOutputter TO;
370 
371  std::ostringstream sout;
372  TabularOutputter outputter(sout);
373 
374  TEST_THROW(outputter.outputHeader(), TO::MissingFieldsError);
375 
376 }
377 
378 
379 #endif // TEUCHOS_DEBUG
380 
381 
382 } // 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 .