1 | |
---|
2 | #include "config.h" |
---|
3 | #include <misc/auxiliary.h> |
---|
4 | #include <omalloc/omalloc.h> |
---|
5 | |
---|
6 | #include <reporter/reporter.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 | namespace |
---|
29 | { |
---|
30 | |
---|
31 | void TestSum(const coeffs r, const unsigned long N) |
---|
32 | { |
---|
33 | clog << ( _2S("TEST: sum[0..") + _2S(N) + "]: "); |
---|
34 | clog << endl; |
---|
35 | |
---|
36 | assume( N > 0 ); // just for now... |
---|
37 | |
---|
38 | const unsigned long ssss = (N * (N+1)) / 2; |
---|
39 | |
---|
40 | number sum1 = n_Init(ssss, r); |
---|
41 | clog<< "N*(N+1)/2 (int: " << ssss << "): "; PrintSized(sum1, r); |
---|
42 | |
---|
43 | number s, ss, i, res; |
---|
44 | |
---|
45 | s = n_Init(N , r); |
---|
46 | i = n_Init(N+1, r); |
---|
47 | ndInpMult(s, i, r); |
---|
48 | n_Delete(&i, r); |
---|
49 | |
---|
50 | clog<< "N*(N+1): ("<< N*(N+1) << ")"; PrintSized(s, r); |
---|
51 | |
---|
52 | i = n_Init(2, r); |
---|
53 | clog<< "2: "; PrintSized(i, r); |
---|
54 | |
---|
55 | if( !n_IsZero( i, r) ) |
---|
56 | { |
---|
57 | TS_ASSERT( n_DivBy(s, i, r) ); |
---|
58 | res = n_Div(s, i, r); |
---|
59 | |
---|
60 | clog<< "N*(N+1)/2: "; PrintSized(res, r); |
---|
61 | number d = n_Sub(res, sum1, r); |
---|
62 | |
---|
63 | TS_ASSERT( ndIsZeroDivisor(d, r) ); |
---|
64 | |
---|
65 | if( n_GetChar(r) == 0 ) |
---|
66 | { |
---|
67 | TS_ASSERT( n_Equal(sum1, res, r) ); |
---|
68 | TS_ASSERT( n_Equal(res, sum1, r) ); |
---|
69 | } |
---|
70 | } else |
---|
71 | TS_ASSERT_EQUALS( n_GetChar(r), 2); |
---|
72 | |
---|
73 | |
---|
74 | n_Delete(&s, r); n_Delete(&i, r); |
---|
75 | |
---|
76 | n_Delete(&sum1, r); n_Delete(&res, r); |
---|
77 | |
---|
78 | |
---|
79 | s = n_Init(0 , r); |
---|
80 | ss = n_Init(0 , r); |
---|
81 | for( int k = N; k >= 0; k-- ) |
---|
82 | { |
---|
83 | i = n_Init(k, r); |
---|
84 | ndInpAdd(s, i, r); // s += i |
---|
85 | |
---|
86 | i = n_Neg(i, r); |
---|
87 | ndInpAdd(ss, i, r); // ss -= i |
---|
88 | |
---|
89 | n_Delete(&i, r); |
---|
90 | } |
---|
91 | |
---|
92 | ss = n_Neg(ss, r); // ss = -ss |
---|
93 | |
---|
94 | clog<< "real sum : "; PrintSized(s, r); |
---|
95 | clog<< "real sum(--): "; PrintSized(ss, r); |
---|
96 | |
---|
97 | TS_ASSERT( n_Equal(s, ss, r) ); |
---|
98 | TS_ASSERT( n_Equal(ss, s, r) ); |
---|
99 | |
---|
100 | n_Delete(&s, r); |
---|
101 | n_Delete(&ss, r); |
---|
102 | |
---|
103 | clog << ( " >>> TEST DONE!" ); |
---|
104 | clog << endl; |
---|
105 | |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | void TestArith(const coeffs r) |
---|
110 | { |
---|
111 | clog << ("TEST: Simple Arithmetics: "); |
---|
112 | clog << endl; |
---|
113 | |
---|
114 | number a = n_Init(66666, r); |
---|
115 | |
---|
116 | clog<< "a: "; PrintSized(a, r); |
---|
117 | |
---|
118 | number two = n_Init(2, r); |
---|
119 | |
---|
120 | clog<< "two: "; PrintSized(two, r); |
---|
121 | |
---|
122 | number aa = n_Add(a, a, r); |
---|
123 | |
---|
124 | clog<< "aa = a + a: "; PrintSized(aa, r); |
---|
125 | |
---|
126 | number aa2 = n_Mult(a, two, r); |
---|
127 | |
---|
128 | clog<< "aa2 = a * 2: "; PrintSized(aa2, r); |
---|
129 | |
---|
130 | number aa1 = n_Mult(two, a, r); |
---|
131 | |
---|
132 | clog<< "aa1 = 2 * a: "; PrintSized(aa1, r); |
---|
133 | |
---|
134 | n_Delete(&a, r); |
---|
135 | n_Delete(&two, r); |
---|
136 | |
---|
137 | |
---|
138 | a = n_Sub( aa, aa1, r ); |
---|
139 | |
---|
140 | clog<< "a = aa - aa1: "; PrintSized(a, r); |
---|
141 | |
---|
142 | TS_ASSERT( n_IsZero(a, r) ); |
---|
143 | |
---|
144 | n_Delete(&a, r); |
---|
145 | |
---|
146 | a = n_Sub( aa, aa2, r ); |
---|
147 | |
---|
148 | clog<< "a = aa - aa2: "; PrintSized(a, r); |
---|
149 | |
---|
150 | TS_ASSERT( n_IsZero(a, r) ); |
---|
151 | |
---|
152 | n_Delete(&a, r); |
---|
153 | |
---|
154 | |
---|
155 | a = n_Sub( aa1, aa2, r ); |
---|
156 | |
---|
157 | clog<< "a = aa1 - aa2: "; PrintSized(a, r); |
---|
158 | |
---|
159 | TS_ASSERT( n_IsZero(a, r) ); |
---|
160 | |
---|
161 | n_Delete(&a, r); |
---|
162 | |
---|
163 | |
---|
164 | |
---|
165 | TS_ASSERT( n_Equal(aa, aa1, r) ); |
---|
166 | |
---|
167 | TS_ASSERT( n_Equal(aa, aa2, r) ); |
---|
168 | |
---|
169 | TS_ASSERT( n_Equal(aa1, aa2, r) ); |
---|
170 | |
---|
171 | |
---|
172 | n_Delete(&aa, r); |
---|
173 | n_Delete(&aa1, r); |
---|
174 | n_Delete(&aa2, r); |
---|
175 | |
---|
176 | |
---|
177 | clog << ( " >>> TEST DONE!" ); |
---|
178 | clog << endl; |
---|
179 | |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | BOOLEAN Test(const n_coeffType type, void* p = NULLp) |
---|
187 | { |
---|
188 | |
---|
189 | clog << endl; |
---|
190 | clog << ( "----------------------- Testing coeffs: [" + _2S(type) + ", " + _2S(p) + "]: -----------------------"); |
---|
191 | clog << endl; |
---|
192 | |
---|
193 | const coeffs r = nInitChar( type, p ); |
---|
194 | |
---|
195 | if( r == NULLp ) |
---|
196 | { |
---|
197 | clog << ( "Test: could not get this coeff. domain" ); |
---|
198 | return false; |
---|
199 | }; |
---|
200 | |
---|
201 | if (getCoeffType(r) == n_GF) //some special test for GF |
---|
202 | { |
---|
203 | number z = nfPar (0, r); // also any integer instead of 0//? |
---|
204 | clog << "Generator: "; PrintSized(z, r); |
---|
205 | n_Delete(&z, r); |
---|
206 | } |
---|
207 | |
---|
208 | clog << "Char: " << n_GetChar(r) << endl; |
---|
209 | |
---|
210 | |
---|
211 | TS_ASSERT_DIFFERS( r, NULLp ); |
---|
212 | nSetChar( r ); |
---|
213 | TS_ASSERT_EQUALS( getCoeffType(r), type ); |
---|
214 | |
---|
215 | TS_ASSERT_DIFFERS( r->cfInit, NULLp ); |
---|
216 | TS_ASSERT_DIFFERS( r->cfWrite, NULLp ); |
---|
217 | TS_ASSERT_DIFFERS( r->cfAdd, NULLp ); |
---|
218 | TS_ASSERT_DIFFERS( r->cfDelete, NULLp ); |
---|
219 | |
---|
220 | switch( type ) |
---|
221 | { |
---|
222 | case n_Q: |
---|
223 | { |
---|
224 | TS_ASSERT_EQUALS( r->cfInit, nlInit ); |
---|
225 | TS_ASSERT_EQUALS( r->cfWrite, nlWrite ); |
---|
226 | TS_ASSERT_EQUALS( r->cfAdd, nlAdd ); |
---|
227 | TS_ASSERT_EQUALS( r->cfDelete, nlDelete ); |
---|
228 | |
---|
229 | TS_ASSERT( nCoeff_is_Q( r )); |
---|
230 | TS_ASSERT( nCoeff_is_Domain( r )); |
---|
231 | |
---|
232 | TS_ASSERT( !nCoeff_has_Units( r )); // ? |
---|
233 | TS_ASSERT( !nCoeff_has_simple_inverse( r ));// ? |
---|
234 | TS_ASSERT( !nCoeff_has_simple_Alloc( r )); // ? |
---|
235 | |
---|
236 | TS_ASSERT( !nCoeff_is_Ring_2toM( r )); |
---|
237 | TS_ASSERT( !nCoeff_is_Ring_ModN( r )); |
---|
238 | TS_ASSERT( !nCoeff_is_Ring_PtoM( r )); |
---|
239 | TS_ASSERT( !nCoeff_is_Ring_Z( r )); |
---|
240 | TS_ASSERT( !nCoeff_is_Ring( r )); |
---|
241 | TS_ASSERT( !nCoeff_is_Zp( r )); |
---|
242 | TS_ASSERT( !nCoeff_is_numeric( r )); |
---|
243 | TS_ASSERT( !nCoeff_is_R( r )); |
---|
244 | TS_ASSERT( !nCoeff_is_Q_a( r )); |
---|
245 | TS_ASSERT( !nCoeff_is_Zp_a( r )); |
---|
246 | TS_ASSERT( !nCoeff_is_GF( r )); |
---|
247 | TS_ASSERT( !nCoeff_is_long_R( r )); |
---|
248 | TS_ASSERT( !nCoeff_is_long_C( r )); |
---|
249 | TS_ASSERT( !nCoeff_is_CF( r )); |
---|
250 | TS_ASSERT( !nCoeff_is_Extension( r )); |
---|
251 | |
---|
252 | break; |
---|
253 | } |
---|
254 | case n_long_R: |
---|
255 | { |
---|
256 | TS_ASSERT_EQUALS( r->cfInit, ngfInit ); |
---|
257 | TS_ASSERT_EQUALS( r->cfWrite, ngfWrite ); |
---|
258 | TS_ASSERT_EQUALS( r->cfAdd, ngfAdd ); |
---|
259 | TS_ASSERT_EQUALS( r->cfDelete, ngfDelete ); |
---|
260 | break; |
---|
261 | } |
---|
262 | case n_long_C: |
---|
263 | { |
---|
264 | TS_ASSERT_EQUALS( r->cfInit, ngcInit ); |
---|
265 | TS_ASSERT_EQUALS( r->cfWrite, ngcWrite ); |
---|
266 | TS_ASSERT_EQUALS( r->cfAdd, ngcAdd ); |
---|
267 | TS_ASSERT_EQUALS( r->cfDelete, ngcDelete ); |
---|
268 | break; |
---|
269 | } |
---|
270 | case n_R: |
---|
271 | { |
---|
272 | TS_ASSERT_EQUALS( r->cfInit, nrInit ); |
---|
273 | TS_ASSERT_EQUALS( r->cfWrite, nrWrite ); |
---|
274 | TS_ASSERT_EQUALS( r->cfAdd, nrAdd ); |
---|
275 | // TS_ASSERT_EQUALS( r->cfDelete, nrDelete ); // No? |
---|
276 | break; |
---|
277 | } |
---|
278 | case n_GF: |
---|
279 | { |
---|
280 | TS_ASSERT_EQUALS( r->cfInit, nfInit ); |
---|
281 | TS_ASSERT_EQUALS( r->cfWrite, nfWrite ); |
---|
282 | TS_ASSERT_EQUALS( r->cfAdd, nfAdd ); |
---|
283 | //TS_ASSERT_EQUALS( r->cfDelete, nfDelete ); |
---|
284 | break; |
---|
285 | } |
---|
286 | #ifdef HAVE_RINGS |
---|
287 | case n_Z2m: |
---|
288 | { |
---|
289 | TS_ASSERT_EQUALS( r->cfInit, nr2mInit ); |
---|
290 | TS_ASSERT_EQUALS( r->cfWrite, nr2mWrite ); |
---|
291 | TS_ASSERT_EQUALS( r->cfAdd, nr2mAdd ); |
---|
292 | TS_ASSERT_EQUALS( r->cfDelete, ndDelete ); |
---|
293 | break; |
---|
294 | } |
---|
295 | case n_Zn: |
---|
296 | { |
---|
297 | TS_ASSERT_EQUALS( r->cfInit, nrnInit ); |
---|
298 | TS_ASSERT_EQUALS( r->cfWrite, nrnWrite ); |
---|
299 | TS_ASSERT_EQUALS( r->cfAdd, nrnAdd ); |
---|
300 | TS_ASSERT_EQUALS( r->cfDelete, nrnDelete ); |
---|
301 | break; |
---|
302 | } |
---|
303 | #endif |
---|
304 | default: |
---|
305 | { |
---|
306 | // ... |
---|
307 | } |
---|
308 | } |
---|
309 | |
---|
310 | TestArith( r ); |
---|
311 | TestSum( r, 10 ); |
---|
312 | TestSum( r, 100 ); |
---|
313 | TestSum( r, 101 ); |
---|
314 | TestSum( r, 1001 ); |
---|
315 | TestSum( r, 9000 ); |
---|
316 | |
---|
317 | nKillChar( r ); |
---|
318 | |
---|
319 | return TRUE; |
---|
320 | } |
---|
321 | |
---|
322 | |
---|
323 | } |
---|
324 | |
---|
325 | |
---|
326 | class CoeffsTestSuite : public CxxTest::TestSuite |
---|
327 | { |
---|
328 | public: |
---|
329 | // void test_dummy() { float fnum = 2.00001f; TS_ASSERT_DELTA (fnum, 2.0f, 0.0001f); } |
---|
330 | |
---|
331 | void test_Z2m4() |
---|
332 | { |
---|
333 | #ifdef HAVE_RINGS |
---|
334 | n_coeffType type = nRegister( n_Z2m, nr2mInitChar); TS_ASSERT( type == n_Z2m ); |
---|
335 | TS_ASSERT( Test(type, (void*) 4) ); |
---|
336 | #endif |
---|
337 | } |
---|
338 | |
---|
339 | void test_Zp101() |
---|
340 | { |
---|
341 | n_coeffType type = nRegister( n_Zp, npInitChar); TS_ASSERT( type == n_Zp ); |
---|
342 | TS_ASSERT( Test(type, (void*) 101) ); |
---|
343 | } |
---|
344 | |
---|
345 | void test_Z2m8() |
---|
346 | { |
---|
347 | #ifdef HAVE_RINGS |
---|
348 | n_coeffType type = nRegister( n_Z2m, nr2mInitChar); TS_ASSERT( type == n_Z2m ); |
---|
349 | TS_ASSERT( Test(type, (void*) 8) ); |
---|
350 | #endif |
---|
351 | } |
---|
352 | |
---|
353 | void simple(const n_coeffType _type, cfInitCharProc p) |
---|
354 | { |
---|
355 | n_coeffType type = nRegister( _type, p); |
---|
356 | TS_ASSERT( type == _type ); // ? |
---|
357 | TS_ASSERT( Test(type) ); |
---|
358 | } |
---|
359 | |
---|
360 | void test_Q() |
---|
361 | { |
---|
362 | simple(n_Q, nlInitChar); |
---|
363 | } |
---|
364 | |
---|
365 | void test_R() |
---|
366 | { |
---|
367 | simple(n_R, nrInitChar); |
---|
368 | } |
---|
369 | |
---|
370 | |
---|
371 | void test_Z() |
---|
372 | { |
---|
373 | #ifdef HAVE_RINGS |
---|
374 | simple(n_Z, nrzInitChar); // No need in GMP? |
---|
375 | #endif |
---|
376 | } |
---|
377 | |
---|
378 | |
---|
379 | void test_GF_toobig() |
---|
380 | { |
---|
381 | n_coeffType type = nRegister( n_GF, nfInitChar); |
---|
382 | TS_ASSERT( type == n_GF ); |
---|
383 | |
---|
384 | GFInfo param; |
---|
385 | |
---|
386 | param.GFChar= 5; |
---|
387 | param.GFDegree= 12; |
---|
388 | param.GFPar_name= (const char*)"q"; |
---|
389 | |
---|
390 | TS_ASSERT( !Test(type, (void*) ¶m) ); |
---|
391 | |
---|
392 | // it should not be used by numbers... right? |
---|
393 | // TODO: what is our policy wrt param-pointer-ownership? |
---|
394 | } |
---|
395 | |
---|
396 | void test_GF() |
---|
397 | { |
---|
398 | // TODO: what if it was already registered? |
---|
399 | // Q: no way to deRegister a type? |
---|
400 | n_coeffType type = nRegister( n_GF, nfInitChar); |
---|
401 | TS_ASSERT( type == n_GF ); |
---|
402 | |
---|
403 | GFInfo param; |
---|
404 | |
---|
405 | param.GFChar= 5; |
---|
406 | param.GFDegree= 2; |
---|
407 | param.GFPar_name= (const char*)"Q"; |
---|
408 | |
---|
409 | TS_ASSERT( Test(type, (void*) ¶m) ); |
---|
410 | |
---|
411 | // it should not be used by numbers... right? |
---|
412 | // TODO: what is our policy wrt param-pointer-ownership? |
---|
413 | } |
---|
414 | |
---|
415 | |
---|
416 | void test_Zn3() |
---|
417 | { |
---|
418 | #ifdef HAVE_RINGS |
---|
419 | // TODO(Somebody, This will result in memory corruption at Z_2^m later on (due to the succs. setGMPFloatDigits?)...!?); // ???? |
---|
420 | n_coeffType type = nRegister( n_Zn, nrnInitChar); TS_ASSERT( type == n_Zn ); |
---|
421 | |
---|
422 | TS_ASSERT( Test(type, (void*) 3) ); |
---|
423 | #endif |
---|
424 | } |
---|
425 | |
---|
426 | void test_Z2m2() |
---|
427 | { |
---|
428 | #ifdef HAVE_RINGS |
---|
429 | n_coeffType type = nRegister( n_Z2m, nr2mInitChar); TS_ASSERT( type == n_Z2m ); |
---|
430 | |
---|
431 | TS_ASSERT( Test(type, (void*) 2) ); |
---|
432 | #endif |
---|
433 | } |
---|
434 | |
---|
435 | TODO(Somebody, floating arithmetics via GMP rely on two global variables (see setGMPFloatDigits). Please fix it!); |
---|
436 | |
---|
437 | void test_LR() |
---|
438 | { |
---|
439 | 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?)!???? |
---|
440 | simple(n_long_R, ngfInitChar); |
---|
441 | } |
---|
442 | |
---|
443 | void test_LC() |
---|
444 | { |
---|
445 | 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?)!???? |
---|
446 | simple(n_long_C, ngcInitChar); |
---|
447 | } |
---|
448 | |
---|
449 | |
---|
450 | // polynomial rings needed for extentions: n_Zp_a, n_Q_a! |
---|
451 | |
---|
452 | }; |
---|
453 | |
---|