1 /*
2 Copyright (c) 2014, Randolph Voorhies, Shane Grant
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of cereal nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES AND SHANE GRANT BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 #ifndef CEREAL_TEST_COMPLEX_H_
28 #define CEREAL_TEST_COMPLEX_H_
29 #include "common.hpp"
30
31 template <class IArchive, class OArchive> inline
test_complex()32 void test_complex()
33 {
34 std::random_device rd;
35 std::mt19937 gen(rd());
36
37 auto rngF = [&](){ return random_value<float>(gen); };
38 auto rngD = [&](){ return random_value<double>(gen); };
39 auto rngLD = [&](){ return random_value<long double>(gen); };
40
41 for(int ii=0; ii<100; ++ii)
42 {
43 std::complex<float> o_float( rngF(), rngF() );
44 std::complex<double> o_double( rngD(), rngD() );
45 std::complex<long double> o_ldouble( rngLD(), rngLD() );
46
47 std::ostringstream os;
48 {
49 OArchive oar(os);
50
51 oar(o_float);
52 oar(o_double);
53 oar(o_ldouble);
54 }
55
56 std::complex<float> i_float;
57 std::complex<double> i_double;
58 std::complex<long double> i_ldouble;
59
60 std::istringstream is(os.str());
61 {
62 IArchive iar(is);
63
64 iar(i_float);
65 iar(i_double);
66 iar(i_ldouble);
67 }
68
69 CHECK_EQ( o_float, i_float );
70 CHECK_EQ( o_double.real(), doctest::Approx(i_double.real()).epsilon(1e-5) );
71 CHECK_EQ( o_double.imag(), doctest::Approx(i_double.imag()).epsilon(1e-5) );
72 CHECK_EQ( o_ldouble.real(), doctest::Approx(i_ldouble.real()).epsilon(1e-5L) );
73 CHECK_EQ( o_ldouble.imag(), doctest::Approx(i_ldouble.imag()).epsilon(1e-5L) );
74 }
75 }
76
77 #endif // CEREAL_TEST_COMPLEX_H_
78