summaryrefslogtreecommitdiff
path: root/libs/math/test/test_cstdfloat.cpp
blob: 2ce3c5fdc7966dacd90ea3efbfa5d539f7dcc047 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//  Copyright Christopher Kormanyos 2014.
//  Copyright John Maddock 2014.
//  Copyright Paul A. Bristow 2014.

//  Use, modification and distribution are subject to the
//  Boost Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <cmath>
#include <complex>
#include <limits>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <boost/math/cstdfloat/cstdfloat_types.hpp>

#ifdef _MSC_VER
#  pragma warning(disable : 4127) // conditional expression is constant.
#  pragma warning(disable : 4512) // assignment operator could not be generated.
#  pragma warning(disable : 4996) // use -D_SCL_SECURE_NO_WARNINGS.
#endif

#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp> // Boost.Test
#include <boost/test/floating_point_comparison.hpp>

//
// DESCRIPTION:
// ~~~~~~~~~~~~
//
// This file tests the implementation of floating-point typedefs having
// specified widths, as implemented in <boost/cstdfloat.hpp> and described
// in N3626 (proposed for C++14).

// For more information on <boost/cstdfloat.hpp> and the corresponding
// proposal of "Floating-Point Typedefs Having Specified Widths",
// see: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3626.pdf

// The tests:
//
// Perform sanity checks on boost::float16_t, boost::float32_t,
// boost::float64_t, boost::float80_t, and boost::float128_t when
// these types are available. In the sanity checks, we verify the
// formal behavior of the types and the macros for creating literal
// floating-point constants.
//
// An extended check is included for boost::float128_t. This checks
// the functionality of <cmath>, I/O stream operations, and <complex>
// functions for boost::float128_t.

#define TEST_CSTDFLOAT_SANITY_CHECK(the_digits)                                                      \
void sanity_check_##the_digits##_func()                                                              \
{                                                                                                    \
  typedef boost::float##the_digits##_t float_type;                                                   \
                                                                                                     \
  BOOST_CONSTEXPR_OR_CONST int my_digits10 = std::numeric_limits<float_type>::digits10;              \
                                                                                                     \
  {                                                                                                  \
    BOOST_CONSTEXPR_OR_CONST float_type x =                                                          \
      BOOST_FLOAT##the_digits##_C(0.33333333333333333333333333333333333333333);                      \
    std::stringstream ss;                                                                            \
    ss << std::setprecision(my_digits10 - 1)                                                         \
       << x;                                                                                         \
    std::string str = "0.";                                                                          \
    str += std::string(std::string::size_type(my_digits10 - 1), char('3'));                          \
    BOOST_CHECK_EQUAL( ss.str(), str );                                                              \
  }                                                                                                  \
  {                                                                                                  \
    BOOST_CONSTEXPR_OR_CONST float_type x =                                                          \
      BOOST_FLOAT##the_digits##_C(0.66666666666666666666666666666666666666666);                      \
    std::stringstream ss;                                                                            \
    ss << std::setprecision(my_digits10 - 1)                                                         \
       << x;                                                                                         \
    std::string str = "0.";                                                                          \
    str += std::string(std::string::size_type(my_digits10 - 2), char('6'));                          \
    str += "7";                                                                                      \
    BOOST_CHECK_EQUAL( ss.str(), str );                                                              \
  }                                                                                                  \
  {                                                                                                  \
    const float_type x = BOOST_FLOAT##the_digits##_C(1.0) / test_cstdfloat::zero;                    \
    const bool the_inf_test = (   std::numeric_limits<float_type>::has_infinity                      \
                               && (x == std::numeric_limits<float_type>::infinity()));               \
    BOOST_CHECK_EQUAL( the_inf_test, true );                                                         \
  }                                                                                                  \
  {                                                                                                  \
    using std::sqrt;                                                                                 \
    const float_type x = sqrt(float_type(test_cstdfloat::minus_one));                                \
    const bool the_nan_test = (   std::numeric_limits<float_type>::has_quiet_NaN                     \
                               && (x != x));                                                         \
    BOOST_CHECK_EQUAL( the_nan_test, true );                                                         \
  }                                                                                                  \
  {                                                                                                  \
    const bool the_lim_test =                                                                        \
      (std::numeric_limits<boost::floatmax_t>::digits >= std::numeric_limits<float_type>::digits);   \
    BOOST_CHECK_EQUAL( the_lim_test, true );                                                         \
  }                                                                                                  \
}

namespace test_cstdfloat
{
  int zero;
  int minus_one;

  #if defined(BOOST_FLOATMAX_C)
  BOOST_CONSTEXPR_OR_CONST int has_floatmax_t = 1;
  #else
  BOOST_CONSTEXPR_OR_CONST int has_floatmax_t = 0;
  #endif

  #if defined(BOOST_FLOAT16_C)
  TEST_CSTDFLOAT_SANITY_CHECK(16)
  #endif

  #if defined(BOOST_FLOAT32_C)
  TEST_CSTDFLOAT_SANITY_CHECK(32)
  #endif

  #if defined(BOOST_FLOAT64_C)
  TEST_CSTDFLOAT_SANITY_CHECK(64)
  #endif

  #if defined(BOOST_FLOAT80_C)
  TEST_CSTDFLOAT_SANITY_CHECK(80)
  #endif

  #if defined(BOOST_FLOAT128_C)
  TEST_CSTDFLOAT_SANITY_CHECK(128)

  void extend_check_128_func()
  {
  }
  #endif // defined (BOOST_FLOAT128_C)
}

BOOST_AUTO_TEST_CASE(test_main)
{
  test_cstdfloat::zero      =  0;
  test_cstdfloat::minus_one = -1;

  // Perform basic sanity checks that verify both the existence of the proper
  // floating-point literal macros as well as the correct digit handling
  // for a given floating-point typedef having specified width.

  BOOST_CHECK_EQUAL( test_cstdfloat::has_floatmax_t, 1 );

  #if defined(BOOST_FLOAT16_C)
  test_cstdfloat::sanity_check_16_func();
  #endif

  #if defined(BOOST_FLOAT32_C)
  test_cstdfloat::sanity_check_32_func();
  #endif

  #if defined(BOOST_FLOAT64_C)
  test_cstdfloat::sanity_check_64_func();
  #endif

  #if defined(BOOST_FLOAT80_C)
  test_cstdfloat::sanity_check_80_func();
  #endif

  #if defined(BOOST_FLOAT128_C)
  test_cstdfloat::sanity_check_128_func();

  // Perform an extended check of boost::float128_t including
  // a variety of functions from the C++ standard library.
  test_cstdfloat::extend_check_128_func();
  #endif // defined (BOOST_FLOAT128_C)
}