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 <http://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_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_preinv) (d, a, b, c, cinv, ctx);
52         TEMPLATE(T, poly_compose) (e, a, b, ctx);
53         TEMPLATE(T, poly_rem) (e, e, c, ctx);
54 
55         if (!TEMPLATE(T, poly_equal) (d, e, ctx))
56         {
57             flint_printf("FAIL (composition):\n");
58             flint_printf("a:\n");
59             TEMPLATE(T, poly_print) (a, ctx);
60             flint_printf("\n");
61             flint_printf("b:\n");
62             TEMPLATE(T, poly_print) (b, ctx);
63             flint_printf("\n");
64             flint_printf("c:\n");
65             TEMPLATE(T, poly_print) (c, ctx);
66             flint_printf("\n");
67             flint_printf("d:\n");
68             TEMPLATE(T, poly_print) (d, ctx);
69             flint_printf("\n");
70             flint_printf("e:\n");
71             TEMPLATE(T, poly_print) (e, ctx);
72             flint_printf("\n");
73             abort();
74         }
75 
76         TEMPLATE(T, poly_clear) (a, ctx);
77         TEMPLATE(T, poly_clear) (b, ctx);
78         TEMPLATE(T, poly_clear) (c, ctx);
79         TEMPLATE(T, poly_clear) (cinv, ctx);
80         TEMPLATE(T, poly_clear) (d, ctx);
81         TEMPLATE(T, poly_clear) (e, ctx);
82 
83         TEMPLATE(T, ctx_clear) (ctx);
84     }
85 
86     /* Test aliasing of res and a */
87     for (i = 0; i < 20 * flint_test_multiplier(); i++)
88     {
89         TEMPLATE(T, ctx_t) ctx;
90         TEMPLATE(T, poly_t) a, b, c, cinv, d;
91 
92         TEMPLATE(T, ctx_randtest) (ctx, state);
93 
94         TEMPLATE(T, poly_init) (a, ctx);
95         TEMPLATE(T, poly_init) (b, ctx);
96         TEMPLATE(T, poly_init) (c, ctx);
97         TEMPLATE(T, poly_init) (cinv, ctx);
98         TEMPLATE(T, poly_init) (d, ctx);
99 
100         TEMPLATE(T, poly_randtest) (a, state, n_randint(state, 20) + 1, ctx);
101         TEMPLATE(T, poly_randtest) (b, state, n_randint(state, 20) + 1, ctx);
102         TEMPLATE(T, poly_randtest_not_zero) (c, state,
103                                              n_randint(state, 20) + 1, ctx);
104 
105         TEMPLATE(T, poly_reverse) (cinv, c, c->length, ctx);
106         TEMPLATE(T, poly_inv_series_newton) (cinv, cinv, c->length, ctx);
107 
108         TEMPLATE(T, poly_rem) (a, a, c, ctx);
109         TEMPLATE(T, poly_compose_mod_preinv) (d, a, b, c, cinv, ctx);
110         TEMPLATE(T, poly_compose_mod_preinv) (a, a, b, c, cinv, ctx);
111 
112         if (!TEMPLATE(T, poly_equal) (d, a, ctx))
113         {
114             flint_printf("FAIL (aliasing a):\n");
115             flint_printf("a:\n");
116             TEMPLATE(T, poly_print) (a, ctx);
117             flint_printf("\n");
118             flint_printf("b:\n");
119             TEMPLATE(T, poly_print) (b, ctx);
120             flint_printf("\n");
121             flint_printf("c:\n");
122             TEMPLATE(T, poly_print) (c, ctx);
123             flint_printf("\n");
124             flint_printf("d:\n");
125             TEMPLATE(T, poly_print) (d, ctx);
126             flint_printf("\n");
127             abort();
128         }
129 
130         TEMPLATE(T, poly_clear) (a, ctx);
131         TEMPLATE(T, poly_clear) (b, ctx);
132         TEMPLATE(T, poly_clear) (c, ctx);
133         TEMPLATE(T, poly_clear) (cinv, ctx);
134         TEMPLATE(T, poly_clear) (d, ctx);
135 
136         TEMPLATE(T, ctx_clear) (ctx);
137     }
138 
139     /* Test aliasing of res and b */
140     for (i = 0; i < 20 * flint_test_multiplier(); i++)
141     {
142         TEMPLATE(T, ctx_t) ctx;
143         TEMPLATE(T, poly_t) a, b, c, cinv, d;
144 
145         TEMPLATE(T, ctx_randtest) (ctx, state);
146 
147         TEMPLATE(T, poly_init) (a, ctx);
148         TEMPLATE(T, poly_init) (b, ctx);
149         TEMPLATE(T, poly_init) (c, ctx);
150         TEMPLATE(T, poly_init) (cinv, ctx);
151         TEMPLATE(T, poly_init) (d, ctx);
152 
153         TEMPLATE(T, poly_randtest) (a, state, n_randint(state, 20) + 1, ctx);
154         TEMPLATE(T, poly_randtest) (b, state, n_randint(state, 20) + 1, ctx);
155         TEMPLATE(T, poly_randtest_not_zero) (c, state,
156                                              n_randint(state, 20) + 1, ctx);
157 
158         TEMPLATE(T, poly_reverse) (cinv, c, c->length, ctx);
159         TEMPLATE(T, poly_inv_series_newton) (cinv, cinv, c->length, ctx);
160 
161         TEMPLATE(T, poly_rem) (a, a, c, ctx);
162         TEMPLATE(T, poly_compose_mod_preinv) (d, a, b, c, cinv, ctx);
163         TEMPLATE(T, poly_compose_mod_preinv) (b, a, b, c, cinv, ctx);
164 
165         if (!TEMPLATE(T, poly_equal) (d, b, ctx))
166         {
167             flint_printf("FAIL (aliasing b)\n");
168             flint_printf("a:\n");
169             TEMPLATE(T, poly_print) (a, ctx);
170             flint_printf("\n");
171             flint_printf("b:\n");
172             TEMPLATE(T, poly_print) (b, ctx);
173             flint_printf("\n");
174             flint_printf("c:\n");
175             TEMPLATE(T, poly_print) (c, ctx);
176             flint_printf("\n");
177             flint_printf("d:\n");
178             TEMPLATE(T, poly_print) (d, ctx);
179             flint_printf("\n");
180             abort();
181         }
182 
183         TEMPLATE(T, poly_clear) (a, ctx);
184         TEMPLATE(T, poly_clear) (b, ctx);
185         TEMPLATE(T, poly_clear) (c, ctx);
186         TEMPLATE(T, poly_clear) (cinv, ctx);
187         TEMPLATE(T, poly_clear) (d, ctx);
188 
189         TEMPLATE(T, ctx_clear) (ctx);
190     }
191 
192     /* Test aliasing of res and c */
193     for (i = 0; i < 20 * flint_test_multiplier(); i++)
194     {
195         TEMPLATE(T, ctx_t) ctx;
196         TEMPLATE(T, poly_t) a, b, c, cinv, d;
197 
198         TEMPLATE(T, ctx_randtest) (ctx, state);
199 
200         TEMPLATE(T, poly_init) (a, ctx);
201         TEMPLATE(T, poly_init) (b, ctx);
202         TEMPLATE(T, poly_init) (c, ctx);
203         TEMPLATE(T, poly_init) (cinv, ctx);
204         TEMPLATE(T, poly_init) (d, ctx);
205 
206         TEMPLATE(T, poly_randtest) (a, state, n_randint(state, 20) + 1, ctx);
207         TEMPLATE(T, poly_randtest) (b, state, n_randint(state, 20) + 1, ctx);
208         TEMPLATE(T, poly_randtest_not_zero) (c, state,
209                                              n_randint(state, 20) + 1, ctx);
210 
211         TEMPLATE(T, poly_reverse) (cinv, c, c->length, ctx);
212         TEMPLATE(T, poly_inv_series_newton) (cinv, cinv, c->length, ctx);
213 
214         TEMPLATE(T, poly_rem) (a, a, c, ctx);
215         TEMPLATE(T, poly_compose_mod_preinv) (d, a, b, c, cinv, ctx);
216         TEMPLATE(T, poly_compose_mod_preinv) (c, a, b, c, cinv, ctx);
217 
218         if (!TEMPLATE(T, poly_equal) (d, c, ctx))
219         {
220             flint_printf("FAIL (aliasing c)\n");
221             flint_printf("a:\n");
222             TEMPLATE(T, poly_print) (a, ctx);
223             flint_printf("\n");
224             flint_printf("b:\n");
225             TEMPLATE(T, poly_print) (b, ctx);
226             flint_printf("\n");
227             flint_printf("c:\n");
228             TEMPLATE(T, poly_print) (c, ctx);
229             flint_printf("\n");
230             flint_printf("d:\n");
231             TEMPLATE(T, poly_print) (d, ctx);
232             flint_printf("\n");
233             abort();
234         }
235 
236         TEMPLATE(T, poly_clear) (a, ctx);
237         TEMPLATE(T, poly_clear) (b, ctx);
238         TEMPLATE(T, poly_clear) (c, ctx);
239         TEMPLATE(T, poly_clear) (cinv, ctx);
240         TEMPLATE(T, poly_clear) (d, ctx);
241 
242         TEMPLATE(T, ctx_clear) (ctx);
243     }
244 
245     FLINT_TEST_CLEANUP(state);
246     flint_printf("PASS\n");
247     return 0;
248 }
249 
250 
251 #endif
252