Ticket #9177: t.cpp

File t.cpp, 3.4 KB (added by John Maddock, 9 years ago)
Line 
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#define BOOST_TEST_MAIN
7#include <boost/test/unit_test.hpp> // Boost.Test
8#include <boost/random/mersenne_twister.hpp>
9#include <boost/random/uniform_01.hpp>
10#include <boost/timer.hpp>
11
12#include <iostream>
13#include <iomanip>
14#include <sstream>
15#include <boost/archive/text_iarchive.hpp>
16#include <boost/archive/text_oarchive.hpp>
17#include <boost/archive/binary_iarchive.hpp>
18#include <boost/archive/binary_oarchive.hpp>
19#include <boost/exception/all.hpp>
20
21template <class T>
22void test()
23{
24 boost::timer tim;
25 boost::random::mt19937 gen;
26 boost::random::uniform_01<T> d;
27
28 while(true)
29 {
30 T val = d(gen);
31
32 try{
33 {
34 std::stringstream ss(std::ios_base::in | std::ios_base::out | std::ios_base::binary);
35 boost::archive::text_oarchive oa(ss);
36 oa << static_cast<const T&>(val);
37 boost::archive::text_iarchive ia(ss);
38 T val2;
39 ia >> val2;
40 BOOST_CHECK_EQUAL(val, val2);
41 }
42 {
43 std::stringstream ss(std::ios_base::in | std::ios_base::out | std::ios_base::binary);
44 boost::archive::binary_oarchive ba(ss);
45 ba << static_cast<const T&>(val);
46 boost::archive::binary_iarchive ib(ss);
47 T val2;
48 ib >> val2;
49 BOOST_CHECK_EQUAL(val, val2);
50 }
51 {
52 std::stringstream ss(std::ios_base::in | std::ios_base::out | std::ios_base::binary);
53 val = -val;
54 boost::archive::text_oarchive oa2(ss);
55 oa2 << static_cast<const T&>(val);
56 boost::archive::text_iarchive ia2(ss);
57 T val2;
58 ia2 >> val2;
59 BOOST_CHECK_EQUAL(val, val2);
60 }
61 {
62 std::stringstream ss(std::ios_base::in | std::ios_base::out | std::ios_base::binary);
63 boost::archive::binary_oarchive ba2(ss);
64 ba2 << static_cast<const T&>(val);
65 boost::archive::binary_iarchive ib2(ss);
66 T val2;
67 ib2 >> val2;
68 BOOST_CHECK_EQUAL(val, val2);
69 }
70 }
71 catch(const boost::exception& e)
72 {
73 std::cout << "Caught boost::exception with:\n";
74 std::cout << diagnostic_information(e);
75 BOOST_ERROR("Unexpected exception");
76 break;
77 }
78 catch(const std::exception& e)
79 {
80 std::cout << "Caught std::exception with:\n";
81 std::cout << e.what() << std::endl;
82 BOOST_ERROR("Unexpected exception");
83 break;
84 }
85 //
86 // Check to see if test is taking too long.
87 // Tests run on the compiler farm time out after 300 seconds,
88 // so don't get too close to that:
89 //
90 if(tim.elapsed() > 150)
91 {
92 std::cout << "Timeout reached, aborting tests now....\n";
93 break;
94 }
95 }
96}
97
98#if !defined(TEST_FLOAT) && !defined(TEST_DOUBLE) && !defined(TEST_LONG_DOUBLE)
99# error "No test type specified!!"
100#endif
101
102BOOST_AUTO_TEST_CASE( test_main )
103{
104#ifdef TEST_FLOAT
105 test<float>();
106#endif
107#ifdef TEST_DOUBLE
108 test<double>();
109#endif
110#ifdef TEST_LONG_DOUBLE
111 test<long double>();
112#endif
113}
114