You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
748 B

25 years ago
25 years ago
  1. #include "test_I.h"
  2. int test_I_isqrt (int iterations)
  3. {
  4. int error = 0;
  5. int i;
  6. // Check against "*".
  7. for (i = iterations; i > 0; i--) {
  8. cl_I a = testrandom_I();
  9. if (a >= 0) {
  10. cl_I w;
  11. bool squarep = isqrt(a,&w);
  12. ASSERT1(w >= 0 && expt_pos(w,2) <= a && a < expt_pos(w+1,2), a);
  13. ASSERT1(squarep ? w*w==a : w*w<a, a);
  14. }
  15. }
  16. // Check certain special cases.
  17. for (i = iterations; i > 0; i--) {
  18. cl_I a = abs(testrandom_I());
  19. cl_I w;
  20. // Check a^2 is a square.
  21. ASSERT1(isqrt(a*a,&w) && w == a, a);
  22. // Check a^2+1 is not a square, except when a=0.
  23. ASSERT1((a==0) || (!isqrt(a*a+1,&w) && w == a), a);
  24. // Check a^2+2*a is not a square, except when a=0.
  25. ASSERT1(isqrt(a*(a+2),&w)==(a==0) && w == a, a);
  26. }
  27. return error;
  28. }