Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
ScalarTraits_test.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 
44 #include "Teuchos_ScalarTraits.hpp"
49 #include "Teuchos_Version.hpp"
50 
51 
52 namespace {
53 
54 
55 //
56 // Output of the ordinal that will avoid printing non-asci chars.
57 //
58 // The issue is that if you just print a raw char then if it prints non-asci
59 // character, it can not be printed in some cases and causes trouble with
60 // CTest/CDash.
61 //
62 
63 // For general types, just print the type
64 template <class T>
65 T outputOrdinal(const T &t)
66 {
67  return t;
68 }
69 
70 // For char, print the int value (avoid non-ansi chars)
71 int outputOrdinal(const char& t)
72 {
73  return t;
74 }
75 
76 
77 //
78 // Type ouptutting
79 //
80 
81 template <class T>
82 void TYPE_CHAIN_A(Teuchos::FancyOStream &out) {
84  out << Teuchos::typeName(b);
85  if (typeid(b) != typeid(d)) {
86  out << " -> ";
87  TYPE_CHAIN_A<typename Teuchos::ScalarTraits<T>::doublePrecision>(out);
88  }
89 }
90 
91 template <class T>
92 void TYPE_CHAIN_D(Teuchos::FancyOStream &out) {
94  out << Teuchos::typeName(b);
95  if (typeid(b) != typeid(d)) {
96  out << " -> ";
97  TYPE_CHAIN_D<typename Teuchos::ScalarTraits<T>::halfPrecision>(out);
98  }
99 }
100 
101 
102 inline std::string passfail(bool result)
103 {
104  return ( result ? "passed" : "failed" );
105 }
106 
107 
108 template<class Scalar>
109 bool testScalarTraits(
111  )
112 {
113 
115 
116  bool success = true;
117  bool result;
118 
119  out << "\nTesting: " << Teuchos::TypeNameTraits<ST>::name() << " ...\n";
120 
121  Teuchos::OSTab tab(out);
122 
123  const Scalar nan = ST::nan();
124 
125  out << "Type chain (ascending) : "; TYPE_CHAIN_A<Scalar>(out); out << "\n";
126  out << "Type chain (descending): "; TYPE_CHAIN_D<Scalar>(out); out << "\n";
127 
128  out << "\nTesting that squareroot(NaN) == NaN! ...\n";
129  {
130  const Scalar sqrtNan = ST::squareroot(nan);
131  result = (ST::isnaninf(sqrtNan));
132  if (!result) success = false;
133  out
134  << "squareroot("<<nan<<") = " << sqrtNan << " == " << nan << " : "
135  << passfail(result) << "\n";
136  }
137 
138  out << "\nTesting that squareroot(-NaN) == NaN! ...\n";
139  {
140  const Scalar negNan = -nan;
141  const Scalar sqrtNegNan = ST::squareroot(negNan);
142  result = (ST::isnaninf(sqrtNegNan));
143  if (!result) success = false;
144  out
145  << "squareroot("<<negNan<<") = " << sqrtNegNan << " == " << nan << " : "
146  << passfail(result) << "\n";
147  }
148 
149  if (ST::isComplex == false)
150  {
151  out << "\nTesting that squareroot(-1) == NaN! ...\n";
152  {
153  const Scalar negOne = -ST::one();
154  const Scalar sqrtNegOne = ST::squareroot(negOne);
155  result = (ST::isnaninf(sqrtNegOne));
156  if (!result) success = false;
157  out
158  << "squareroot("<<negOne<<") = " << sqrtNegOne << " == " << nan << " : "
159  << passfail(result) << "\n";
160  }
161  }
162 
163 #ifdef HAVE_NUMERIC_LIMITS
164 
165  out << "\nTesting that squareroot(quiet_NaN) == NaN! ...\n";
166  {
167  const Scalar nan = std::numeric_limits<Scalar>::quiet_NaN();
168  const Scalar sqrtNan = ST::squareroot(nan);
169  result = (ST::isnaninf(sqrtNan));
170  if (!result) success = false;
171  out
172  << "squareroot("<<nan<<") = " << sqrtNan << " == " << nan << " : "
173  << passfail(result) << "\n";
174  }
175 
176  out << "\nTesting that squareroot(signaling_NaN) == NaN! ...\n";
177  {
178  const Scalar nan = std::numeric_limits<Scalar>::signaling_NaN();
179  const Scalar sqrtNan = ST::squareroot(nan);
180  result = (ST::isnaninf(sqrtNan));
181  if (!result) success = false;
182  out
183  << "squareroot("<<nan<<") = " << sqrtNan << " == " << nan << " : "
184  << passfail(result) << "\n";
185  }
186 
187  out << "\nTesting that squareroot(inf) == NaN! ...\n";
188  {
189  const Scalar inf = std::numeric_limits<Scalar>::infinity();
190  const Scalar sqrtInf = ST::squareroot(inf);
191  result = (ST::isnaninf(sqrtInf));
192  if (!result) success = false;
193  out
194  << "squareroot("<<inf<<") = " << sqrtInf << " == " << nan << " : "
195  << passfail(result) << "\n";
196  }
197 
198 #endif // HAVE_NUMERIC_LIMITS
199 
200  return success;
201 
202 }
203 
204 
205 template<class Ordinal>
206 bool testOrdinalTraits(
208  )
209 {
210 
212 
213  bool success = true;
214  bool result;
215 
216  out << "\nTesting: " << Teuchos::TypeNameTraits<OT>::name() << " ...\n";
217 
218  Teuchos::OSTab tab(out);
219 
220  const Ordinal zero = OT::zero();
221  const Ordinal one = OT::one();
222  const Ordinal max = OT::max();
223  const Ordinal invalid = OT::invalid();
224  out << "\nmax() == " << outputOrdinal(max) << "\n";
225  out << "\ninvalid() == " << outputOrdinal(invalid) << "\n";
226 
227  out << "\nTesting that zero() * one() == zero() ...\n";
228  {
229  const Ordinal zto = zero*one;
230  result = (zto == zero);
231  if (!result) success = false;
232  out
233  << "zero*one = " << outputOrdinal(zto) << " == " << outputOrdinal(zero) << " : "
234  << passfail(result) << "\n";
235  }
236 
237  out << "\nTesting that one() * one() == one() ...\n";
238  {
239  const Ordinal oto = one*one;
240  result = (oto == one);
241  if (!result) success = false;
242  out
243  << "one*one = " << outputOrdinal(oto) << " == " << outputOrdinal(one) << " : "
244  << passfail(result) << "\n";
245  }
246 
247  out << "\nTesting that one() + zero() == zero() + one() == one() ...\n";
248  {
249  const Ordinal opz = one+zero;
250  const Ordinal zpo = zero+one;
251  result = (opz == one) && (zpo == one);
252  if (!result) success = false;
253  out
254  << "one+zero = " << outputOrdinal(opz) << " == zero+one = "
255  << outputOrdinal(zpo) << " == " << outputOrdinal(one) << " : "
256  << passfail(result) << "\n";
257  }
258 
259  out << "\nTesting that one() - one() == zero() ...\n";
260  {
261  const Ordinal omo = one-one;
262  result = (omo == zero);
263  if (!result) success = false;
264  out
265  << "one-one = " << outputOrdinal(omo) << " == " << outputOrdinal(zero) << " : "
266  << passfail(result) << "\n";
267  }
268 
269  out << "\nTesting that zero() < one() <= max() ...\n";
270  {
271  result = (zero < one) && (one <= max) && (zero < max);
272  if (!result) success = false;
273  out
274  << "(zero < one) = " << (zero < one) << " == "
275  << "(one <= max) = " << (one <= max) << " == "
276  << "(zero < max) = " << (zero < max) << " == "
277  << true << " : "
278  << passfail(result) << "\n";
279  }
280 
281  out << "\nTesting that invalid() not in [zero(),max()]...\n";
282  {
283  result = !( (invalid > zero || invalid==zero) && (invalid <= max) );
284  if (!result) success = false;
285  out
286  << "invalid in [zero,max] == false : " << passfail(result) << "\n";
287  }
288 
289  return success;
290 
291 }
292 
293 
294 } // namespace
295 
296 
297 int main( int argc, char* argv[] ) {
298 
300 
301  bool success = true;
302  bool result;
303 
304  Teuchos::GlobalMPISession mpiSession(&argc,&argv);
305  //const int procRank = Teuchos::GlobalMPISession::getRank();
306 
309 
310  try {
311 
312  // Read options from the commandline
313  CommandLineProcessor clp(false); // Don't throw exceptions
314  CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
315  if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) {
316  *out << "\nEnd Result: TEST FAILED" << std::endl;
317  return parse_return;
318  }
319 
320  result = testScalarTraits<float>(*out);
321  if(!result) success = false;
322 
323  result = testScalarTraits<double>(*out);
324  if(!result) success = false;
325 
326  result = testOrdinalTraits<char>(*out);
327  if(!result) success = false;
328 
329  result = testOrdinalTraits<short int>(*out);
330  if(!result) success = false;
331 
332  result = testOrdinalTraits<int>(*out);
333  if(!result) success = false;
334 
335  result = testOrdinalTraits<long int>(*out);
336  if(!result) success = false;
337 
338  result = testOrdinalTraits<size_t>(*out);
339  if(!result) success = false;
340 
341 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
342  result = testOrdinalTraits<long long int>(*out);
343  if(!result) success = false;
344 #endif
345 
346 #ifdef HAVE_TEUCHOS___INT64
347  result = testOrdinalTraits<__int64>(*out);
348  if(!result) success = false;
349 
350  result = testOrdinalTraits<unsigned __int64>(*out);
351  if(!result) success = false;
352 #endif
353 
354 // #ifdef HAVE_TEUCHOS_COMPLEX
355 // result = testScalarTraits<std::complex<double> >(*out);
356 // if(!result) success = false;
357 // #endif
358 
359 #ifdef HAVE_TEUCHOS_QD
360  result = testScalarTraits<dd_real>(*out);
361  if(!result) success = false;
362  result = testScalarTraits<qd_real>(*out);
363  if(!result) success = false;
364 #endif
365 
366  }
367  TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
368 
369  if(success)
370  *out << "\nEnd Result: TEST PASSED\n" << std::endl;
371  else
372  *out << "\nEnd Result: TEST FAILED\n" << std::endl;
373 
374  return ( success ? 0 : 1 );
375 
376 }
std::string typeName(const T &t)
Template function for returning the concrete type name of a passed-in object.
Tabbing class for helping to create formated, indented output for a basic_FancyOStream object...
Initialize, finalize, and query the global MPI session.
const std::string passfail(const bool result)
Return "passed" or "failed".
This structure defines some basic traits for a scalar field type.
int main(int argc, char *argv[])
std::ostream subclass that performs the magic of indenting data sent to an std::ostream object among ...
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
Simple macro that catches and reports standard exceptions and other exceptions.
static RCP< FancyOStream > getDefaultOStream()
Get the default output stream object.
This structure defines some basic traits for the ordinal field type.
T doublePrecision
Typedef for double precision.
Defines basic traits for the ordinal field type.
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
Basic command line parser for input from (argc,argv[])
Defines basic traits for the scalar field type.
Smart reference counting pointer class for automatic garbage collection.
T halfPrecision
Typedef for half precision.
RCP< basic_FancyOStream< CharT, Traits > > tab(const RCP< basic_FancyOStream< CharT, Traits > > &out, const int tabs=1, const std::basic_string< CharT, Traits > linePrefix="")
Create a tab for an RCP-wrapped basic_FancyOStream object to cause the indentation of all output auto...
Class that helps parse command line input arguments from (argc,argv[]) and set options.