cmath_test.cpp revision e46c9386c4f79aa40185f79a19fc5b2a7ef528b3
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//
19class CMathTest : public CPPUNIT_NS::TestCase
20{
21  CPPUNIT_TEST_SUITE(CMathTest);
22#if defined (STLPORT) && !defined (_STLP_USE_NAMESPACES)
23  CPPUNIT_IGNORE;
24#endif
25  CPPUNIT_TEST(import_checks);
26  CPPUNIT_TEST_SUITE_END();
27
28  protected:
29    void import_checks();
30};
31
32CPPUNIT_TEST_SUITE_REGISTRATION(CMathTest);
33
34//
35// tests implementation
36//
37void CMathTest::import_checks()
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) );
92  CPPUNIT_CHECK( are_equals(std::log(std::exp(1.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) );
101  CPPUNIT_CHECK( are_equals(std::cos(std::acos(1.0)), 1.0) );
102  CPPUNIT_CHECK( are_equals(std::sin(std::asin(1.0)), 1.0) );
103  CPPUNIT_CHECK( are_equals(std::tan(std::atan(1.0)), 1.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) );
117  CPPUNIT_CHECK( are_equals(std::log(std::exp(1.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) );
126  CPPUNIT_CHECK( are_equals(std::cos(std::acos(1.0f)), 1.0f) );
127  CPPUNIT_CHECK( are_equals(std::sin(std::asin(1.0f)), 1.0f) );
128  CPPUNIT_CHECK( are_equals(std::tan(std::atan(1.0f)), 1.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) );
143  CPPUNIT_CHECK( are_equals(std::log(std::exp(1.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) );
152  CPPUNIT_CHECK( are_equals(std::cos(std::acos(1.0l)), 1.0l) );
153  CPPUNIT_CHECK( are_equals(std::sin(std::asin(1.0l)), 1.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
164  CPPUNIT_CHECK( are_equals(std::sqrt(std::sqrt(std::sqrt(256.0))), 2.0) );
165  CPPUNIT_CHECK( are_equals(std::sqrt(std::sqrt(std::sqrt(256.0f))), 2.0f) );
166#  if !defined (_STLP_NO_LONG_DOUBLE)
167  CPPUNIT_CHECK( are_equals(std::sqrt(std::sqrt(std::sqrt(256.0l))), 2.0l) );
168#  endif
169#endif
170}
171