source: git/libpolys/coeffs/test.cc @ 9eb0f9

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