source: git/libpolys/coeffs/test.cc @ c462b55

fieker-DuValspielwiese
Last change on this file since c462b55 was 16f8f1, checked in by Oleksandr Motsak <motsak@…>, 13 years ago
ADD: minor changes to coeffs, numbers and tests
  • Property mode set to 100644
File size: 7.4 KB
Line 
1#include <coeffs/config.h>
2
3#include <misc/auxiliary.h>
4
5#include <coeffs/coeffs.h>
6#include <coeffs/numbers.h>
7#include <reporter/reporter.h>
8#include <omalloc/omalloc.h>
9
10#include <coeffs/longrat.h>
11#include <coeffs/gnumpfl.h>
12#include <coeffs/gnumpc.h>
13#include <coeffs/shortfl.h>
14#include <coeffs/ffields.h>
15#include <coeffs/modulop.h>
16#include <coeffs/rmodulon.h>
17#include <coeffs/rmodulo2m.h>
18#include <coeffs/rintegers.h>
19
20#include <iostream>
21
22using namespace std;
23
24
25#pragma GCC diagnostic ignored "-Wwrite-strings"
26
27void Print(/*const*/ number a, const coeffs r, BOOLEAN eoln = TRUE)
28{
29  n_Test(a,r);
30
31  StringSetS("");
32  n_Write(a, r);
33
34  char* s = NULL; 
35
36  if( eoln ) 
37    s = StringAppend("\n");
38  else
39    s = StringAppend("");
40
41  PrintS(s);
42
43  // free s?
44}
45
46
47void PrintSized(/*const*/ number a, const coeffs r, BOOLEAN eoln = TRUE)
48{
49  Print(a, r, FALSE);
50  Print(", of size: %d", n_Size(a, r));
51 
52  if( eoln ) 
53    PrintLn();
54}
55 
56
57
58bool TestArith(const coeffs r)
59{
60  number a = n_Init(66666, r);
61   
62  PrintS("a: "); PrintSized(a, r);
63
64  number two = n_Init(2, r);
65 
66  PrintS("two: "); PrintSized(two, r);
67
68  if (getCoeffType(r) == n_GF) //some special test for GF
69  {
70    number z = nfPar (0, r); // also any integer instead of 0//?
71
72    PrintS("Generator: "); PrintSized(z, r);
73   
74    n_Delete(&z, r);   
75  }
76 
77  number aa = n_Add(a, a, r);
78
79  PrintS("aa = a + a: "); PrintSized(aa, r);
80 
81  number aa2 = n_Mult(a, two, r);
82
83  PrintS("aa2 = a * 2: "); PrintSized(aa2, r);
84
85  number aa1 = n_Mult(two, a, r);
86 
87  PrintS("aa1 = 2 * a: "); PrintSized(aa1, r);
88
89  n_Delete(&a, r);
90  n_Delete(&two, r);
91
92
93  a = n_Sub( aa, aa1, r );
94 
95  PrintS("a = aa - aa1: "); PrintSized(a, r);
96
97  if( !n_IsZero(a, r) )
98    WarnS("TestArith: ERROR: a != 0 !!!\n");
99
100  n_Delete(&a, r);
101
102  a = n_Sub( aa, aa2, r );
103
104  PrintS("a = aa - aa2: "); PrintSized(a, r);
105
106  if( !n_IsZero(a, r) )
107    WarnS("TestArith: ERROR: a != 0 !!!\n");
108
109  n_Delete(&a, r);
110
111
112  a = n_Sub( aa1, aa2, r );
113
114  PrintS("a = aa1 - aa2: "); PrintSized(a, r);
115
116  if( !n_IsZero(a, r) )
117    WarnS("TestArith: ERROR: a != 0 !!!\n");
118
119  n_Delete(&a, r);
120
121
122 
123  if( !n_Equal(aa, aa1, r) )
124    WarnS("TestArith: ERROR: aa != aa1  !!!\n");
125
126  if( !n_Equal(aa, aa2, r) )
127    WarnS("TestArith: ERROR: aa != aa2  !!!\n");
128
129  if( !n_Equal(aa1, aa2, r) )
130    WarnS("TestArith: ERROR: aa1 != aa2  !!!\n");
131 
132
133 
134
135  n_Delete(&aa, r);
136  n_Delete(&aa1, r);
137  n_Delete(&aa2, r);
138
139  return false;
140}
141
142
143
144namespace
145{
146  static inline ostream& operator<< (ostream& o, const n_coeffType& type)
147  {
148#define CASE(A) case A: return o << (" " # A) << " ";
149    switch( type )
150    {
151      CASE(n_unknown);
152      CASE(n_Zp);
153      CASE(n_Q);
154      CASE(n_R);
155      CASE(n_GF);
156      CASE(n_long_R);
157      CASE(n_Zp_a);
158      CASE(n_Q_a);
159      CASE(n_long_C);
160      CASE(n_Z);
161      CASE(n_Zn);
162      CASE(n_Zpn);
163      CASE(n_Z2m);
164      CASE(n_CF);
165      default: return o << "Unknown type: [" << (const unsigned long) type << "]"; 
166    }   
167#undef CASE
168    return o;
169  }
170}
171 
172
173bool Test(const n_coeffType type, void* p = NULL)
174{
175  cout  << endl << "----------------------- Testing coeffs: [" << type << ", " << p << 
176                "]: -----------------------" << endl;
177
178  const coeffs r = nInitChar( type, p );
179
180  if( r == NULL ) { cout << "Test: could not get the specified coeff. domain for type: " << type << " and the parameter: " << p << endl; return false; };
181
182  assume( r != NULL );
183  nSetChar( r );
184  assume( getCoeffType(r) == type );
185
186  assume( r->cfInit != NULL );
187  assume( r->cfWrite != NULL );
188  assume( r->cfAdd != NULL );
189  assume( r->cfDelete != NULL );
190
191  if( type == n_Q )
192  {
193    assume( r->cfInit == nlInit );
194    assume( r->cfWrite == nlWrite );
195    assume( r->cfAdd == nlAdd );
196    assume( r->cfDelete == nlDelete );   
197  }
198  else if( type == n_long_R )
199  {
200    assume( r->cfInit == ngfInit );
201    assume( r->cfWrite == ngfWrite );
202    assume( r->cfAdd == ngfAdd );
203    assume( r->cfDelete == ngfDelete );
204  }
205  else if( type == n_long_C )
206  {
207    assume( r->cfInit == ngcInit );
208    assume( r->cfWrite == ngcWrite );
209    assume( r->cfAdd == ngcAdd );
210    assume( r->cfDelete == ngcDelete );   
211  }
212  else if( type == n_R )
213  {
214    assume( r->cfInit == nrInit );
215    assume( r->cfWrite == nrWrite );
216    assume( r->cfAdd == nrAdd );
217//    assume( r->cfDelete == nrDelete ); // No?
218  }
219#ifdef HAVE_RINGS
220  else if( type == n_Z2m )
221  {
222    assume( r->cfInit == nr2mInit );
223    assume( r->cfWrite == nr2mWrite );
224    assume( r->cfAdd == nr2mAdd );
225    assume( r->cfDelete == ndDelete );
226  }
227  else if( type == n_Zn )
228  {
229    assume( r->cfInit == nrnInit );
230    assume( r->cfWrite == nrnWrite );
231    assume( r->cfAdd == nrnAdd );
232    assume( r->cfDelete == nrnDelete );
233  }
234#endif
235  else if( type == n_GF )
236  {
237    assume( r->cfInit == nfInit );
238    assume( r->cfWrite == nfWrite );
239    assume( r->cfAdd == nfAdd );
240    //assume( r->cfDelete == nfDelete );
241  }
242  else
243  {
244    // ...
245  }
246
247  bool ret = TestArith( r );
248
249  nKillChar( r );
250
251  return ret;
252}
253
254
255
256
257int main()
258{
259  int c = 0;
260 
261  n_coeffType type;
262
263
264#ifdef HAVE_RINGS
265//  TODO(Frank, Segmentation fault! (if used wihout omalloc???). Please_ investigate!);
266  type = nRegister( n_Z2m, nr2mInitChar); assume( type == n_Z2m ); 
267  if( Test(type, (void*) 4) )
268    c ++;
269#endif
270
271  type = nRegister( n_Zp, npInitChar); assume( type == n_Zp );
272  if( Test(type, (void*) 101) )
273    c ++;
274
275#ifdef HAVE_RINGS
276//  TODO(Frank, memmory corruption_ if used wihout omalloc??? Please_ investigate!);
277
278  type = nRegister( n_Z2m, nr2mInitChar); assume( type == n_Z2m ); 
279  if( Test(type, (void*) 8) )
280    c ++;
281
282#endif
283
284 
285  type = nRegister( n_Q, nlInitChar); assume( type == n_Q );
286  if( Test(type) )
287    c ++;
288
289  type = nRegister( n_R, nrInitChar); assume( type == n_R );
290  if( Test(type) )
291    c ++;
292
293#ifdef HAVE_RINGS
294  type = nRegister( n_Z, nrzInitChar); assume( type == n_Z ); // No need in GMP?
295  if( Test(type) )
296    c ++;
297#endif
298   type = nRegister( n_GF, nfInitChar); assume( type == n_GF );
299
300
301   GFInfo* param = new GFInfo();
302
303   param->GFChar= 5;
304   param->GFDegree= 12;
305   param->GFPar_name= (const char*)"q";
306
307   if( Test(type, (void*) param) )
308     c ++;
309
310   // it should not be used by numbers... right?
311   // TODO: what is our policy wrt param-pointer-ownership?
312   delete param; 
313   // Q: no way to deRegister a type?
314
315   param = new GFInfo();
316
317   param->GFChar= 5;
318   param->GFDegree= 2;
319   param->GFPar_name= (const char*)"Q";
320
321   if( Test(type, (void*) param) )
322     c ++;
323
324   delete param;
325
326
327
328
329#ifdef HAVE_RINGS
330//  TODO(Somebody, This will result in memory corruption at Z_2^m later on (due to the succs. setGMPFloatDigits?)...!?); // ????
331
332  type = nRegister( n_Zn, nrnInitChar); assume( type == n_Zn );
333
334  if( Test(type, (void*) 3) )
335    c ++;
336
337#endif
338
339  TODO(Somebody, floating arithmetics via GMP rely on two global variables (see setGMPFloatDigits). Please fix it!);
340  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?)!????
341
342
343  type = nRegister( n_long_C, ngcInitChar); assume( type == n_long_C );
344  if( Test(type) )
345    c ++;
346
347  type = nRegister( n_long_R, ngfInitChar); assume( type == n_long_R );
348  if( Test(type) )
349    c ++;
350
351#ifdef HAVE_RINGS
352  type = nRegister( n_Z2m, nr2mInitChar); assume( type == n_Z2m ); 
353  if( Test(type, (void*) 2) )
354    c ++;
355#endif
356
357
358#ifdef HAVE_RINGS
359  type = nRegister( n_Zn, nrnInitChar); assume( type == n_Zn );
360
361  if( Test(type, (void*) 3) )
362    c ++;
363#endif
364 
365  // polynomial rings needed for: n_Zp_a, n_Q_a ?
366 
367  return c;
368
369}
Note: See TracBrowser for help on using the repository browser.