ReactOS 0.4.15-dev-7994-gb388cb6
limits_test.cpp File Reference
#include <limits>
#include "cppunit/cppunit_proxy.h"
Include dependency graph for limits_test.cpp:

Go to the source code of this file.

Classes

class  LimitTest
 
class  ArbitraryType
 

Macros

#define CHECK_COND(X)   if (!check_cond(X)) { CPPUNIT_MESSAGE(#X); return false; }
 

Functions

 CPPUNIT_TEST_SUITE_REGISTRATION (LimitTest)
 
bool check_cond (const bool &cond)
 
bool valid_sign_info (bool, bool)
 
template<class _Tp >
bool valid_sign_info (bool limit_is_signed, const _Tp &)
 
template<class _Tp >
bool test_integral_limits_base (const _Tp &, bool unknown_sign=true, bool is_signed=true)
 
template<class _Tp >
bool test_integral_limits (const _Tp &val, bool unknown_sign=true, bool is_signed=true)
 
template<class _Tp >
bool test_signed_integral_limits (const _Tp &__val)
 
template<class _Tp >
bool test_unsigned_integral_limits (const _Tp &__val)
 
template<class _Tp >
bool test_float_values (_Tp lhs, _Tp rhs)
 
template<class _Tp >
bool test_float_limits (const _Tp &)
 
template<class _Tp >
bool test_qnan (const _Tp &)
 

Macro Definition Documentation

◆ CHECK_COND

#define CHECK_COND (   X)    if (!check_cond(X)) { CPPUNIT_MESSAGE(#X); return false; }

Definition at line 57 of file limits_test.cpp.

Function Documentation

◆ check_cond()

bool check_cond ( const bool cond)

Definition at line 56 of file limits_test.cpp.

56{ return cond; }

◆ CPPUNIT_TEST_SUITE_REGISTRATION()

CPPUNIT_TEST_SUITE_REGISTRATION ( LimitTest  )

◆ test_float_limits()

template<class _Tp >
bool test_float_limits ( const _Tp &  )

Definition at line 124 of file limits_test.cpp.

124 {
125 typedef numeric_limits<_Tp> lim;
126 CHECK_COND(lim::is_specialized);
127 CHECK_COND(!lim::is_modulo);
128 CHECK_COND(!lim::is_integer);
129 CHECK_COND(lim::is_signed);
130
131 CHECK_COND(lim::max() > 1000);
132 CHECK_COND(lim::min() > 0);
133 CHECK_COND(lim::min() < 0.001);
134 CHECK_COND(lim::epsilon() > 0);
135
136 if (lim::is_iec559) {
137 CHECK_COND(lim::has_infinity);
138 CHECK_COND(lim::has_quiet_NaN);
139 CHECK_COND(lim::has_signaling_NaN);
140 CHECK_COND(lim::has_denorm == denorm_present);
141 }
142
143 if (lim::has_denorm == denorm_absent) {
144 CHECK_COND(lim::denorm_min() == lim::min());
145 _Tp tmp = lim::min();
146 tmp /= 2;
147 if (tmp > 0 && tmp < lim::min()) {
148 // has_denorm could be denorm_present
149 CPPUNIT_MESSAGE("It looks like your compiler/platform supports denormalized floating point representation.");
150 }
151 }
152 else if (lim::has_denorm == denorm_present) {
153 CHECK_COND(lim::denorm_min() > 0);
154 CHECK_COND(lim::denorm_min() < lim::min());
155
156 _Tp tmp = lim::min();
157 while (tmp != 0) {
158 _Tp old_tmp = tmp;
159 tmp /= 2;
160 CHECK_COND(tmp < old_tmp);
161 CHECK_COND(tmp >= lim::denorm_min() || tmp == (_Tp)0);
162 //ostringstream str;
163 //str << "denorm_min = " << lim::denorm_min() << ", tmp = " << tmp;
164 //CPPUNIT_MESSAGE(str.str().c_str());
165 }
166 }
167
168 if (lim::has_infinity) {
169 const _Tp infinity = lim::infinity();
170 /* Make sure those values are not 0 or similar nonsense.
171 * Infinity must compare as if larger than the maximum representable value. */
172
173 _Tp val = lim::max();
174 val *= 2;
175
176 /* We use test_float_values because without it some compilers (gcc) perform weird
177 * optimization on the test giving unexpected result. */
178 CHECK_COND(test_float_values(val, infinity));
179
180 /*
181 ostringstream str;
182 str << "lim::max() = " << lim::max() << ", val = " << val << ", infinity = " << infinity;
183 CPPUNIT_MESSAGE( str.str().c_str() );
184 str.str(string());
185 str << "sizeof(_Tp) = " << sizeof(_Tp);
186 CPPUNIT_MESSAGE( str.str().c_str() );
187 if (sizeof(_Tp) == 4) {
188 str.str(string());
189 str << "val in hexa: " << showbase << hex << *((const unsigned int*)&val);
190 str << ", infinity in hexa: " << showbase << hex << *((const unsigned int*)&infinity);
191 }
192#if defined (_STLP_LONG_LONG)
193 else if (sizeof(_Tp) == sizeof(_STLP_LONG_LONG)) {
194 str.str(string());
195 str << "val in hexa: " << showbase << hex << *((const unsigned _STLP_LONG_LONG*)&val);
196 str << ", infinity in hexa: " << showbase << hex << *((const unsigned _STLP_LONG_LONG*)&infinity);
197 }
198#endif
199 else {
200 str.str(string());
201 str << "val: ";
202 for (int i = 0; i != sizeof(_Tp) / sizeof(unsigned short); ++i) {
203 if (i != 0) str << ' ';
204 str << showbase << hex << setw(4) << setfill('0') << *((const unsigned short*)&val + i);
205 }
206 str << ", infinity: ";
207 for (int i = 0; i != sizeof(_Tp) / sizeof(unsigned short); ++i) {
208 if (i != 0) str << ' ';
209 str << showbase << hex << setw(4) << setfill('0') << *((const unsigned short*)&infinity + i);
210 }
211 }
212 CPPUNIT_MESSAGE( str.str().c_str() );
213 str.str(string());
214 str << dec;
215 str << "lim::digits = " << lim::digits << ", lim::digits10 = " << lim::digits10 << endl;
216 str << "lim::min_exponent = " << lim::min_exponent << ", lim::min_exponent10 = " << lim::min_exponent10 << endl;
217 str << "lim::max_exponent = " << lim::max_exponent << ", lim::max_exponent10 = " << lim::max_exponent10 << endl;
218 CPPUNIT_MESSAGE( str.str().c_str() );
219 */
220
221 CHECK_COND(infinity == infinity);
222 CHECK_COND(infinity > lim::max());
223 CHECK_COND(-infinity < -lim::max());
224 }
225
226 return true;
227}
@ denorm_present
Definition: _limits.h:52
@ denorm_absent
Definition: _limits.h:51
#define CPPUNIT_MESSAGE(m)
Definition: cppunit_mini.h:223
GLuint GLfloat * val
Definition: glext.h:7180
bool test_float_values(_Tp lhs, _Tp rhs)
#define CHECK_COND(X)
Definition: limits_test.cpp:57

Referenced by LimitTest::test().

◆ test_float_values()

template<class _Tp >
bool test_float_values ( _Tp  lhs,
_Tp  rhs 
)

Definition at line 120 of file limits_test.cpp.

121{ return lhs == rhs; }

Referenced by test_float_limits().

◆ test_integral_limits()

template<class _Tp >
bool test_integral_limits ( const _Tp &  val,
bool  unknown_sign = true,
bool  is_signed = true 
)

Definition at line 88 of file limits_test.cpp.

88 {
89 if (!test_integral_limits_base(val, unknown_sign, is_signed))
90 return false;
91
92 typedef numeric_limits<_Tp> lim;
93
94 CHECK_COND(lim::is_modulo);
95
96 if (lim::is_bounded ||
97 (!lim::is_bounded && !lim::is_signed)) {
98 _Tp tmp = lim::min();
99 CHECK_COND( --tmp > lim::min() );
100 }
101
102 if (lim::is_bounded) {
103 _Tp tmp = lim::max();
104 CHECK_COND( ++tmp < lim::max() );
105 }
106
107 return true;
108}
bool test_integral_limits_base(const _Tp &, bool unknown_sign=true, bool is_signed=true)
Definition: limits_test.cpp:70

Referenced by LimitTest::test(), test_signed_integral_limits(), and test_unsigned_integral_limits().

◆ test_integral_limits_base()

template<class _Tp >
bool test_integral_limits_base ( const _Tp &  ,
bool  unknown_sign = true,
bool  is_signed = true 
)

Definition at line 70 of file limits_test.cpp.

70 {
71 typedef numeric_limits<_Tp> lim;
72
73 CHECK_COND(lim::is_specialized);
74 CHECK_COND(lim::is_exact);
75 CHECK_COND(lim::is_integer);
76 CHECK_COND(!lim::is_iec559);
77 CHECK_COND(lim::min() < lim::max());
78 CHECK_COND((unknown_sign && ((lim::is_signed && (lim::min() != 0)) || (!lim::is_signed && (lim::min() == 0)))) ||
79 (!unknown_sign && ((lim::is_signed && is_signed) || (!lim::is_signed && !is_signed))));
80
81 if (unknown_sign) {
82 CHECK_COND(valid_sign_info(lim::is_signed, _Tp()));
83 }
84 return true;
85}
bool valid_sign_info(bool, bool)
Definition: limits_test.cpp:60

Referenced by LimitTest::test(), and test_integral_limits().

◆ test_qnan()

template<class _Tp >
bool test_qnan ( const _Tp &  )

Definition at line 233 of file limits_test.cpp.

233 {
234 typedef numeric_limits<_Tp> lim;
235 if (lim::has_quiet_NaN) {
236 const _Tp qnan = lim::quiet_NaN();
237
238 //if (sizeof(_Tp) == 4) {
239 // ostringstream str;
240 // str << "qnan " << qnan << ", in hexa: " << showbase << hex << *((unsigned int*)&qnan);
241 // CPPUNIT_MESSAGE( str.str().c_str() );
242 // str.str("");
243 // float val = generate_nan(0.0f);
244 // str << "val " << val << ", in hexa: " << showbase << hex << *((unsigned int*)&val);
245 // CPPUNIT_MESSAGE( str.str().c_str() );
246 // str.str("");
247 // val = -qnan;
248 // str << "-qnan " << val << ", in hexa: " << showbase << hex << *((unsigned int*)&val);
249 // CPPUNIT_MESSAGE( str.str().c_str() );
250 //}
251 /* NaNs shall always compare "false" when compared for equality
252 * If one of these fail, your compiler may be optimizing incorrectly,
253 * or the STLport is incorrectly configured.
254 */
255 CHECK_COND(! (qnan == 42));
256 CHECK_COND(! (qnan == qnan));
257 CHECK_COND(qnan != 42);
258 CHECK_COND(qnan != qnan);
259
260 /* The following tests may cause arithmetic traps.
261 * CHECK_COND(! (qnan < 42));
262 * CHECK_COND(! (qnan > 42));
263 * CHECK_COND(! (qnan <= 42));
264 * CHECK_COND(! (qnan >= 42));
265 */
266 }
267 return true;
268}

Referenced by LimitTest::qnan_test().

◆ test_signed_integral_limits()

template<class _Tp >
bool test_signed_integral_limits ( const _Tp &  __val)

Definition at line 111 of file limits_test.cpp.

111 {
112 return test_integral_limits(__val, false, true);
113}
_STLP_INLINE_LOOP _InputIter const _Tp & __val
Definition: _algobase.h:656
bool test_integral_limits(const _Tp &val, bool unknown_sign=true, bool is_signed=true)
Definition: limits_test.cpp:88

Referenced by LimitTest::test().

◆ test_unsigned_integral_limits()

template<class _Tp >
bool test_unsigned_integral_limits ( const _Tp &  __val)

Definition at line 115 of file limits_test.cpp.

115 {
116 return test_integral_limits(__val, false, false);
117}

Referenced by LimitTest::test().

◆ valid_sign_info() [1/2]

template<class _Tp >
bool valid_sign_info ( bool  limit_is_signed,
const _Tp &   
)

Definition at line 64 of file limits_test.cpp.

64 {
65 return (limit_is_signed && _Tp(-1) < 0) ||
66 (!limit_is_signed && _Tp(-1) > 0);
67}

◆ valid_sign_info() [2/2]

bool valid_sign_info ( bool  ,
bool   
)

Definition at line 60 of file limits_test.cpp.

61{ return true; }

Referenced by test_integral_limits_base().