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

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