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.

103 lines
2.9 KiB

25 years ago
25 years ago
25 years ago
25 years ago
  1. #ifndef _EXAM_H
  2. #define _EXAM_H
  3. #include <cln/number.h>
  4. #include <cln/io.h>
  5. using namespace std;
  6. using namespace cln;
  7. // Michael Stoll 23. 3. 1993
  8. // C++ version: Bruno Haible 1.11.1995
  9. struct plus_test {
  10. const char * arg1;
  11. const char * arg2;
  12. const char * result;
  13. };
  14. struct minus_test {
  15. const char * arg1;
  16. const char * arg2;
  17. const char * result;
  18. };
  19. struct mul_test {
  20. const char * arg1;
  21. const char * arg2;
  22. const char * result;
  23. };
  24. struct floor_test {
  25. const char * arg1;
  26. const char * arg2;
  27. const char * result1;
  28. const char * result2;
  29. };
  30. struct div_test {
  31. const char * arg1;
  32. const char * arg2;
  33. const char * result;
  34. };
  35. #define num_elements(array) (sizeof(array)/sizeof(array[0]))
  36. #define DO_BINOP_TEST(typename,type,rtype,opname,op) \
  37. static int test_##typename##_##opname (void) \
  38. { \
  39. int error = 0; \
  40. for (unsigned int i = 0; i < num_elements(typename##_##opname##_tests); i++) { \
  41. opname##_test& test = typename##_##opname##_tests[i]; \
  42. type arg1 = type(test.arg1); \
  43. type arg2 = type(test.arg2); \
  44. rtype computed_result = arg1 op arg2; \
  45. rtype result = rtype(test.result); \
  46. if (computed_result != result) { \
  47. std::cerr << "Error in " #typename "_" #opname "_tests[" << i << "] !" << endl; \
  48. std::cerr << "Result should be: " << result << endl; \
  49. std::cerr << "Result computed : " << computed_result << endl << endl; \
  50. error = 1; \
  51. } \
  52. } \
  53. return error; \
  54. }
  55. #define DO_FLOOR_TEST(typename,type) \
  56. static int test_##typename##_floor (void) \
  57. { \
  58. int error = 0; \
  59. for (unsigned int i = 0; i < num_elements(typename##_floor_tests); i++) { \
  60. floor_test& test = typename##_floor_tests[i]; \
  61. type arg1 = type(test.arg1); \
  62. type arg2 = type(test.arg2); \
  63. type##_div_t computed_result = floor2(arg1,arg2); \
  64. cl_I result1 = cl_I(test.result1); \
  65. type result2 = type(test.result2); \
  66. if ((computed_result.quotient != result1) || (computed_result.remainder != result2)) { \
  67. std::cerr << "Error in " #typename "_floor_tests[" << i << endl; \
  68. std::cerr << "Results should be: " << result1 << ", " << result2 << endl; \
  69. std::cerr << "Results computed : " << computed_result.quotient << ", " << computed_result.remainder << endl << endl; \
  70. error = 1; \
  71. } \
  72. } \
  73. return error; \
  74. }
  75. #define DO_TESTS(typename,type,qtype) \
  76. DO_BINOP_TEST(typename,type,type,plus,+) \
  77. DO_BINOP_TEST(typename,type,type,minus,-) \
  78. DO_BINOP_TEST(typename,type,type,mul,*) \
  79. DO_FLOOR_TEST(typename,type) \
  80. DO_BINOP_TEST(typename,type,qtype,div,/) \
  81. int test_##typename (void) \
  82. { \
  83. int error = 0; \
  84. error |= test_##typename##_plus(); \
  85. error |= test_##typename##_minus(); \
  86. error |= test_##typename##_mul(); \
  87. error |= test_##typename##_floor(); \
  88. error |= test_##typename##_div(); \
  89. return error; \
  90. }
  91. #endif /* _EXAM_H */