]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Distributed under the Boost Software License, Version 1.0. |
2 | // (See accompanying file LICENSE_1_0.txt | |
3 | // or copy at http://www.boost.org/LICENSE_1_0.txt) | |
4 | ||
5 | // Copyright (c) 2006 Johan Rade | |
6 | // Copyright (c) 2011 Paul A. Bristow | |
7 | ||
8 | /*! | |
9 | \file | |
10 | \brief Basic tests of nonfinite loopback. | |
11 | ||
12 | \detail Basic loopback test outputs using nonfinite facets | |
13 | (output and input) and reads back in, and checks if loopback OK. | |
14 | ||
15 | Expected to work portably on all platforms. | |
16 | ||
17 | */ | |
18 | ||
19 | #ifdef _MSC_VER | |
20 | # pragma warning(disable : 4702) | |
21 | # pragma warning(disable : 4127) // conditional expression is constant. | |
22 | #endif | |
23 | ||
24 | #include <boost/math/special_functions/nonfinite_num_facets.hpp> | |
25 | using boost::math::nonfinite_num_get; | |
26 | using boost::math::nonfinite_num_put; | |
27 | ||
28 | #include <iostream> | |
29 | using std::cout; | |
30 | using std::endl; | |
31 | ||
32 | #include <locale> | |
33 | using std::locale; | |
34 | ||
35 | #include <sstream> | |
36 | using std::stringstream; | |
37 | #include <limits> | |
38 | using std::numeric_limits; | |
39 | ||
40 | #include <assert.h> | |
41 | ||
42 | int main() | |
43 | { | |
44 | ||
45 | if((std::numeric_limits<double>::has_infinity == false) || (std::numeric_limits<double>::infinity() == 0)) | |
46 | { | |
47 | std::cout << "Infinity not supported on this platform." << std::endl; | |
48 | return 0; | |
49 | } | |
50 | ||
51 | if((std::numeric_limits<double>::has_quiet_NaN == false) || (std::numeric_limits<double>::quiet_NaN() == 0)) | |
52 | { | |
53 | std::cout << "NaN not supported on this platform." << std::endl; | |
54 | return 0; | |
55 | } | |
56 | //locale old_locale; // Current global locale. | |
92f5a8d4 | 57 | // Create tmp_locale and store the output nonfinite_num_put facet in it. |
7c673cae | 58 | //locale tmp_locale(old_locale, new nonfinite_num_put<char>); |
92f5a8d4 | 59 | // Create new_locale and store the input nonfinite_num_get facet in it. |
7c673cae | 60 | //locale new_locale(tmp_locale, new nonfinite_num_get<char>); |
92f5a8d4 TL |
61 | // Can only add one facet at a time, hence need a tmp_locale, |
62 | // unless we write: | |
7c673cae FG |
63 | |
64 | std::locale new_locale(std::locale(std::locale(std::locale(), | |
65 | new boost::math::nonfinite_num_put<char>), | |
66 | new boost::math::nonfinite_num_get<char>)); | |
92f5a8d4 | 67 | |
7c673cae | 68 | stringstream ss; // Both input and output, so need both get and put facets. |
92f5a8d4 | 69 | |
7c673cae FG |
70 | ss.imbue(new_locale); |
71 | ||
72 | double inf = numeric_limits<double>::infinity(); | |
73 | ss << inf; // Write out. | |
74 | double r; | |
75 | ss >> r; // Read back in. | |
76 | ||
92f5a8d4 | 77 | BOOST_ASSERT(inf == r); // OK MSVC <= 10.0! |
7c673cae FG |
78 | |
79 | } // int main() | |
80 | ||
81 | /* | |
82 | ||
83 | Output: | |
84 | ||
85 | nonfinite_loopback_ok.vcxproj -> J:\Cpp\fp_facet\fp_facet\Debug\nonfinite_loopback_ok.exe | |
86 | ||
87 | */ | |
88 | ||
89 |