lexer: when switching to another output stream, clean last read character.
[ginac.git] / check / numeric_archive.cpp
1 /**
2  * @file numeric_archive.cpp Check for a bug in numeric::archive
3  *
4  * numeric::archive used to fail if the real part of a complex number
5  * is a rational number and the imaginary part is a floating point one.
6  */
7 #include <cln/cln.h>
8 #include <iostream>
9 #include <stdexcept>
10 #include <vector>
11 #include <iterator>
12 #include <algorithm>
13 #include <sstream>
14 #include "ginac.h"
15 using namespace cln;
16 using namespace GiNaC;
17
18 struct archive_unarchive_check
19 {
20         cl_N operator()(const cl_N& n) const
21         {
22                 ex e = numeric(n);
23                 archive ar;
24                 ar.archive_ex(e, "test");
25                 lst l;
26                 ex check = ar.unarchive_ex(l, "test");
27                 if (!check.is_equal(e)) {
28                         std::ostringstream s;
29                         s << __FILE__ << ':' << __LINE__ << ": expected: " << e << ", got " << check;
30                         throw std::logic_error(s.str());
31                 }
32                 return n;
33         }
34 };
35
36 int main(int argc, char** argv)
37 {
38         const cl_I one(1);
39         std::cout << "checking if numeric::archive handles complex numbers properly" << std::endl;
40         const cl_R three_fp = cl_float(3.0, default_float_format);
41         std::vector<cl_N> numbers;
42         numbers.push_back(complex(one, three_fp));
43         numbers.push_back(complex(three_fp, one));
44         numbers.push_back(complex(three_fp, three_fp));
45         numbers.push_back(complex(one, one));
46         std::for_each(numbers.begin(), numbers.end(), archive_unarchive_check());
47         return 0;
48 }