source: git/libpolys/tests/coeffs_test.h @ 8c9912

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