1 /*
2     Copyright (C) 2011 Fredrik Johansson
3     Copyright (C) 2012 Lina Kulakova
4     Copyright (C) 2013 Martin Lee
5     Copyright (C) 2013 Mike Hansen
6 
7     This file is part of FLINT.
8 
9     FLINT is free software: you can redistribute it and/or modify it under
10     the terms of the GNU Lesser General Public License (LGPL) as published
11     by the Free Software Foundation; either version 2.1 of the License, or
12     (at your option) any later version.  See <https://www.gnu.org/licenses/>.
13 */
14 
15 #ifdef T
16 
17 #include "templates.h"
18 
19 int
main(void)20 main(void)
21 {
22     int i;
23     FLINT_TEST_INIT(state);
24 
25     flint_printf("compose_mod_brent_kung_preinv....");
26     fflush(stdout);
27 
28     for (i = 0; i < 20 * flint_test_multiplier(); i++)
29     {
30         TEMPLATE(T, ctx_t) ctx;
31         TEMPLATE(T, poly_t) a, b, c, cinv, d, e;
32 
33         TEMPLATE(T, ctx_randtest) (ctx, state);
34 
35         TEMPLATE(T, poly_init) (a, ctx);
36         TEMPLATE(T, poly_init) (b, ctx);
37         TEMPLATE(T, poly_init) (c, ctx);
38         TEMPLATE(T, poly_init) (cinv, ctx);
39         TEMPLATE(T, poly_init) (d, ctx);
40         TEMPLATE(T, poly_init) (e, ctx);
41 
42         TEMPLATE(T, poly_randtest) (a, state, n_randint(state, 20) + 1, ctx);
43         TEMPLATE(T, poly_randtest) (b, state, n_randint(state, 20) + 1, ctx);
44         TEMPLATE(T, poly_randtest_not_zero) (c, state,
45                                              n_randint(state, 20) + 1, ctx);
46 
47         TEMPLATE(T, poly_reverse) (cinv, c, c->length, ctx);
48         TEMPLATE(T, poly_inv_series_newton) (cinv, cinv, c->length, ctx);
49 
50         TEMPLATE(T, poly_rem) (a, a, c, ctx);
51         TEMPLATE(T, poly_compose_mod_brent_kung_preinv) (d, a, b, c, cinv,
52                                                          ctx);
53         TEMPLATE(T, poly_compose) (e, a, b, ctx);
54         TEMPLATE(T, poly_rem) (e, e, c, ctx);
55 
56         if (!TEMPLATE(T, poly_equal) (d, e, ctx))
57         {
58             flint_printf("FAIL (composition):\n");
59             flint_printf("a:\n");
60             TEMPLATE(T, poly_print) (a, ctx);
61             flint_printf("\n");
62             flint_printf("b:\n");
63             TEMPLATE(T, poly_print) (b, ctx);
64             flint_printf("\n");
65             flint_printf("c:\n");
66             TEMPLATE(T, poly_print) (c, ctx);
67             flint_printf("\n");
68             flint_printf("d:\n");
69             TEMPLATE(T, poly_print) (d, ctx);
70             flint_printf("\n");
71             flint_printf("e:\n");
72             TEMPLATE(T, poly_print) (e, ctx);
73             flint_printf("\n");
74             abort();
75         }
76 
77         TEMPLATE(T, poly_clear) (a, ctx);
78         TEMPLATE(T, poly_clear) (b, ctx);
79         TEMPLATE(T, poly_clear) (c, ctx);
80         TEMPLATE(T, poly_clear) (cinv, ctx);
81         TEMPLATE(T, poly_clear) (d, ctx);
82         TEMPLATE(T, poly_clear) (e, ctx);
83 
84         TEMPLATE(T, ctx_clear) (ctx);
85     }
86 
87     /* Test aliasing of res and a */
88     for (i = 0; i < 20 * flint_test_multiplier(); i++)
89     {
90         TEMPLATE(T, ctx_t) ctx;
91         TEMPLATE(T, poly_t) a, b, c, cinv, d;
92 
93         TEMPLATE(T, ctx_randtest) (ctx, state);
94 
95         TEMPLATE(T, poly_init) (a, ctx);
96         TEMPLATE(T, poly_init) (b, ctx);
97         TEMPLATE(T, poly_init) (c, ctx);
98         TEMPLATE(T, poly_init) (cinv, ctx);
99         TEMPLATE(T, poly_init) (d, ctx);
100 
101         TEMPLATE(T, poly_randtest) (a, state, n_randint(state, 20) + 1, ctx);
102         TEMPLATE(T, poly_randtest) (b, state, n_randint(state, 20) + 1, ctx);
103         TEMPLATE(T, poly_randtest_not_zero) (c, state,
104                                              n_randint(state, 20) + 1, ctx);
105 
106         TEMPLATE(T, poly_reverse) (cinv, c, c->length, ctx);
107         TEMPLATE(T, poly_inv_series_newton) (cinv, cinv, c->length, ctx);
108 
109         TEMPLATE(T, poly_rem) (a, a, c, ctx);
110         TEMPLATE(T, poly_compose_mod_brent_kung_preinv) (d, a, b, c, cinv,
111                                                          ctx);
112         TEMPLATE(T, poly_compose_mod_brent_kung_preinv) (a, a, b, c, cinv,
113                                                          ctx);
114 
115         if (!TEMPLATE(T, poly_equal) (d, a, ctx))
116         {
117             flint_printf("FAIL (aliasing a):\n");
118             flint_printf("a:\n");
119             TEMPLATE(T, poly_print) (a, ctx);
120             flint_printf("\n");
121             flint_printf("b:\n");
122             TEMPLATE(T, poly_print) (b, ctx);
123             flint_printf("\n");
124             flint_printf("c:\n");
125             TEMPLATE(T, poly_print) (c, ctx);
126             flint_printf("\n");
127             flint_printf("d:\n");
128             TEMPLATE(T, poly_print) (d, ctx);
129             flint_printf("\n");
130             abort();
131         }
132 
133         TEMPLATE(T, poly_clear) (a, ctx);
134         TEMPLATE(T, poly_clear) (b, ctx);
135         TEMPLATE(T, poly_clear) (c, ctx);
136         TEMPLATE(T, poly_clear) (cinv, ctx);
137         TEMPLATE(T, poly_clear) (d, ctx);
138 
139         TEMPLATE(T, ctx_clear) (ctx);
140     }
141 
142     /* Test aliasing of res and b */
143     for (i = 0; i < 20 * flint_test_multiplier(); i++)
144     {
145         TEMPLATE(T, ctx_t) ctx;
146         TEMPLATE(T, poly_t) a, b, c, cinv, d;
147 
148         TEMPLATE(T, ctx_randtest) (ctx, state);
149 
150         TEMPLATE(T, poly_init) (a, ctx);
151         TEMPLATE(T, poly_init) (b, ctx);
152         TEMPLATE(T, poly_init) (c, ctx);
153         TEMPLATE(T, poly_init) (cinv, ctx);
154         TEMPLATE(T, poly_init) (d, ctx);
155 
156         TEMPLATE(T, poly_randtest) (a, state, n_randint(state, 20) + 1, ctx);
157         TEMPLATE(T, poly_randtest) (b, state, n_randint(state, 20) + 1, ctx);
158         TEMPLATE(T, poly_randtest_not_zero) (c, state,
159                                              n_randint(state, 20) + 1, ctx);
160 
161         TEMPLATE(T, poly_reverse) (cinv, c, c->length, ctx);
162         TEMPLATE(T, poly_inv_series_newton) (cinv, cinv, c->length, ctx);
163 
164         TEMPLATE(T, poly_rem) (a, a, c, ctx);
165         TEMPLATE(T, poly_compose_mod_brent_kung_preinv) (d, a, b, c, cinv,
166                                                          ctx);
167         TEMPLATE(T, poly_compose_mod_brent_kung_preinv) (b, a, b, c, cinv,
168                                                          ctx);
169 
170         if (!TEMPLATE(T, poly_equal) (d, b, ctx))
171         {
172             flint_printf("FAIL (aliasing b)\n");
173             flint_printf("a:\n");
174             TEMPLATE(T, poly_print) (a, ctx);
175             flint_printf("\n");
176             flint_printf("b:\n");
177             TEMPLATE(T, poly_print) (b, ctx);
178             flint_printf("\n");
179             flint_printf("c:\n");
180             TEMPLATE(T, poly_print) (c, ctx);
181             flint_printf("\n");
182             flint_printf("d:\n");
183             TEMPLATE(T, poly_print) (d, ctx);
184             flint_printf("\n");
185             abort();
186         }
187 
188         TEMPLATE(T, poly_clear) (a, ctx);
189         TEMPLATE(T, poly_clear) (b, ctx);
190         TEMPLATE(T, poly_clear) (c, ctx);
191         TEMPLATE(T, poly_clear) (cinv, ctx);
192         TEMPLATE(T, poly_clear) (d, ctx);
193 
194         TEMPLATE(T, ctx_clear) (ctx);
195     }
196 
197     /* Test aliasing of res and c */
198     for (i = 0; i < 20 * flint_test_multiplier(); i++)
199     {
200         TEMPLATE(T, ctx_t) ctx;
201         TEMPLATE(T, poly_t) a, b, c, cinv, d;
202 
203         TEMPLATE(T, ctx_randtest) (ctx, state);
204 
205         TEMPLATE(T, poly_init) (a, ctx);
206         TEMPLATE(T, poly_init) (b, ctx);
207         TEMPLATE(T, poly_init) (c, ctx);
208         TEMPLATE(T, poly_init) (cinv, ctx);
209         TEMPLATE(T, poly_init) (d, ctx);
210 
211         TEMPLATE(T, poly_randtest) (a, state, n_randint(state, 20) + 1, ctx);
212         TEMPLATE(T, poly_randtest) (b, state, n_randint(state, 20) + 1, ctx);
213         TEMPLATE(T, poly_randtest_not_zero) (c, state,
214                                              n_randint(state, 20) + 1, ctx);
215 
216         TEMPLATE(T, poly_reverse) (cinv, c, c->length, ctx);
217         TEMPLATE(T, poly_inv_series_newton) (cinv, cinv, c->length, ctx);
218 
219         TEMPLATE(T, poly_rem) (a, a, c, ctx);
220         TEMPLATE(T, poly_compose_mod_brent_kung_preinv) (d, a, b, c, cinv,
221                                                          ctx);
222         TEMPLATE(T, poly_compose_mod_brent_kung_preinv) (c, a, b, c, cinv,
223                                                          ctx);
224 
225         if (!TEMPLATE(T, poly_equal) (d, c, ctx))
226         {
227             flint_printf("FAIL (aliasing c)\n");
228             flint_printf("a:\n");
229             TEMPLATE(T, poly_print) (a, ctx);
230             flint_printf("\n");
231             flint_printf("b:\n");
232             TEMPLATE(T, poly_print) (b, ctx);
233             flint_printf("\n");
234             flint_printf("c:\n");
235             TEMPLATE(T, poly_print) (c, ctx);
236             flint_printf("\n");
237             flint_printf("d:\n");
238             TEMPLATE(T, poly_print) (d, ctx);
239             flint_printf("\n");
240             abort();
241         }
242 
243         TEMPLATE(T, poly_clear) (a, ctx);
244         TEMPLATE(T, poly_clear) (b, ctx);
245         TEMPLATE(T, poly_clear) (c, ctx);
246         TEMPLATE(T, poly_clear) (cinv, ctx);
247         TEMPLATE(T, poly_clear) (d, ctx);
248 
249         TEMPLATE(T, ctx_clear) (ctx);
250     }
251 
252     FLINT_TEST_CLEANUP(state);
253     flint_printf("PASS\n");
254     return 0;
255 }
256 
257 
258 #endif
259