1 | #ifndef TESTS_COMMON_H |
---|
2 | #define TESTS_COMMON_H |
---|
3 | |
---|
4 | #include <iostream> |
---|
5 | #include <fstream> |
---|
6 | #include <string.h> |
---|
7 | |
---|
8 | #include <cxxtest/TestSuite.h> |
---|
9 | #include <cxxtest/GlobalFixture.h> |
---|
10 | |
---|
11 | #include <omalloc/omalloc.h> |
---|
12 | #include <misc/auxiliary.h> |
---|
13 | |
---|
14 | #include <coeffs/coeffs.h> |
---|
15 | #include <coeffs/numbers.h> |
---|
16 | |
---|
17 | #include <reporter/reporter.h> |
---|
18 | #include <resources/feResource.h> |
---|
19 | |
---|
20 | #ifdef HAVE_FACTORY |
---|
21 | int initializeGMP(){ return 1; } |
---|
22 | #endif |
---|
23 | |
---|
24 | // #pragma GCC diagnostic ignored "-Wwrite-strings" |
---|
25 | namespace |
---|
26 | { |
---|
27 | static inline std::ostream& operator<< (std::ostream& o, const n_coeffType& type) |
---|
28 | { |
---|
29 | #define CASE(A) case A: return o << (" " # A) << " "; |
---|
30 | switch( type ) |
---|
31 | { |
---|
32 | CASE(n_unknown); |
---|
33 | CASE(n_Zp); |
---|
34 | CASE(n_Q); |
---|
35 | CASE(n_R); |
---|
36 | CASE(n_GF); |
---|
37 | CASE(n_long_R); |
---|
38 | CASE(n_Ext); |
---|
39 | CASE(n_long_C); |
---|
40 | CASE(n_Z); |
---|
41 | CASE(n_Zn); |
---|
42 | CASE(n_Zpn); |
---|
43 | CASE(n_Z2m); |
---|
44 | CASE(n_CF); |
---|
45 | default: return o << "Unknown type: [" << (const unsigned long) type << "]"; |
---|
46 | } |
---|
47 | #undef CASE |
---|
48 | return o; |
---|
49 | } |
---|
50 | |
---|
51 | template<typename T> |
---|
52 | static inline std::string _2S(T i) |
---|
53 | { |
---|
54 | std::stringstream ss; |
---|
55 | ss << i; |
---|
56 | // std::string s = ss.str(); |
---|
57 | return ss.str(); |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | static inline std::string _2S(number a, const coeffs r) |
---|
62 | { |
---|
63 | n_Test(a,r); |
---|
64 | StringSetS(""); |
---|
65 | n_Write(a, r); |
---|
66 | |
---|
67 | const char* s = StringAppendS(""); |
---|
68 | |
---|
69 | std::stringstream ss; ss << s; |
---|
70 | |
---|
71 | StringSetS(""); |
---|
72 | return ss.str(); |
---|
73 | |
---|
74 | } |
---|
75 | |
---|
76 | static inline void PrintSized(/*const*/ number a, const coeffs r, BOOLEAN eoln = TRUE) |
---|
77 | { |
---|
78 | std::clog << _2S(a, r) << ", of size: " << n_Size(a, r); |
---|
79 | |
---|
80 | if( eoln ) |
---|
81 | std::clog << std::endl; |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | |
---|
86 | } |
---|
87 | |
---|
88 | class GlobalPrintingFixture : public CxxTest::GlobalFixture |
---|
89 | { |
---|
90 | std::ofstream _ofs; |
---|
91 | bool _redirect; |
---|
92 | public: |
---|
93 | GlobalPrintingFixture(bool redirect = false): _redirect(redirect){} |
---|
94 | |
---|
95 | ~GlobalPrintingFixture() |
---|
96 | { |
---|
97 | if( _ofs) |
---|
98 | _ofs.close(); |
---|
99 | } |
---|
100 | |
---|
101 | void Redirect() |
---|
102 | { |
---|
103 | const int ll = strlen(argv0); |
---|
104 | const int l = 5 + ll; |
---|
105 | char* s = (char *)omAlloc0(l); |
---|
106 | s = strncpy(s, argv0, ll); |
---|
107 | strncpy(s + ll, ".log", 4); |
---|
108 | _ofs.open(s); // , ios_base::out) |
---|
109 | omFreeSize((ADDRESS)s, l); |
---|
110 | |
---|
111 | std::clog.rdbuf(_ofs.rdbuf()); |
---|
112 | } |
---|
113 | |
---|
114 | virtual bool setUpWorld() |
---|
115 | { |
---|
116 | if( _redirect ) |
---|
117 | Redirect(); |
---|
118 | |
---|
119 | std::clog << std::endl << ( "<world>" ) << std::endl << std::endl; |
---|
120 | feInitResources(argv0); |
---|
121 | return true; |
---|
122 | } |
---|
123 | |
---|
124 | virtual bool tearDownWorld() |
---|
125 | { |
---|
126 | std::clog << std::endl << std::endl <<( "</world>" ) << std::endl << std::endl ; |
---|
127 | return true; |
---|
128 | } |
---|
129 | virtual bool setUp() { std::clog << std::endl << std::endl <<( "<test>" ) << std::endl << std::endl; return true; } |
---|
130 | virtual bool tearDown() { std::clog << std::endl << std::endl <<( "</test>" ) << std::endl << std::endl; return true; } |
---|
131 | }; |
---|
132 | |
---|
133 | |
---|
134 | #endif /* TESTS_COMMON_H */ |
---|