source: git/libpolys/coeffs/test.cc @ 06df101

spielwiese
Last change on this file since 06df101 was 7af488e, checked in by Hans Schoenemann <hannes@…>, 13 years ago
nRegister not needed any more for standard coeffs
  • Property mode set to 100644
File size: 7.1 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_algExt);
163      CASE(n_transExt);
164      CASE(n_long_C);
165      CASE(n_Z);
166      CASE(n_Zn);
167      CASE(n_Zpn);
168      CASE(n_Z2m);
169      CASE(n_CF);
170      default: return o << "Unknown type: [" << (const unsigned long) type << "]"; 
171    }   
172#undef CASE
173    return o;
174  }
175}
176 
177
178bool Test(const n_coeffType type, void* p = NULL)
179{
180  cout  << endl << "----------------------- Testing coeffs: [" << type << ", " << p << 
181                "]: -----------------------" << endl;
182
183  const coeffs r = nInitChar( type, p );
184
185  if( r == NULL ) { cout << "Test: could not get the specified coeff. domain for type: " << type << " and the parameter: " << p << endl; return false; };
186
187  assume( r != NULL );
188  nSetChar( r );
189  assume( getCoeffType(r) == type );
190
191  assume( r->cfInit != NULL );
192  assume( r->cfWrite != NULL );
193  assume( r->cfAdd != NULL );
194  assume( r->cfDelete != NULL );
195
196  if( type == n_Q )
197  {
198    assume( r->cfInit == nlInit );
199    assume( r->cfWrite == nlWrite );
200    assume( r->cfAdd == nlAdd );
201    assume( r->cfDelete == nlDelete );   
202  }
203  else if( type == n_long_R )
204  {
205    assume( r->cfInit == ngfInit );
206    assume( r->cfWrite == ngfWrite );
207    assume( r->cfAdd == ngfAdd );
208    assume( r->cfDelete == ngfDelete );
209  }
210  else if( type == n_long_C )
211  {
212    assume( r->cfInit == ngcInit );
213    assume( r->cfWrite == ngcWrite );
214    assume( r->cfAdd == ngcAdd );
215    assume( r->cfDelete == ngcDelete );   
216  }
217  else if( type == n_R )
218  {
219    assume( r->cfInit == nrInit );
220    assume( r->cfWrite == nrWrite );
221    assume( r->cfAdd == nrAdd );
222//    assume( r->cfDelete == nrDelete ); // No?
223  }
224#ifdef HAVE_RINGS
225  else if( type == n_Z2m )
226  {
227    assume( r->cfInit == nr2mInit );
228    assume( r->cfWrite == nr2mWrite );
229    assume( r->cfAdd == nr2mAdd );
230    assume( r->cfDelete == ndDelete );
231  }
232  else if( type == n_Zn )
233  {
234    assume( r->cfInit == nrnInit );
235    assume( r->cfWrite == nrnWrite );
236    assume( r->cfAdd == nrnAdd );
237    assume( r->cfDelete == nrnDelete );
238  }
239#endif
240  else if( type == n_GF )
241  {
242    assume( r->cfInit == nfInit );
243    assume( r->cfWrite == nfWrite );
244    assume( r->cfAdd == nfAdd );
245    //assume( r->cfDelete == nfDelete );
246  }
247  else
248  {
249    // ...
250  }
251
252  bool ret = TestArith( r );
253
254  nKillChar( r );
255
256  return ret;
257}
258
259
260int main( int, char *argv[] ) 
261{
262  feInitResources(argv[0]);
263
264  StringSetS("ressources in use (as reported by feStringAppendResources(0):\n");
265  feStringAppendResources(0);
266  PrintS(StringAppendS("\n"));
267
268
269  int c = 0;
270 
271  n_coeffType type;
272
273
274#ifdef HAVE_RINGS
275//  TODO(Frank, Segmentation fault! (if used wihout omalloc???). Please_ investigate!);
276  type =  n_Z2m;
277  if( Test(type, (void*) 4) )
278    c ++;
279#endif
280
281  type =  n_Zp;
282  if( Test(type, (void*) 101) )
283    c ++;
284
285#ifdef HAVE_RINGS
286//  TODO(Frank, memmory corruption_ if used wihout omalloc??? Please_ investigate!);
287
288  type = n_Z2m;
289  if( Test(type, (void*) 8) )
290    c ++;
291
292#endif
293
294 
295  type =  n_Q;
296  if( Test(type) )
297    c ++;
298
299  type = n_R;
300  if( Test(type) )
301    c ++;
302
303#ifdef HAVE_RINGS
304  type = n_Z;
305  if( Test(type) )
306    c ++;
307#endif
308   type = n_GF;
309
310
311   GFInfo* param = new GFInfo();
312
313   param->GFChar= 5;
314   param->GFDegree= 12;
315   param->GFPar_name= (const char*)"q";
316
317   if( Test(type, (void*) param) )
318     c ++;
319
320   // it should not be used by numbers... right?
321   // TODO: what is our policy wrt param-pointer-ownership?
322   delete param; 
323   // Q: no way to deRegister a type?
324
325   param = new GFInfo();
326
327   param->GFChar= 5;
328   param->GFDegree= 2;
329   param->GFPar_name= (const char*)"Q";
330
331   if( Test(type, (void*) param) )
332     c ++;
333
334   delete param;
335
336
337
338
339#ifdef HAVE_RINGS
340//  TODO(Somebody, This will result in memory corruption at Z_2^m later on (due to the succs. setGMPFloatDigits?)...!?); // ????
341
342  type = n_Zn;
343
344  if( Test(type, (void*) 3) )
345    c ++;
346
347#endif
348
349  TODO(Somebody, floating arithmetics via GMP rely on two global variables (see setGMPFloatDigits). Please fix it!);
350  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?)!????
351
352
353  type = n_long_C;
354  if( Test(type) )
355    c ++;
356
357  type = n_long_R;
358  if( Test(type) )
359    c ++;
360
361#ifdef HAVE_RINGS
362  type = n_Z2m;
363  if( Test(type, (void*) 2) )
364    c ++;
365#endif
366
367
368#ifdef HAVE_RINGS
369  type = n_Zn;
370
371  if( Test(type, (void*) 3) )
372    c ++;
373#endif
374 
375  // polynomial rings needed for: n_algExt, n_transExt !
376 
377  return c;
378
379}
Note: See TracBrowser for help on using the repository browser.