1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/limits_test.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,199 @@
1.4 +//
1.5 +// boost limits_test.cpp test your <limits> file for important
1.6 +// Copyright Jens Maurer 2000
1.7 +// Permission to use, copy, modify, sell, and distribute this software
1.8 +// is hereby granted without fee provided that the above copyright notice
1.9 +// appears in all copies and that both that copyright notice and this
1.10 +// permission notice appear in supporting documentation,
1.11 +// Jens Maurer makes no representations about the suitability of this
1.12 +// software for any purpose. It is provided "as is" without express or
1.13 +// implied warranty.
1.14 +//
1.15 +//
1.16 +
1.17 +#include <limits>
1.18 +
1.19 +#include "cppunit/cppunit_proxy.h"
1.20 +
1.21 +#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
1.22 +using namespace std;
1.23 +#endif
1.24 +
1.25 +//
1.26 +// TestCase class
1.27 +//
1.28 +class LimitTest : public CPPUNIT_NS::TestCase
1.29 +{
1.30 + CPPUNIT_TEST_SUITE(LimitTest);
1.31 + CPPUNIT_TEST(test);
1.32 +# if defined (__BORLANDC__)
1.33 + CPPUNIT_IGNORE;
1.34 +# endif
1.35 + CPPUNIT_TEST(qnan_test);
1.36 + CPPUNIT_TEST(limits_cov);
1.37 + CPPUNIT_TEST_SUITE_END();
1.38 +
1.39 +protected:
1.40 + void test();
1.41 + void qnan_test();
1.42 + void limits_cov();
1.43 +};
1.44 +
1.45 +CPPUNIT_TEST_SUITE_REGISTRATION(LimitTest);
1.46 +
1.47 +# define CHECK_COND(X) if (!(X)) return false;
1.48 +
1.49 +bool valid_sign_info(bool, bool)
1.50 +{ return true; }
1.51 +
1.52 +template <class _Tp>
1.53 +bool valid_sign_info(bool limit_is_signed, const _Tp &) {
1.54 + return limit_is_signed && _Tp(-1) < 0 ||
1.55 + !limit_is_signed && _Tp(-1) > 0;
1.56 +}
1.57 +
1.58 +template <class _Tp>
1.59 +bool test_integral_limits(const _Tp &, bool unknown_sign = true, bool is_signed = true) {
1.60 + typedef numeric_limits<_Tp> lim;
1.61 +
1.62 + CHECK_COND(lim::is_specialized);
1.63 + CHECK_COND(lim::is_integer);
1.64 + /*CHECK_COND(lim::is_modulo);*/
1.65 + CHECK_COND(lim::min() < lim::max());
1.66 + CHECK_COND((unknown_sign && ((lim::is_signed && (lim::min() != 0)) || (!lim::is_signed && (lim::min() == 0)))) ||
1.67 + (!unknown_sign && ((lim::is_signed && is_signed) || (!lim::is_signed && !is_signed))));
1.68 +
1.69 + if (unknown_sign) {
1.70 + CHECK_COND(valid_sign_info(lim::is_signed, _Tp()));
1.71 + }
1.72 + return true;
1.73 +}
1.74 +
1.75 +template <class _Tp>
1.76 +bool test_signed_integral_limits(const _Tp &__val) {
1.77 + return test_integral_limits(__val, false, true);
1.78 +}
1.79 +template <class _Tp>
1.80 +bool test_unsigned_integral_limits(const _Tp &__val) {
1.81 + return test_integral_limits(__val, false, false);
1.82 +}
1.83 +
1.84 +template <class _Tp>
1.85 +bool test_float_limits(const _Tp &) {
1.86 + typedef numeric_limits<_Tp> lim;
1.87 + CHECK_COND(lim::is_specialized);
1.88 + CHECK_COND(!lim::is_modulo);
1.89 + CHECK_COND(!lim::is_integer);
1.90 + CHECK_COND(lim::is_signed);
1.91 +
1.92 + CHECK_COND(lim::max() > 1000);
1.93 + CHECK_COND(lim::min() > 0);
1.94 + CHECK_COND(lim::min() < 0.001);
1.95 + CHECK_COND(lim::epsilon() > 0);
1.96 +
1.97 + if (lim::is_iec559) {
1.98 + CHECK_COND(lim::has_infinity);
1.99 + CHECK_COND(lim::has_quiet_NaN);
1.100 + CHECK_COND(lim::has_signaling_NaN);
1.101 + CHECK_COND(lim::signaling_NaN);
1.102 + }
1.103 +
1.104 + if (lim::has_infinity) {
1.105 + const _Tp infinity = lim::infinity();
1.106 + /* Make sure those values are not 0 or similar nonsense.
1.107 + * Infinity must compare as if larger than the maximum representable value.
1.108 + */
1.109 + CHECK_COND(infinity > lim::max());
1.110 + CHECK_COND(-infinity < -lim::max());
1.111 + }
1.112 + return true;
1.113 +}
1.114 +
1.115 +template <class _Tp>
1.116 +bool test_qnan(const _Tp &) {
1.117 + typedef numeric_limits<_Tp> lim;
1.118 + if (lim::has_quiet_NaN) {
1.119 + const _Tp qnan = lim::quiet_NaN();
1.120 +
1.121 + /* NaNs shall always compare "false" when compared for equality
1.122 + * If one of these fail, your compiler may be optimizing incorrectly,
1.123 + * or the STLport is incorrectly configured.
1.124 + */
1.125 + CHECK_COND(! (qnan == 42));
1.126 + CHECK_COND(! (qnan == qnan));
1.127 + CHECK_COND(qnan != 42);
1.128 + CHECK_COND(qnan != qnan);
1.129 +
1.130 + /* The following tests may cause arithmetic traps.
1.131 + * CHECK_COND(! (qnan < 42));
1.132 + * CHECK_COND(! (qnan > 42));
1.133 + * CHECK_COND(! (qnan <= 42));
1.134 + * CHECK_COND(! (qnan >= 42));
1.135 + */
1.136 + }
1.137 + return true;
1.138 +}
1.139 +void LimitTest::test() {
1.140 + CPPUNIT_ASSERT(test_integral_limits(bool()));
1.141 + /*
1.142 + It is implementation-defined whether a char object can hold
1.143 + negative values. Characters can be explicitly declared unsigned or signed. Plain char, signed char, and unsigned
1.144 + char are three distinct types.
1.145 + */
1.146 + //CPPUNIT_ASSERT(test_integral_limits(char()));
1.147 + typedef signed char signed_char;
1.148 + CPPUNIT_ASSERT(test_signed_integral_limits(signed_char()));
1.149 + typedef unsigned char unsigned_char;
1.150 + CPPUNIT_ASSERT(test_unsigned_integral_limits(unsigned_char()));
1.151 +# if defined (_STLP_HAS_WCHAR_T) && !defined (_STLP_WCHAR_T_IS_USHORT)
1.152 + CPPUNIT_ASSERT(test_integral_limits(wchar_t()));
1.153 +# endif
1.154 + CPPUNIT_ASSERT(test_signed_integral_limits(short()));
1.155 + typedef unsigned short unsigned_short;
1.156 + CPPUNIT_ASSERT(test_unsigned_integral_limits(unsigned_short()));
1.157 + CPPUNIT_ASSERT(test_signed_integral_limits(int()));
1.158 + typedef unsigned int unsigned_int;
1.159 + CPPUNIT_ASSERT(test_unsigned_integral_limits(unsigned_int()));
1.160 + CPPUNIT_ASSERT(test_signed_integral_limits(long()));
1.161 + typedef unsigned long unsigned_long;
1.162 + CPPUNIT_ASSERT(test_unsigned_integral_limits(unsigned_long()));
1.163 +# if defined (_STLP_LONG_LONG)
1.164 + typedef _STLP_LONG_LONG long_long;
1.165 + CPPUNIT_ASSERT(test_signed_integral_limits(long_long()));
1.166 + typedef unsigned _STLP_LONG_LONG unsigned_long_long;
1.167 + CPPUNIT_ASSERT(test_unsigned_integral_limits(unsigned_long_long()));
1.168 +#endif
1.169 +
1.170 + CPPUNIT_ASSERT(test_float_limits(float()));
1.171 + CPPUNIT_ASSERT(test_float_limits(double()));
1.172 +# if !defined ( _STLP_NO_LONG_DOUBLE )
1.173 + typedef long double long_double;
1.174 + CPPUNIT_ASSERT(test_float_limits(long_double()));
1.175 +# endif
1.176 +}
1.177 +
1.178 +void LimitTest::qnan_test() {
1.179 + CPPUNIT_ASSERT(test_qnan(float()));
1.180 + CPPUNIT_ASSERT(test_qnan(double()));
1.181 +# if !defined ( _STLP_NO_LONG_DOUBLE )
1.182 + typedef long double long_double;
1.183 + CPPUNIT_ASSERT(test_qnan(long_double()));
1.184 +# endif
1.185 +}
1.186 +void LimitTest::limits_cov()
1.187 + {
1.188 + if(numeric_limits<float>::has_denorm == false)
1.189 + {
1.190 + CPPUNIT_ASSERT(numeric_limits<float>::denorm_min( )); // denorm_min for float
1.191 + CPPUNIT_ASSERT(numeric_limits<double>::denorm_min( )); // denorm_min for double
1.192 + CPPUNIT_ASSERT(numeric_limits<long double>::denorm_min( )); // denorm_min for long double
1.193 + }
1.194 + numeric_limits<float>::round_error( ); // round_error for float
1.195 + numeric_limits<double>::round_error( ); // round_error for double
1.196 + numeric_limits<long double>::round_error( ); // round_error for long double
1.197 + if(numeric_limits<long double>::is_iec559)
1.198 + {
1.199 + CPPUNIT_ASSERT(numeric_limits<long double>::infinity( )); // infinity for long double
1.200 + CPPUNIT_ASSERT(numeric_limits<long double>::quiet_NaN( ));// quiet_NaN for long double
1.201 + }
1.202 + }