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.

119 lines
3.0 KiB

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