source: git/libpolys/tests/coeffs_test.h @ ae85a3

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