source: git/libpolys/tests/polys_test.h @ b5b9dc

spielwiese
Last change on this file since b5b9dc was b5b9dc, checked in by Hans Schoenemann <hannes@…>, 13 years ago
fix poly_test
  • Property mode set to 100644
File size: 6.6 KB
Line 
1#include "config.h"
2
3#include "common.h"
4using namespace std;
5
6// the following headers are private...
7#include <coeffs/longrat.h>
8#include <coeffs/gnumpfl.h>
9#include <coeffs/gnumpc.h>
10#include <coeffs/shortfl.h>
11#include <coeffs/ffields.h>
12#include <coeffs/modulop.h>
13#include <coeffs/rmodulon.h>
14#include <coeffs/rmodulo2m.h>
15#include <coeffs/rintegers.h>
16
17
18#include <polys/monomials/ring.h>
19#include <polys/monomials/p_polys.h>
20
21
22
23
24class MyGlobalPrintingFixture : public GlobalPrintingFixture
25{
26  public:
27    virtual bool setUpWorld()
28    {
29   
30      GlobalPrintingFixture::setUpWorld();
31     
32
33      TS_ASSERT_EQUALS( nRegister( n_Zp, npInitChar), n_Zp );
34      TS_ASSERT_EQUALS( nRegister( n_GF, nfInitChar), n_GF );
35      TS_ASSERT_EQUALS( nRegister( n_R, nrInitChar), n_R );
36      TS_ASSERT_EQUALS( nRegister( n_Q, nlInitChar), n_Q );
37      TS_ASSERT_EQUALS( nRegister( n_R, nrInitChar), n_R );
38     
39#ifdef HAVE_RINGS
40      TS_ASSERT_EQUALS( nRegister( n_Z, nrzInitChar), n_Z ); // these are UNusable at the moment!
41#endif
42     
43      return true;
44    }
45};
46
47
48//
49// We can rely on this file being included exactly once
50// and declare this global variable in the header file.
51//
52static MyGlobalPrintingFixture globalPrintingFixture;
53
54
55namespace
56{
57 
58  void PrintRing(const ring r)
59  {
60    rWrite(r); PrintLn();
61  #ifdef  RDEBUG
62    rDebugPrint(r); PrintLn();
63  #endif
64  }
65
66  static inline std::string _2S(poly a, const ring r)
67  {
68    p_Test(a,r);
69   
70    StringSetS("");
71    p_Write(a, r);
72
73    const char* s = StringAppendS("");
74
75    std::stringstream ss;  ss << s;
76
77    StringSetS(""); 
78    return ss.str();
79  }
80
81  static inline void PrintSized(/*const*/ poly a, const ring r, BOOLEAN eoln = TRUE)
82  {
83    std::clog << _2S(a, r) << ", of size: " << p_Size(a, r);
84
85    if( eoln ) 
86      std::clog << std::endl; 
87  }
88
89static inline void Delete(poly &p, const ring r)
90{
91  if( p != NULL )
92    p_Delete(&p, r);
93
94  p = NULL;
95}
96
97void TestSum(const ring r, const int N)
98{
99  TS_ASSERT_DIFFERS( r    , NULLp);
100  TS_ASSERT_DIFFERS( r->cf, NULLp);
101 
102 
103  clog << ( _2S("TEST: sum[0..") + _2S(N) + "]: ");
104  clog << endl;
105
106  assume( N > 0 ); // just for now...
107
108  const int ssss = (N * (N+1)) / 2;
109 
110  poly sum1 = p_ISet(ssss, r);
111  clog<< "poly(N*(N+1)/2) (int: " << ssss << "): "; PrintSized(sum1, r);
112
113  poly s=NULL, ss=NULL, i=NULL, res=NULL;
114
115  s = p_ISet(, r);
116  i = p_ISet(N+1, r);
117
118  i = p_Mult_q(s, i, r); s = NULL;
119
120  clog<< "poly(N)*poly(N+1): (int: "<< N*(N+1) << "): "; PrintSized(i, r); 
121
122  number t = n_Init(2, r->cf);
123  clog<< "number(2): "; PrintSized(t, r->cf); 
124
125  if( !n_IsZero( t, r->cf) )
126  {
127    if( i != NULL )
128    {
129      number ii = p_GetCoeff(i, r);
130      clog<< "number(poly(N)*poly(N+1)): "; PrintSized(ii, r->cf); 
131
132      TS_ASSERT( n_DivBy(ii, t, r->cf) );
133      res = p_Div_nn(i, t, r); i = NULL;
134    }
135   
136     
137
138    clog<< "(poly(N)*poly(N+1))/number(2): "; PrintSized(res, r);
139    poly d = p_Sub(p_Copy(res, r), p_Copy(sum1, r), r);
140
141    if( d != NULL )
142      TS_ASSERT( n_IsZeroDivisor(p_GetCoeff(d, r), r->cf) );
143
144    Delete(d, r);
145
146    if( n_GetChar(r->cf) == 0 )
147    {
148      TS_ASSERT( p_EqualPolys(sum1, res, r) );
149      TS_ASSERT( p_EqualPolys(res, sum1, r) );
150    }
151  } else
152    TS_ASSERT_EQUALS( n_GetChar(r->cf), 2);
153 
154  n_Delete(&t, r->cf);
155
156
157  s = NULL;
158  ss = NULL;
159  for( int k = N; k >= 0; k-- )
160  {
161    i = p_ISet(k, r);
162    s = p_Add_q(s, i, r); // s += i
163
164    i = p_Neg( p_ISet(k, r), r );
165    ss = p_Add_q(ss, i, r); // ss -= i
166  }
167 
168  clog<< "ss(-sum): "; PrintSized(ss, r); 
169
170  ss = p_Neg(ss, r); // ss = -ss
171 
172  clog<< "real sum    : "; PrintSized(s, r);
173  clog<< "real sum(--): "; PrintSized(ss, r); 
174
175  TS_ASSERT( p_EqualPolys(s, ss, r) );
176  TS_ASSERT( p_EqualPolys(ss, s, r) );
177
178  TODO(somebody, fix the delete method!);
179
180  Delete(sum1, r); 
181  Delete(res, r);
182
183  Delete(s, r);   
184  Delete(ss, r);   
185
186  clog << ( " >>> TEST DONE!" );
187  clog << endl;
188
189}
190   
191void Test(const ring r)
192{
193  TestSum( r, 10 );
194  TestSum( r, 100 );
195  TestSum( r, 101 );
196  TestSum( r, 1001 );
197  TestSum( r, 9000 );
198}
199
200}
201
202class PolysTestSuite : public CxxTest::TestSuite
203{
204public:
205  void test_Z13_t()
206  {
207    clog << "Creating  Z/13[t]: " << endl;
208
209    char* n[] = {"t"};
210    ring r = rDefault( 13, 1, n);     
211    TS_ASSERT_DIFFERS( r, NULLp );
212
213    PrintRing(r);
214
215    TS_ASSERT( rField_is_Domain(r) );
216    TS_ASSERT( !rField_is_Q(r) );
217
218    TS_ASSERT( rField_is_Zp(r) );
219    TS_ASSERT( !rField_is_Zp(r, 11) );
220    TS_ASSERT( rField_is_Zp(r, 13) );
221
222    TS_ASSERT_EQUALS( rVar(r), 1);
223
224    Test(r);
225     
226    rDelete(r);
227  }
228
229  void test_QQ_t()
230  {
231    clog << "Creating  Q[s]: " << endl;
232
233    char* n[] = {"s"};
234    ring r = rDefault( 0, 1, n);     
235    TS_ASSERT_DIFFERS( r, NULLp );
236
237    PrintRing(r);
238
239    TS_ASSERT( rField_is_Domain(r) );
240    TS_ASSERT( rField_is_Q(r) );
241   
242    TS_ASSERT( !rField_is_Zp(r) );
243    TS_ASSERT( !rField_is_Zp(r, 11) );
244
245    TS_ASSERT_EQUALS( rVar(r), 1);
246
247    Test(r);
248
249    rDelete(r);
250  }
251 
252  void test_Z11_x_y_z()
253  {
254     clog << "Creating  Z/11[x, y, z]: " << endl;
255     
256     char* n[] = {"x", "y", "z"};
257     ring r = rDefault( 11, 3, n);     
258     TS_ASSERT_DIFFERS( r, NULLp );
259
260     PrintRing(r);
261     
262     TS_ASSERT( rField_is_Domain(r) );
263     TS_ASSERT( !rField_is_Q(r) );
264
265     TS_ASSERT( rField_is_Zp(r) );
266     TS_ASSERT( rField_is_Zp(r, 11) );
267     TS_ASSERT( !rField_is_Zp(r, 13) );
268
269     TS_ASSERT_EQUALS( rVar(r), 3);
270
271     Test(r);
272     
273     rDelete(r);
274  }
275   void test_QQ_x_y_z()
276   {
277     clog << "Creating  QQ[x, y, z, u]: " << endl;
278
279     char* n[] = {"x", "y", "z", "u"};
280     ring r = rDefault( 0, 4, n);     
281     TS_ASSERT_DIFFERS( r, NULLp );
282
283     PrintRing(r);
284
285     TS_ASSERT( rField_is_Domain(r) );
286     TS_ASSERT( rField_is_Q(r) );
287
288     TS_ASSERT( !rField_is_Zp(r) );
289     TS_ASSERT( !rField_is_Zp(r, 11) );
290
291     TS_ASSERT_EQUALS( rVar(r), 4);
292
293     Test(r);
294     
295     rDelete(r);
296   }
297
298
299   void test_Z13_t_GF()
300   {
301     clog << "Creating  GF[t]: " << endl;
302
303     char* n[] = {"t"};
304
305     GFInfo param;
306
307     param.GFChar= 5;
308     param.GFDegree= 2;
309     param.GFPar_name= (const char*)"Q";
310
311     const coeffs cf = nInitChar( n_GF, &param );
312
313     TS_ASSERT_DIFFERS( cf, NULLp );
314
315     ring r = rDefault( cf, 1, n);  // now cf belongs to r!
316     TS_ASSERT_DIFFERS( r, NULLp );
317
318     PrintRing(r);
319
320     TS_ASSERT( rField_is_Domain(r) );
321     TS_ASSERT( !rField_is_Q(r) );
322
323     TS_ASSERT( !rField_is_Zp(r) );
324     TS_ASSERT( !rField_is_Zp(r, 11) );
325     TS_ASSERT( !rField_is_Zp(r, 13) );
326     TS_ASSERT( rField_is_GF(r) );
327
328     TS_ASSERT( rField_is_GF(r, 5) );
329     TS_ASSERT( !rField_is_GF(r, 25) );
330
331     TS_ASSERT_EQUALS( rVar(r), 1);
332
333     Test(r);
334
335     rDelete(r); // kills 'cf' as well!
336   }
337
338};
339
Note: See TracBrowser for help on using the repository browser.