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 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00039 //
00040 // ***********************************************************************
00041 // @HEADER
00042 */
00043 
00044 #include "Teuchos_UnitTestHarness.hpp"
00045 #include "Teuchos_TabularOutputter.hpp"
00046 
00047 
00048 namespace {
00049 
00050 
00051 using Teuchos::null;
00052 using Teuchos::RCP;
00053 using Teuchos::rcp;
00054 using Teuchos::TabularOutputter;
00055 
00056 
00057 TEUCHOS_UNIT_TEST( TabularOutputter, basic1 )
00058 {
00059 
00060   typedef Teuchos::TabularOutputter TO;
00061 
00062   std::stringstream sout;
00063   sout << "\n";
00064 
00065   TabularOutputter outputter(sout);
00066 
00067   outputter.pushFieldSpec("very long col name", TO::INT);
00068   outputter.pushFieldSpec("col b", TO::DOUBLE);
00069   outputter.pushFieldSpec("col cc", TO::STRING, TO::LEFT, TO::GENERAL, 6);
00070   outputter.pushFieldSpec("col d", TO::DOUBLE);
00071   outputter.pushFieldSpec("col e", TO::STRING);
00072 
00073   outputter.outputHeader();
00074 
00075   outputter.outputField(1);
00076   outputter.outputField(1.2);
00077   outputter.outputField("s13");
00078   outputter.outputField(1.4);
00079   outputter.outputField("s15");
00080   outputter.nextRow();
00081 
00082   outputter.outputField(2);
00083   outputter.outputField(2.2);
00084   outputter.outputField("s23");
00085   outputter.outputField(2.4);
00086   outputter.outputField("s25");
00087   outputter.nextRow();
00088 
00089   outputter.outputField(3);
00090   outputter.outputField(3.2);
00091   outputter.outputField("s33");
00092   outputter.outputField(3.4);
00093   outputter.outputField("s35");
00094   outputter.nextRow();
00095 
00096   std::stringstream expectedOutput;
00097   expectedOutput
00098     << "\n"
00099     << "  very long col name  col b         col cc  col d         col e\n"
00100     << "  ------------------  ------------  ------  ------------  -----\n"
00101     << "                   1    1.2000e+00  s13       1.4000e+00    s15\n"
00102     << "                   2    2.2000e+00  s23       2.4000e+00    s25\n"
00103     << "                   3    3.2000e+00  s33       3.4000e+00    s35\n"
00104     ;
00105 
00106   TEST_EQUALITY_CONST( sout.str(), expectedOutput.str() );
00107 
00108   // 2008/11/12: rabartl: Note: The above test may not be portable because it
00109   // requires the numeric formatting of the doubles to be the same.  To make
00110   // this more portable, I may have to do some work.
00111 
00112 }
00113 
00114 
00115 TEUCHOS_UNIT_TEST( TabularOutputter, basic2 )
00116 {
00117 
00118   typedef Teuchos::TabularOutputter TO;
00119 
00120   std::stringstream sout;
00121   sout << "\n";
00122 
00123   TabularOutputter outputter(Teuchos::rcpFromRef(sout));
00124 
00125   outputter.setFieldTypePrecision(TO::DOUBLE, 8);
00126   outputter.setFieldTypePrecision(TO::INT, 4);
00127   outputter.setFieldTypePrecision(TO::STRING, 5);
00128 
00129   outputter.pushFieldSpec("col a", TO::INT);
00130   outputter.pushFieldSpec("col b", TO::DOUBLE);
00131   outputter.pushFieldSpec("col cc", TO::STRING, TO::LEFT, TO::GENERAL, 6);
00132   outputter.pushFieldSpec("col d", TO::DOUBLE);
00133   outputter.pushFieldSpec("col e", TO::STRING);
00134 
00135   outputter.outputHeader();
00136 
00137   outputter.outputField(1);
00138   outputter.outputField(1.2);
00139   outputter.outputField("s13");
00140   outputter.outputField(1.4);
00141   outputter.outputField("s15");
00142   outputter.nextRow();
00143 
00144   outputter.outputField(2);
00145   outputter.outputField(2.2);
00146   outputter.outputField("s23");
00147   outputter.outputField(2.4);
00148   outputter.outputField("s25");
00149   outputter.nextRow();
00150 
00151   outputter.outputField(3);
00152   outputter.outputField(3.2);
00153   outputter.outputField("s33");
00154   outputter.outputField(3.4);
00155   outputter.outputField("s35");
00156   outputter.nextRow();
00157 
00158   std::stringstream expectedOutput;
00159   expectedOutput
00160     << "\n"
00161     << "  col a  col b             col cc  col d             col e\n"
00162     << "  -----  ----------------  ------  ----------------  -----\n"
00163     << "      1    1.20000000e+00  s13       1.40000000e+00    s15\n"
00164     << "      2    2.20000000e+00  s23       2.40000000e+00    s25\n"
00165     << "      3    3.20000000e+00  s33       3.40000000e+00    s35\n"
00166     ;
00167 
00168   TEST_EQUALITY_CONST( sout.str(), expectedOutput.str() );
00169 
00170   // 2008/11/12: rabartl: Note: See the comment in the basic1 test above!
00171 
00172 }
00173 
00174 
00175 TEUCHOS_UNIT_TEST( TabularOutputter, perfTiming )
00176 {
00177 
00178   typedef Teuchos::TabularOutputter TO;
00179 
00180   std::stringstream sout;
00181   sout << "\n";
00182 
00183   TabularOutputter outputter(sout);
00184 
00185   outputter.pushFieldSpec("num loops", TO::INT);
00186   outputter.pushFieldSpec("vecTime", TO::DOUBLE);
00187   outputter.pushFieldSpec("dequeTime", TO::DOUBLE);
00188 
00189   outputter.outputHeader();
00190 
00191   const int numLoops = 15;
00192 
00193   // num loops
00194   outputter.outputField(numLoops);
00195 
00196   // vecTime
00197   TEUCHOS_START_PERF_OUTPUT_TIMER(outputter, numLoops)
00198   {
00199     std::vector<int> a(numLoops);
00200     std::vector<int> b(numLoops);
00201     a = b;
00202   }
00203   TEUCHOS_END_PERF_OUTPUT_TIMER(outputter, vecTime);
00204   TEST_INEQUALITY_CONST(vecTime, 0);
00205 
00206   // dequeTime
00207   TEUCHOS_START_PERF_OUTPUT_TIMER(outputter, numLoops)
00208   {
00209     std::deque<int> a(numLoops);
00210     std::deque<int> b(numLoops);
00211     a = b;
00212   }
00213   TEUCHOS_END_PERF_OUTPUT_TIMER(outputter, dequeTime);
00214   TEST_INEQUALITY_CONST(dequeTime, 0);
00215 
00216   outputter.nextRow();
00217 
00218   std::stringstream expectedOutput;
00219   expectedOutput
00220     << "\n"
00221     << "Nothing\n"
00222     ;
00223 
00224   TEST_INEQUALITY_CONST( sout.str(), expectedOutput.str() );
00225 
00226   // 2008/11/12: rabartl: Above, this is not the greatest test but it would be
00227   // hard to produce the exact same formatted output since it involves timing
00228   // results.
00229 
00230 }
00231 
00232 
00233 #ifdef TEUCHOS_DEBUG
00234 
00235 
00236 TEUCHOS_UNIT_TEST( TabularOutputter, nullOStream )
00237 {
00238   //typedef Teuchos::TabularOutputter TO; // unused
00239 
00240   TabularOutputter outputter(out);
00241 
00242   TEST_THROW(
00243     outputter.setOStream(Teuchos::null),
00244     Teuchos::NullReferenceError
00245     );
00246 }
00247 
00248 
00249 TEUCHOS_UNIT_TEST( TabularOutputter, invalidFieldSpecError )
00250 {
00251 
00252   typedef Teuchos::TabularOutputter TO;
00253 
00254   TabularOutputter outputter(out);
00255 
00256   outputter.setFieldTypePrecision(TO::DOUBLE, 8);
00257   outputter.setFieldTypePrecision(TO::INT, 4);
00258   outputter.setFieldTypePrecision(TO::STRING, 3);
00259 
00260   outputter.pushFieldSpec("col d", TO::DOUBLE);
00261 
00262   TEST_THROW(
00263     outputter.pushFieldSpec(
00264       "very long field name", TO::INT, TO::LEFT, TO::GENERAL, 4),
00265     TO::InvalidFieldSpecError
00266     );
00267 
00268 }
00269 
00270 
00271 TEUCHOS_UNIT_TEST( TabularOutputter, missingHeaderError )
00272 {
00273 
00274   typedef Teuchos::TabularOutputter TO;
00275 
00276   TabularOutputter outputter(out);
00277 
00278   outputter.pushFieldSpec("col a", TO::INT);
00279   outputter.pushFieldSpec("col b", TO::DOUBLE);
00280   outputter.pushFieldSpec("col c", TO::STRING);
00281   outputter.pushFieldSpec("col d", TO::DOUBLE);
00282 
00283   TEST_THROW(outputter.outputField(1), TO::MissingHeaderError);
00284 
00285 }
00286 
00287 
00288 TEUCHOS_UNIT_TEST( TabularOutputter, missingNextRowError )
00289 {
00290 
00291   typedef Teuchos::TabularOutputter TO;
00292 
00293   TabularOutputter outputter(out);
00294 
00295   outputter.pushFieldSpec("col a", TO::INT);
00296   outputter.pushFieldSpec("col b", TO::DOUBLE);
00297   outputter.pushFieldSpec("col c", TO::STRING);
00298   outputter.pushFieldSpec("col d", TO::DOUBLE);
00299 
00300   outputter.outputHeader();
00301 
00302   outputter.outputField(1);
00303   outputter.outputField(1.2);
00304   outputter.outputField("s13");
00305   outputter.outputField(1.4);
00306 
00307   // Missing nextRow()!
00308 
00309   TEST_THROW(outputter.outputField(2), TO::InvalidFieldOutputError);
00310 
00311 }
00312 
00313 
00314 TEUCHOS_UNIT_TEST( TabularOutputter, missingFieldOutputError )
00315 {
00316 
00317   typedef Teuchos::TabularOutputter TO;
00318 
00319   TabularOutputter outputter(out);
00320 
00321   outputter.pushFieldSpec("col a", TO::INT);
00322   outputter.pushFieldSpec("col b", TO::DOUBLE);
00323   outputter.pushFieldSpec("col c", TO::STRING);
00324   outputter.pushFieldSpec("col d", TO::DOUBLE);
00325 
00326   outputter.outputHeader();
00327 
00328   outputter.outputField(1);
00329   outputter.outputField(1.2);
00330   outputter.outputField("s13");
00331 
00332   // Missing a call to outputField(...);
00333 
00334   out << "\n\n";
00335 
00336   TEST_THROW(outputter.nextRow(), TO::InvalidFieldOutputError);
00337 
00338 }
00339 
00340 
00341 TEUCHOS_UNIT_TEST( TabularOutputter, missingFieldOutputOkay )
00342 {
00343 
00344   typedef Teuchos::TabularOutputter TO;
00345 
00346   TabularOutputter outputter(out);
00347 
00348   outputter.pushFieldSpec("col a", TO::INT);
00349   outputter.pushFieldSpec("col b", TO::DOUBLE);
00350   outputter.pushFieldSpec("col c", TO::STRING);
00351   outputter.pushFieldSpec("col d", TO::DOUBLE);
00352 
00353   outputter.outputHeader();
00354 
00355   outputter.outputField(1);
00356   outputter.outputField(1.2);
00357   outputter.outputField("s13");
00358 
00359   // Missing a call to outputField(...);
00360 
00361   outputter.nextRow(true); // Just fine!
00362 
00363 }
00364 
00365 
00366 TEUCHOS_UNIT_TEST( TabularOutputter, missingFields )
00367 {
00368 
00369   typedef Teuchos::TabularOutputter TO;
00370 
00371   std::ostringstream sout;
00372   TabularOutputter outputter(sout);
00373 
00374   TEST_THROW(outputter.outputHeader(), TO::MissingFieldsError);
00375 
00376 }
00377 
00378 
00379 #endif // TEUCHOS_DEBUG
00380 
00381 
00382 } // namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines