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