1 /*
2     Copyright (C) 2010, 2020 William Hart
3 
4     This file is part of FLINT.
5 
6     FLINT is free software: you can redistribute it and/or modify it under
7     the terms of the GNU Lesser General Public License (LGPL) as published
8     by the Free Software Foundation; either version 2.1 of the License, or
9     (at your option) any later version.  See <https://www.gnu.org/licenses/>.
10 */
11 
12 #ifdef T
13 
14 #include "templates.h"
15 
16 #include <stdio.h>
17 #include <stdlib.h>
18 
19 #include "ulong_extras.h"
20 #include "long_extras.h"
21 
22 int
main(void)23 main(void)
24 {
25     int i, result;
26     FLINT_TEST_INIT(state);
27 
28     flint_printf("pow_trunc_binexp....");
29     fflush(stdout);
30 
31     /* Check powering against naive method */
32     for (i = 0; i < 1000 * flint_test_multiplier(); i++)
33     {
34         TEMPLATE(T, poly_t) a, b, c;
35         TEMPLATE(T, ctx_t) ctx;
36         TEMPLATE(T, t) d;
37 	slong e, trunc;
38 
39 	TEMPLATE(T, ctx_randtest) (ctx, state);
40 
41         TEMPLATE(T, init) (d, ctx);
42 	TEMPLATE(T, poly_init) (a, ctx);
43         TEMPLATE(T, poly_init) (b, ctx);
44         TEMPLATE(T, poly_init) (c, ctx);
45         TEMPLATE(T, poly_randtest) (a, state, n_randint(state, 30), ctx);
46         e = n_randint(state, 20);
47         trunc = n_randint(state, 30);
48 
49         TEMPLATE(T, poly_pow_trunc_binexp) (b, a, e, trunc, ctx);
50 
51         TEMPLATE(T, poly_pow) (c, a, e, ctx);
52         TEMPLATE(T, poly_truncate) (c, trunc, ctx);
53 
54         TEMPLATE(T, poly_get_coeff) (d, c, 0, ctx);
55         result = (TEMPLATE(T, poly_equal) (b, c, ctx)
56             || (a->length == 0 && e == 0 && c->length == 1 && TEMPLATE(T, is_one) (d, ctx)));
57         if (!result)
58         {
59             flint_printf("FAIL:\n");
60             flint_printf("a->length = %wd, exp = %wd, trunc = %wd\n",
61                 a->length, e, trunc);
62             TEMPLATE(T, poly_print) (a, ctx), flint_printf("\n\n");
63             TEMPLATE(T, poly_print) (b, ctx), flint_printf("\n\n");
64             TEMPLATE(T, poly_print) (c, ctx), flint_printf("\n\n");
65             abort();
66         }
67 
68         TEMPLATE(T, clear) (d, ctx);
69         TEMPLATE(T, poly_clear) (a, ctx);
70         TEMPLATE(T, poly_clear) (b, ctx);
71         TEMPLATE(T, poly_clear) (c, ctx);
72         TEMPLATE(T, ctx_clear) (ctx);
73     }
74 
75     /* Check aliasing */
76     for (i = 0; i < 1000 * flint_test_multiplier(); i++)
77     {
78         TEMPLATE(T, poly_t) a, b, c;
79         TEMPLATE(T, ctx_t) ctx;
80 	slong e, trunc;
81 
82         TEMPLATE(T, ctx_randtest) (ctx, state);
83 
84         TEMPLATE(T, poly_init) (a, ctx);
85         TEMPLATE(T, poly_init) (b, ctx);
86         TEMPLATE(T, poly_init) (c, ctx);
87         TEMPLATE(T, poly_randtest) (a, state, n_randint(state, 30), ctx);
88         e = n_randint(state, 20);
89         trunc = n_randint(state, 30);
90 
91         TEMPLATE(T, poly_pow_trunc_binexp) (b, a, e, trunc, ctx);
92 
93         TEMPLATE(T, poly_set) (c, a, ctx);
94         TEMPLATE(T, poly_pow_trunc_binexp) (c, c, e, trunc, ctx);
95 
96         result = (TEMPLATE(T, poly_equal) (b, c, ctx));
97         if (!result)
98         {
99             flint_printf("FAIL:\n");
100             flint_printf("a->length = %wd, exp = %wd, trunc = %wd\n",
101                 a->length, e, trunc);
102             TEMPLATE(T, poly_print) (a, ctx), flint_printf("\n\n");
103             TEMPLATE(T, poly_print) (b, ctx), flint_printf("\n\n");
104             TEMPLATE(T, poly_print) (c, ctx), flint_printf("\n\n");
105             abort();
106         }
107 
108         TEMPLATE(T, poly_clear) (a, ctx);
109         TEMPLATE(T, poly_clear) (b, ctx);
110         TEMPLATE(T, poly_clear) (c, ctx);
111         TEMPLATE(T, ctx_clear) (ctx);
112     }
113 
114     FLINT_TEST_CLEANUP(state);
115 
116     flint_printf("PASS\n");
117     return 0;
118 }
119 
120 #endif
121 
122