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

spielwiese
Last change on this file since bfde6c was bfde6c, checked in by Oleksandr Motsak <motsak@…>, 13 years ago
FIX: some silly bugs in tests... TODO: correct assumes...?
  • Property mode set to 100644
File size: 6.7 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
89template <typename T>
90static inline poly p_Init(T v, const ring r)
91{ assume(r != NULL); return p_ISet(v, r); }
92
93static inline void Delete(poly &p, const ring r)
94{
95  if( p != NULL )
96    p_Delete(&p, r);
97
98  p = NULL;
99}
100
101void TestSum(const ring r, const unsigned long N)
102{
103  TS_ASSERT_DIFFERS( r    , NULLp);
104  TS_ASSERT_DIFFERS( r->cf, NULLp);
105 
106 
107  clog << ( _2S("TEST: sum[0..") + _2S(N) + "]: ");
108  clog << endl;
109
110  assume( N > 0 ); // just for now...
111
112  const unsigned long ssss = (N * (N+1)) / 2;
113 
114  poly sum1 = p_Init(ssss, r);
115  clog<< "poly(N*(N+1)/2) (int: " << ssss << "): "; PrintSized(sum1, r);
116
117  poly s, ss, i, res; res = NULL;
118
119  s = p_Init(, r);
120  i = p_Init(N+1, r);
121
122  i = p_Mult_q(s, i, r); s = NULL;
123
124  clog<< "poly(N)*poly(N+1): (int: "<< N*(N+1) << "): "; PrintSized(i, r); 
125
126  number t = n_Init(2, r->cf);
127  clog<< "number(2): "; PrintSized(t, r->cf); 
128
129  if( !n_IsZero( t, r->cf) )
130  {
131    if( i != NULL )
132    {
133      number ii = p_GetCoeff(i, r);
134      clog<< "number(poly(N)*poly(N+1)): "; PrintSized(ii, r->cf); 
135
136      TS_ASSERT( n_DivBy(ii, t, r->cf) );
137      res = p_Div_nn(i, t, r); i = NULL;
138    }
139   
140     
141
142    clog<< "(poly(N)*poly(N+1))/number(2): "; PrintSized(res, r);
143    poly d = p_Sub(p_Copy(res, r), p_Copy(sum1, r), r);
144
145    if( d != NULL )
146      TS_ASSERT( n_IsZeroDivisor(p_GetCoeff(d, r), r->cf) );
147
148    Delete(d, r);
149
150    if( n_GetChar(r->cf) == 0 )
151    {
152      TS_ASSERT( p_EqualPolys(sum1, res, r) );
153      TS_ASSERT( p_EqualPolys(res, sum1, r) );
154    }
155  } else
156    TS_ASSERT_EQUALS( n_GetChar(r->cf), 2);
157 
158  n_Delete(&t, r->cf);
159
160
161  s = p_Init(0  , r);
162  ss = p_Init(0 , r);
163  for( int k = N; k >= 0; k-- )
164  {
165    i = p_Init(k, r);
166    s = p_Add_q(s, i, r); // s += i
167
168    i = p_Neg( p_Init(k, r), r );
169    ss = p_Add_q(ss, i, r); // ss -= i
170  }
171 
172  clog<< "ss(-sum): "; PrintSized(ss, r); 
173
174  ss = p_Neg(ss, r); // ss = -ss
175 
176  clog<< "real sum    : "; PrintSized(s, r);
177  clog<< "real sum(--): "; PrintSized(ss, r); 
178
179  TS_ASSERT( p_EqualPolys(s, ss, r) );
180  TS_ASSERT( p_EqualPolys(ss, s, r) );
181
182  TODO(somebody, fix the delete method!);
183
184  Delete(sum1, r); 
185  Delete(res, r);
186
187  Delete(s, r);   
188  Delete(ss, r);   
189
190  clog << ( " >>> TEST DONE!" );
191  clog << endl;
192
193}
194   
195void Test(const ring r)
196{
197  TestSum( r, 10 );
198  TestSum( r, 100 );
199  TestSum( r, 101 );
200  TestSum( r, 1001 );
201  TestSum( r, 9000 );
202}
203
204}
205
206class PolysTestSuite : public CxxTest::TestSuite
207{
208public:
209  void test_Z13_t()
210  {
211    clog << "Creating  Z/13[t]: " << endl;
212
213    char* n[] = {"t"};
214    ring r = rDefault( 13, 1, n);     
215    TS_ASSERT_DIFFERS( r, NULLp );
216
217    PrintRing(r);
218
219    TS_ASSERT( rField_is_Domain(r) );
220    TS_ASSERT( !rField_is_Q(r) );
221
222    TS_ASSERT( rField_is_Zp(r) );
223    TS_ASSERT( !rField_is_Zp(r, 11) );
224    TS_ASSERT( rField_is_Zp(r, 13) );
225
226    TS_ASSERT_EQUALS( rVar(r), 1);
227
228    Test(r);
229     
230    rDelete(r);
231  }
232
233  void test_QQ_t()
234  {
235    clog << "Creating  Q[s]: " << endl;
236
237    char* n[] = {"s"};
238    ring r = rDefault( 0, 1, n);     
239    TS_ASSERT_DIFFERS( r, NULLp );
240
241    PrintRing(r);
242
243    TS_ASSERT( rField_is_Domain(r) );
244    TS_ASSERT( rField_is_Q(r) );
245   
246    TS_ASSERT( !rField_is_Zp(r) );
247    TS_ASSERT( !rField_is_Zp(r, 11) );
248
249    TS_ASSERT_EQUALS( rVar(r), 1);
250
251    Test(r);
252
253    rDelete(r);
254  }
255 
256  void test_Z11_x_y_z()
257  {
258     clog << "Creating  Z/11[x, y, z]: " << endl;
259     
260     char* n[] = {"x", "y", "z"};
261     ring r = rDefault( 11, 3, n);     
262     TS_ASSERT_DIFFERS( r, NULLp );
263
264     PrintRing(r);
265     
266     TS_ASSERT( rField_is_Domain(r) );
267     TS_ASSERT( !rField_is_Q(r) );
268
269     TS_ASSERT( rField_is_Zp(r) );
270     TS_ASSERT( rField_is_Zp(r, 11) );
271     TS_ASSERT( !rField_is_Zp(r, 13) );
272
273     TS_ASSERT_EQUALS( rVar(r), 3);
274
275     Test(r);
276     
277     rDelete(r);
278  }
279   void test_QQ_x_y_z()
280   {
281     clog << "Creating  QQ[x, y, z, u]: " << endl;
282
283     char* n[] = {"x", "y", "z", "u"};
284     ring r = rDefault( 0, 4, n);     
285     TS_ASSERT_DIFFERS( r, NULLp );
286
287     PrintRing(r);
288
289     TS_ASSERT( rField_is_Domain(r) );
290     TS_ASSERT( rField_is_Q(r) );
291
292     TS_ASSERT( !rField_is_Zp(r) );
293     TS_ASSERT( !rField_is_Zp(r, 11) );
294
295     TS_ASSERT_EQUALS( rVar(r), 4);
296
297     Test(r);
298     
299     rDelete(r);
300   }
301
302
303   void test_Z13_t_GF()
304   {
305     clog << "Creating  GF[t]: " << endl;
306
307     char* n[] = {"t"};
308
309     GFInfo param;
310
311     param.GFChar= 5;
312     param.GFDegree= 2;
313     param.GFPar_name= (const char*)"Q";
314
315     const coeffs cf = nInitChar( n_GF, &param );
316
317     TS_ASSERT_DIFFERS( cf, NULLp );
318
319     ring r = rDefault( cf, 1, n);  // now cf belongs to r!
320     TS_ASSERT_DIFFERS( r, NULLp );
321
322     PrintRing(r);
323
324     TS_ASSERT( rField_is_Domain(r) );
325     TS_ASSERT( !rField_is_Q(r) );
326
327     TS_ASSERT( !rField_is_Zp(r) );
328     TS_ASSERT( !rField_is_Zp(r, 11) );
329     TS_ASSERT( !rField_is_Zp(r, 13) );
330     TS_ASSERT( rField_is_GF(r) );
331
332     TS_ASSERT( rField_is_GF(r, 5) );
333     TS_ASSERT( !rField_is_GF(r, 25) );
334
335     TS_ASSERT_EQUALS( rVar(r), 1);
336
337     Test(r);
338
339     rDelete(r); // kills 'cf' as well!
340   }
341
342};
343
Note: See TracBrowser for help on using the repository browser.