source: git/libpolys/tests/coeffs_test.h @ 73a9ffb

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