TabularOutputter_UnitTests.cpp

Go to the documentation of this file.
00001 #include "Teuchos_UnitTestHarness.hpp"
00002 #include "Teuchos_TabularOutputter.hpp"
00003 
00004 
00005 namespace {
00006 
00007 
00008 using Teuchos::null;
00009 using Teuchos::RCP;
00010 using Teuchos::rcp;
00011 using Teuchos::TabularOutputter;
00012 
00013 
00014 TEUCHOS_UNIT_TEST( TabularOutputter, basic1 )
00015 {
00016 
00017   typedef Teuchos::TabularOutputter TO;
00018 
00019   std::stringstream sout;
00020   sout << "\n";
00021 
00022   TabularOutputter outputter(sout);
00023 
00024   outputter.pushFieldSpec("very long col name", TO::INT);
00025   outputter.pushFieldSpec("col b", TO::DOUBLE);
00026   outputter.pushFieldSpec("col cc", TO::STRING, TO::LEFT, TO::GENERAL, 6);
00027   outputter.pushFieldSpec("col d", TO::DOUBLE);
00028   outputter.pushFieldSpec("col e", TO::STRING);
00029 
00030   outputter.outputHeader();
00031 
00032   outputter.outputField(1);
00033   outputter.outputField(1.2);
00034   outputter.outputField("s13");
00035   outputter.outputField(1.4);
00036   outputter.outputField("s15");
00037   outputter.nextRow();
00038 
00039   outputter.outputField(2);
00040   outputter.outputField(2.2);
00041   outputter.outputField("s23");
00042   outputter.outputField(2.4);
00043   outputter.outputField("s25");
00044   outputter.nextRow();
00045 
00046   outputter.outputField(3);
00047   outputter.outputField(3.2);
00048   outputter.outputField("s33");
00049   outputter.outputField(3.4);
00050   outputter.outputField("s35");
00051   outputter.nextRow();
00052 
00053   std::stringstream expectedOutput;
00054   expectedOutput
00055     << "\n"
00056     << "  very long col name  col b         col cc  col d         col e\n"
00057     << "  ------------------  ------------  ------  ------------  -----\n"
00058     << "                   1    1.2000e+00  s13       1.4000e+00    s15\n"
00059     << "                   2    2.2000e+00  s23       2.4000e+00    s25\n"
00060     << "                   3    3.2000e+00  s33       3.4000e+00    s35\n"
00061     ;
00062 
00063   TEST_EQUALITY_CONST( sout.str(), expectedOutput.str() );
00064 
00065   // 2008/11/12: rabartl: Note: The above test may not be portable because it
00066   // requires the numeric formatting of the doubles to be the same.  To make
00067   // this more portable, I may have to do some work.
00068 
00069 }
00070 
00071 
00072 TEUCHOS_UNIT_TEST( TabularOutputter, basic2 )
00073 {
00074 
00075   typedef Teuchos::TabularOutputter TO;
00076 
00077   std::stringstream sout;
00078   sout << "\n";
00079 
00080   TabularOutputter outputter(Teuchos::rcpFromRef(sout));
00081 
00082   outputter.setFieldTypePrecision(TO::DOUBLE, 8);
00083   outputter.setFieldTypePrecision(TO::INT, 4);
00084   outputter.setFieldTypePrecision(TO::STRING, 5);
00085 
00086   outputter.pushFieldSpec("col a", TO::INT);
00087   outputter.pushFieldSpec("col b", TO::DOUBLE);
00088   outputter.pushFieldSpec("col cc", TO::STRING, TO::LEFT, TO::GENERAL, 6);
00089   outputter.pushFieldSpec("col d", TO::DOUBLE);
00090   outputter.pushFieldSpec("col e", TO::STRING);
00091 
00092   outputter.outputHeader();
00093 
00094   outputter.outputField(1);
00095   outputter.outputField(1.2);
00096   outputter.outputField("s13");
00097   outputter.outputField(1.4);
00098   outputter.outputField("s15");
00099   outputter.nextRow();
00100 
00101   outputter.outputField(2);
00102   outputter.outputField(2.2);
00103   outputter.outputField("s23");
00104   outputter.outputField(2.4);
00105   outputter.outputField("s25");
00106   outputter.nextRow();
00107 
00108   outputter.outputField(3);
00109   outputter.outputField(3.2);
00110   outputter.outputField("s33");
00111   outputter.outputField(3.4);
00112   outputter.outputField("s35");
00113   outputter.nextRow();
00114 
00115   std::stringstream expectedOutput;
00116   expectedOutput
00117     << "\n"
00118     << "  col a  col b             col cc  col d             col e\n"
00119     << "  -----  ----------------  ------  ----------------  -----\n"
00120     << "      1    1.20000000e+00  s13       1.40000000e+00    s15\n"
00121     << "      2    2.20000000e+00  s23       2.40000000e+00    s25\n"
00122     << "      3    3.20000000e+00  s33       3.40000000e+00    s35\n"
00123     ;
00124 
00125   TEST_EQUALITY_CONST( sout.str(), expectedOutput.str() );
00126 
00127   // 2008/11/12: rabartl: Note: See the comment in the basic1 test above!
00128 
00129 }
00130 
00131 
00132 TEUCHOS_UNIT_TEST( TabularOutputter, perfTiming )
00133 {
00134 
00135   typedef Teuchos::TabularOutputter TO;
00136 
00137   std::stringstream sout;
00138   sout << "\n";
00139 
00140   TabularOutputter outputter(sout);
00141 
00142   outputter.pushFieldSpec("num loops", TO::INT);
00143   outputter.pushFieldSpec("vecTime", TO::DOUBLE);
00144   outputter.pushFieldSpec("dequeTime", TO::DOUBLE);
00145 
00146   outputter.outputHeader();
00147 
00148   const int numLoops = 15;
00149 
00150   // num loops
00151   outputter.outputField(numLoops);
00152 
00153   // vecTime
00154   TEUCHOS_START_PERF_OUTPUT_TIMER(outputter, numLoops)
00155   {
00156     std::vector<int> a(numLoops);
00157     std::vector<int> b(numLoops);
00158     a = b;
00159   }
00160   TEUCHOS_END_PERF_OUTPUT_TIMER(outputter, vecTime);
00161   TEST_INEQUALITY_CONST(vecTime, 0);
00162 
00163   // dequeTime
00164   TEUCHOS_START_PERF_OUTPUT_TIMER(outputter, numLoops)
00165   {
00166     std::deque<int> a(numLoops);
00167     std::deque<int> b(numLoops);
00168     a = b;
00169   }
00170   TEUCHOS_END_PERF_OUTPUT_TIMER(outputter, dequeTime);
00171   TEST_INEQUALITY_CONST(dequeTime, 0);
00172 
00173   outputter.nextRow();
00174 
00175   std::stringstream expectedOutput;
00176   expectedOutput
00177     << "\n"
00178     << "Nothing\n"
00179     ;
00180 
00181   TEST_INEQUALITY_CONST( sout.str(), expectedOutput.str() );
00182 
00183   // 2008/11/12: rabartl: Above, this is not the greatest test but it would be
00184   // hard to produce the exact same formatted output since it involves timing
00185   // results.
00186 
00187 }
00188 
00189 
00190 #ifdef TEUCHOS_DEBUG
00191 
00192 
00193 TEUCHOS_UNIT_TEST( TabularOutputter, nullOStream )
00194 {
00195 
00196   typedef Teuchos::TabularOutputter TO;
00197 
00198   TabularOutputter outputter(out);
00199 
00200   TEST_THROW(
00201     outputter.setOStream(Teuchos::null),
00202     Teuchos::NullReferenceError
00203     );
00204 
00205 }
00206 
00207 
00208 TEUCHOS_UNIT_TEST( TabularOutputter, invalidFieldSpecError )
00209 {
00210 
00211   typedef Teuchos::TabularOutputter TO;
00212 
00213   TabularOutputter outputter(out);
00214 
00215   outputter.setFieldTypePrecision(TO::DOUBLE, 8);
00216   outputter.setFieldTypePrecision(TO::INT, 4);
00217   outputter.setFieldTypePrecision(TO::STRING, 3);
00218 
00219   outputter.pushFieldSpec("col d", TO::DOUBLE);
00220 
00221   TEST_THROW(
00222     outputter.pushFieldSpec(
00223       "very long field name", TO::INT, TO::LEFT, TO::GENERAL, 4),
00224     TO::InvalidFieldSpecError
00225     );
00226 
00227 }
00228 
00229 
00230 TEUCHOS_UNIT_TEST( TabularOutputter, missingHeaderError )
00231 {
00232 
00233   typedef Teuchos::TabularOutputter TO;
00234 
00235   TabularOutputter outputter(out);
00236 
00237   outputter.pushFieldSpec("col a", TO::INT);
00238   outputter.pushFieldSpec("col b", TO::DOUBLE);
00239   outputter.pushFieldSpec("col c", TO::STRING);
00240   outputter.pushFieldSpec("col d", TO::DOUBLE);
00241 
00242   TEST_THROW(outputter.outputField(1), TO::MissingHeaderError);
00243 
00244 }
00245 
00246 
00247 TEUCHOS_UNIT_TEST( TabularOutputter, missingNextRowError )
00248 {
00249 
00250   typedef Teuchos::TabularOutputter TO;
00251 
00252   TabularOutputter outputter(out);
00253 
00254   outputter.pushFieldSpec("col a", TO::INT);
00255   outputter.pushFieldSpec("col b", TO::DOUBLE);
00256   outputter.pushFieldSpec("col c", TO::STRING);
00257   outputter.pushFieldSpec("col d", TO::DOUBLE);
00258 
00259   outputter.outputHeader();
00260 
00261   outputter.outputField(1);
00262   outputter.outputField(1.2);
00263   outputter.outputField("s13");
00264   outputter.outputField(1.4);
00265 
00266   // Missing nextRow()!
00267 
00268   TEST_THROW(outputter.outputField(2), TO::InvalidFieldOutputError);
00269 
00270 }
00271 
00272 
00273 TEUCHOS_UNIT_TEST( TabularOutputter, missingFieldOutputError )
00274 {
00275 
00276   typedef Teuchos::TabularOutputter TO;
00277 
00278   TabularOutputter outputter(out);
00279 
00280   outputter.pushFieldSpec("col a", TO::INT);
00281   outputter.pushFieldSpec("col b", TO::DOUBLE);
00282   outputter.pushFieldSpec("col c", TO::STRING);
00283   outputter.pushFieldSpec("col d", TO::DOUBLE);
00284 
00285   outputter.outputHeader();
00286 
00287   outputter.outputField(1);
00288   outputter.outputField(1.2);
00289   outputter.outputField("s13");
00290 
00291   // Missing a call to outputField(...);
00292   
00293   out << "\n\n";
00294 
00295   TEST_THROW(outputter.nextRow(), TO::InvalidFieldOutputError);
00296 
00297 }
00298 
00299 
00300 TEUCHOS_UNIT_TEST( TabularOutputter, missingFieldOutputOkay )
00301 {
00302 
00303   typedef Teuchos::TabularOutputter TO;
00304 
00305   TabularOutputter outputter(out);
00306 
00307   outputter.pushFieldSpec("col a", TO::INT);
00308   outputter.pushFieldSpec("col b", TO::DOUBLE);
00309   outputter.pushFieldSpec("col c", TO::STRING);
00310   outputter.pushFieldSpec("col d", TO::DOUBLE);
00311 
00312   outputter.outputHeader();
00313 
00314   outputter.outputField(1);
00315   outputter.outputField(1.2);
00316   outputter.outputField("s13");
00317 
00318   // Missing a call to outputField(...);
00319 
00320   outputter.nextRow(true); // Just fine!
00321 
00322 }
00323 
00324 
00325 TEUCHOS_UNIT_TEST( TabularOutputter, missingFields )
00326 {
00327 
00328   typedef Teuchos::TabularOutputter TO;
00329 
00330   std::ostringstream sout;
00331   TabularOutputter outputter(sout);
00332 
00333   TEST_THROW(outputter.outputHeader(), TO::MissingFieldsError);
00334 
00335 }
00336 
00337 
00338 #endif // TEUCHOS_DEBUG
00339 
00340 
00341 } // namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Tue Oct 20 10:13:59 2009 for Teuchos Package Browser (Single Doxygen Collection) by  doxygen 1.6.1