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.

67 lines
1.6 KiB

25 years ago
  1. #include "test_I.h"
  2. int test_I_div (int iterations)
  3. {
  4. int error = 0;
  5. int i;
  6. // Check floor.
  7. for (i = iterations; i > 0; i--) {
  8. cl_I a = testrandom_I();
  9. cl_I b = testrandom_I();
  10. if (b != 0) {
  11. cl_I_div_t qr = floor2(a,b);
  12. const cl_I& q = qr.quotient;
  13. const cl_I& r = qr.remainder;
  14. ASSERT2(a == q*b+r, a,b);
  15. ASSERT2(b >= 0 ? (r >= 0 && r < b) : (r <= 0 && r > b), a,b);
  16. }
  17. }
  18. // Check ceiling.
  19. for (i = iterations; i > 0; i--) {
  20. cl_I a = testrandom_I();
  21. cl_I b = testrandom_I();
  22. if (b != 0) {
  23. cl_I_div_t qr = ceiling2(a,b);
  24. const cl_I& q = qr.quotient;
  25. const cl_I& r = qr.remainder;
  26. ASSERT2(a == q*b+r, a,b);
  27. ASSERT2(b >= 0 ? (r <= 0 && r > -b) : (r >= 0 && r < -b), a,b);
  28. }
  29. }
  30. // Check truncate.
  31. for (i = iterations; i > 0; i--) {
  32. cl_I a = testrandom_I();
  33. cl_I b = testrandom_I();
  34. if (b != 0) {
  35. cl_I_div_t qr = truncate2(a,b);
  36. const cl_I& q = qr.quotient;
  37. const cl_I& r = qr.remainder;
  38. ASSERT2(a == q*b+r, a,b);
  39. if (b >= 0)
  40. if (a >= 0)
  41. { ASSERT2(r >= 0 && r < b, a,b); }
  42. else
  43. { ASSERT2(r <= 0 && r > -b, a,b); }
  44. else
  45. if (a >= 0)
  46. { ASSERT2(r >= 0 && r < -b, a,b); }
  47. else
  48. { ASSERT2(r <= 0 && r > b, a,b); }
  49. }
  50. }
  51. // Check round.
  52. for (i = iterations; i > 0; i--) {
  53. cl_I a = testrandom_I();
  54. cl_I b = testrandom_I();
  55. if (b != 0) {
  56. cl_I_div_t qr = round2(a,b);
  57. const cl_I& q = qr.quotient;
  58. const cl_I& r = qr.remainder;
  59. ASSERT2(a == q*b+r, a,b);
  60. ASSERT2(2*abs(r) <= abs(b), a,b);
  61. if (2*abs(r) == abs(b))
  62. ASSERT2(evenp(q), a,b);
  63. }
  64. }
  65. return error;
  66. }