ReactOS  r76032
cmath_test.cpp
Go to the documentation of this file.
1 #define _STLP_DO_IMPORT_CSTD_FUNCTIONS
2 
3 #include <limits>
4 #include <cmath>
5 //We also test math functions imported from stdlib.h or
6 //defined in cstdlib
7 #include <cstdlib>
8 
9 #include "math_aux.h"
10 #include "cppunit/cppunit_proxy.h"
11 
12 //This test purpose is to check the right import of math.h C symbols
13 //into the std namespace so we do not use the using namespace std
14 //specification
15 
16 //
17 // TestCase class
18 //
19 class CMathTest : public CPPUNIT_NS::TestCase
20 {
22 #if defined (STLPORT) && !defined (_STLP_USE_NAMESPACES)
24 #endif
27 
28  protected:
29  void import_checks();
30 };
31 
33 
34 //
35 // tests implementation
36 //
38 {
39 #if !defined (STLPORT) || defined (_STLP_USE_NAMESPACES)
40  int int_val = -1;
41  long long_val = -1l;
42  float float_val = -1.0f;
43  double double_val = -1.0;
44 # if !defined (_STLP_NO_LONG_DOUBLE)
45  long double long_double_val = -1.0l;
46 # endif
47 
48  CPPUNIT_CHECK( are_equals(std::abs(int_val), -int_val) );
49  CPPUNIT_CHECK( are_equals(std::abs(long_val), -long_val) );
50  CPPUNIT_CHECK( are_equals(std::labs(long_val), -long_val) );
51  CPPUNIT_CHECK( are_equals(std::abs(float_val), -float_val) );
52  CPPUNIT_CHECK( are_equals(std::abs(double_val), -double_val) );
53 # if !defined (_STLP_NO_LONG_DOUBLE)
54  CPPUNIT_CHECK( are_equals(std::abs(long_double_val), -long_double_val) );
55 # endif
56 
57  CPPUNIT_CHECK( are_equals(std::fabs(float_val), -float_val) );
58  CPPUNIT_CHECK( are_equals(std::fabs(double_val), -double_val) );
59 # if !defined (_STLP_NO_LONG_DOUBLE)
60  CPPUNIT_CHECK( are_equals(std::fabs(long_double_val), -long_double_val) );
61 # endif
62 
63  std::div_t div_res = std::div(3, 2);
64  CPPUNIT_CHECK( div_res.quot == 1 );
65  CPPUNIT_CHECK( div_res.rem == 1 );
66  std::ldiv_t ldiv_res = std::ldiv(3l, 2l);
67  CPPUNIT_CHECK( ldiv_res.quot == 1l );
68  CPPUNIT_CHECK( ldiv_res.rem == 1l );
69  ldiv_res = std::div(3l, 2l);
70  CPPUNIT_CHECK( ldiv_res.quot == 1l );
71  CPPUNIT_CHECK( ldiv_res.rem == 1l );
72 
73  std::srand(2);
74  int rand_val = std::rand();
75  CPPUNIT_CHECK( rand_val >= 0 && rand_val <= RAND_MAX );
76 
77  CPPUNIT_CHECK( are_equals(std::floor(1.5), 1.0) );
78  CPPUNIT_CHECK( are_equals(std::ceil(1.5), 2.0) );
79  CPPUNIT_CHECK( are_equals(std::fmod(1.5, 1.0), 0.5) );
80  CPPUNIT_CHECK( are_equals(std::sqrt(4.0), 2.0) );
81  CPPUNIT_CHECK( are_equals(std::pow(2.0, 2), 4.0) );
82  /*
83  * Uncomment the following to check that it generates an ambiguous call
84  * as there is no Standard pow(int, int) function only pow(double, int),
85  * pow(float, int) and some others...
86  * If it do not generate a compile time error it should at least give
87  * the good result.
88  */
89  //CPPUNIT_CHECK( are_equals(std::pow(10, -2), 0.01) );
90  CPPUNIT_CHECK( are_equals(std::pow(10.0, -2), 0.01) );
91  CPPUNIT_CHECK( are_equals(std::exp(0.0), 1.0) );
93  CPPUNIT_CHECK( are_equals(std::log10(100.0), 2.0) );
94 # if !defined (STLPORT) || !defined (_STLP_USING_PLATFORM_SDK_COMPILER) || !defined (_WIN64)
95  CPPUNIT_CHECK( are_equals(std::modf(100.5, &double_val), 0.5) );
96  CPPUNIT_CHECK( are_equals(double_val, 100.0) );
97 # endif
98  double_val = std::frexp(8.0, &int_val);
99  CPPUNIT_CHECK( are_equals(double_val * std::pow(2.0, int_val), 8.0) );
100  CPPUNIT_CHECK( are_equals(std::ldexp(1.0, 2), 4.0) );
104  CPPUNIT_CHECK( are_equals(std::tan(std::atan2(1.0, 1.0)), 1.0) );
105  CPPUNIT_CHECK( are_equals(std::cosh(0.0), 1.0) );
106  CPPUNIT_CHECK( are_equals(std::sinh(0.0), 0.0) );
107 # if !defined (STLPORT) || !defined (_STLP_USING_PLATFORM_SDK_COMPILER) || !defined (_M_AMD64)
108  CPPUNIT_CHECK( are_equals(std::tanh(0.0), 0.0) );
109 # endif
110 
111  CPPUNIT_CHECK( are_equals(std::floor(1.5f), 1.0f) );
112  CPPUNIT_CHECK( are_equals(std::ceil(1.5f), 2.0f) );
113  CPPUNIT_CHECK( are_equals(std::fmod(1.5f, 1.0f), 0.5f) );
114  CPPUNIT_CHECK( are_equals(std::sqrt(4.0f), 2.0f) );
115  CPPUNIT_CHECK( are_equals(std::pow(2.0f, 2), 4.0f) );
116  CPPUNIT_CHECK( are_equals(std::exp(0.0f), 1.0f) );
118  CPPUNIT_CHECK( are_equals(std::log10(100.0f), 2.0f) );
119 # if !defined (STLPORT) || !defined (_STLP_USING_PLATFORM_SDK_COMPILER) || !defined (_WIN64)
120  CPPUNIT_CHECK( are_equals(std::modf(100.5f, &float_val), 0.5f) );
121  CPPUNIT_CHECK( are_equals(float_val, 100.0f) );
122 # endif
123  float_val = std::frexp(8.0f, &int_val);
124  CPPUNIT_CHECK( are_equals(float_val * std::pow(2.0f, int_val), 8.0f) );
125  CPPUNIT_CHECK( are_equals(std::ldexp(1.0f, 2), 4.0f) );
129  CPPUNIT_CHECK( are_equals(std::tan(std::atan2(1.0f, 1.0f)), 1.0f) );
130  CPPUNIT_CHECK( are_equals(std::cosh(0.0f), 1.0f) );
131  CPPUNIT_CHECK( are_equals(std::sinh(0.0f), 0.0f) );
132 # if !defined (STLPORT) || !defined (_STLP_USING_PLATFORM_SDK_COMPILER) || !defined (_M_AMD64)
133  CPPUNIT_CHECK( are_equals(std::tanh(0.0f), 0.0f) );
134 # endif
135 
136 # if !defined (_STLP_NO_LONG_DOUBLE)
137  CPPUNIT_CHECK( are_equals(std::floor(1.5l), 1.0l) );
138  CPPUNIT_CHECK( are_equals(std::ceil(1.5l), 2.0l) );
139  CPPUNIT_CHECK( are_equals(std::fmod(1.5l, 1.0l), 0.5l) );
140  CPPUNIT_CHECK( are_equals(std::sqrt(4.0l), 2.0l) );
141  CPPUNIT_CHECK( are_equals(std::pow(2.0l, 2), 4.0l) );
142  CPPUNIT_CHECK( are_equals(std::exp(0.0l), 1.0l) );
144  CPPUNIT_CHECK( are_equals(std::log10(100.0l), 2.0l) );
145 # if !defined (STLPORT) || !defined (_STLP_USING_PLATFORM_SDK_COMPILER) || !defined (_WIN64)
146  CPPUNIT_CHECK( are_equals(std::modf(100.5l, &long_double_val), 0.5l) );
147  CPPUNIT_CHECK( are_equals(long_double_val, 100.0l) );
148 # endif
149  long_double_val = std::frexp(8.0l, &int_val);
150  CPPUNIT_CHECK( are_equals(long_double_val * std::pow(2.0l, int_val), 8.0l) );
151  CPPUNIT_CHECK( are_equals(std::ldexp(1.0l, 2), 4.0l) );
154  CPPUNIT_CHECK( are_equals(std::tan(0.0l), 0.0l) );
155  CPPUNIT_CHECK( are_equals(std::atan(0.0l), 0.0l) );
156  CPPUNIT_CHECK( are_equals(std::atan2(0.0l, 1.0l), 0.0l) );
157  CPPUNIT_CHECK( are_equals(std::cosh(0.0l), 1.0l) );
158  CPPUNIT_CHECK( are_equals(std::sinh(0.0l), 0.0l) );
159 # if !defined (STLPORT) || !defined (_STLP_USING_PLATFORM_SDK_COMPILER) || !defined (_M_AMD64)
160  CPPUNIT_CHECK( are_equals(std::tanh(0.0l), 0.0l) );
161 # endif
162 # endif
163 
166 # if !defined (_STLP_NO_LONG_DOUBLE)
168 # endif
169 #endif
170 }
_STLP_DECLSPEC complex< float > _STLP_CALL sqrt(const complex< float > &)
Definition: complex.cpp:188
_STLP_DECLSPEC complex< float > _STLP_CALL sinh(const complex< float > &)
_Check_return_ _CRTIMP double __cdecl ceil(_In_ double x)
#define abs(i)
Definition: fconv.c:206
valarray< _Tp > atan2(const valarray< _Tp > &__x, const valarray< _Tp > &__y)
Definition: _valarray.h:928
_STLP_DECLSPEC complex< float > _STLP_CALL cosh(const complex< float > &)
_Check_return_ _CRTIMP double __cdecl ldexp(_In_ double x, _In_ int y)
void __cdecl srand(_In_ unsigned int _Seed)
bool are_equals(_Tp val, _Tp ref)
Definition: math_aux.h:19
valarray< _Tp > asin(const valarray< _Tp > &__x)
Definition: _valarray.h:910
valarray< _Tp > acos(const valarray< _Tp > &__x)
Definition: _valarray.h:901
_STLP_DECLSPEC complex< float > _STLP_CALL tanh(const complex< float > &)
float pow(float __x, int __y)
Definition: _cmath.h:458
_Check_return_ int __cdecl rand(void)
Definition: rand.c:10
_STLP_DECLSPEC complex< float > _STLP_CALL cos(const complex< float > &)
r l[0]
Definition: byte_order.h:167
void import_checks()
Definition: cmath_test.cpp:37
GLfloat f
Definition: glext.h:7540
_Check_return_ double __cdecl fmod(_In_ double x, _In_ double y)
#define CPPUNIT_CHECK(X)
Definition: cppunit_mini.h:195
CPPUNIT_TEST(import_checks)
_Check_return_ ldiv_t __cdecl ldiv(_In_ long _Numerator, _In_ long _Denominator)
_STLP_VENDOR_CSTD::ldiv_t div(long __x, long __y)
Definition: _cstdlib.h:137
_STLP_DECLSPEC complex< float > _STLP_CALL log10(const complex< float > &)
Definition: complex.cpp:230
CPPUNIT_TEST_SUITE(CMathTest)
CPPUNIT_TEST_SUITE_REGISTRATION(CMathTest)
valarray< _Tp > atan(const valarray< _Tp > &__x)
Definition: _valarray.h:919
CPPUNIT_TEST_SUITE_END()
struct _div_t div_t
#define RAND_MAX
Definition: stdlib.h:99
_Check_return_ _CRT_JIT_INTRINSIC double __cdecl fabs(_In_ double x)
_Check_return_ _CRTIMP double __cdecl frexp(_In_ double x, _Out_ int *y)
DWORD exp
Definition: msg.c:15520
struct _ldiv_t ldiv_t
_Check_return_ long __cdecl labs(_In_ long x)
_STLP_DECLSPEC complex< float > _STLP_CALL sin(const complex< float > &)
#define CPPUNIT_IGNORE
Definition: cppunit_mini.h:185
_Check_return_ _CRTIMP double __cdecl floor(_In_ double x)
_STLP_DECLSPEC complex< float > _STLP_CALL tan(const complex< float > &)
#define log(outFile, fmt,...)
Definition: util.h:15
_Check_return_ _CRTIMP double __cdecl modf(_In_ double x, _Out_ double *y)