source: git/libpolys/tests/coeffs_test.h @ 1820e7

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