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
|
///////////////////////////////////////////////////////////////
// Copyright 2013 John Maddock. 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_
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/timer.hpp>
#include "test.hpp"
#include <iostream>
#include <iomanip>
#include <sstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/exception/all.hpp>
#ifndef BOOST_MP_TEST_FLOAT_SERIAL_HPP
#define BOOST_MP_TEST_FLOAT_SERIAL_HPP
template <class T>
T generate_random(unsigned bits_wanted)
{
typedef typename T::backend_type::exponent_type e_type;
static boost::random::mt19937 gen;
T val = gen();
T prev_val = -1;
while(val != prev_val)
{
val *= (gen.max)();
prev_val = val;
val += gen();
}
e_type e;
val = frexp(val, &e);
static boost::random::uniform_int_distribution<e_type> ui(std::numeric_limits<T>::min_exponent + 1, std::numeric_limits<T>::max_exponent - 1);
return ldexp(val, ui(gen));
}
template <class T>
void test()
{
boost::timer tim;
while(true)
{
T val = generate_random<T>(boost::math::tools::digits<T>());
int test_id = 0;
std::string stream_contents;
#ifndef BOOST_NO_EXCEPTIONS
try{
#endif
test_id = 0;
{
std::stringstream ss(std::ios_base::in | std::ios_base::out | std::ios_base::binary);
boost::archive::text_oarchive oa(ss);
oa << static_cast<const T&>(val);
stream_contents = ss.str();
boost::archive::text_iarchive ia(ss);
T val2;
ia >> val2;
BOOST_CHECK_EQUAL(val, val2);
}
{
std::stringstream ss(std::ios_base::in | std::ios_base::out | std::ios_base::binary);
++test_id;
boost::archive::binary_oarchive ba(ss);
ba << static_cast<const T&>(val);
stream_contents = ss.str();
boost::archive::binary_iarchive ib(ss);
T val2;
ib >> val2;
BOOST_CHECK_EQUAL(val, val2);
}
{
std::stringstream ss(std::ios_base::in | std::ios_base::out | std::ios_base::binary);
val = -val;
++test_id;
boost::archive::text_oarchive oa2(ss);
oa2 << static_cast<const T&>(val);
stream_contents = ss.str();
boost::archive::text_iarchive ia2(ss);
T val2;
ia2 >> val2;
BOOST_CHECK_EQUAL(val, val2);
}
{
std::stringstream ss(std::ios_base::in | std::ios_base::out | std::ios_base::binary);
++test_id;
boost::archive::binary_oarchive ba2(ss);
ba2 << static_cast<const T&>(val);
stream_contents = ss.str();
boost::archive::binary_iarchive ib2(ss);
T val2;
ib2 >> val2;
BOOST_CHECK_EQUAL(val, val2);
}
#ifndef BOOST_NO_EXCEPTIONS
}
catch(const boost::exception& e)
{
std::cout << "Caught boost::exception with:\n";
std::cout << diagnostic_information(e);
std::cout << "Failed test ID = " << test_id << std::endl;
std::cout << "Stream contents were: \n" << stream_contents << std::endl;
++boost::detail::test_errors();
break;
}
catch(const std::exception& e)
{
std::cout << "Caught std::exception with:\n";
std::cout << e.what() << std::endl;
std::cout << "Failed test ID = " << test_id << std::endl;
std::cout << "Stream contents were: \n" << stream_contents << std::endl;
++boost::detail::test_errors();
break;
}
#endif
//
// Check to see if test is taking too long.
// Tests run on the compiler farm time out after 300 seconds,
// so don't get too close to that:
//
if(tim.elapsed() > 150)
{
std::cout << "Timeout reached, aborting tests now....\n";
break;
}
}
}
#endif
|