1 | #include "misc/auxiliary.h" |
---|
2 | #include "omalloc/omalloc.h" |
---|
3 | |
---|
4 | |
---|
5 | #include "reporter/reporter.h" |
---|
6 | #include "resources/feResource.h" |
---|
7 | |
---|
8 | #include "coeffs/coeffs.h" |
---|
9 | #include "coeffs/numbers.h" |
---|
10 | |
---|
11 | |
---|
12 | // the following headers are private... |
---|
13 | #include "coeffs/longrat.h" |
---|
14 | #include "coeffs/gnumpfl.h" |
---|
15 | #include "coeffs/gnumpc.h" |
---|
16 | #include "coeffs/shortfl.h" |
---|
17 | #include "coeffs/ffields.h" |
---|
18 | #include "coeffs/modulop.h" |
---|
19 | #include "coeffs/rmodulon.h" |
---|
20 | #include "coeffs/rmodulo2m.h" |
---|
21 | #include "coeffs/rintegers.h" |
---|
22 | |
---|
23 | |
---|
24 | #include "common.h" |
---|
25 | using namespace std; |
---|
26 | |
---|
27 | |
---|
28 | void TestSum(const coeffs r, const unsigned long N) |
---|
29 | { |
---|
30 | clog << ( _2S("TEST: sum[0..") + _2S(N) + "]: "); |
---|
31 | clog << endl; |
---|
32 | |
---|
33 | assume( N > 0 ); // just for now... |
---|
34 | |
---|
35 | const unsigned long ssss = (N * (N+1)) / 2; |
---|
36 | |
---|
37 | number sum1 = n_Init(ssss, r); |
---|
38 | clog<< "N*(N+1)/2 (int: " << ssss << "): "; PrintSized(sum1, r); |
---|
39 | |
---|
40 | number s, ss, i, res; |
---|
41 | |
---|
42 | s = n_Init(N , r); |
---|
43 | i = n_Init(N+1, r); |
---|
44 | n_InpMult(s, i, r); |
---|
45 | n_Delete(&i, r); |
---|
46 | |
---|
47 | clog<< "N*(N+1): ("<< N*(N+1) << ")"; PrintSized(s, r); |
---|
48 | |
---|
49 | i = n_Init(2, r); |
---|
50 | clog<< "2: "; PrintSized(i, r); |
---|
51 | |
---|
52 | if( !n_IsZero( i, r) ) |
---|
53 | { |
---|
54 | #ifdef HAVE_RINGS |
---|
55 | TS_ASSERT( n_DivBy(s, i, r) ); |
---|
56 | #endif |
---|
57 | |
---|
58 | res = n_Div(s, i, r); |
---|
59 | |
---|
60 | clog<< "N*(N+1)/2: "; PrintSized(res, r); |
---|
61 | |
---|
62 | |
---|
63 | number d = n_Sub(res, sum1, r); |
---|
64 | TS_ASSERT( n_IsZeroDivisor(d, r) ); |
---|
65 | n_Delete(&d, r); |
---|
66 | |
---|
67 | if( n_GetChar(r) == 0 ) |
---|
68 | { |
---|
69 | TS_ASSERT( n_Equal(sum1, res, r) ); |
---|
70 | TS_ASSERT( n_Equal(res, sum1, r) ); |
---|
71 | } |
---|
72 | } else |
---|
73 | TS_ASSERT_EQUALS( n_GetChar(r), 2); |
---|
74 | |
---|
75 | |
---|
76 | n_Delete(&s, r); n_Delete(&i, r); |
---|
77 | |
---|
78 | n_Delete(&sum1, r); n_Delete(&res, r); |
---|
79 | |
---|
80 | |
---|
81 | s = n_Init(0 , r); |
---|
82 | ss = n_Init(0 , r); |
---|
83 | for( int k = N; k >= 0; k-- ) |
---|
84 | { |
---|
85 | i = n_Init(k, r); |
---|
86 | n_InpAdd(s, i, r); // s += i |
---|
87 | |
---|
88 | i = n_InpNeg(i, r); |
---|
89 | n_InpAdd(ss, i, r); // ss -= i |
---|
90 | |
---|
91 | n_Delete(&i, r); |
---|
92 | } |
---|
93 | clog<< "ss: "; PrintSized(ss, r); |
---|
94 | |
---|
95 | ss = n_InpNeg(ss, r); // ss = -ss |
---|
96 | |
---|
97 | clog<< "real sum : "; PrintSized(s, r); |
---|
98 | clog<< "real sum(--): "; PrintSized(ss, r); |
---|
99 | |
---|
100 | TS_ASSERT( n_Equal(s, ss, r) ); |
---|
101 | TS_ASSERT( n_Equal(ss, s, r) ); |
---|
102 | |
---|
103 | n_Delete(&s, r); |
---|
104 | n_Delete(&ss, r); |
---|
105 | |
---|
106 | clog << ( " >>> TEST DONE!" ); |
---|
107 | clog << endl; |
---|
108 | |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | void TestArith(const coeffs r) |
---|
113 | { |
---|
114 | clog << ("TEST: Simple Arithmetics: "); |
---|
115 | clog << endl; |
---|
116 | |
---|
117 | number two = n_Init(2, r); |
---|
118 | |
---|
119 | number t = n_Init(1, r); |
---|
120 | n_InpAdd(t, t, r); |
---|
121 | TS_ASSERT( n_Equal(two, t, r) ); |
---|
122 | n_Delete(&t, r); |
---|
123 | |
---|
124 | if( getCoeffType(r) == n_Q ) |
---|
125 | { |
---|
126 | number t = n_Init(1, r); |
---|
127 | n_InpAdd(t, t, r); |
---|
128 | TS_ASSERT( n_Equal(two, t, r) ); |
---|
129 | n_Delete(&t, r); |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | |
---|
134 | |
---|
135 | const int N = 66666; |
---|
136 | |
---|
137 | number a = n_Init(N, r); |
---|
138 | |
---|
139 | clog<< "a: "; PrintSized(a, r); |
---|
140 | |
---|
141 | |
---|
142 | clog<< "two: "; PrintSized(two, r); |
---|
143 | |
---|
144 | number aa0 = n_Init(N*2, r); |
---|
145 | |
---|
146 | number aa = n_Add(a, a, r); |
---|
147 | |
---|
148 | clog<< "aa = a + a: "; PrintSized(aa, r); |
---|
149 | |
---|
150 | number aa2 = n_Mult(a, two, r); |
---|
151 | |
---|
152 | clog<< "aa2 = a * 2: "; PrintSized(aa2, r); |
---|
153 | |
---|
154 | number aa1 = n_Mult(two, a, r); |
---|
155 | |
---|
156 | clog<< "aa1 = 2 * a: "; PrintSized(aa1, r); |
---|
157 | |
---|
158 | n_Delete(&a, r); |
---|
159 | n_Delete(&two, r); |
---|
160 | |
---|
161 | |
---|
162 | a = n_Sub( aa, aa1, r ); |
---|
163 | |
---|
164 | clog<< "a = aa - aa1: "; PrintSized(a, r); |
---|
165 | |
---|
166 | TS_ASSERT( n_IsZero(a, r) ); |
---|
167 | |
---|
168 | n_Delete(&a, r); |
---|
169 | |
---|
170 | a = n_Sub( aa, aa2, r ); |
---|
171 | |
---|
172 | clog<< "a = aa - aa2: "; PrintSized(a, r); |
---|
173 | |
---|
174 | TS_ASSERT( n_IsZero(a, r) ); |
---|
175 | |
---|
176 | n_Delete(&a, r); |
---|
177 | |
---|
178 | |
---|
179 | a = n_Sub( aa1, aa2, r ); |
---|
180 | |
---|
181 | clog<< "a = aa1 - aa2: "; PrintSized(a, r); |
---|
182 | |
---|
183 | TS_ASSERT( n_IsZero(a, r) ); |
---|
184 | |
---|
185 | n_Delete(&a, r); |
---|
186 | |
---|
187 | |
---|
188 | |
---|
189 | TS_ASSERT( n_Equal(aa, aa1, r) ); |
---|
190 | TS_ASSERT( n_Equal(aa, aa2, r) ); |
---|
191 | TS_ASSERT( n_Equal(aa1, aa2, r) ); |
---|
192 | |
---|
193 | TS_ASSERT( n_Equal(aa0, aa, r) ); |
---|
194 | TS_ASSERT( n_Equal(aa0, aa1, r) ); |
---|
195 | TS_ASSERT( n_Equal(aa0, aa2, r) ); |
---|
196 | |
---|
197 | n_Delete(&aa, r); |
---|
198 | n_Delete(&aa1, r); |
---|
199 | n_Delete(&aa2, r); |
---|
200 | |
---|
201 | n_Delete(&aa0, r); |
---|
202 | |
---|
203 | clog << ( " >>> TEST DONE!" ); |
---|
204 | clog << endl; |
---|
205 | |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | |
---|
210 | |
---|
211 | |
---|
212 | BOOLEAN Test(const n_coeffType type, void* p = NULLp) |
---|
213 | { |
---|
214 | |
---|
215 | clog << endl; |
---|
216 | clog << ( "----------------------- Testing coeffs: [" + _2S(type) + ", " + _2S(p) + "]: -----------------------"); |
---|
217 | clog << endl; |
---|
218 | |
---|
219 | const coeffs r = nInitChar( type, p ); |
---|
220 | |
---|
221 | if( r == NULLp ) |
---|
222 | { |
---|
223 | clog << ( "Test: could not get this coeff. domain" ); |
---|
224 | return FALSE; |
---|
225 | }; |
---|
226 | |
---|
227 | TS_ASSERT_DIFFERS( r->cfCoeffWrite, NULLp ); |
---|
228 | |
---|
229 | if( r->cfCoeffWrite != NULL ) |
---|
230 | { |
---|
231 | clog << "Coeff-domain: " << endl; |
---|
232 | n_CoeffWrite(r); PrintLn(); |
---|
233 | } |
---|
234 | |
---|
235 | if (n_NumberOfParameters(r) > 0) |
---|
236 | { |
---|
237 | number z = n_Param(1, r); // also any integer instead of 0//? |
---|
238 | PrintS("Parameter: "); PrintSized(z, r); |
---|
239 | n_Delete(&z, r); |
---|
240 | } |
---|
241 | |
---|
242 | |
---|
243 | clog << "Char: " << n_GetChar(r) << endl; |
---|
244 | |
---|
245 | |
---|
246 | TS_ASSERT_DIFFERS( r, NULLp ); |
---|
247 | nSetChar( r ); |
---|
248 | TS_ASSERT_EQUALS( getCoeffType(r), type ); |
---|
249 | |
---|
250 | TS_ASSERT_DIFFERS( r->cfInit, NULLp ); |
---|
251 | TS_ASSERT_DIFFERS( r->cfWriteLong, NULLp ); |
---|
252 | TS_ASSERT_DIFFERS( r->cfAdd, NULLp ); |
---|
253 | TS_ASSERT_DIFFERS( r->cfDelete, NULLp ); |
---|
254 | |
---|
255 | switch( type ) |
---|
256 | { |
---|
257 | case n_Q: |
---|
258 | { |
---|
259 | //TS_ASSERT_EQUALS( r->cfInit, nlInit ); |
---|
260 | //TS_ASSERT_EQUALS( r->cfAdd, nlAdd ); |
---|
261 | //TS_ASSERT_EQUALS( r->cfDelete, nlDelete ); |
---|
262 | |
---|
263 | TS_ASSERT( nCoeff_is_Q( r )); |
---|
264 | TS_ASSERT( nCoeff_is_Domain( r )); |
---|
265 | |
---|
266 | TS_ASSERT( !nCoeff_has_Units( r )); // ? |
---|
267 | TS_ASSERT( !nCoeff_has_simple_inverse( r ));// ? |
---|
268 | TS_ASSERT( !nCoeff_has_simple_Alloc( r )); // ? |
---|
269 | |
---|
270 | TS_ASSERT( !nCoeff_is_Ring_2toM( r )); |
---|
271 | TS_ASSERT( !nCoeff_is_Zn( r )); |
---|
272 | TS_ASSERT( !nCoeff_is_Ring_PtoM( r )); |
---|
273 | TS_ASSERT( !nCoeff_is_Z( r )); |
---|
274 | TS_ASSERT( !nCoeff_is_Ring( r )); |
---|
275 | TS_ASSERT( !nCoeff_is_Zp( r )); |
---|
276 | TS_ASSERT( !nCoeff_is_numeric( r )); |
---|
277 | TS_ASSERT( !nCoeff_is_R( r )); |
---|
278 | TS_ASSERT( !nCoeff_is_GF( r )); |
---|
279 | TS_ASSERT( !nCoeff_is_long_R( r )); |
---|
280 | TS_ASSERT( !nCoeff_is_long_C( r )); |
---|
281 | TS_ASSERT( !nCoeff_is_CF( r )); |
---|
282 | TS_ASSERT( !nCoeff_is_Extension( r )); |
---|
283 | |
---|
284 | break; |
---|
285 | } |
---|
286 | case n_long_R: |
---|
287 | { |
---|
288 | //TS_ASSERT_EQUALS( r->cfInit, ngfInit ); |
---|
289 | //TS_ASSERT_EQUALS( r->cfAdd, ngfAdd ); |
---|
290 | //TS_ASSERT_EQUALS( r->cfDelete, ngfDelete ); |
---|
291 | break; |
---|
292 | } |
---|
293 | case n_long_C: |
---|
294 | { |
---|
295 | // TS_ASSERT_EQUALS( r->cfInit, ngcInit ); |
---|
296 | // TS_ASSERT_EQUALS( r->cfAdd, ngcAdd ); |
---|
297 | // TS_ASSERT_EQUALS( r->cfDelete, ngcDelete ); |
---|
298 | break; |
---|
299 | } |
---|
300 | case n_R: |
---|
301 | { |
---|
302 | //TS_ASSERT_EQUALS( r->cfInit, nrInit ); |
---|
303 | //TS_ASSERT_EQUALS( r->cfAdd, nrAdd ); |
---|
304 | // TS_ASSERT_EQUALS( r->cfDelete, nrDelete ); // No? |
---|
305 | break; |
---|
306 | } |
---|
307 | case n_GF: |
---|
308 | { |
---|
309 | // TS_ASSERT_EQUALS( r->cfInit, nfInit ); |
---|
310 | // TS_ASSERT_EQUALS( r->cfAdd, nfAdd ); |
---|
311 | //TS_ASSERT_EQUALS( r->cfDelete, nfDelete ); |
---|
312 | break; |
---|
313 | } |
---|
314 | #ifdef HAVE_RINGS |
---|
315 | case n_Z2m: |
---|
316 | { |
---|
317 | //TS_ASSERT_EQUALS( r->cfInit, nr2mInit ); |
---|
318 | //TS_ASSERT_EQUALS( r->cfAdd, nr2mAdd ); |
---|
319 | //TS_ASSERT_EQUALS( r->cfDelete, ndDelete ); |
---|
320 | break; |
---|
321 | } |
---|
322 | case n_Zn: |
---|
323 | { |
---|
324 | //TS_ASSERT_EQUALS( r->cfInit, nrnInit ); |
---|
325 | //TS_ASSERT_EQUALS( r->cfAdd, nrnAdd ); |
---|
326 | //TS_ASSERT_EQUALS( r->cfDelete, nrnDelete ); |
---|
327 | break; |
---|
328 | } |
---|
329 | #endif |
---|
330 | default: |
---|
331 | { |
---|
332 | // ... |
---|
333 | } |
---|
334 | } |
---|
335 | |
---|
336 | TestArith( r ); |
---|
337 | TestSum( r, 10 ); |
---|
338 | TestSum( r, 100 ); |
---|
339 | TestSum( r, 101 ); |
---|
340 | TestSum( r, 1001 ); |
---|
341 | TestSum( r, 9000 ); |
---|
342 | |
---|
343 | nKillChar( r ); |
---|
344 | |
---|
345 | return TRUE; |
---|
346 | } |
---|
347 | |
---|
348 | |
---|
349 | |
---|
350 | // We can rely on this file being included exactly once |
---|
351 | // and declare this global variable in the header file. |
---|
352 | // |
---|
353 | static GlobalPrintingFixture globalPrintingFixture; |
---|
354 | |
---|
355 | |
---|
356 | class CoeffsTestSuite : public CxxTest::TestSuite |
---|
357 | { |
---|
358 | public: |
---|
359 | // void test_dummy() { float fnum = 2.00001f; TS_ASSERT_DELTA (fnum, 2.0f, 0.0001f); } |
---|
360 | |
---|
361 | void test_Z2m4() |
---|
362 | { |
---|
363 | #ifdef HAVE_RINGS |
---|
364 | n_coeffType type = n_Z2m; |
---|
365 | TS_ASSERT( Test(type, (void*) 4) ); |
---|
366 | #endif |
---|
367 | } |
---|
368 | |
---|
369 | void test_Zp101() |
---|
370 | { |
---|
371 | n_coeffType type = n_Zp; |
---|
372 | TS_ASSERT( Test(type, (void*) 101) ); |
---|
373 | } |
---|
374 | |
---|
375 | void test_Z2m8() |
---|
376 | { |
---|
377 | #ifdef HAVE_RINGS |
---|
378 | n_coeffType type = n_Z2m; |
---|
379 | TS_ASSERT( Test(type, (void*) 8) ); |
---|
380 | #endif |
---|
381 | } |
---|
382 | |
---|
383 | void simple(const n_coeffType _type) |
---|
384 | { |
---|
385 | n_coeffType type = _type; |
---|
386 | TS_ASSERT( type == _type ); // ? |
---|
387 | TS_ASSERT( Test(type) ); |
---|
388 | } |
---|
389 | |
---|
390 | void test_Q() |
---|
391 | { |
---|
392 | simple(n_Q); |
---|
393 | } |
---|
394 | |
---|
395 | void test_R() |
---|
396 | { |
---|
397 | simple(n_R); |
---|
398 | } |
---|
399 | |
---|
400 | |
---|
401 | void test_Z() |
---|
402 | { |
---|
403 | #ifdef HAVE_RINGS |
---|
404 | simple(n_Z); // No need in GMP? |
---|
405 | #endif |
---|
406 | } |
---|
407 | |
---|
408 | |
---|
409 | void test_GF_toobig() |
---|
410 | { |
---|
411 | n_coeffType type = n_GF; |
---|
412 | |
---|
413 | GFInfo param; |
---|
414 | |
---|
415 | param.GFChar= 5; |
---|
416 | param.GFDegree= 12; |
---|
417 | param.GFPar_name= (const char*)"q"; |
---|
418 | |
---|
419 | TS_ASSERT( !Test(type, (void*) ¶m) ); |
---|
420 | |
---|
421 | // it should not be used by numbers... right? |
---|
422 | // TODO: what is our policy wrt param-pointer-ownership? |
---|
423 | } |
---|
424 | |
---|
425 | void test_GF() |
---|
426 | { |
---|
427 | // TODO: what if it was already registered? |
---|
428 | // Q: no way to deRegister a type? |
---|
429 | n_coeffType type = n_GF; |
---|
430 | |
---|
431 | GFInfo param; |
---|
432 | |
---|
433 | param.GFChar= 5; |
---|
434 | param.GFDegree= 2; |
---|
435 | param.GFPar_name= (const char*)"Q"; |
---|
436 | |
---|
437 | TS_ASSERT( Test(type, (void*) ¶m) ); |
---|
438 | |
---|
439 | // it should not be used by numbers... right? |
---|
440 | // TODO: what is our policy wrt param-pointer-ownership? |
---|
441 | } |
---|
442 | |
---|
443 | |
---|
444 | void test_Zn3() |
---|
445 | { |
---|
446 | #ifdef HAVE_RINGS |
---|
447 | n_coeffType type = n_Zn; |
---|
448 | |
---|
449 | ZnmInfo Znmparam; |
---|
450 | Znmparam.base= (mpz_ptr) omAlloc (sizeof (mpz_t)); |
---|
451 | mpz_init_set_ui (Znmparam.base, 3); |
---|
452 | Znmparam.exp= 1; |
---|
453 | |
---|
454 | TS_ASSERT( Test(type, (void*) &Znmparam) ); |
---|
455 | #endif |
---|
456 | } |
---|
457 | |
---|
458 | void test_Z2m2() |
---|
459 | { |
---|
460 | #ifdef HAVE_RINGS |
---|
461 | n_coeffType type = n_Z2m; |
---|
462 | |
---|
463 | TS_ASSERT( Test(type, (void*) 2) ); |
---|
464 | #endif |
---|
465 | } |
---|
466 | |
---|
467 | void test_LR() |
---|
468 | { |
---|
469 | simple(n_long_R); |
---|
470 | } |
---|
471 | |
---|
472 | void test_LC() |
---|
473 | { |
---|
474 | simple(n_long_C); |
---|
475 | } |
---|
476 | |
---|
477 | void test_Q_special() |
---|
478 | { |
---|
479 | const coeffs cf = nInitChar(n_Q, NULLp); |
---|
480 | |
---|
481 | if (cf == NULLp) |
---|
482 | clog << ( "Test: could not get this coeff. domain" ); |
---|
483 | |
---|
484 | TS_ASSERT_DIFFERS(cf->cfCoeffWrite, NULLp); |
---|
485 | |
---|
486 | if (cf->cfCoeffWrite != NULL ) |
---|
487 | { |
---|
488 | clog << "Coeff-domain: " << endl; |
---|
489 | n_CoeffWrite(cf); PrintLn(); |
---|
490 | } |
---|
491 | |
---|
492 | number q1 = n_Init(21, cf); |
---|
493 | number q2 = n_Init(2, cf); |
---|
494 | number q3 = n_Div(q1, q2, cf); |
---|
495 | number q4 = n_Init(30, cf); |
---|
496 | number q5 = n_Mult(q3, q4, cf); |
---|
497 | TS_ASSERT(n_Test(q5, cf)); |
---|
498 | Print("21/2 * 30 = %ld\n", n_Int(q5, cf)); |
---|
499 | TS_ASSERT(n_Test(q5, cf)); |
---|
500 | n_Delete(&q1, cf); |
---|
501 | n_Delete(&q2, cf); |
---|
502 | n_Delete(&q3, cf); |
---|
503 | n_Delete(&q4, cf); |
---|
504 | n_Delete(&q5, cf); |
---|
505 | } |
---|
506 | }; |
---|
507 | |
---|