1 | #include "config.h" |
---|
2 | #include <misc/auxiliary.h> |
---|
3 | |
---|
4 | #ifdef HAVE_FACTORY |
---|
5 | #include <factory/factory.h> |
---|
6 | #endif |
---|
7 | |
---|
8 | #include <omalloc/omalloc.h> |
---|
9 | |
---|
10 | #include <reporter/reporter.h> |
---|
11 | #include <resources/feResource.h> |
---|
12 | |
---|
13 | #include <coeffs/coeffs.h> |
---|
14 | #include <coeffs/numbers.h> |
---|
15 | |
---|
16 | #include <coeffs/longrat.h> |
---|
17 | #include <coeffs/gnumpfl.h> |
---|
18 | #include <coeffs/gnumpc.h> |
---|
19 | #include <coeffs/shortfl.h> |
---|
20 | #include <coeffs/ffields.h> |
---|
21 | #include <coeffs/modulop.h> |
---|
22 | #include <coeffs/rmodulon.h> |
---|
23 | #include <coeffs/rmodulo2m.h> |
---|
24 | #include <coeffs/rintegers.h> |
---|
25 | |
---|
26 | |
---|
27 | #ifdef HAVE_FACTORY |
---|
28 | int initializeGMP(){ return 1; } |
---|
29 | #endif |
---|
30 | |
---|
31 | #include <iostream> |
---|
32 | |
---|
33 | using namespace std; |
---|
34 | |
---|
35 | #pragma GCC diagnostic ignored "-Wwrite-strings" |
---|
36 | |
---|
37 | void Print(/*const*/ number a, const coeffs r, BOOLEAN eoln = TRUE) |
---|
38 | { |
---|
39 | n_Test(a,r); |
---|
40 | |
---|
41 | StringSetS(""); |
---|
42 | n_Write(a, r); |
---|
43 | |
---|
44 | char* s = NULL; |
---|
45 | |
---|
46 | if( eoln ) |
---|
47 | s = StringAppend("\n"); |
---|
48 | else |
---|
49 | s = StringAppend(""); |
---|
50 | |
---|
51 | PrintS(s); |
---|
52 | |
---|
53 | // free s? |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | void PrintSized(/*const*/ number a, const coeffs r, BOOLEAN eoln = TRUE) |
---|
58 | { |
---|
59 | Print(a, r, FALSE); |
---|
60 | Print(", of size: %d", n_Size(a, r)); |
---|
61 | |
---|
62 | if( eoln ) |
---|
63 | PrintLn(); |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | |
---|
68 | bool TestArith(const coeffs r) |
---|
69 | { |
---|
70 | number a = n_Init(66666, r); |
---|
71 | |
---|
72 | PrintS("a: "); PrintSized(a, r); |
---|
73 | |
---|
74 | number two = n_Init(2, r); |
---|
75 | |
---|
76 | PrintS("two: "); PrintSized(two, r); |
---|
77 | |
---|
78 | if (getCoeffType(r) == n_GF) //some special test for GF |
---|
79 | { |
---|
80 | number z = nfPar (0, r); // also any integer instead of 0//? |
---|
81 | |
---|
82 | PrintS("Generator: "); PrintSized(z, r); |
---|
83 | |
---|
84 | n_Delete(&z, r); |
---|
85 | } |
---|
86 | |
---|
87 | number aa = n_Add(a, a, r); |
---|
88 | |
---|
89 | PrintS("aa = a + a: "); PrintSized(aa, r); |
---|
90 | |
---|
91 | number aa2 = n_Mult(a, two, r); |
---|
92 | |
---|
93 | PrintS("aa2 = a * 2: "); PrintSized(aa2, r); |
---|
94 | |
---|
95 | number aa1 = n_Mult(two, a, r); |
---|
96 | |
---|
97 | PrintS("aa1 = 2 * a: "); PrintSized(aa1, r); |
---|
98 | |
---|
99 | n_Delete(&a, r); |
---|
100 | n_Delete(&two, r); |
---|
101 | |
---|
102 | |
---|
103 | a = n_Sub( aa, aa1, r ); |
---|
104 | |
---|
105 | PrintS("a = aa - aa1: "); PrintSized(a, r); |
---|
106 | |
---|
107 | if( !n_IsZero(a, r) ) |
---|
108 | WarnS("TestArith: ERROR: a != 0 !!!\n"); |
---|
109 | |
---|
110 | n_Delete(&a, r); |
---|
111 | |
---|
112 | a = n_Sub( aa, aa2, r ); |
---|
113 | |
---|
114 | PrintS("a = aa - aa2: "); PrintSized(a, r); |
---|
115 | |
---|
116 | if( !n_IsZero(a, r) ) |
---|
117 | WarnS("TestArith: ERROR: a != 0 !!!\n"); |
---|
118 | |
---|
119 | n_Delete(&a, r); |
---|
120 | |
---|
121 | |
---|
122 | a = n_Sub( aa1, aa2, r ); |
---|
123 | |
---|
124 | PrintS("a = aa1 - aa2: "); PrintSized(a, r); |
---|
125 | |
---|
126 | if( !n_IsZero(a, r) ) |
---|
127 | WarnS("TestArith: ERROR: a != 0 !!!\n"); |
---|
128 | |
---|
129 | n_Delete(&a, r); |
---|
130 | |
---|
131 | |
---|
132 | |
---|
133 | if( !n_Equal(aa, aa1, r) ) |
---|
134 | WarnS("TestArith: ERROR: aa != aa1 !!!\n"); |
---|
135 | |
---|
136 | if( !n_Equal(aa, aa2, r) ) |
---|
137 | WarnS("TestArith: ERROR: aa != aa2 !!!\n"); |
---|
138 | |
---|
139 | if( !n_Equal(aa1, aa2, r) ) |
---|
140 | WarnS("TestArith: ERROR: aa1 != aa2 !!!\n"); |
---|
141 | |
---|
142 | |
---|
143 | |
---|
144 | |
---|
145 | n_Delete(&aa, r); |
---|
146 | n_Delete(&aa1, r); |
---|
147 | n_Delete(&aa2, r); |
---|
148 | |
---|
149 | return false; |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | |
---|
154 | namespace |
---|
155 | { |
---|
156 | static inline ostream& operator<< (ostream& o, const n_coeffType& type) |
---|
157 | { |
---|
158 | #define CASE(A) case A: return o << (" " # A) << " "; |
---|
159 | switch( type ) |
---|
160 | { |
---|
161 | CASE(n_unknown); |
---|
162 | CASE(n_Zp); |
---|
163 | CASE(n_Q); |
---|
164 | CASE(n_R); |
---|
165 | CASE(n_GF); |
---|
166 | CASE(n_long_R); |
---|
167 | CASE(n_algExt); |
---|
168 | CASE(n_transExt); |
---|
169 | CASE(n_long_C); |
---|
170 | CASE(n_Z); |
---|
171 | CASE(n_Zn); |
---|
172 | CASE(n_Zpn); |
---|
173 | CASE(n_Z2m); |
---|
174 | CASE(n_CF); |
---|
175 | default: return o << "Unknown type: [" << (const unsigned long) type << "]"; |
---|
176 | } |
---|
177 | #undef CASE |
---|
178 | return o; |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | bool Test(const n_coeffType type, void* p = NULL) |
---|
184 | { |
---|
185 | cout << endl << "----------------------- Testing coeffs: [" << type << ", " << p << |
---|
186 | "]: -----------------------" << endl; |
---|
187 | |
---|
188 | const coeffs r = nInitChar( type, p ); |
---|
189 | |
---|
190 | if( r == NULL ) { cout << "Test: could not get the specified coeff. domain for type: " << type << " and the parameter: " << p << endl; return false; }; |
---|
191 | |
---|
192 | assume( r != NULL ); |
---|
193 | nSetChar( r ); |
---|
194 | assume( getCoeffType(r) == type ); |
---|
195 | |
---|
196 | assume( r->cfInit != NULL ); |
---|
197 | assume( r->cfWrite != NULL ); |
---|
198 | assume( r->cfAdd != NULL ); |
---|
199 | assume( r->cfDelete != NULL ); |
---|
200 | |
---|
201 | if( type == n_Q ) |
---|
202 | { |
---|
203 | assume( r->cfInit == nlInit ); |
---|
204 | assume( r->cfWrite == nlWrite ); |
---|
205 | assume( r->cfAdd == nlAdd ); |
---|
206 | assume( r->cfDelete == nlDelete ); |
---|
207 | } |
---|
208 | else if( type == n_long_R ) |
---|
209 | { |
---|
210 | assume( r->cfInit == ngfInit ); |
---|
211 | assume( r->cfWrite == ngfWrite ); |
---|
212 | assume( r->cfAdd == ngfAdd ); |
---|
213 | assume( r->cfDelete == ngfDelete ); |
---|
214 | } |
---|
215 | else if( type == n_long_C ) |
---|
216 | { |
---|
217 | assume( r->cfInit == ngcInit ); |
---|
218 | assume( r->cfWrite == ngcWrite ); |
---|
219 | assume( r->cfAdd == ngcAdd ); |
---|
220 | assume( r->cfDelete == ngcDelete ); |
---|
221 | } |
---|
222 | else if( type == n_R ) |
---|
223 | { |
---|
224 | assume( r->cfInit == nrInit ); |
---|
225 | assume( r->cfWrite == nrWrite ); |
---|
226 | assume( r->cfAdd == nrAdd ); |
---|
227 | // assume( r->cfDelete == nrDelete ); // No? |
---|
228 | } |
---|
229 | #ifdef HAVE_RINGS |
---|
230 | else if( type == n_Z2m ) |
---|
231 | { |
---|
232 | assume( r->cfInit == nr2mInit ); |
---|
233 | assume( r->cfWrite == nr2mWrite ); |
---|
234 | assume( r->cfAdd == nr2mAdd ); |
---|
235 | assume( r->cfDelete == ndDelete ); |
---|
236 | } |
---|
237 | else if( type == n_Zn ) |
---|
238 | { |
---|
239 | assume( r->cfInit == nrnInit ); |
---|
240 | assume( r->cfWrite == nrnWrite ); |
---|
241 | assume( r->cfAdd == nrnAdd ); |
---|
242 | assume( r->cfDelete == nrnDelete ); |
---|
243 | } |
---|
244 | #endif |
---|
245 | else if( type == n_GF ) |
---|
246 | { |
---|
247 | assume( r->cfInit == nfInit ); |
---|
248 | assume( r->cfWrite == nfWrite ); |
---|
249 | assume( r->cfAdd == nfAdd ); |
---|
250 | //assume( r->cfDelete == nfDelete ); |
---|
251 | } |
---|
252 | else |
---|
253 | { |
---|
254 | // ... |
---|
255 | } |
---|
256 | |
---|
257 | bool ret = TestArith( r ); |
---|
258 | |
---|
259 | nKillChar( r ); |
---|
260 | |
---|
261 | return ret; |
---|
262 | } |
---|
263 | |
---|
264 | |
---|
265 | int main( int, char *argv[] ) |
---|
266 | { |
---|
267 | feInitResources(argv[0]); |
---|
268 | |
---|
269 | StringSetS("ressources in use (as reported by feStringAppendResources(0):\n"); |
---|
270 | feStringAppendResources(0); |
---|
271 | PrintS(StringAppendS("\n")); |
---|
272 | |
---|
273 | |
---|
274 | int c = 0; |
---|
275 | |
---|
276 | n_coeffType type; |
---|
277 | |
---|
278 | |
---|
279 | #ifdef HAVE_RINGS |
---|
280 | // TODO(Frank, Segmentation fault! (if used wihout omalloc???). Please_ investigate!); |
---|
281 | type = n_Z2m; |
---|
282 | if( Test(type, (void*) 4) ) |
---|
283 | c ++; |
---|
284 | #endif |
---|
285 | |
---|
286 | type = n_Zp; |
---|
287 | if( Test(type, (void*) 101) ) |
---|
288 | c ++; |
---|
289 | |
---|
290 | #ifdef HAVE_RINGS |
---|
291 | // TODO(Frank, memmory corruption_ if used wihout omalloc??? Please_ investigate!); |
---|
292 | |
---|
293 | type = n_Z2m; |
---|
294 | if( Test(type, (void*) 8) ) |
---|
295 | c ++; |
---|
296 | |
---|
297 | #endif |
---|
298 | |
---|
299 | |
---|
300 | type = n_Q; |
---|
301 | if( Test(type) ) |
---|
302 | c ++; |
---|
303 | |
---|
304 | type = n_R; |
---|
305 | if( Test(type) ) |
---|
306 | c ++; |
---|
307 | |
---|
308 | #ifdef HAVE_RINGS |
---|
309 | type = n_Z; |
---|
310 | if( Test(type) ) |
---|
311 | c ++; |
---|
312 | #endif |
---|
313 | type = n_GF; |
---|
314 | |
---|
315 | |
---|
316 | GFInfo* param = new GFInfo(); |
---|
317 | |
---|
318 | param->GFChar= 5; |
---|
319 | param->GFDegree= 12; |
---|
320 | param->GFPar_name= (const char*)"q"; |
---|
321 | |
---|
322 | if( Test(type, (void*) param) ) |
---|
323 | c ++; |
---|
324 | |
---|
325 | // it should not be used by numbers... right? |
---|
326 | // TODO: what is our policy wrt param-pointer-ownership? |
---|
327 | delete param; |
---|
328 | // Q: no way to deRegister a type? |
---|
329 | |
---|
330 | param = new GFInfo(); |
---|
331 | |
---|
332 | param->GFChar= 5; |
---|
333 | param->GFDegree= 2; |
---|
334 | param->GFPar_name= (const char*)"Q"; |
---|
335 | |
---|
336 | if( Test(type, (void*) param) ) |
---|
337 | c ++; |
---|
338 | |
---|
339 | delete param; |
---|
340 | |
---|
341 | |
---|
342 | |
---|
343 | |
---|
344 | #ifdef HAVE_RINGS |
---|
345 | // TODO(Somebody, This will result in memory corruption at Z_2^m later on (due to the succs. setGMPFloatDigits?)...!?); // ???? |
---|
346 | |
---|
347 | type = n_Zn; |
---|
348 | |
---|
349 | if( Test(type, (void*) 3) ) |
---|
350 | c ++; |
---|
351 | |
---|
352 | #endif |
---|
353 | |
---|
354 | TODO(Somebody, floating arithmetics via GMP rely on two global variables (see setGMPFloatDigits). Please fix it!); |
---|
355 | setGMPFloatDigits( 10, 5 ); // Init global variables in mpr_complex.cc for gmp_float's... // Note that this seems also to be required for Z_2^m (and Zn?)!???? |
---|
356 | |
---|
357 | |
---|
358 | type = n_long_C; |
---|
359 | if( Test(type) ) |
---|
360 | c ++; |
---|
361 | |
---|
362 | type = n_long_R; |
---|
363 | if( Test(type) ) |
---|
364 | c ++; |
---|
365 | |
---|
366 | #ifdef HAVE_RINGS |
---|
367 | type = n_Z2m; |
---|
368 | if( Test(type, (void*) 2) ) |
---|
369 | c ++; |
---|
370 | #endif |
---|
371 | |
---|
372 | |
---|
373 | #ifdef HAVE_RINGS |
---|
374 | type = n_Zn; |
---|
375 | |
---|
376 | if( Test(type, (void*) 3) ) |
---|
377 | c ++; |
---|
378 | #endif |
---|
379 | |
---|
380 | // polynomial rings needed for: n_algExt, n_transExt ! |
---|
381 | |
---|
382 | return c; |
---|
383 | |
---|
384 | } |
---|