source: git/libpolys/tests/coeffs_test.h @ 2c889f

spielwiese
Last change on this file since 2c889f was 2c889f, checked in by Oleksandr Motsak <motsak@…>, 11 years ago
Code update corresponding to renaming findexec into resources
  • Property mode set to 100644
File size: 9.8 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>
[2c889f]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;
[f323dd1]37
[1820e7]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);
[f323dd1]47
48  clog<< "N*(N+1): ("<< N*(N+1) << ")"; PrintSized(s, r);
49
[1820e7]50  i = n_Init(2, r);
[f323dd1]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
[f323dd1]58
[1820e7]59    res = n_Div(s, i, r);
[f323dd1]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);
[f323dd1]67
[e5422d]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);
[f323dd1]75
[1820e7]76
77  n_Delete(&s, r);  n_Delete(&i, r);
[f323dd1]78
79  n_Delete(&sum1, r); n_Delete(&res, r);
80
[1820e7]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
[f323dd1]91
92    n_Delete(&i, r);
[1820e7]93  }
[f323dd1]94  clog<< "ss: "; PrintSized(ss, r);
[1820e7]95
96  ss = n_Neg(ss, r); // ss = -ss
[f323dd1]97
[e5422d]98  clog<< "real sum    : "; PrintSized(s, r);
[f323dd1]99  clog<< "real sum(--): "; PrintSized(ss, r);
[e5422d]100
[1820e7]101  TS_ASSERT( n_Equal(s, ss, r) );
102  TS_ASSERT( n_Equal(ss, s, r) );
103
[f323dd1]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
[99ae0c]118  number two = n_Init(2, r);
119
[f323dd1]120  number t = n_Init(1, r);
121  ndInpAdd(t, t, r);
[99ae0c]122  TS_ASSERT( n_Equal(two, t, r) );
123  n_Delete(&t, r);
[f323dd1]124
[99ae0c]125  if( getCoeffType(r) == n_Q )
126  {
[f323dd1]127    number t = n_Init(1, r);
[99ae0c]128    nlInpAdd(t, t, r);
129    TS_ASSERT( n_Equal(two, t, r) );
130    n_Delete(&t, r);
131  }
132
133
134
[f323dd1]135
[99ae0c]136  const int N = 66666;
137
138  number a = n_Init(N, r);
[f323dd1]139
[1820e7]140  clog<< "a: "; PrintSized(a, r);
141
[f323dd1]142
[1820e7]143  clog<< "two: "; PrintSized(two, r);
144
[99ae0c]145  number aa0 = n_Init(N*2, r);
146
[1820e7]147  number aa = n_Add(a, a, r);
148
149  clog<< "aa = a + a: "; PrintSized(aa, r);
[f323dd1]150
[1820e7]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);
[f323dd1]156
[1820e7]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 );
[f323dd1]164
[1820e7]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
[f323dd1]189
[1820e7]190  TS_ASSERT( n_Equal(aa, aa1, r) );
191  TS_ASSERT( n_Equal(aa, aa2, r) );
192  TS_ASSERT( n_Equal(aa1, aa2, r) );
[f323dd1]193
[99ae0c]194  TS_ASSERT( n_Equal(aa0, aa, r) );
195  TS_ASSERT( n_Equal(aa0, aa1, r) );
196  TS_ASSERT( n_Equal(aa0, aa2, r) );
[1820e7]197
198  n_Delete(&aa, r);
199  n_Delete(&aa1, r);
200  n_Delete(&aa2, r);
[e5422d]201
[99ae0c]202  n_Delete(&aa0, r);
[e5422d]203
204  clog << ( " >>> TEST DONE!" );
205  clog << endl;
206
[1820e7]207}
208
209
210
[f323dd1]211
[1820e7]212
213BOOLEAN Test(const n_coeffType type, void* p = NULLp)
214{
[f323dd1]215
[1820e7]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" );
[ae85a3]225    return FALSE;
[1820e7]226  };
227
[ae85a3]228  TS_ASSERT_DIFFERS( r->cfCoeffWrite, NULLp );
229
230  if( r->cfCoeffWrite != NULL )
231  {
[f323dd1]232    clog << "Coeff-domain: "  << endl;
[ae85a3]233    n_CoeffWrite(r); PrintLn();
234  }
235
[7fee876]236  if (n_NumberOfParameters(r) > 0) 
[1820e7]237  {
[7fee876]238    number z = n_Param(1, r); // also any integer instead of 0//?
239    PrintS("Parameter: "); PrintSized(z, r);
240    n_Delete(&z, r);   
[1820e7]241  }
[7fee876]242   
[1820e7]243
[f323dd1]244  clog << "Char: " << n_GetChar(r) << endl;
245
[1820e7]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 );
[ce1f78]252  TS_ASSERT_DIFFERS( r->cfWriteLong, NULLp );
[1820e7]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->cfAdd, nlAdd );
262      TS_ASSERT_EQUALS( r->cfDelete, nlDelete );
[f323dd1]263
[1820e7]264      TS_ASSERT(  nCoeff_is_Q( r ));
265      TS_ASSERT(  nCoeff_is_Domain( r ));
[e5422d]266
267      TS_ASSERT( !nCoeff_has_Units( r )); // ?
268      TS_ASSERT( !nCoeff_has_simple_inverse( r ));// ?
269      TS_ASSERT( !nCoeff_has_simple_Alloc( r )); // ?
[1820e7]270
271      TS_ASSERT( !nCoeff_is_Ring_2toM( r ));
272      TS_ASSERT( !nCoeff_is_Ring_ModN( r ));
273      TS_ASSERT( !nCoeff_is_Ring_PtoM( r ));
274      TS_ASSERT( !nCoeff_is_Ring_Z( r ));
275      TS_ASSERT( !nCoeff_is_Ring( r ));
276      TS_ASSERT( !nCoeff_is_Zp( r ));
277      TS_ASSERT( !nCoeff_is_numeric( r ));
278      TS_ASSERT( !nCoeff_is_R( r ));
279      TS_ASSERT( !nCoeff_is_GF( r ));
280      TS_ASSERT( !nCoeff_is_long_R( r ));
281      TS_ASSERT( !nCoeff_is_long_C( r ));
282      TS_ASSERT( !nCoeff_is_CF( r ));
283      TS_ASSERT( !nCoeff_is_Extension( r ));
[f323dd1]284
[1820e7]285      break;
286    }
287    case n_long_R:
288    {
289      TS_ASSERT_EQUALS( r->cfInit, ngfInit );
290      TS_ASSERT_EQUALS( r->cfAdd, ngfAdd );
291      TS_ASSERT_EQUALS( r->cfDelete, ngfDelete );
292      break;
293    }
294    case n_long_C:
295    {
[7fee876]296//       TS_ASSERT_EQUALS( r->cfInit, ngcInit );
297//       TS_ASSERT_EQUALS( r->cfAdd, ngcAdd );
298//       TS_ASSERT_EQUALS( r->cfDelete, ngcDelete );
[1820e7]299      break;
300    }
301    case n_R:
302    {
303      TS_ASSERT_EQUALS( r->cfInit, nrInit );
304      TS_ASSERT_EQUALS( r->cfAdd, nrAdd );
305  //    TS_ASSERT_EQUALS( r->cfDelete, nrDelete ); // No?
306      break;
307    }
308    case n_GF:
309    {
[7fee876]310//       TS_ASSERT_EQUALS( r->cfInit, nfInit );
311//       TS_ASSERT_EQUALS( r->cfAdd, nfAdd );
[1820e7]312      //TS_ASSERT_EQUALS( r->cfDelete, nfDelete );
313      break;
314    }
315#ifdef HAVE_RINGS
316    case n_Z2m:
317    {
318      TS_ASSERT_EQUALS( r->cfInit, nr2mInit );
319      TS_ASSERT_EQUALS( r->cfAdd, nr2mAdd );
320      TS_ASSERT_EQUALS( r->cfDelete, ndDelete );
321      break;
322    }
323    case n_Zn:
324    {
325      TS_ASSERT_EQUALS( r->cfInit, nrnInit );
326      TS_ASSERT_EQUALS( r->cfAdd, nrnAdd );
327      TS_ASSERT_EQUALS( r->cfDelete, nrnDelete );
328      break;
329    }
330#endif
331    default:
332    {
333      // ...
334    }
335  }
336
337  TestArith( r );
[e5422d]338  TestSum( r, 10 );
[1820e7]339  TestSum( r, 100 );
[e5422d]340  TestSum( r, 101 );
341  TestSum( r, 1001 );
342  TestSum( r, 9000 );
[1820e7]343
344  nKillChar( r );
345
346  return TRUE;
347}
348
349
[4f684a]350
[ae85a3]351// We can rely on this file being included exactly once
352// and declare this global variable in the header file.
353//
354static GlobalPrintingFixture globalPrintingFixture;
355
356
[f323dd1]357class CoeffsTestSuite : public CxxTest::TestSuite
[1820e7]358{
359 public:
[4f684a]360//   void test_dummy() { float fnum = 2.00001f; TS_ASSERT_DELTA (fnum, 2.0f, 0.0001f);  }
[1820e7]361
362   void test_Z2m4()
363   {
364#ifdef HAVE_RINGS
[7af488e]365     n_coeffType type = n_Z2m;
[1820e7]366     TS_ASSERT( Test(type, (void*) 4) );
367#endif
368   }
369
370   void test_Zp101()
371   {
[7af488e]372     n_coeffType type = n_Zp;
[1820e7]373     TS_ASSERT( Test(type, (void*) 101) );
374   }
375
376   void test_Z2m8()
377   {
378#ifdef HAVE_RINGS
[7af488e]379     n_coeffType type =  n_Z2m;
[1820e7]380     TS_ASSERT( Test(type, (void*) 8) );
381#endif
382   }
[f323dd1]383
[7af488e]384   void simple(const n_coeffType _type)
[1820e7]385   {
[7af488e]386     n_coeffType type = _type;
[1820e7]387     TS_ASSERT( type == _type ); // ?
388     TS_ASSERT( Test(type) );
389   }
[f323dd1]390
[1820e7]391   void test_Q()
392   {
[7af488e]393     simple(n_Q);
[1820e7]394   }
[f323dd1]395
[1820e7]396   void test_R()
397   {
[7af488e]398     simple(n_R);
[1820e7]399   }
400
401
402   void test_Z()
403   {
404#ifdef HAVE_RINGS
[7af488e]405     simple(n_Z);  // No need in GMP?
[1820e7]406#endif
407   }
408
[f323dd1]409
[1820e7]410   void test_GF_toobig()
411   {
[7af488e]412     n_coeffType type = n_GF;
[1820e7]413
414     GFInfo param;
415
416     param.GFChar= 5;
417     param.GFDegree= 12;
418     param.GFPar_name= (const char*)"q";
419
420     TS_ASSERT( !Test(type, (void*) &param) );
421
[f323dd1]422      // it should not be used by numbers... right?
[1820e7]423      // TODO: what is our policy wrt param-pointer-ownership?
424   }
425
426   void test_GF()
427   {
428     // TODO: what if it was already registered?
429     // Q: no way to deRegister a type?
[7af488e]430     n_coeffType type = n_GF;
[1820e7]431
432     GFInfo param;
433
434     param.GFChar= 5;
435     param.GFDegree= 2;
436     param.GFPar_name= (const char*)"Q";
437
438     TS_ASSERT( Test(type, (void*) &param) );
439
[f323dd1]440      // it should not be used by numbers... right?
[1820e7]441      // TODO: what is our policy wrt param-pointer-ownership?
442   }
[f323dd1]443
[1820e7]444
445   void test_Zn3()
446   {
447#ifdef HAVE_RINGS
[7af488e]448     n_coeffType type = n_Zn;
[1820e7]449
[c3304a]450     ZnmInfo Znmparam;
451     Znmparam.base= (mpz_ptr) omAlloc (sizeof (mpz_t));
452     mpz_init_set_ui (Znmparam.base, 3);
453     Znmparam.exp= 1;
[c7d05f]454
[c3304a]455     TS_ASSERT( Test(type, (void*) &Znmparam) );
[1820e7]456#endif
457   }
458
459   void test_Z2m2()
460   {
461#ifdef HAVE_RINGS
[7af488e]462     n_coeffType type = n_Z2m;
[1820e7]463
464     TS_ASSERT( Test(type, (void*) 2) );
465#endif
466   }
467
468   void test_LR()
469   {
[7af488e]470     simple(n_long_R);
[1820e7]471   }
[f323dd1]472
[1820e7]473   void test_LC()
474   {
[7af488e]475     simple(n_long_C);
[06df101]476   }
[f323dd1]477
[06df101]478   void test_Q_special()
479   {
480     const coeffs cf = nInitChar(n_Q, NULLp);
481
482     if (cf == NULLp)
483       clog << ( "Test: could not get this coeff. domain" );
484
485     TS_ASSERT_DIFFERS(cf->cfCoeffWrite, NULLp);
486
487     if (cf->cfCoeffWrite != NULL )
488     {
[f323dd1]489       clog << "Coeff-domain: "  << endl;
[06df101]490       n_CoeffWrite(cf); PrintLn();
491     }
[f323dd1]492
[06df101]493     number q1 = n_Init(21, cf);
494     number q2 = n_Init(2, cf);
495     number q3 = n_Div(q1, q2, cf);
496     number q4 = n_Init(30, cf);
497     number q5 = n_Mult(q3, q4, cf);
498     TS_ASSERT(n_Test(q5, cf));
499     Print("21/2 * 30 = %d\n", n_Int(q5, cf));
500     TS_ASSERT(n_Test(q5, cf));
501     n_Delete(&q1, cf);
502     n_Delete(&q2, cf);
503     n_Delete(&q3, cf);
504     n_Delete(&q4, cf);
505     n_Delete(&q5, cf);
506   }
[1820e7]507};
508
Note: See TracBrowser for help on using the repository browser.