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.

112 lines
2.9 KiB

25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
  1. #include <cln/integer.h>
  2. #include <cln/integer_io.h>
  3. #include <cln/io.h>
  4. using namespace std;
  5. using namespace cln;
  6. #define ASSERT(expr) \
  7. if (!(expr)) { \
  8. std::cerr << "Assertion failed! File " << __FILE__ << ", line " << __LINE__ << endl; \
  9. error = 1; \
  10. }
  11. struct gcd_test {
  12. const char * arg1;
  13. const char * arg2;
  14. const char * result;
  15. };
  16. #define num_elements(array) (sizeof(array)/sizeof(array[0]))
  17. // Note: This macro differs slightly from the one in "exam.h".
  18. #define DO_BINOP_TEST(typename,type,rtype,opname) \
  19. static int test_##typename##_##opname (void) \
  20. { \
  21. int error = 0; \
  22. for (unsigned int i = 0; i < num_elements(typename##_##opname##_tests); i++) { \
  23. opname##_test& test = typename##_##opname##_tests[i]; \
  24. type arg1 = type(test.arg1); \
  25. type arg2 = type(test.arg2); \
  26. rtype computed_result = opname(arg1,arg2); \
  27. rtype result = rtype(test.result); \
  28. if (computed_result != result) { \
  29. std::cerr << "Error in " #typename "_" #opname "_tests[" << i << "] !" << endl; \
  30. std::cerr << "Result should be: " << result << endl; \
  31. std::cerr << "Result computed : " << computed_result << endl << endl; \
  32. error = 1; \
  33. } \
  34. } \
  35. return error; \
  36. }
  37. static gcd_test integer_gcd_tests[] = {
  38. { "123456789", "345", "3" },
  39. { "345", "123456789", "3" },
  40. { "10", "0", "10" },
  41. { "0", "10", "10" },
  42. { "2523533737", "855322739", "1" },
  43. { "855322739", "2523533737", "1" },
  44. { "101611479673163974026724715741235467160607959655653420075620", "533177863832047932237026621580126811198495699416238676294977", "1" },
  45. { "30729415811", "323233683197", "31071199" },
  46. { "77874422", "32223899", "1" },
  47. { "974507656412513757857315037382926980395082974811562770185617915360", "-1539496810360685510909469177732386446833404488164283", "1" },
  48. { "2823618260546496405819033080103700734250203999069672146446", "18374686479688400895", "1" }
  49. };
  50. DO_BINOP_TEST(integer,cl_I,cl_I,gcd)
  51. int test_gcd (void)
  52. {
  53. int error = 0;
  54. error |= test_integer_gcd();
  55. return error;
  56. }
  57. int test_xgcd (void)
  58. {
  59. int error = 0;
  60. {
  61. cl_I a = 77874422;
  62. cl_I b = 32223899;
  63. cl_I u;
  64. cl_I v;
  65. cl_I g = xgcd(a,b, &u,&v);
  66. ASSERT(g == 1);
  67. ASSERT(g == a*u+b*v);
  68. ASSERT(u == -9206830);
  69. ASSERT(v == 22249839);
  70. }
  71. {
  72. cl_I a = "560014183";
  73. cl_I b = 312839871;
  74. cl_I u;
  75. cl_I v;
  76. cl_I g = xgcd(a,b, &u,&v);
  77. ASSERT(g == 1);
  78. ASSERT(g == a*u+b*v);
  79. ASSERT(u == 77165803);
  80. ASSERT(v == -138134388);
  81. }
  82. {
  83. cl_I a = "#x80000000";
  84. cl_I b = "#x-C0000000";
  85. cl_I u;
  86. cl_I v;
  87. cl_I g = xgcd(a,b, &u,&v);
  88. ASSERT(g == (cl_I)"#x40000000");
  89. ASSERT(g == a*u+b*v);
  90. ASSERT(u == -1);
  91. ASSERT(v == -1);
  92. }
  93. {
  94. cl_I a = "974507656412513757857315037382926980395082974811562770185617915360";
  95. cl_I b = "-1539496810360685510909469177732386446833404488164283";
  96. cl_I u;
  97. cl_I v;
  98. cl_I g = xgcd(a,b, &u,&v);
  99. ASSERT(g == 1);
  100. ASSERT(g == a*u+b*v);
  101. }
  102. return error;
  103. }