File: test_lexical_cast.cpp

package info (click to toggle)
scipy 1.16.0-1exp7
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 234,820 kB
  • sloc: cpp: 503,145; python: 344,611; ansic: 195,638; javascript: 89,566; fortran: 56,210; cs: 3,081; f90: 1,150; sh: 848; makefile: 785; pascal: 284; csh: 135; lisp: 134; xml: 56; perl: 51
file content (122 lines) | stat: -rw-r--r-- 4,798 bytes parent folder | download | duplicates (14)
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

// Copyright (c) 2006 Johan Rade

// Copyright (c) 2011 Paul A. Bristow incorporated Boost.Math

// Distributed under 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)

//#ifdef _MSC_VER
//#   pragma warning(disable : 4127 4511 4512 4701 4702)
//#endif

#define BOOST_TEST_MAIN

#include <limits>
#include <locale>
#include <string>
#include <boost/lexical_cast.hpp>
#include <boost/test/unit_test.hpp>

#include <boost/math/special_functions/nonfinite_num_facets.hpp>
#include <boost/math/special_functions/sign.hpp>
#include <boost/math/special_functions/fpclassify.hpp>
#include "almost_equal.ipp"
#include "s_.ipp"

namespace {

// the anonymous namespace resolves ambiguities on platforms
// with fpclassify etc functions at global scope

using boost::lexical_cast;

using namespace boost::math;
using boost::math::signbit;
using boost::math::changesign;
using boost::math::isnan;

//------------------------------------------------------------------------------

template<class CharType, class ValType> void lexical_cast_test_impl();

BOOST_AUTO_TEST_CASE(lexical_cast_test)
{
    lexical_cast_test_impl<char, float>();
    lexical_cast_test_impl<char, double>();
    lexical_cast_test_impl<char, long double>();
    lexical_cast_test_impl<wchar_t, float>();
    lexical_cast_test_impl<wchar_t, double>();
    lexical_cast_test_impl<wchar_t, long double>();
}

template<class CharType, class ValType> void lexical_cast_test_impl()
{
    if((std::numeric_limits<ValType>::has_infinity == 0) || (std::numeric_limits<ValType>::infinity() == 0))
       return;
    if((std::numeric_limits<ValType>::has_quiet_NaN == 0) || (std::numeric_limits<ValType>::quiet_NaN() == 0))
       return;

    std::locale old_locale;
    std::locale tmp_locale(old_locale,
        new nonfinite_num_put<CharType>(signed_zero));
    std::locale new_locale(tmp_locale, new nonfinite_num_get<CharType>);
    std::locale::global(new_locale);

    ValType a1 = static_cast<ValType>(0);
    ValType a2 = static_cast<ValType>(13);
    ValType a3 = std::numeric_limits<ValType>::infinity();
    ValType a4 = std::numeric_limits<ValType>::quiet_NaN();
    ValType a5 = std::numeric_limits<ValType>::signaling_NaN();
    ValType a6 = (changesign)(static_cast<ValType>(0));
    ValType a7 = static_cast<ValType>(-57);
    ValType a8 = -std::numeric_limits<ValType>::infinity();
    ValType a9 = (changesign)(std::numeric_limits<ValType>::quiet_NaN()); // -NaN
    ValType a10 = (changesign)(std::numeric_limits<ValType>::signaling_NaN()); // -NaN

    std::basic_string<CharType> s1 = S_("0");
    std::basic_string<CharType> s2 = S_("13");
    std::basic_string<CharType> s3 = S_("inf");
    std::basic_string<CharType> s4 = S_("nan");
    std::basic_string<CharType> s5 = S_("nan");
    std::basic_string<CharType> s6 = S_("-0");
    std::basic_string<CharType> s7 = S_("-57");
    std::basic_string<CharType> s8 = S_("-inf");
    std::basic_string<CharType> s9 = S_("-nan");
    std::basic_string<CharType> s10 = S_("-nan");

    BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a1) == s1);
    BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a2) == s2);
    BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a3) == s3);
    BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a4) == s4);
    BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a5) == s5);
    BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a6) == s6);
    BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a7) == s7);
    BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a8) == s8);
    BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a9) == s9);
    BOOST_CHECK(lexical_cast<std::basic_string<CharType> >(a10) == s10);

    BOOST_CHECK(lexical_cast<ValType>(s1) == a1);
    BOOST_CHECK(!(signbit)(lexical_cast<ValType>(s1)));
    BOOST_CHECK(lexical_cast<ValType>(s2) == a2);
    BOOST_CHECK(lexical_cast<ValType>(s3) == a3);
    BOOST_CHECK((isnan)(lexical_cast<ValType>(s4)));
    BOOST_CHECK(!(signbit)(lexical_cast<ValType>(s4)));
    BOOST_CHECK((isnan)(lexical_cast<ValType>(s5)));
    BOOST_CHECK(!(signbit)(lexical_cast<ValType>(s5)));
    BOOST_CHECK(lexical_cast<ValType>(a6) == a6);
    BOOST_CHECK((signbit)(lexical_cast<ValType>(s6)));
    BOOST_CHECK(lexical_cast<ValType>(s7) == a7);
    BOOST_CHECK(lexical_cast<ValType>(s8) == a8);
    BOOST_CHECK((isnan)(lexical_cast<ValType>(s9)));
    BOOST_CHECK((signbit)(lexical_cast<ValType>(s9)));
    BOOST_CHECK((isnan)(lexical_cast<ValType>(s10)));
    BOOST_CHECK((signbit)(lexical_cast<ValType>(s10)));

    std::locale::global(old_locale);
}

//------------------------------------------------------------------------------

}   // anonymous namespace