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.3 KiB

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