]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/multiprecision/test/test_float_serial.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / multiprecision / test / test_float_serial.hpp
CommitLineData
7c673cae
FG
1///////////////////////////////////////////////////////////////
2// Copyright 2013 John Maddock. Distributed under the Boost
3// Software License, Version 1.0. (See accompanying file
4// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_
5
6#include <boost/random/mersenne_twister.hpp>
7#include <boost/random/uniform_int.hpp>
8#include <boost/timer.hpp>
9#include "test.hpp"
10
11#include <iostream>
12#include <iomanip>
13#include <sstream>
14#include <boost/archive/text_iarchive.hpp>
15#include <boost/archive/text_oarchive.hpp>
16#include <boost/archive/binary_iarchive.hpp>
17#include <boost/archive/binary_oarchive.hpp>
18#include <boost/exception/all.hpp>
19
20
21#ifndef BOOST_MP_TEST_FLOAT_SERIAL_HPP
22#define BOOST_MP_TEST_FLOAT_SERIAL_HPP
23
24template <class T>
25T generate_random(unsigned /*bits_wanted*/)
26{
27 typedef typename T::backend_type::exponent_type e_type;
28 static boost::random::mt19937 gen;
29 T val = gen();
30 T prev_val = -1;
31 while(val != prev_val)
32 {
33 val *= (gen.max)();
34 prev_val = val;
35 val += gen();
36 }
37 e_type e;
38 val = frexp(val, &e);
39
40 static boost::random::uniform_int_distribution<e_type> ui(std::numeric_limits<T>::min_exponent + 1, std::numeric_limits<T>::max_exponent - 1);
41 return ldexp(val, ui(gen));
42}
43
44template <class T>
45void test()
46{
47 boost::timer tim;
48
49 while(true)
50 {
51 T val = generate_random<T>(boost::math::tools::digits<T>());
52 int test_id = 0;
53 std::string stream_contents;
54#ifndef BOOST_NO_EXCEPTIONS
55 try{
56#endif
57 test_id = 0;
58 {
59 std::stringstream ss(std::ios_base::in | std::ios_base::out | std::ios_base::binary);
60 boost::archive::text_oarchive oa(ss);
61 oa << static_cast<const T&>(val);
62 stream_contents = ss.str();
63 boost::archive::text_iarchive ia(ss);
64 T val2;
65 ia >> val2;
66 BOOST_CHECK_EQUAL(val, val2);
67 }
68 {
69 std::stringstream ss(std::ios_base::in | std::ios_base::out | std::ios_base::binary);
70 ++test_id;
71 boost::archive::binary_oarchive ba(ss);
72 ba << static_cast<const T&>(val);
73 stream_contents = ss.str();
74 boost::archive::binary_iarchive ib(ss);
75 T val2;
76 ib >> val2;
77 BOOST_CHECK_EQUAL(val, val2);
78 }
79 {
80 std::stringstream ss(std::ios_base::in | std::ios_base::out | std::ios_base::binary);
81 val = -val;
82 ++test_id;
83 boost::archive::text_oarchive oa2(ss);
84 oa2 << static_cast<const T&>(val);
85 stream_contents = ss.str();
86 boost::archive::text_iarchive ia2(ss);
87 T val2;
88 ia2 >> val2;
89 BOOST_CHECK_EQUAL(val, val2);
90 }
91 {
92 std::stringstream ss(std::ios_base::in | std::ios_base::out | std::ios_base::binary);
93 ++test_id;
94 boost::archive::binary_oarchive ba2(ss);
95 ba2 << static_cast<const T&>(val);
96 stream_contents = ss.str();
97 boost::archive::binary_iarchive ib2(ss);
98 T val2;
99 ib2 >> val2;
100 BOOST_CHECK_EQUAL(val, val2);
101 }
102#ifndef BOOST_NO_EXCEPTIONS
103 }
104 catch(const boost::exception& e)
105 {
106 std::cout << "Caught boost::exception with:\n";
107 std::cout << diagnostic_information(e);
108 std::cout << "Failed test ID = " << test_id << std::endl;
109 std::cout << "Stream contents were: \n" << stream_contents << std::endl;
110 ++boost::detail::test_errors();
111 break;
112 }
113 catch(const std::exception& e)
114 {
115 std::cout << "Caught std::exception with:\n";
116 std::cout << e.what() << std::endl;
117 std::cout << "Failed test ID = " << test_id << std::endl;
118 std::cout << "Stream contents were: \n" << stream_contents << std::endl;
119 ++boost::detail::test_errors();
120 break;
121 }
122#endif
123 //
124 // Check to see if test is taking too long.
125 // Tests run on the compiler farm time out after 300 seconds,
126 // so don't get too close to that:
127 //
b32b8144 128#ifndef CI_SUPPRESS_KNOWN_ISSUES
7c673cae 129 if(tim.elapsed() > 150)
b32b8144
FG
130#else
131 if(tim.elapsed() > 25)
132#endif
7c673cae
FG
133 {
134 std::cout << "Timeout reached, aborting tests now....\n";
135 break;
136 }
137 }
138}
139
140#endif