]> www.ginac.de Git - cln.git/blob - tests/test_I_sqrtp.cc
Fix crashes in find_univpoly_ring and related functions
[cln.git] / tests / test_I_sqrtp.cc
1 #include "test_I.h"
2 #define floor(a,b)  ((a) / (b))
3
4 int test_I_sqrtp (int iterations)
5 {
6         int error = 0;
7         int i;
8         // Check against isqrt.
9         for (i = iterations; i > 0; i--) {
10                 cl_I a = testrandom_I();
11                 if (a >= 0) {
12                         cl_I w;
13                         cl_boolean squarep = sqrtp(a,&w);
14                         cl_I correct_w;
15                         cl_boolean correct_squarep = isqrt(a,&correct_w);
16                         ASSERT1(squarep == correct_squarep, a);
17                         if (squarep)
18                                 ASSERT1(w == correct_w, a);
19                 }
20         }
21         // Check certain special cases.
22         for (i = iterations; i > 0; i--) {
23                 cl_I a = abs(testrandom_I());
24                 cl_I w;
25                 // Check a^2 is a square.
26                 ASSERT1(sqrtp(a*a,&w) && w == a, a);
27                 // Check a^2+1 is not a square, except when a=0.
28                 if (a > 0) ASSERT1(!isqrt(a*a+1,&w) && w == a, a);
29                 // Check a^2+2*a is not a square, except when a=0.
30                 ASSERT1(isqrt(a*(a+2),&w)==(a==0) && w == a, a);
31         }
32         return error;
33 }