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