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