source: git/libpolys/tests/coeffs_test.h @ 6c5f32

spielwiese
Last change on this file since 6c5f32 was 6c5f32, checked in by Oleksandr Motsak <motsak@…>, 13 years ago
FIX: n_DivBy exists for rings only... FIX: p_Div_nn should always exist!
  • Property mode set to 100644
File size: 10.1 KB
RevLine 
[1820e7]1#include "config.h"
2#include <misc/auxiliary.h>
3#include <omalloc/omalloc.h>
4
[b27c052]5
[1820e7]6#include <reporter/reporter.h>
[ae85a3]7#include <resources/feResource.h>
[1820e7]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
[4f684a]25#include "common.h"
[1820e7]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);
[e5422d]39  clog<< "N*(N+1)/2 (int: " << ssss << "): "; PrintSized(sum1, r);
[1820e7]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 
[e5422d]48  clog<< "N*(N+1): ("<< N*(N+1) << ")"; PrintSized(s, r); 
49 
[1820e7]50  i = n_Init(2, r);
[e5422d]51  clog<< "2: "; PrintSized(i, r); 
[1820e7]52
53  if( !n_IsZero( i, r) )
54  {
[6c5f32]55#ifdef HAVE_RINGS
[1820e7]56    TS_ASSERT( n_DivBy(s, i, r) );
[6c5f32]57#endif
58     
[1820e7]59    res = n_Div(s, i, r);
60 
[e5422d]61    clog<< "N*(N+1)/2: "; PrintSized(res, r);
[b27c052]62
63
[e5422d]64    number d = n_Sub(res, sum1, r);
[6c084af]65    TS_ASSERT( n_IsZeroDivisor(d, r) );
[b27c052]66    n_Delete(&d, r);
[e5422d]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    }
[1820e7]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  }
[ae85a3]94  clog<< "ss: "; PrintSized(ss, r); 
[1820e7]95
96  ss = n_Neg(ss, r); // ss = -ss
[e5422d]97 
98  clog<< "real sum    : "; PrintSized(s, r);
99  clog<< "real sum(--): "; PrintSized(ss, r); 
100
[1820e7]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);   
[e5422d]106
107  clog << ( " >>> TEST DONE!" );
108  clog << endl;
109
[1820e7]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);
[e5422d]179
180
181  clog << ( " >>> TEST DONE!" );
182  clog << endl;
183
[1820e7]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" );
[ae85a3]202    return FALSE;
[1820e7]203  };
204
[ae85a3]205  TS_ASSERT_DIFFERS( r->cfCoeffWrite, NULLp );
206
207  if( r->cfCoeffWrite != NULL )
208  {
[8480db]209    clog << "Coeff-domain: "  << endl; 
[ae85a3]210    n_CoeffWrite(r); PrintLn();
211  }
212
213
[1820e7]214  if (getCoeffType(r) == n_GF) //some special test for GF
215  {
216    number z = nfPar (0, r); // also any integer instead of 0//?
217    clog << "Generator: "; PrintSized(z, r);
218    n_Delete(&z, r);   
219  }
220
221  clog << "Char: " << n_GetChar(r) << endl; 
222
223 
224  TS_ASSERT_DIFFERS( r, NULLp );
225  nSetChar( r );
226  TS_ASSERT_EQUALS( getCoeffType(r), type );
227
228  TS_ASSERT_DIFFERS( r->cfInit, NULLp );
229  TS_ASSERT_DIFFERS( r->cfWrite, NULLp );
230  TS_ASSERT_DIFFERS( r->cfAdd, NULLp );
231  TS_ASSERT_DIFFERS( r->cfDelete, NULLp );
232
233  switch( type )
234  {
235    case n_Q:
236    {
237      TS_ASSERT_EQUALS( r->cfInit, nlInit );
238      TS_ASSERT_EQUALS( r->cfWrite, nlWrite );
239      TS_ASSERT_EQUALS( r->cfAdd, nlAdd );
240      TS_ASSERT_EQUALS( r->cfDelete, nlDelete );
241     
242      TS_ASSERT(  nCoeff_is_Q( r ));
243      TS_ASSERT(  nCoeff_is_Domain( r ));
[e5422d]244
245      TS_ASSERT( !nCoeff_has_Units( r )); // ?
246      TS_ASSERT( !nCoeff_has_simple_inverse( r ));// ?
247      TS_ASSERT( !nCoeff_has_simple_Alloc( r )); // ?
[1820e7]248
249      TS_ASSERT( !nCoeff_is_Ring_2toM( r ));
250      TS_ASSERT( !nCoeff_is_Ring_ModN( r ));
251      TS_ASSERT( !nCoeff_is_Ring_PtoM( r ));
252      TS_ASSERT( !nCoeff_is_Ring_Z( r ));
253      TS_ASSERT( !nCoeff_is_Ring( r ));
254      TS_ASSERT( !nCoeff_is_Zp( r ));
255      TS_ASSERT( !nCoeff_is_numeric( r ));
256      TS_ASSERT( !nCoeff_is_R( r ));
257      TS_ASSERT( !nCoeff_is_Q_a( r ));
258      TS_ASSERT( !nCoeff_is_Zp_a( r ));
259      TS_ASSERT( !nCoeff_is_GF( r ));
260      TS_ASSERT( !nCoeff_is_long_R( r ));
261      TS_ASSERT( !nCoeff_is_long_C( r ));
262      TS_ASSERT( !nCoeff_is_CF( r ));
263      TS_ASSERT( !nCoeff_is_Extension( r ));
264     
265      break;
266    }
267    case n_long_R:
268    {
269      TS_ASSERT_EQUALS( r->cfInit, ngfInit );
270      TS_ASSERT_EQUALS( r->cfWrite, ngfWrite );
271      TS_ASSERT_EQUALS( r->cfAdd, ngfAdd );
272      TS_ASSERT_EQUALS( r->cfDelete, ngfDelete );
273      break;
274    }
275    case n_long_C:
276    {
277      TS_ASSERT_EQUALS( r->cfInit, ngcInit );
278      TS_ASSERT_EQUALS( r->cfWrite, ngcWrite );
279      TS_ASSERT_EQUALS( r->cfAdd, ngcAdd );
280      TS_ASSERT_EQUALS( r->cfDelete, ngcDelete );   
281      break;
282    }
283    case n_R:
284    {
285      TS_ASSERT_EQUALS( r->cfInit, nrInit );
286      TS_ASSERT_EQUALS( r->cfWrite, nrWrite );
287      TS_ASSERT_EQUALS( r->cfAdd, nrAdd );
288  //    TS_ASSERT_EQUALS( r->cfDelete, nrDelete ); // No?
289      break;
290    }
291    case n_GF:
292    {
293      TS_ASSERT_EQUALS( r->cfInit, nfInit );
294      TS_ASSERT_EQUALS( r->cfWrite, nfWrite );
295      TS_ASSERT_EQUALS( r->cfAdd, nfAdd );
296      //TS_ASSERT_EQUALS( r->cfDelete, nfDelete );
297      break;
298    }
299#ifdef HAVE_RINGS
300    case n_Z2m:
301    {
302      TS_ASSERT_EQUALS( r->cfInit, nr2mInit );
303      TS_ASSERT_EQUALS( r->cfWrite, nr2mWrite );
304      TS_ASSERT_EQUALS( r->cfAdd, nr2mAdd );
305      TS_ASSERT_EQUALS( r->cfDelete, ndDelete );
306      break;
307    }
308    case n_Zn:
309    {
310      TS_ASSERT_EQUALS( r->cfInit, nrnInit );
311      TS_ASSERT_EQUALS( r->cfWrite, nrnWrite );
312      TS_ASSERT_EQUALS( r->cfAdd, nrnAdd );
313      TS_ASSERT_EQUALS( r->cfDelete, nrnDelete );
314      break;
315    }
316#endif
317    default:
318    {
319      // ...
320    }
321  }
322
323  TestArith( r );
[e5422d]324  TestSum( r, 10 );
[1820e7]325  TestSum( r, 100 );
[e5422d]326  TestSum( r, 101 );
327  TestSum( r, 1001 );
328  TestSum( r, 9000 );
[1820e7]329
330  nKillChar( r );
331
332  return TRUE;
333}
334
335
[4f684a]336
[ae85a3]337// We can rely on this file being included exactly once
338// and declare this global variable in the header file.
339//
340static GlobalPrintingFixture globalPrintingFixture;
341
342
[1820e7]343class CoeffsTestSuite : public CxxTest::TestSuite
344{
345 public:
[4f684a]346//   void test_dummy() { float fnum = 2.00001f; TS_ASSERT_DELTA (fnum, 2.0f, 0.0001f);  }
[1820e7]347
348   void test_Z2m4()
349   {
350#ifdef HAVE_RINGS
351     n_coeffType type = nRegister( n_Z2m, nr2mInitChar); TS_ASSERT( type == n_Z2m ); 
352     TS_ASSERT( Test(type, (void*) 4) );
353#endif
354   }
355
356   void test_Zp101()
357   {
358     n_coeffType type = nRegister( n_Zp, npInitChar); TS_ASSERT( type == n_Zp );
359     TS_ASSERT( Test(type, (void*) 101) );
360   }
361
362   void test_Z2m8()
363   {
364#ifdef HAVE_RINGS
365     n_coeffType type = nRegister( n_Z2m, nr2mInitChar); TS_ASSERT( type == n_Z2m ); 
366     TS_ASSERT( Test(type, (void*) 8) );
367#endif
368   }
369   
370   void simple(const n_coeffType _type, cfInitCharProc p)
371   {
372     n_coeffType type = nRegister( _type, p);
373     TS_ASSERT( type == _type ); // ?
374     TS_ASSERT( Test(type) );
375   }
376   
377   void test_Q()
378   {
379     simple(n_Q, nlInitChar);
380   }
381   
382   void test_R()
383   {
384     simple(n_R, nrInitChar);
385   }
386
387
388   void test_Z()
389   {
390#ifdef HAVE_RINGS
391     simple(n_Z, nrzInitChar);  // No need in GMP?
392#endif
393   }
394
395   
396   void test_GF_toobig()
397   {
398     n_coeffType type = nRegister( n_GF, nfInitChar);
399     TS_ASSERT( type == n_GF );
400
401     GFInfo param;
402
403     param.GFChar= 5;
404     param.GFDegree= 12;
405     param.GFPar_name= (const char*)"q";
406
407     TS_ASSERT( !Test(type, (void*) &param) );
408
409      // it should not be used by numbers... right?
410      // TODO: what is our policy wrt param-pointer-ownership?
411   }
412
413   void test_GF()
414   {
415     // TODO: what if it was already registered?
416     // Q: no way to deRegister a type?
417     n_coeffType type = nRegister( n_GF, nfInitChar);
418     TS_ASSERT( type == n_GF );
419
420     GFInfo param;
421
422     param.GFChar= 5;
423     param.GFDegree= 2;
424     param.GFPar_name= (const char*)"Q";
425
426     TS_ASSERT( Test(type, (void*) &param) );
427
428      // it should not be used by numbers... right?
429      // TODO: what is our policy wrt param-pointer-ownership?
430   }
431   
432
433   void test_Zn3()
434   {
435#ifdef HAVE_RINGS
436     //  TODO(Somebody, This will result in memory corruption at Z_2^m later on (due to the succs. setGMPFloatDigits?)...!?); // ????
437     n_coeffType type = nRegister( n_Zn, nrnInitChar); TS_ASSERT( type == n_Zn );
438
439     TS_ASSERT( Test(type, (void*) 3) );
440#endif
441   }
442
443   void test_Z2m2()
444   {
445#ifdef HAVE_RINGS
446     n_coeffType type = nRegister( n_Z2m, nr2mInitChar); TS_ASSERT( type == n_Z2m );
447
448     TS_ASSERT( Test(type, (void*) 2) );
449#endif
450   }
451
452   TODO(Somebody, floating arithmetics via GMP rely on two global variables (see setGMPFloatDigits). Please fix it!);
453
454   void test_LR()
455   {
456     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?)!????
457     simple(n_long_R, ngfInitChar);
458   }
459   
460   void test_LC()
461   {
462     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?)!????
463     simple(n_long_C, ngcInitChar);
464   } 
465
466   
[26c66ae]467   // polynomial rings needed for extentions: n_Ext !
[1820e7]468   
469};
470
Note: See TracBrowser for help on using the repository browser.