Teuchos Package Browser (Single Doxygen Collection) Version of the Day
TabularOutputter_UnitTests.cpp
Go to the documentation of this file.
00001 /*
00002 // @HEADER
00003 // ***********************************************************************
00004 //
00005 //                    Teuchos: Common Tools Package
00006 //                 Copyright (2004) Sandia Corporation
00007 //
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 //
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA
00025 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00026 //
00027 // ***********************************************************************
00028 // @HEADER
00029 */
00030 
00031 #include "Teuchos_UnitTestHarness.hpp"
00032 #include "Teuchos_TabularOutputter.hpp"
00033 
00034 
00035 namespace {
00036 
00037 
00038 using Teuchos::null;
00039 using Teuchos::RCP;
00040 using Teuchos::rcp;
00041 using Teuchos::TabularOutputter;
00042 
00043 
00044 TEUCHOS_UNIT_TEST( TabularOutputter, basic1 )
00045 {
00046 
00047   typedef Teuchos::TabularOutputter TO;
00048 
00049   std::stringstream sout;
00050   sout << "\n";
00051 
00052   TabularOutputter outputter(sout);
00053 
00054   outputter.pushFieldSpec("very long col name", TO::INT);
00055   outputter.pushFieldSpec("col b", TO::DOUBLE);
00056   outputter.pushFieldSpec("col cc", TO::STRING, TO::LEFT, TO::GENERAL, 6);
00057   outputter.pushFieldSpec("col d", TO::DOUBLE);
00058   outputter.pushFieldSpec("col e", TO::STRING);
00059 
00060   outputter.outputHeader();
00061 
00062   outputter.outputField(1);
00063   outputter.outputField(1.2);
00064   outputter.outputField("s13");
00065   outputter.outputField(1.4);
00066   outputter.outputField("s15");
00067   outputter.nextRow();
00068 
00069   outputter.outputField(2);
00070   outputter.outputField(2.2);
00071   outputter.outputField("s23");
00072   outputter.outputField(2.4);
00073   outputter.outputField("s25");
00074   outputter.nextRow();
00075 
00076   outputter.outputField(3);
00077   outputter.outputField(3.2);
00078   outputter.outputField("s33");
00079   outputter.outputField(3.4);
00080   outputter.outputField("s35");
00081   outputter.nextRow();
00082 
00083   std::stringstream expectedOutput;
00084   expectedOutput
00085     << "\n"
00086     << "  very long col name  col b         col cc  col d         col e\n"
00087     << "  ------------------  ------------  ------  ------------  -----\n"
00088     << "                   1    1.2000e+00  s13       1.4000e+00    s15\n"
00089     << "                   2    2.2000e+00  s23       2.4000e+00    s25\n"
00090     << "                   3    3.2000e+00  s33       3.4000e+00    s35\n"
00091     ;
00092 
00093   TEST_EQUALITY_CONST( sout.str(), expectedOutput.str() );
00094 
00095   // 2008/11/12: rabartl: Note: The above test may not be portable because it
00096   // requires the numeric formatting of the doubles to be the same.  To make
00097   // this more portable, I may have to do some work.
00098 
00099 }
00100 
00101 
00102 TEUCHOS_UNIT_TEST( TabularOutputter, basic2 )
00103 {
00104 
00105   typedef Teuchos::TabularOutputter TO;
00106 
00107   std::stringstream sout;
00108   sout << "\n";
00109 
00110   TabularOutputter outputter(Teuchos::rcpFromRef(sout));
00111 
00112   outputter.setFieldTypePrecision(TO::DOUBLE, 8);
00113   outputter.setFieldTypePrecision(TO::INT, 4);
00114   outputter.setFieldTypePrecision(TO::STRING, 5);
00115 
00116   outputter.pushFieldSpec("col a", TO::INT);
00117   outputter.pushFieldSpec("col b", TO::DOUBLE);
00118   outputter.pushFieldSpec("col cc", TO::STRING, TO::LEFT, TO::GENERAL, 6);
00119   outputter.pushFieldSpec("col d", TO::DOUBLE);
00120   outputter.pushFieldSpec("col e", TO::STRING);
00121 
00122   outputter.outputHeader();
00123 
00124   outputter.outputField(1);
00125   outputter.outputField(1.2);
00126   outputter.outputField("s13");
00127   outputter.outputField(1.4);
00128   outputter.outputField("s15");
00129   outputter.nextRow();
00130 
00131   outputter.outputField(2);
00132   outputter.outputField(2.2);
00133   outputter.outputField("s23");
00134   outputter.outputField(2.4);
00135   outputter.outputField("s25");
00136   outputter.nextRow();
00137 
00138   outputter.outputField(3);
00139   outputter.outputField(3.2);
00140   outputter.outputField("s33");
00141   outputter.outputField(3.4);
00142   outputter.outputField("s35");
00143   outputter.nextRow();
00144 
00145   std::stringstream expectedOutput;
00146   expectedOutput
00147     << "\n"
00148     << "  col a  col b             col cc  col d             col e\n"
00149     << "  -----  ----------------  ------  ----------------  -----\n"
00150     << "      1    1.20000000e+00  s13       1.40000000e+00    s15\n"
00151     << "      2    2.20000000e+00  s23       2.40000000e+00    s25\n"
00152     << "      3    3.20000000e+00  s33       3.40000000e+00    s35\n"
00153     ;
00154 
00155   TEST_EQUALITY_CONST( sout.str(), expectedOutput.str() );
00156 
00157   // 2008/11/12: rabartl: Note: See the comment in the basic1 test above!
00158 
00159 }
00160 
00161 
00162 TEUCHOS_UNIT_TEST( TabularOutputter, perfTiming )
00163 {
00164 
00165   typedef Teuchos::TabularOutputter TO;
00166 
00167   std::stringstream sout;
00168   sout << "\n";
00169 
00170   TabularOutputter outputter(sout);
00171 
00172   outputter.pushFieldSpec("num loops", TO::INT);
00173   outputter.pushFieldSpec("vecTime", TO::DOUBLE);
00174   outputter.pushFieldSpec("dequeTime", TO::DOUBLE);
00175 
00176   outputter.outputHeader();
00177 
00178   const int numLoops = 15;
00179 
00180   // num loops
00181   outputter.outputField(numLoops);
00182 
00183   // vecTime
00184   TEUCHOS_START_PERF_OUTPUT_TIMER(outputter, numLoops)
00185   {
00186     std::vector<int> a(numLoops);
00187     std::vector<int> b(numLoops);
00188     a = b;
00189   }
00190   TEUCHOS_END_PERF_OUTPUT_TIMER(outputter, vecTime);
00191   TEST_INEQUALITY_CONST(vecTime, 0);
00192 
00193   // dequeTime
00194   TEUCHOS_START_PERF_OUTPUT_TIMER(outputter, numLoops)
00195   {
00196     std::deque<int> a(numLoops);
00197     std::deque<int> b(numLoops);
00198     a = b;
00199   }
00200   TEUCHOS_END_PERF_OUTPUT_TIMER(outputter, dequeTime);
00201   TEST_INEQUALITY_CONST(dequeTime, 0);
00202 
00203   outputter.nextRow();
00204 
00205   std::stringstream expectedOutput;
00206   expectedOutput
00207     << "\n"
00208     << "Nothing\n"
00209     ;
00210 
00211   TEST_INEQUALITY_CONST( sout.str(), expectedOutput.str() );
00212 
00213   // 2008/11/12: rabartl: Above, this is not the greatest test but it would be
00214   // hard to produce the exact same formatted output since it involves timing
00215   // results.
00216 
00217 }
00218 
00219 
00220 #ifdef TEUCHOS_DEBUG
00221 
00222 
00223 TEUCHOS_UNIT_TEST( TabularOutputter, nullOStream )
00224 {
00225 
00226   typedef Teuchos::TabularOutputter TO;
00227 
00228   TabularOutputter outputter(out);
00229 
00230   TEST_THROW(
00231     outputter.setOStream(Teuchos::null),
00232     Teuchos::NullReferenceError
00233     );
00234 
00235 }
00236 
00237 
00238 TEUCHOS_UNIT_TEST( TabularOutputter, invalidFieldSpecError )
00239 {
00240 
00241   typedef Teuchos::TabularOutputter TO;
00242 
00243   TabularOutputter outputter(out);
00244 
00245   outputter.setFieldTypePrecision(TO::DOUBLE, 8);
00246   outputter.setFieldTypePrecision(TO::INT, 4);
00247   outputter.setFieldTypePrecision(TO::STRING, 3);
00248 
00249   outputter.pushFieldSpec("col d", TO::DOUBLE);
00250 
00251   TEST_THROW(
00252     outputter.pushFieldSpec(
00253       "very long field name", TO::INT, TO::LEFT, TO::GENERAL, 4),
00254     TO::InvalidFieldSpecError
00255     );
00256 
00257 }
00258 
00259 
00260 TEUCHOS_UNIT_TEST( TabularOutputter, missingHeaderError )
00261 {
00262 
00263   typedef Teuchos::TabularOutputter TO;
00264 
00265   TabularOutputter outputter(out);
00266 
00267   outputter.pushFieldSpec("col a", TO::INT);
00268   outputter.pushFieldSpec("col b", TO::DOUBLE);
00269   outputter.pushFieldSpec("col c", TO::STRING);
00270   outputter.pushFieldSpec("col d", TO::DOUBLE);
00271 
00272   TEST_THROW(outputter.outputField(1), TO::MissingHeaderError);
00273 
00274 }
00275 
00276 
00277 TEUCHOS_UNIT_TEST( TabularOutputter, missingNextRowError )
00278 {
00279 
00280   typedef Teuchos::TabularOutputter TO;
00281 
00282   TabularOutputter outputter(out);
00283 
00284   outputter.pushFieldSpec("col a", TO::INT);
00285   outputter.pushFieldSpec("col b", TO::DOUBLE);
00286   outputter.pushFieldSpec("col c", TO::STRING);
00287   outputter.pushFieldSpec("col d", TO::DOUBLE);
00288 
00289   outputter.outputHeader();
00290 
00291   outputter.outputField(1);
00292   outputter.outputField(1.2);
00293   outputter.outputField("s13");
00294   outputter.outputField(1.4);
00295 
00296   // Missing nextRow()!
00297 
00298   TEST_THROW(outputter.outputField(2), TO::InvalidFieldOutputError);
00299 
00300 }
00301 
00302 
00303 TEUCHOS_UNIT_TEST( TabularOutputter, missingFieldOutputError )
00304 {
00305 
00306   typedef Teuchos::TabularOutputter TO;
00307 
00308   TabularOutputter outputter(out);
00309 
00310   outputter.pushFieldSpec("col a", TO::INT);
00311   outputter.pushFieldSpec("col b", TO::DOUBLE);
00312   outputter.pushFieldSpec("col c", TO::STRING);
00313   outputter.pushFieldSpec("col d", TO::DOUBLE);
00314 
00315   outputter.outputHeader();
00316 
00317   outputter.outputField(1);
00318   outputter.outputField(1.2);
00319   outputter.outputField("s13");
00320 
00321   // Missing a call to outputField(...);
00322   
00323   out << "\n\n";
00324 
00325   TEST_THROW(outputter.nextRow(), TO::InvalidFieldOutputError);
00326 
00327 }
00328 
00329 
00330 TEUCHOS_UNIT_TEST( TabularOutputter, missingFieldOutputOkay )
00331 {
00332 
00333   typedef Teuchos::TabularOutputter TO;
00334 
00335   TabularOutputter outputter(out);
00336 
00337   outputter.pushFieldSpec("col a", TO::INT);
00338   outputter.pushFieldSpec("col b", TO::DOUBLE);
00339   outputter.pushFieldSpec("col c", TO::STRING);
00340   outputter.pushFieldSpec("col d", TO::DOUBLE);
00341 
00342   outputter.outputHeader();
00343 
00344   outputter.outputField(1);
00345   outputter.outputField(1.2);
00346   outputter.outputField("s13");
00347 
00348   // Missing a call to outputField(...);
00349 
00350   outputter.nextRow(true); // Just fine!
00351 
00352 }
00353 
00354 
00355 TEUCHOS_UNIT_TEST( TabularOutputter, missingFields )
00356 {
00357 
00358   typedef Teuchos::TabularOutputter TO;
00359 
00360   std::ostringstream sout;
00361   TabularOutputter outputter(sout);
00362 
00363   TEST_THROW(outputter.outputHeader(), TO::MissingFieldsError);
00364 
00365 }
00366 
00367 
00368 #endif // TEUCHOS_DEBUG
00369 
00370 
00371 } // namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines