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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
// Copyright (c) 2006 Johan Rade
// Copyright (c) 2011 Paul A. Bristow - filename changes for boost-trunk.
// 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 : 4511 4512 4702)
//#endif
#define BOOST_TEST_MAIN
#include <limits>
#include <locale>
#include <sstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_wiarchive.hpp>
#include <boost/archive/text_woarchive.hpp>
#include <boost/archive/codecvt_null.hpp>
#include <boost/test/auto_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"
namespace {
// The anonymous namespace resolves ambiguities on platforms
// with fpclassify etc functions at global scope.
using namespace boost::archive;
using namespace boost::math;
using boost::math::signbit;
using boost::math::changesign;
using (boost::math::isnan)(;
//------------------------------------------------------------------------------
void archive_basic_test();
void archive_put_trap_test();
void archive_get_trap_test();
BOOST_AUTO_TEST_CASE(archive_test)
{
archive_basic_test();
archive_put_trap_test();
archive_get_trap_test();
}
//------------------------------------------------------------------------------
template<class CharType, class OArchiveType, class IArchiveType, class ValType>
void archive_basic_test_impl();
void archive_basic_test()
{
archive_basic_test_impl<char, text_oarchive, text_iarchive, float>();
archive_basic_test_impl<char, text_oarchive, text_iarchive, double>();
archive_basic_test_impl<
char, text_oarchive, text_iarchive, long double>();
archive_basic_test_impl<
wchar_t, text_woarchive, text_wiarchive, float>();
archive_basic_test_impl<
wchar_t, text_woarchive, text_wiarchive, double>();
archive_basic_test_impl<
wchar_t, text_woarchive, text_wiarchive, long double>();
}
template<class CharType, class OArchiveType, class IArchiveType, class ValType>
void archive_basic_test_impl()
{
if((std::numeric_limits<ValType>::has_infinity == 0) || (std::numeric_limits<ValType>::infinity() == 0))
return;
std::locale default_locale(std::locale::classic(),
new boost::archive::codecvt_null<CharType>);
std::locale tmp_locale(default_locale, new nonfinite_num_put<CharType>);
std::locale my_locale(tmp_locale, new nonfinite_num_get<CharType>);
std::basic_stringstream<CharType> ss;
ss.imbue(my_locale);
ValType a1 = static_cast<ValType>(0);
ValType a2 = static_cast<ValType>(2307.35);
ValType a3 = std::numeric_limits<ValType>::infinity();
BOOST_CHECK((boost::math::isinf)(a3));
ValType a4 = std::numeric_limits<ValType>::quiet_NaN();
BOOST_CHECK(((boost::math::isnan)()(a4));
ValType a5 = std::numeric_limits<ValType>::signaling_NaN();
BOOST_CHECK(((boost::math::isnan)()(a5));
ValType a6 = (changesign)(static_cast<ValType>(0));
ValType a7 = static_cast<ValType>(-57.13);
ValType a8 = -std::numeric_limits<ValType>::infinity();
BOOST_CHECK((boost::math::isinf)(a8));
ValType a9 = -std::numeric_limits<ValType>::quiet_NaN();
BOOST_CHECK(((boost::math::isnan)()(a9));
ValType a10 = -std::numeric_limits<ValType>::signaling_NaN();
BOOST_CHECK(((boost::math::isnan)()(a10));
{
OArchiveType oa(ss, no_codecvt);
oa & a1 & a2 & a3 & a4 & a5 & a6 & a7 & a8 & a9 & a10;
}
ValType b1, b2, b3, b4, b5, b6, b7, b8, b9, b10;
{
IArchiveType ia(ss, no_codecvt);
ia & b1 & b2 & b3 & b4 & b5 & b6 & b7 & b8 & b9 & b10;
}
BOOST_CHECK(a1 == b1);
BOOST_CHECK(almost_equal(a2, b2));
BOOST_CHECK(a3 == b3);
BOOST_CHECK((isnan)(b4));
BOOST_CHECK(!(signbit)(b4));
BOOST_CHECK((isnan)(b5));
BOOST_CHECK(!(signbit)(b5));
BOOST_CHECK(a6 == b6);
BOOST_CHECK(almost_equal(a7, b7));
BOOST_CHECK(a8 == b8);
BOOST_CHECK((isnan)(b9));
BOOST_CHECK((signbit)(b9));
BOOST_CHECK((isnan)(b10));
BOOST_CHECK((signbit)(b10));
}
//------------------------------------------------------------------------------
template<class CharType, class OArchiveType, class IArchiveType, class ValType>
void archive_put_trap_test_impl();
void archive_put_trap_test()
{
archive_put_trap_test_impl<char, text_oarchive, text_iarchive, float>();
archive_put_trap_test_impl<char, text_oarchive, text_iarchive, double>();
archive_put_trap_test_impl<
char, text_oarchive, text_iarchive, long double>();
archive_put_trap_test_impl<
wchar_t, text_woarchive, text_wiarchive, float>();
archive_put_trap_test_impl<
wchar_t, text_woarchive, text_wiarchive, double>();
archive_put_trap_test_impl<
wchar_t, text_woarchive, text_wiarchive, long double>();
}
template<class CharType, class OArchiveType, class IArchiveType, class ValType>
void archive_put_trap_test_impl()
{
if((std::numeric_limits<ValType>::has_infinity == 0) || (std::numeric_limits<ValType>::infinity() == 0))
return;
std::locale default_locale(std::locale::classic(),
new boost::archive::codecvt_null<CharType>);
std::locale new_locale(default_locale,
new nonfinite_num_put<CharType>(trap_infinity));
std::basic_stringstream<CharType> ss;
ss.exceptions(std::ios_base::failbit | std::ios_base::badbit);
ss.imbue(new_locale);
ValType a = std::numeric_limits<ValType>::infinity();
OArchiveType oa(ss, no_codecvt);
try {
oa & a;
}
catch(std::exception&) {
ss.clear();
return;
}
BOOST_CHECK(false);
}
//------------------------------------------------------------------------------
template<class CharType, class OArchiveType, class IArchiveType, class ValType>
void archive_get_trap_test_impl();
void archive_get_trap_test()
{
archive_get_trap_test_impl<char, text_oarchive, text_iarchive, float>();
archive_get_trap_test_impl<char, text_oarchive, text_iarchive, double>();
archive_get_trap_test_impl<
char, text_oarchive, text_iarchive, long double>();
archive_get_trap_test_impl<
wchar_t, text_woarchive, text_wiarchive, float>();
archive_get_trap_test_impl<
wchar_t, text_woarchive, text_wiarchive, double>();
archive_get_trap_test_impl<
wchar_t, text_woarchive, text_wiarchive, long double>();
}
template<class CharType, class OArchiveType, class IArchiveType, class ValType>
void archive_get_trap_test_impl()
{
if((std::numeric_limits<ValType>::has_infinity == 0) || (std::numeric_limits<ValType>::infinity() == 0))
return;
std::locale default_locale(std::locale::classic(),
new boost::archive::codecvt_null<CharType>);
std::locale tmp_locale(default_locale, new nonfinite_num_put<CharType>);
std::locale my_locale(tmp_locale,
new nonfinite_num_get<CharType>(trap_nan));
std::basic_stringstream<CharType> ss;
ss.exceptions(std::ios_base::failbit);
ss.imbue(my_locale);
ValType a = -std::numeric_limits<ValType>::quiet_NaN();
{
OArchiveType oa(ss, no_codecvt);
oa & a;
}
ValType b;
{
IArchiveType ia(ss, no_codecvt);
try {
ia & b;
}
catch(std::exception&) {
return;
}
}
BOOST_CHECK(false);
}
//------------------------------------------------------------------------------
} // anonymous namespace
|