1 /*
2     Copyright (C) 2013 Fredrik Johansson
3     Copyright (C) 2013 William Hart
4     Copyright (C) 2011 Sebastian Pancratz
5 
6     This file is part of FLINT.
7 
8     FLINT is free software: you can redistribute it and/or modify it under
9     the terms of the GNU Lesser General Public License (LGPL) as published
10     by the Free Software Foundation; either version 2.1 of the License, or
11     (at your option) any later version.  See <http://www.gnu.org/licenses/>.
12 */
13 
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <string.h>
17 
18 #include "fmpz_poly_q.h"
19 
test_set(char * in,char * out)20 void test_set(char * in, char * out)
21 {
22     int ans;
23     fmpz_poly_q_t rop, op;
24     char * res;
25 
26     fmpz_poly_q_init(op);
27     fmpz_poly_q_set_str(op, in);
28 
29     fmpz_poly_q_init(rop);
30     fmpz_poly_q_set(rop, op);
31 
32     res = fmpz_poly_q_get_str(rop);
33     ans = !strcmp(out, res);
34 
35     if (!ans)
36     {
37         flint_printf("test_set: failed\n");
38         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
39         abort();
40     }
41 
42     fmpz_poly_q_clear(op);
43     fmpz_poly_q_clear(rop);
44     flint_free(res);
45 }
46 
test_set_si(slong x,char * out)47 void test_set_si(slong x, char * out)
48 {
49     int ans;
50     fmpz_poly_q_t rop;
51     char * res;
52 
53     fmpz_poly_q_init(rop);
54     fmpz_poly_q_set_si(rop, x);
55 
56     res = fmpz_poly_q_get_str(rop);
57     ans = !strcmp(out, res);
58 
59     if (!ans)
60     {
61         flint_printf("test_set_si: failed\n");
62         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
63         abort();
64     }
65 
66     fmpz_poly_q_clear(rop);
67     flint_free(res);
68 }
69 
test_swap(char * in1,char * in2,char * out1,char * out2)70 void test_swap(char * in1, char * in2, char * out1, char * out2)
71 {
72     int ans;
73     fmpz_poly_q_t op1, op2;
74     char * res1;
75     char * res2;
76 
77     fmpz_poly_q_init(op1);
78     fmpz_poly_q_set_str(op1, in1);
79     fmpz_poly_q_init(op2);
80     fmpz_poly_q_set_str(op2, in2);
81 
82     fmpz_poly_q_swap(op1, op2);
83 
84     res1 = fmpz_poly_q_get_str(op1);
85     res2 = fmpz_poly_q_get_str(op2);
86 
87     ans = !strcmp(out1, res1) && !strcmp(out2, res2);
88 
89     if (!ans)
90     {
91         flint_printf("test_swap: failed\n");
92         flint_printf("    Expected \"%s\" \"%s\", got \"%s\" \"%s\"\n", out1, out2, res1, res2);
93         abort();
94     }
95 
96     fmpz_poly_q_clear(op1);
97     fmpz_poly_q_clear(op2);
98     flint_free(res1);
99     flint_free(res2);
100 }
101 
test_zero(char * in,char * out)102 void test_zero(char * in, char * out)
103 {
104     int ans;
105     fmpz_poly_q_t op;
106     char * res;
107 
108     fmpz_poly_q_init(op);
109     fmpz_poly_q_set_str(op, in);
110 
111     fmpz_poly_q_zero(op);
112 
113     res = fmpz_poly_q_get_str(op);
114     ans = !strcmp(res, out);
115 
116     if (!ans)
117     {
118         flint_printf("test_zero: failed\n");
119         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
120         abort();
121     }
122 
123     fmpz_poly_q_clear(op);
124     flint_free(res);
125 }
126 
test_neg(char * in,char * out)127 void test_neg(char * in, char * out)
128 {
129     int ans;
130     fmpz_poly_q_t rop, op;
131     char * res;
132 
133     fmpz_poly_q_init(op);
134     fmpz_poly_q_set_str(op, in);
135 
136     fmpz_poly_q_init(rop);
137     fmpz_poly_q_neg(rop, op);
138 
139     res = fmpz_poly_q_get_str(rop);
140     ans = !strcmp(res, out);
141 
142     if (!ans)
143     {
144         flint_printf("test_neg: failed\n");
145         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
146         abort();
147     }
148 
149     fmpz_poly_q_clear(op);
150     fmpz_poly_q_clear(rop);
151     flint_free(res);
152 }
153 
test_inv(char * in,char * out)154 void test_inv(char * in, char * out)
155 {
156     int ans;
157     fmpz_poly_q_t rop, op;
158     char * res;
159 
160     fmpz_poly_q_init(op);
161     fmpz_poly_q_set_str(op, in);
162     fmpz_poly_q_init(rop);
163     fmpz_poly_q_inv(rop, op);
164 
165     res = fmpz_poly_q_get_str(rop);
166     ans = !strcmp(res, out);
167 
168     if (!ans)
169     {
170         flint_printf("test_inv: failed\n");
171         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
172         abort();
173     }
174 
175     fmpz_poly_q_clear(op);
176     fmpz_poly_q_clear(rop);
177     flint_free(res);
178 }
179 
test_inv_inplace(char * in,char * out)180 void test_inv_inplace(char * in, char * out)
181 {
182     int ans;
183     fmpz_poly_q_t rop;
184     char * res;
185 
186     fmpz_poly_q_init(rop);
187     fmpz_poly_q_set_str(rop, in);
188     fmpz_poly_q_inv(rop, rop);
189 
190     res = fmpz_poly_q_get_str(rop);
191     ans = !strcmp(res, out);
192 
193     if (!ans)
194     {
195         flint_printf("test_inv_inplace: failed\n");
196         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
197         abort();
198     }
199 
200     fmpz_poly_q_clear(rop);
201     flint_free(res);
202 }
203 
test_is_zero(char * in,int out)204 void test_is_zero(char * in, int out)
205 {
206     int ans;
207     fmpz_poly_q_t op;
208     int res;
209 
210     fmpz_poly_q_init(op);
211     fmpz_poly_q_set_str(op, in);
212 
213     res = fmpz_poly_q_is_zero(op);
214     ans = (res == out);
215 
216     if (!ans)
217     {
218         flint_printf("test_equal: failed\n");
219         flint_printf("    Expected \"%d\", got \"%d\"\n", out, res);
220         abort();
221     }
222 
223     fmpz_poly_q_clear(op);
224 }
225 
test_is_one(char * in,int out)226 void test_is_one(char * in, int out)
227 {
228     int ans;
229     fmpz_poly_q_t op;
230     int res;
231 
232     fmpz_poly_q_init(op);
233     fmpz_poly_q_set_str(op, in);
234 
235     res = fmpz_poly_q_is_one(op);
236     ans = (res == out);
237 
238     if (!ans)
239     {
240         flint_printf("test_equal: failed\n");
241         flint_printf("    Expected \"%d\", got \"%d\"\n", out, res);
242         abort();
243     }
244 
245     fmpz_poly_q_clear(op);
246 }
247 
test_equal(char * in1,char * in2,int out)248 void test_equal(char * in1, char * in2, int out)
249 {
250     int ans;
251     fmpz_poly_q_t op1, op2;
252     int res;
253 
254     fmpz_poly_q_init(op1);
255     fmpz_poly_q_set_str(op1, in1);
256 
257     fmpz_poly_q_init(op2);
258     fmpz_poly_q_set_str(op2, in2);
259 
260     res = fmpz_poly_q_equal(op1, op2);
261     ans = (res == out);
262 
263     if (!ans)
264     {
265         flint_printf("test_equal: failed\n");
266         flint_printf("    Expected \"%d\", got \"%d\"\n", out, res);
267         abort();
268     }
269 
270     fmpz_poly_q_clear(op1);
271     fmpz_poly_q_clear(op2);
272 }
273 
test_add(char * in1,char * in2,char * out)274 void test_add(char * in1, char * in2, char * out)
275 {
276     int ans;
277     fmpz_poly_q_t rop, op1, op2;
278     char * res;
279 
280     fmpz_poly_q_init(op1);
281     fmpz_poly_q_set_str(op1, in1);
282 
283     fmpz_poly_q_init(op2);
284     fmpz_poly_q_set_str(op2, in2);
285 
286     fmpz_poly_q_init(rop);
287     fmpz_poly_q_add(rop, op1, op2);
288 
289     res = fmpz_poly_q_get_str(rop);
290     ans = !strcmp(out, res);
291 
292     if (!ans)
293     {
294         flint_printf("test_add: failed\n");
295         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
296         abort();
297     }
298 
299     fmpz_poly_q_clear(op1);
300     fmpz_poly_q_clear(op2);
301     fmpz_poly_q_clear(rop);
302     flint_free(res);
303 }
304 
305 /* Runs in1 = in1 + in2 */
test_add_in_place1(char * in1,char * in2,char * out)306 void test_add_in_place1(char * in1, char * in2, char * out)
307 {
308     int ans;
309     fmpz_poly_q_t op1, op2;
310     char * res;
311 
312     fmpz_poly_q_init(op1);
313     fmpz_poly_q_set_str(op1, in1);
314 
315     fmpz_poly_q_init(op2);
316     fmpz_poly_q_set_str(op2, in2);
317 
318     fmpz_poly_q_add(op1, op1, op2);
319 
320     res = fmpz_poly_q_get_str(op1);
321     ans = !strcmp(out, res);
322 
323     if (!ans)
324     {
325         flint_printf("test_add_in_place1: failed\n");
326         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
327         abort();
328     }
329 
330     fmpz_poly_q_clear(op1);
331     fmpz_poly_q_clear(op2);
332     flint_free(res);
333 }
334 
335 /* Runs in2 = in1 + in2 */
test_add_in_place2(char * in1,char * in2,char * out)336 void test_add_in_place2(char * in1, char * in2, char * out)
337 {
338     int ans;
339     fmpz_poly_q_t op1, op2;
340     char * res;
341 
342     fmpz_poly_q_init(op1);
343     fmpz_poly_q_set_str(op1, in1);
344 
345     fmpz_poly_q_init(op2);
346     fmpz_poly_q_set_str(op2, in2);
347 
348     fmpz_poly_q_add(op2, op1, op2);
349 
350     res = fmpz_poly_q_get_str(op2);
351     ans = !strcmp(out, res);
352 
353     if (!ans)
354     {
355         flint_printf("test_add_in_place2: failed\n");
356         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
357         abort();
358     }
359 
360     fmpz_poly_q_clear(op1);
361     fmpz_poly_q_clear(op2);
362     flint_free(res);
363 }
364 
365 /* Runs out = in + in */
test_add_in_place3(char * in,char * out)366 void test_add_in_place3(char * in, char * out)
367 {
368     int ans;
369     fmpz_poly_q_t rop, op;
370     char * res;
371 
372     fmpz_poly_q_init(op);
373     fmpz_poly_q_set_str(op, in);
374 
375     fmpz_poly_q_init(rop);
376     fmpz_poly_q_add(rop, op, op);
377 
378     res = fmpz_poly_q_get_str(rop);
379     ans = !strcmp(out, res);
380 
381     if (!ans)
382     {
383         flint_printf("test_add_in_place3: failed\n");
384         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
385         abort();
386     }
387 
388     fmpz_poly_q_clear(rop);
389     fmpz_poly_q_clear(op);
390     flint_free(res);
391 }
392 
test_sub(char * in1,char * in2,char * out)393 void test_sub(char * in1, char * in2, char * out)
394 {
395     int ans;
396     fmpz_poly_q_t rop, op1, op2;
397     char * res;
398 
399     fmpz_poly_q_init(op1);
400     fmpz_poly_q_set_str(op1, in1);
401 
402     fmpz_poly_q_init(op2);
403     fmpz_poly_q_set_str(op2, in2);
404 
405     fmpz_poly_q_init(rop);
406     fmpz_poly_q_sub(rop, op1, op2);
407 
408     res = fmpz_poly_q_get_str(rop);
409     ans = !strcmp(out, res);
410 
411     if (!ans)
412     {
413         flint_printf("test_sub: failed\n");
414         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
415         abort();
416     }
417 
418     fmpz_poly_q_clear(op1);
419     fmpz_poly_q_clear(op2);
420     fmpz_poly_q_clear(rop);
421     flint_free(res);
422 }
423 
424 /* in1 = in1 + in2 */
test_sub_in_place1(char * in1,char * in2,char * out)425 void test_sub_in_place1(char * in1, char * in2, char * out)
426 {
427     int ans;
428     fmpz_poly_q_t op1, op2;
429     char * res;
430 
431     fmpz_poly_q_init(op1);
432     fmpz_poly_q_set_str(op1, in1);
433 
434     fmpz_poly_q_init(op2);
435     fmpz_poly_q_set_str(op2, in2);
436 
437     fmpz_poly_q_sub(op1, op1, op2);
438 
439     res = fmpz_poly_q_get_str(op1);
440     ans = !strcmp(out, res);
441 
442     if (!ans)
443     {
444         flint_printf("test_sub_in_place1: failed\n");
445         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
446         abort();
447     }
448 
449     fmpz_poly_q_clear(op1);
450     fmpz_poly_q_clear(op2);
451     flint_free(res);
452 }
453 
454 /* in2 = in1 + in2 */
test_sub_in_place2(char * in1,char * in2,char * out)455 void test_sub_in_place2(char * in1, char * in2, char * out)
456 {
457     int ans;
458     fmpz_poly_q_t op1, op2;
459     char * res;
460 
461     fmpz_poly_q_init(op1);
462     fmpz_poly_q_set_str(op1, in1);
463 
464     fmpz_poly_q_init(op2);
465     fmpz_poly_q_set_str(op2, in2);
466 
467     fmpz_poly_q_sub(op2, op1, op2);
468 
469     res = fmpz_poly_q_get_str(op2);
470     ans = !strcmp(out, res);
471 
472     if (!ans)
473     {
474         flint_printf("test_sub_in_place2: failed\n");
475         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
476         abort();
477     }
478 
479     fmpz_poly_q_clear(op1);
480     fmpz_poly_q_clear(op2);
481     flint_free(res);
482 }
483 
484 /* Runs out = in - in */
test_sub_in_place3(char * in,char * out)485 void test_sub_in_place3(char * in, char * out)
486 {
487     int ans;
488     fmpz_poly_q_t rop, op;
489     char * res;
490 
491     fmpz_poly_q_init(op);
492     fmpz_poly_q_set_str(op, in);
493 
494     fmpz_poly_q_init(rop);
495     fmpz_poly_q_sub(rop, op, op);
496 
497     res = fmpz_poly_q_get_str(rop);
498     ans = !strcmp(out, res);
499 
500     if (!ans)
501     {
502         flint_printf("test_sub_in_place3: failed\n");
503         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
504         abort();
505     }
506 
507     fmpz_poly_q_clear(rop);
508     fmpz_poly_q_clear(op);
509     flint_free(res);
510 }
511 
test_scalar_mul_si(char * in,slong x,char * out)512 void test_scalar_mul_si(char * in, slong x, char * out)
513 {
514     int ans;
515     fmpz_poly_q_t rop, op;
516     char * res;
517 
518     fmpz_poly_q_init(op);
519     fmpz_poly_q_set_str(op, in);
520 
521     fmpz_poly_q_init(rop);
522     fmpz_poly_q_scalar_mul_si(rop, op, x);
523 
524     res = fmpz_poly_q_get_str(rop);
525     ans = !strcmp(out, res);
526 
527     if (!ans)
528     {
529         flint_printf("test_scalar_mul_si: failed\n");
530         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
531         abort();
532     }
533 
534     fmpz_poly_q_clear(op);
535     fmpz_poly_q_clear(rop);
536     flint_free(res);
537 }
538 
test_scalar_mul_mpz(char * in,mpz_t x,char * out)539 void test_scalar_mul_mpz(char * in, mpz_t x, char * out)
540 {
541     int ans;
542     fmpz_poly_q_t rop, op;
543     char * res;
544 
545     fmpz_poly_q_init(op);
546     fmpz_poly_q_set_str(op, in);
547 
548     fmpz_poly_q_init(rop);
549     fmpz_poly_q_scalar_mul_mpz(rop, op, x);
550 
551     res = fmpz_poly_q_get_str(rop);
552     ans = !strcmp(out, res);
553 
554     if (!ans)
555     {
556         flint_printf("test_scalar_mul_mpz: failed\n");
557         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
558         abort();
559     }
560 
561     fmpz_poly_q_clear(op);
562     fmpz_poly_q_clear(rop);
563     flint_free(res);
564 }
565 
test_scalar_mul_mpq(char * in,mpq_t x,char * out)566 void test_scalar_mul_mpq(char * in, mpq_t x, char * out)
567 {
568     int ans;
569     fmpz_poly_q_t rop, op;
570     char * res;
571 
572     fmpz_poly_q_init(op);
573     fmpz_poly_q_set_str(op, in);
574 
575     fmpz_poly_q_init(rop);
576     fmpz_poly_q_scalar_mul_mpq(rop, op, x);
577 
578     res = fmpz_poly_q_get_str(rop);
579     ans = !strcmp(out, res);
580 
581     if (!ans)
582     {
583         flint_printf("test_scalar_mul_mpq: failed\n");
584         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
585         abort();
586     }
587 
588     fmpz_poly_q_clear(op);
589     fmpz_poly_q_clear(rop);
590     flint_free(res);
591 }
592 
test_scalar_div_si(char * in,slong x,char * out)593 void test_scalar_div_si(char * in, slong x, char * out)
594 {
595     int ans;
596     fmpz_poly_q_t rop, op;
597     char * res;
598 
599     fmpz_poly_q_init(op);
600     fmpz_poly_q_set_str(op, in);
601 
602     fmpz_poly_q_init(rop);
603     fmpz_poly_q_scalar_div_si(rop, op, x);
604 
605     res = fmpz_poly_q_get_str(rop);
606     ans = !strcmp(out, res);
607 
608     if (!ans)
609     {
610         flint_printf("test_scalar_div_si: failed\n");
611         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
612         abort();
613     }
614 
615     fmpz_poly_q_clear(op);
616     fmpz_poly_q_clear(rop);
617     flint_free(res);
618 }
619 
test_scalar_div_mpz(char * in,mpz_t x,char * out)620 void test_scalar_div_mpz(char * in, mpz_t x, char * out)
621 {
622     int ans;
623     fmpz_poly_q_t rop, op;
624     char * res;
625 
626     fmpz_poly_q_init(op);
627     fmpz_poly_q_set_str(op, in);
628 
629     fmpz_poly_q_init(rop);
630     fmpz_poly_q_scalar_div_mpz(rop, op, x);
631 
632     res = fmpz_poly_q_get_str(rop);
633     ans = !strcmp(out, res);
634 
635     if (!ans)
636     {
637         flint_printf("test_scalar_div_mpz: failed\n");
638         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
639         abort();
640     }
641 
642     fmpz_poly_q_clear(op);
643     fmpz_poly_q_clear(rop);
644     flint_free(res);
645 }
646 
test_scalar_div_mpq(char * in,mpq_t x,char * out)647 void test_scalar_div_mpq(char * in, mpq_t x, char * out)
648 {
649     int ans;
650     fmpz_poly_q_t rop, op;
651     char * res;
652 
653     fmpz_poly_q_init(op);
654     fmpz_poly_q_set_str(op, in);
655 
656     fmpz_poly_q_init(rop);
657     fmpz_poly_q_scalar_div_mpq(rop, op, x);
658 
659     res = fmpz_poly_q_get_str(rop);
660     ans = !strcmp(out, res);
661 
662     if (!ans)
663     {
664         flint_printf("test_scalar_div_mpq: failed\n");
665         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
666         abort();
667     }
668 
669     fmpz_poly_q_clear(op);
670     fmpz_poly_q_clear(rop);
671     flint_free(res);
672 }
673 
test_mul(char * in1,char * in2,char * out)674 void test_mul(char * in1, char * in2, char * out)
675 {
676     int ans;
677     fmpz_poly_q_t rop, op1, op2;
678     char * res;
679 
680     fmpz_poly_q_init(op1);
681     fmpz_poly_q_set_str(op1, in1);
682 
683     fmpz_poly_q_init(op2);
684     fmpz_poly_q_set_str(op2, in2);
685 
686     fmpz_poly_q_init(rop);
687     fmpz_poly_q_mul(rop, op1, op2);
688 
689     res = fmpz_poly_q_get_str(rop);
690     ans = !strcmp(out, res);
691 
692     if (!ans)
693     {
694         flint_printf("test_mul: failed\n");
695         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
696         abort();
697     }
698 
699     fmpz_poly_q_clear(op1);
700     fmpz_poly_q_clear(op2);
701     fmpz_poly_q_clear(rop);
702     flint_free(res);
703 }
704 
705 /* in1 = in1 * in2 */
test_mul_in_place1(char * in1,char * in2,char * out)706 void test_mul_in_place1(char * in1, char * in2, char * out)
707 {
708     int ans;
709     fmpz_poly_q_t op1, op2;
710     char * res;
711 
712     fmpz_poly_q_init(op1);
713     fmpz_poly_q_set_str(op1, in1);
714 
715     fmpz_poly_q_init(op2);
716     fmpz_poly_q_set_str(op2, in2);
717 
718     fmpz_poly_q_mul(op1, op1, op2);
719 
720     res = fmpz_poly_q_get_str(op1);
721     ans = !strcmp(out, res);
722 
723     if (!ans)
724     {
725         flint_printf("test_mul_in_place1: failed\n");
726         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
727         abort();
728     }
729 
730     fmpz_poly_q_clear(op1);
731     fmpz_poly_q_clear(op2);
732     flint_free(res);
733 }
734 
735 /* in2 = in1 * in2 */
test_mul_in_place2(char * in1,char * in2,char * out)736 void test_mul_in_place2(char * in1, char * in2, char * out)
737 {
738     int ans;
739     fmpz_poly_q_t op1, op2;
740     char * res;
741 
742     fmpz_poly_q_init(op1);
743     fmpz_poly_q_set_str(op1, in1);
744 
745     fmpz_poly_q_init(op2);
746     fmpz_poly_q_set_str(op2, in2);
747 
748     fmpz_poly_q_mul(op2, op1, op2);
749 
750     res = fmpz_poly_q_get_str(op2);
751     ans = !strcmp(out, res);
752 
753     if (!ans)
754     {
755         flint_printf("test_mul_in_place2: failed\n");
756         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
757         abort();
758     }
759 
760     fmpz_poly_q_clear(op1);
761     fmpz_poly_q_clear(op2);
762     flint_free(res);
763 }
764 
765 /* Runs out = in * in */
test_mul_in_place3(char * in,char * out)766 void test_mul_in_place3(char * in, char * out)
767 {
768     int ans;
769     fmpz_poly_q_t rop, op;
770     char * res;
771 
772     fmpz_poly_q_init(op);
773     fmpz_poly_q_set_str(op, in);
774 
775     fmpz_poly_q_init(rop);
776     fmpz_poly_q_mul(rop, op, op);
777 
778     res = fmpz_poly_q_get_str(rop);
779     ans = !strcmp(out, res);
780 
781     if (!ans)
782     {
783         flint_printf("test_mul_in_place3: failed\n");
784         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
785         abort();
786     }
787 
788     fmpz_poly_q_clear(rop);
789     fmpz_poly_q_clear(op);
790     flint_free(res);
791 }
792 
test_div(char * in1,char * in2,char * out)793 void test_div(char * in1, char * in2, char * out)
794 {
795     int ans;
796     fmpz_poly_q_t rop, op1, op2;
797     char * res;
798 
799     fmpz_poly_q_init(op1);
800     fmpz_poly_q_set_str(op1, in1);
801 
802     fmpz_poly_q_init(op2);
803     fmpz_poly_q_set_str(op2, in2);
804 
805     fmpz_poly_q_init(rop);
806     fmpz_poly_q_div(rop, op1, op2);
807 
808     res = fmpz_poly_q_get_str(rop);
809     ans = !strcmp(out, res);
810 
811     if (!ans)
812     {
813         flint_printf("test_div: failed\n");
814         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
815         abort();
816     }
817 
818     fmpz_poly_q_clear(op1);
819     fmpz_poly_q_clear(op2);
820     fmpz_poly_q_clear(rop);
821     flint_free(res);
822 }
823 
824 /* in1 = in1 / in2 */
test_div_in_place1(char * in1,char * in2,char * out)825 void test_div_in_place1(char * in1, char * in2, char * out)
826 {
827     int ans;
828     fmpz_poly_q_t op1, op2;
829     char * res;
830 
831     fmpz_poly_q_init(op1);
832     fmpz_poly_q_set_str(op1, in1);
833 
834     fmpz_poly_q_init(op2);
835     fmpz_poly_q_set_str(op2, in2);
836 
837     fmpz_poly_q_div(op1, op1, op2);
838 
839     res = fmpz_poly_q_get_str(op1);
840     ans = !strcmp(out, res);
841 
842     if (!ans)
843     {
844         flint_printf("test_div_in_place1: failed\n");
845         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
846         abort();
847     }
848 
849     fmpz_poly_q_clear(op1);
850     fmpz_poly_q_clear(op2);
851     flint_free(res);
852 }
853 
854 /* in2 = in1 / in2 */
test_div_in_place2(char * in1,char * in2,char * out)855 void test_div_in_place2(char * in1, char * in2, char * out)
856 {
857     int ans;
858     fmpz_poly_q_t op1, op2;
859     char * res;
860 
861     fmpz_poly_q_init(op1);
862     fmpz_poly_q_set_str(op1, in1);
863 
864     fmpz_poly_q_init(op2);
865     fmpz_poly_q_set_str(op2, in2);
866 
867     fmpz_poly_q_div(op2, op1, op2);
868 
869     res = fmpz_poly_q_get_str(op2);
870     ans = !strcmp(out, res);
871 
872     if (!ans)
873     {
874         flint_printf("test_div_in_place2: failed\n");
875         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
876         abort();
877     }
878 
879     fmpz_poly_q_clear(op1);
880     fmpz_poly_q_clear(op2);
881     flint_free(res);
882 }
883 
884 /* Runs out = in / in */
test_div_in_place3(char * in,char * out)885 void test_div_in_place3(char * in, char * out)
886 {
887     int ans;
888     fmpz_poly_q_t rop, op;
889     char * res;
890 
891     fmpz_poly_q_init(op);
892     fmpz_poly_q_set_str(op, in);
893 
894     fmpz_poly_q_init(rop);
895     fmpz_poly_q_div(rop, op, op);
896 
897     res = fmpz_poly_q_get_str(rop);
898     ans = !strcmp(out, res);
899 
900     if (!ans)
901     {
902         flint_printf("test_div_in_place3: failed\n");
903         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
904         abort();
905     }
906 
907     fmpz_poly_q_clear(rop);
908     fmpz_poly_q_clear(op);
909     flint_free(res);
910 }
911 
test_pow(char * in,ulong x,char * out)912 void test_pow(char * in, ulong x, char * out)
913 {
914     int ans;
915     fmpz_poly_q_t rop, op;
916     char * res;
917 
918     fmpz_poly_q_init(op);
919     fmpz_poly_q_set_str(op, in);
920 
921     fmpz_poly_q_init(rop);
922     fmpz_poly_q_pow(rop, op, x);
923 
924     res = fmpz_poly_q_get_str(rop);
925     ans = !strcmp(out, res);
926 
927     if (!ans)
928     {
929         flint_printf("test_pow: failed\n");
930         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
931         abort();
932     }
933 
934     fmpz_poly_q_clear(op);
935     fmpz_poly_q_clear(rop);
936     flint_free(res);
937 }
938 
test_derivative(char * in,char * out)939 void test_derivative(char * in, char * out)
940 {
941     int ans;
942     fmpz_poly_q_t rop, op;
943     char * res;
944 
945     fmpz_poly_q_init(op);
946     fmpz_poly_q_set_str(op, in);
947 
948     fmpz_poly_q_init(rop);
949     fmpz_poly_q_derivative(rop, op);
950 
951     res = fmpz_poly_q_get_str(rop);
952     ans = !strcmp(out, res);
953 
954     if (!ans)
955     {
956         flint_printf("test_derivative: failed\n");
957         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
958         abort();
959     }
960 
961     fmpz_poly_q_clear(op);
962     fmpz_poly_q_clear(rop);
963     flint_free(res);
964 }
965 
test_evaluate(char * in,int numa,int numb,char * out)966 void test_evaluate(char * in, int numa, int numb, char * out)
967 {
968     int ans, pole;
969     fmpz_poly_q_t op;
970     mpq_t rop, a;
971     char *res = NULL;
972 
973     fmpz_poly_q_init(op);
974     fmpz_poly_q_set_str(op, in);
975 
976     mpq_init(a);
977     flint_mpq_set_si(a, numa, numb);
978     mpq_init(rop);
979     pole = fmpz_poly_q_evaluate(rop, op, a);
980 
981     if (pole && strcmp(out, "P"))
982     {
983         flint_printf("test_evaluate: failed\n");
984         flint_printf("    Expected \"%s\", got a pole\n", out);
985         abort();
986     }
987     if (!pole && !strcmp(out, "P"))
988     {
989         res = mpq_get_str(NULL, 10, rop);
990         flint_printf("test_evaluate: failed\n");
991         flint_printf("    Expected a pole, got \"%s\"\n", res);
992         abort();
993     }
994     if (!pole)
995     {
996         res = mpq_get_str(NULL, 10, rop);
997         ans = (strcmp(out, res) == 0);
998 
999         if (!ans)
1000         {
1001             flint_printf("test_evaluate: failed\n");
1002             flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
1003             abort();
1004         }
1005     }
1006 
1007     fmpz_poly_q_clear(op);
1008     mpq_clear(rop);
1009     mpq_clear(a);
1010     flint_free(res);
1011 }
1012 
test_get_str_pretty(char * in,char * out)1013 void test_get_str_pretty(char * in, char * out)
1014 {
1015     int ans;
1016     fmpz_poly_q_t rop;
1017     char * res;
1018 
1019     fmpz_poly_q_init(rop);
1020     fmpz_poly_q_set_str(rop, in);
1021     res = fmpz_poly_q_get_str_pretty(rop, "t");
1022     ans = !strcmp(out, res);
1023 
1024     if (!ans)
1025     {
1026         flint_printf("test_get_str_pretty: failed\n");
1027         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
1028         abort();
1029     }
1030 
1031     fmpz_poly_q_clear(rop);
1032     flint_free(res);
1033 }
1034 
test_addmul(char * in1,char * in2,char * in3,char * out)1035 void test_addmul(char * in1, char * in2, char * in3, char * out)
1036 {
1037     int ans;
1038     fmpz_poly_q_t rop, op1, op2;
1039     char * res;
1040 
1041     fmpz_poly_q_init(rop);
1042     fmpz_poly_q_set_str(rop, in1);
1043     fmpz_poly_q_init(op1);
1044     fmpz_poly_q_set_str(op1, in2);
1045     fmpz_poly_q_init(op2);
1046     fmpz_poly_q_set_str(op2, in3);
1047 
1048     fmpz_poly_q_addmul(rop, op1, op2);
1049 
1050     res = fmpz_poly_q_get_str(rop);
1051     ans = !strcmp(out, res);
1052 
1053     if (!ans)
1054     {
1055         flint_printf("test_addmul: failed\n");
1056         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
1057         abort();
1058     }
1059 
1060     fmpz_poly_q_clear(rop);
1061     fmpz_poly_q_clear(op1);
1062     fmpz_poly_q_clear(op2);
1063     flint_free(res);
1064 }
1065 
test_submul(char * in1,char * in2,char * in3,char * out)1066 void test_submul(char * in1, char * in2, char * in3, char * out)
1067 {
1068     int ans;
1069     fmpz_poly_q_t rop, op1, op2;
1070     char * res;
1071 
1072     fmpz_poly_q_init(rop);
1073     fmpz_poly_q_set_str(rop, in1);
1074     fmpz_poly_q_init(op1);
1075     fmpz_poly_q_set_str(op1, in2);
1076     fmpz_poly_q_init(op2);
1077     fmpz_poly_q_set_str(op2, in3);
1078 
1079     fmpz_poly_q_submul(rop, op1, op2);
1080 
1081     res = fmpz_poly_q_get_str(rop);
1082     ans = !strcmp(out, res);
1083 
1084     if (!ans)
1085     {
1086         flint_printf("test_submul: failed\n");
1087         flint_printf("    Expected \"%s\", got \"%s\"\n", out, res);
1088         abort();
1089     }
1090 
1091     fmpz_poly_q_clear(rop);
1092     fmpz_poly_q_clear(op1);
1093     fmpz_poly_q_clear(op2);
1094     flint_free(res);
1095 }
1096 
main(int argc,char * argv[])1097 int main(int argc, char *argv[])
1098 {
1099     int ans;
1100     char *str, *strout;
1101 
1102     fmpz_poly_t zpoly;
1103     fmpz_poly_q_t qpoly1;
1104 
1105     mpz_t mpzzero, mpzone, mpztwo;
1106     mpq_t mpqzero, mpqone, mpqtwo, mpqtwoinv;
1107 
1108     FLINT_TEST_INIT(state);
1109 
1110     flint_printf("all... ");
1111     fflush(stdout);
1112 
1113     /* Accessing numerator and denominator ***********************************/
1114 
1115     fmpz_poly_q_init(qpoly1);
1116     fmpz_poly_q_set_str(qpoly1, "2  -1 1/2  0 1");
1117     str = "2  -1 1";
1118     strout = fmpz_poly_get_str(fmpz_poly_q_numref(qpoly1));
1119     ans = !strcmp(str, strout);
1120     if (!ans)
1121     {
1122         flint_printf("test_numref: failed\n");
1123         flint_printf("    Expected \"%s\", got \"%s\"\n", str, strout);
1124         flint_printf("    qpoly1 = \""), fmpz_poly_q_print(qpoly1), flint_printf("\"\n");
1125         abort();
1126     }
1127     fmpz_poly_q_clear(qpoly1);
1128     flint_free(strout);
1129 
1130     fmpz_poly_q_init(qpoly1);
1131     fmpz_poly_q_set_str(qpoly1, "2  -1 1/2  0 1");
1132     str = "2  0 1";
1133     strout = fmpz_poly_get_str(fmpz_poly_q_denref(qpoly1));
1134     ans = !strcmp(str, strout);
1135     if (!ans)
1136     {
1137         flint_printf("test_denref: failed\n");
1138         flint_printf("    Expected \"%s\", got \"%s\"\n", str, strout);
1139         abort();
1140     }
1141     fmpz_poly_q_clear(qpoly1);
1142     flint_free(strout);
1143 
1144     fmpz_poly_q_init(qpoly1);
1145     fmpz_poly_init(zpoly);
1146     fmpz_poly_q_set_str(qpoly1, "2  -1 1/2  0 1");
1147     fmpz_poly_set(zpoly, fmpz_poly_q_numref(qpoly1));
1148     str = "2  -1 1";
1149     strout = fmpz_poly_get_str(zpoly);
1150     ans = !strcmp(str, strout);
1151     if (!ans)
1152     {
1153         flint_printf("test_get_num: failed\n");
1154         flint_printf("    Expected \"%s\", got \"%s\"\n", str, strout);
1155         abort();
1156     }
1157     fmpz_poly_q_clear(qpoly1);
1158     fmpz_poly_clear(zpoly);
1159     flint_free(strout);
1160 
1161     fmpz_poly_q_init(qpoly1);
1162     fmpz_poly_init(zpoly);
1163     fmpz_poly_q_set_str(qpoly1, "2  -1 1/2  0 1");
1164     fmpz_poly_set(zpoly, fmpz_poly_q_denref(qpoly1));
1165 
1166     str = "2  0 1";
1167     strout = fmpz_poly_get_str(zpoly);
1168     ans = !strcmp(str, strout);
1169     if (!ans)
1170     {
1171         flint_printf("test_get_den: failed\n");
1172         flint_printf("    Expected \"%s\", got \"%s\"\n", str, strout);
1173         abort();
1174     }
1175     fmpz_poly_q_clear(qpoly1);
1176     fmpz_poly_clear(zpoly);
1177     flint_free(strout);
1178 
1179     fmpz_poly_q_init(qpoly1);
1180     fmpz_poly_init(zpoly);
1181     fmpz_poly_q_set_str(qpoly1, "1  1/1  1");
1182     fmpz_poly_set_str(zpoly, "2  0 1");
1183     fmpz_poly_set(fmpz_poly_q_numref(qpoly1), zpoly);
1184     str = "2  0 1";
1185     strout = fmpz_poly_get_str(fmpz_poly_q_numref(qpoly1));
1186     ans = !strcmp(str, strout);
1187     if (!ans)
1188     {
1189         flint_printf("test_set_num: failed\n");
1190         flint_printf("    Expected \"%s\", got \"%s\"\n", str, strout);
1191         abort();
1192     }
1193     fmpz_poly_q_clear(qpoly1);
1194     fmpz_poly_clear(zpoly);
1195     flint_free(strout);
1196 
1197     fmpz_poly_q_init(qpoly1);
1198     fmpz_poly_init(zpoly);
1199     fmpz_poly_q_set_str(qpoly1, "1  1/1  1");
1200     fmpz_poly_set_str(zpoly, "2  0 1");
1201     fmpz_poly_set(fmpz_poly_q_denref(qpoly1), zpoly);
1202     str = "2  0 1";
1203     strout = fmpz_poly_get_str(fmpz_poly_q_denref(qpoly1));
1204     ans = !strcmp(str, strout);
1205     if (!ans)
1206     {
1207         flint_printf("test_set_den: failed\n");
1208         flint_printf("    Expected \"%s\", got \"%s\"\n", str, strout);
1209         abort();
1210     }
1211     fmpz_poly_q_clear(qpoly1);
1212     fmpz_poly_clear(zpoly);
1213     flint_free(strout);
1214 
1215     /* Canonicalise **********************************************************/
1216 
1217     fmpz_poly_q_init(qpoly1);
1218     str = "2  -1 1/2  0 1";
1219     fmpz_poly_q_set_str(qpoly1, str);
1220     strout = fmpz_poly_q_get_str(qpoly1);
1221     ans = !strcmp(str, strout);
1222     if (!ans)
1223     {
1224         flint_printf("test_canonicalize: failed\n");
1225         flint_printf("    Expected \"%s\", got \"%s\"\n", str, strout);
1226         abort();
1227     }
1228     fmpz_poly_q_clear(qpoly1);
1229     flint_free(strout);
1230 
1231     fmpz_poly_q_init(qpoly1);
1232     str = "2  -1 -1/2  0 1";
1233     fmpz_poly_q_set_str(qpoly1, "2  1 1/2  0 -1");
1234     strout = fmpz_poly_q_get_str(qpoly1);
1235     ans = !strcmp("2  -1 -1/2  0 1", strout);
1236     if (!ans)
1237     {
1238         flint_printf("test_canonicalize: failed\n");
1239         flint_printf("    Expected \"%s\", got \"%s\"\n", str, strout);
1240         abort();
1241     }
1242     flint_free(strout);
1243     fmpz_poly_q_clear(qpoly1);
1244 
1245     /* Initialization, memory management and basic operations ****************/
1246 
1247     test_set("0", "0");
1248     test_set("0/1  1", "0");
1249     test_set("3  -1 0 1/2  0 1", "3  -1 0 1/2  0 1");
1250     test_set("3  -1 0 1/2  1 1", "2  -1 1");
1251 
1252     test_set_si(-1, "1  -1");
1253     test_set_si(13, "1  13");
1254     test_set_si(0, "0");
1255 
1256     test_swap("3  -1 0 1/2  0 1", "1  2/1  3", "1  2/1  3", "3  -1 0 1/2  0 1");
1257 
1258     test_zero("0", "0");
1259     test_zero("0/1  1", "0");
1260     test_zero("3  -1 0 1/2  0 1", "0");
1261 
1262     test_neg("0", "0");
1263     test_neg("1  1/1  2", "1  -1/1  2");
1264     test_neg("3  -1 0 1/2  0 1", "3  1 0 -1/2  0 1");
1265 
1266     test_inv("1  1/1  2", "1  2");
1267     test_inv("3  -1 0 1/2  0 1", "2  0 1/3  -1 0 1");
1268     test_inv("3  -1 0 -1/2  0 1", "2  0 -1/3  1 0 1");
1269 
1270     test_inv_inplace("1  1/1  2", "1  2");
1271     test_inv_inplace("3  -1 0 1/2  0 1", "2  0 1/3  -1 0 1");
1272     test_inv_inplace("3  -1 0 -1/2  0 1", "2  0 -1/3  1 0 1");
1273 
1274     test_is_zero("0", 1);
1275     test_is_zero("0/1  1", 1);
1276     test_is_zero("3  -1 0 1/2  0 1", 0);
1277     test_is_zero("3  -1 0 1/2  1 1", 0);
1278 
1279     test_is_one("0", 0);
1280     test_is_one("0/1  1", 0);
1281     test_is_one("1  1/1  1", 1);
1282     test_is_one("2  1 1/2  1 1", 1);
1283     test_is_one("3  -1 0 1/2  0 1", 0);
1284 
1285     test_equal("1  1/1  2", "1  1/1  2", 1);
1286     test_equal("1  1/1  2", "1  1/1  2", 1);
1287     test_equal("3  -1 0 1/2  1 1", "2  -1 1", 1);
1288     test_equal("3  -1 0 1/2  -1 1", "2  -1 1", 0);
1289 
1290     /* Addition and subtraction **********************************************/
1291 
1292     test_add("3  1 0 1/2  0 1", "2  0 -1/3  1 0 1", "5  1 0 1 0 1/4  0 1 0 1");
1293     test_add("3  -1 0 1/2  1 1", "1  2/2  -1 1", "3  3 -2 1/2  -1 1");
1294     test_add("0/2  1 1", "1  2/1  1", "1  2");
1295     test_add("1  -3/1  4", "0/3  1 0 1", "1  -3/1  4");
1296     test_add("2  1 1/1  1", "2  -1 1/1  1", "2  0 2");
1297     test_add("2  1 1/2  0 1", "2  2 1/2  -1 1", "3  -1 2 2/3  0 -1 1");
1298     test_add("2  -1 1/2  2 1", "3  4 4 1/2  1 1", "4  7 12 7 1/3  2 3 1");
1299     test_add("2  1 1/2  -1 1", "2  1 1", "3  0 1 1/2  -1 1");
1300     test_add("1  1/2  1 1", "2  0 1/2  1 1", "1  1");
1301     test_add("2  1 1/3  4 -4 1", "1  1/2  -2 1", "2  -1 2/3  4 -4 1");
1302     test_add("3  0 1 1/3  1 2 1", "2  0 -1/2  1 1", "0");
1303     test_add("2  1 1/2  0 1", "2  -1 1/2  0 1", "1  2");
1304     test_add("1  1/3  3 5 2", "1  1/3  6 7 2", "1  1/3  2 3 1");
1305 
1306     test_add_in_place1("3  1 0 1/2  0 1", "2  0 -1/3  1 0 1", "5  1 0 1 0 1/4  0 1 0 1");
1307     test_add_in_place1("3  -1 0 1/2  1 1", "1  2/2  -1 1", "3  3 -2 1/2  -1 1");
1308     test_add_in_place1("0/2  1 1", "1  2/1  1", "1  2");
1309     test_add_in_place1("1  -3/1  4", "0/3  1 0 1", "1  -3/1  4");
1310     test_add_in_place1("2  1 1/1  1", "2  -1 1/1  1", "2  0 2");
1311     test_add_in_place1("2  1 1/2  0 1", "2  2 1/2  -1 1", "3  -1 2 2/3  0 -1 1");
1312     test_add_in_place1("2  -1 1/2  2 1", "3  4 4 1/2  1 1", "4  7 12 7 1/3  2 3 1");
1313     test_add_in_place1("2  1 1/2  -1 1", "2  1 1", "3  0 1 1/2  -1 1");
1314     test_add_in_place1("1  1/2  1 1", "2  0 1/2  1 1", "1  1");
1315     test_add_in_place1("2  1 1/3  4 -4 1", "1  1/2  -2 1", "2  -1 2/3  4 -4 1");
1316     test_add_in_place1("3  0 1 1/3  1 2 1", "2  0 -1/2  1 1", "0");
1317     test_add_in_place1("2  1 1/2  0 1", "2  -1 1/2  0 1", "1  2");
1318 
1319     test_add_in_place2("3  1 0 1/2  0 1", "2  0 -1/3  1 0 1", "5  1 0 1 0 1/4  0 1 0 1");
1320     test_add_in_place2("3  -1 0 1/2  1 1", "1  2/2  -1 1", "3  3 -2 1/2  -1 1");
1321     test_add_in_place2("0/2  1 1", "1  2/1  1", "1  2");
1322     test_add_in_place2("1  -3/1  4", "0/3  1 0 1", "1  -3/1  4");
1323     test_add_in_place2("2  1 1/1  1", "2  -1 1/1  1", "2  0 2");
1324     test_add_in_place2("2  1 1/2  0 1", "2  2 1/2  -1 1", "3  -1 2 2/3  0 -1 1");
1325     test_add_in_place2("2  -1 1/2  2 1", "3  4 4 1/2  1 1", "4  7 12 7 1/3  2 3 1");
1326     test_add_in_place2("2  1 1/2  -1 1", "2  1 1", "3  0 1 1/2  -1 1");
1327     test_add_in_place2("1  1/2  1 1", "2  0 1/2  1 1", "1  1");
1328     test_add_in_place2("2  1 1/3  4 -4 1", "1  1/2  -2 1", "2  -1 2/3  4 -4 1");
1329     test_add_in_place2("3  0 1 1/3  1 2 1", "2  0 -1/2  1 1", "0");
1330     test_add_in_place2("2  1 1/2  0 1", "2  -1 1/2  0 1", "1  2");
1331 
1332     test_add_in_place3("2  1 1", "2  2 2");
1333     test_add_in_place3("2  1 1/1  2", "2  1 1");
1334 
1335     test_sub("3  1 0 1/2  0 1", "2  0 -1/3  1 0 1", "5  1 0 3 0 1/4  0 1 0 1");
1336     test_sub("3  -1 0 1/2  1 1", "1  2/2  -1 1", "3  -1 -2 1/2  -1 1");
1337     test_sub("0/2  1 1", "1  2/1  1", "1  -2");
1338     test_sub("1  -3/1  4", "0/3  1 0 1", "1  -3/1  4");
1339     test_sub("2  1 1/1  1", "2  -1 1/1  1", "1  2");
1340     test_sub("2  1 1/2  0 1", "2  2 1/2  -1 1", "2  -1 -2/3  0 -1 1");
1341     test_sub("2  -1 1/2  2 1", "3  4 4 1/2  1 1", "4  -9 -12 -5 -1/3  2 3 1");
1342     test_sub("2  -1 1/2  0 1", "1  1", "1  -1/2  0 1");
1343     test_sub("3  1 0 1/2  0 1", "2  0 -1/3  1 0 1", "5  1 0 3 0 1/4  0 1 0 1");
1344     test_sub("3  -1 0 1/2  1 1", "1  2/2  -1 1", "3  -1 -2 1/2  -1 1");
1345     test_sub("0/2  1 1", "1  2/1  1", "1  -2");
1346     test_sub("1  -3/1  4", "0/3  1 0 1", "1  -3/1  4");
1347     test_sub("2  1 1/1  1", "2  -1 1/1  1", "1  2");
1348     test_sub("2  1 1/2  0 1", "2  2 1/2  -1 1", "2  -1 -2/3  0 -1 1");
1349     test_sub("2  -1 1/2  2 1", "3  4 4 1/2  1 1", "4  -9 -12 -5 -1/3  2 3 1");
1350     test_sub("2  1 1/2  -1 1", "2  1 1", "3  2 1 -1/2  -1 1");
1351     test_sub("1  1/2  1 1", "2  0 1/2  1 1", "2  1 -1/2  1 1");
1352     test_sub("2  1 1/3  4 -4 1", "1  1/2  -2 1", "1  3/3  4 -4 1");
1353     test_sub("3  0 1 1/3  1 2 1", "2  0 -1/2  1 1", "2  0 2/2  1 1");
1354     test_sub("2  1 1/2  0 1", "2  -1 1/2  0 1", "1  2/2  0 1");
1355     test_sub("1  1/3  3 5 2", "1  1/3  6 7 2", "1  1/4  6 13 9 2");
1356     test_sub("2  1 1/2  0 2", "2  1 1/2  0 2", "0");
1357     test_sub("2  -1 2/2  0 1", "2  -1 1/2  0 1", "1  1");
1358 
1359     test_sub_in_place1("3  1 0 1/2  0 1", "2  0 -1/3  1 0 1", "5  1 0 3 0 1/4  0 1 0 1");
1360     test_sub_in_place1("3  -1 0 1/2  1 1", "1  2/2  -1 1", "3  -1 -2 1/2  -1 1");
1361     test_sub_in_place1("0/2  1 1", "1  2/1  1", "1  -2");
1362     test_sub_in_place1("1  -3/1  4", "0/3  1 0 1", "1  -3/1  4");
1363     test_sub_in_place1("2  1 1/1  1", "2  -1 1/1  1", "1  2");
1364     test_sub_in_place1("2  1 1/2  0 1", "2  2 1/2  -1 1", "2  -1 -2/3  0 -1 1");
1365     test_sub_in_place1("2  -1 1/2  2 1", "3  4 4 1/2  1 1", "4  -9 -12 -5 -1/3  2 3 1");
1366 
1367     test_sub_in_place2("3  1 0 1/2  0 1", "2  0 -1/3  1 0 1", "5  1 0 3 0 1/4  0 1 0 1");
1368     test_sub_in_place2("3  -1 0 1/2  1 1", "1  2/2  -1 1", "3  -1 -2 1/2  -1 1");
1369     test_sub_in_place2("0/2  1 1", "1  2/1  1", "1  -2");
1370     test_sub_in_place2("1  -3/1  4", "0/3  1 0 1", "1  -3/1  4");
1371     test_sub_in_place2("2  1 1/1  1", "2  -1 1/1  1", "1  2");
1372     test_sub_in_place2("2  1 1/2  0 1", "2  2 1/2  -1 1", "2  -1 -2/3  0 -1 1");
1373     test_sub_in_place2("2  -1 1/2  2 1", "3  4 4 1/2  1 1", "4  -9 -12 -5 -1/3  2 3 1");
1374 
1375     test_sub_in_place3("2  -1 1/2  2 1", "0");
1376 
1377     test_addmul("1  1/2  0 2", "2  3 1/1  4", "3  1 0 1/4  -2 0 0 1", "5  -4 3 1 5 1/5  0 -8 0 0 4");
1378 
1379     test_submul("1  1/2  0 2", "2  3 1/1  4", "3  1 0 1/4  -2 0 0 1", "5  -4 -3 -1 -1 -1/5  0 -8 0 0 4");
1380 
1381     /* Scalar multiplication and devision ************************************/
1382 
1383     flint_mpz_init_set_si(mpzzero, 0);
1384     flint_mpz_init_set_si(mpzone, 1);
1385     flint_mpz_init_set_si(mpztwo, 2);
1386 
1387     mpq_init(mpqzero); flint_mpq_set_si(mpqzero, 0, 1);
1388     mpq_init(mpqone); flint_mpq_set_si(mpqone, 1, 1);
1389     mpq_init(mpqtwo); flint_mpq_set_si(mpqtwo, 2, 1);
1390     mpq_init(mpqtwoinv); flint_mpq_set_si(mpqtwoinv, 1, 2);
1391 
1392     test_scalar_mul_si("0", 1, "0");
1393     test_scalar_mul_si("0", 0, "0");
1394     test_scalar_mul_si("1  2", 0, "0");
1395     test_scalar_mul_si("1  1/1  2", -2, "1  -1");
1396     test_scalar_mul_si("2  1 1/2  -2 3", 5, "2  5 5/2  -2 3");
1397     test_scalar_mul_si("2  1 1/2  -2 2", 3, "2  3 3/2  -2 2");
1398 
1399     test_scalar_mul_mpz("0", mpzone, "0");
1400     test_scalar_mul_mpz("0", mpzzero, "0");
1401     test_scalar_mul_mpz("1  2", mpzzero, "0");
1402     test_scalar_mul_mpz("1  1/1  2", mpztwo, "1  1");
1403 
1404     test_scalar_mul_mpq("0", mpqone, "0");
1405     test_scalar_mul_mpq("0", mpqzero, "0");
1406     test_scalar_mul_mpq("1  2", mpqzero, "0");
1407     test_scalar_mul_mpq("1  1/1  2", mpqtwo, "1  1");
1408     test_scalar_mul_mpq("1  -2/1  1", mpqtwoinv, "1  -1");
1409 
1410     test_scalar_div_si("0", 1, "0");
1411     test_scalar_div_si("1  2", 2, "1  1");
1412     test_scalar_div_si("1  1/1  2", -2, "1  -1/1  4");
1413     test_scalar_div_si("3  -5 0 3/2  1 1", 2, "3  -5 0 3/2  2 2");
1414     test_scalar_div_si("3  2 8 4/2  0 1", 3, "3  2 8 4/2  0 3");
1415     test_scalar_div_si("3  2 8 4/2  0 1", -3, "3  -2 -8 -4/2  0 3");
1416     test_scalar_div_si("3  -27 0 9/2  0 1", -3, "3  9 0 -3/2  0 1");
1417 
1418     test_scalar_div_mpz("0", mpzone, "0");
1419     test_scalar_div_mpz("1  2", mpztwo, "1  1");
1420     test_scalar_div_mpz("1  1/1  2", mpztwo, "1  1/1  4");
1421 
1422     test_scalar_div_mpq("0", mpqone, "0");
1423     test_scalar_div_mpq("1  2", mpqone, "1  2");
1424     test_scalar_div_mpq("1  1/1  2", mpqtwo, "1  1/1  4");
1425     test_scalar_div_mpq("1  -2/1  1", mpqtwoinv, "1  -4");
1426 
1427     mpz_clear(mpzzero);
1428     mpz_clear(mpzone);
1429     mpz_clear(mpztwo);
1430     mpq_clear(mpqzero);
1431     mpq_clear(mpqone);
1432     mpq_clear(mpqtwo);
1433     mpq_clear(mpqtwoinv);
1434 
1435     /* Multiplication, division and powing *********************************/
1436 
1437     test_mul("3  1 0 1/2  0 1", "2  0 -1/3  1 0 1", "1  -1");
1438     test_mul("3  -1 0 1/2  1 1", "1  2/2  -1 1", "1  2");
1439     test_mul("0/2  1 1", "1  2/1  1", "0");
1440     test_mul("1  -3/1  4", "0/3  1 0 1", "0");
1441     test_mul("2  1 1/1  1", "2  -1 1/1  1", "3  -1 0 1");
1442     test_mul("2  1 1/2  0 1", "2  2 1/2  -1 1", "3  2 3 1/3  0 -1 1");
1443     test_mul("2  -1 1/2  2 1", "3  4 4 1/2  1 1", "3  -2 1 1/2  1 1");
1444 
1445     test_mul_in_place1("3  1 0 1/2  0 1", "2  0 -1/3  1 0 1", "1  -1");
1446     test_mul_in_place1("3  -1 0 1/2  1 1", "1  2/2  -1 1", "1  2");
1447     test_mul_in_place1("0/2  1 1", "1  2/1  1", "0");
1448     test_mul_in_place1("1  -3/1  4", "0/3  1 0 1", "0");
1449     test_mul_in_place1("2  1 1/1  1", "2  -1 1/1  1", "3  -1 0 1");
1450     test_mul_in_place1("2  1 1/2  0 1", "2  2 1/2  -1 1", "3  2 3 1/3  0 -1 1");
1451     test_mul_in_place1("2  -1 1/2  2 1", "3  4 4 1/2  1 1", "3  -2 1 1/2  1 1");
1452 
1453     test_mul_in_place2("3  1 0 1/2  0 1", "2  0 -1/3  1 0 1", "1  -1");
1454     test_mul_in_place2("3  -1 0 1/2  1 1", "1  2/2  -1 1", "1  2");
1455     test_mul_in_place2("0/2  1 1", "1  2/1  1", "0");
1456     test_mul_in_place2("1  -3/1  4", "0/3  1 0 1", "0");
1457     test_mul_in_place2("2  1 1/1  1", "2  -1 1/1  1", "3  -1 0 1");
1458     test_mul_in_place2("2  1 1/2  0 1", "2  2 1/2  -1 1", "3  2 3 1/3  0 -1 1");
1459     test_mul_in_place2("2  -1 1/2  2 1", "3  4 4 1/2  1 1", "3  -2 1 1/2  1 1");
1460 
1461     test_mul_in_place3("2  0 1/2  1 1", "3  0 0 1/3  1 2 1");
1462 
1463     test_div("3  -1 0 1/1  2", "2  1 1/1  1", "2  -1 1/1  2");
1464     test_div("0/2  1 1", "2  1 1/1  1", "0");
1465     test_div("3  -1 0 1/1  4", "2  -1 -1/1  2", "2  1 -1/1  2");
1466     test_div("2  1 1", "2  1 -1/2  1 -1", "2  1 1");
1467     test_div("2  1 1/3  4 4 1", "2  -1 1/3  6 5 1", "3  3 4 1/3  -2 1 1");
1468 
1469     test_div_in_place1("3  -1 0 1/1  2", "2  1 1/1  1", "2  -1 1/1  2");
1470     test_div_in_place1("0/2  1 1", "2  1 1/1  1", "0");
1471     test_div_in_place1("3  -1 0 1/1  4", "2  -1 -1/1  2", "2  1 -1/1  2");
1472     test_div_in_place1("2  1 1", "2  1 -1/2  1 -1", "2  1 1");
1473     test_div_in_place1("2  1 1/3  4 4 1", "2  -1 1/3  6 5 1", "3  3 4 1/3  -2 1 1");
1474     test_div_in_place1("0", "1  2/2  3 5", "0");
1475 
1476     test_div_in_place2("3  -1 0 1/1  2", "2  1 1/1  1", "2  -1 1/1  2");
1477     test_div_in_place2("0/2  1 1", "2  1 1/1  1", "0");
1478     test_div_in_place2("3  -1 0 1/1  4", "2  -1 -1/1  2", "2  1 -1/1  2");
1479     test_div_in_place2("2  1 1", "2  1 -1/2  1 -1", "2  1 1");
1480     test_div_in_place2("2  1 1/3  4 4 1", "2  -1 1/3  6 5 1", "3  3 4 1/3  -2 1 1");
1481 
1482     test_div_in_place3("3  -1 0 1/1  2", "1  1");
1483 
1484     test_pow("2  0 -1/1  2", 3, "4  0 0 0 -1/1  8");
1485     test_pow("0", 0, "1  1");
1486     test_pow("2  1 -1", 0, "1  1");
1487     test_pow("2  1 1/2  0 1", 0, "1  1");
1488 
1489     /* Derivative ************************************************************/
1490 
1491     test_derivative("0", "0");
1492     test_derivative("1  2", "0");
1493     test_derivative("1  -1/1  2", "0");
1494     test_derivative("2  0 1", "1  1");
1495     test_derivative("3  1 0 1", "2  0 2");
1496     test_derivative("1  1/2  0 1", "1  -1/3  0 0 1");
1497     test_derivative("2  2 1/2  -1 1", "1  -3/3  1 -2 1");
1498 
1499     test_derivative("2  0 1/3  1 2 1", "2  1 -1/4  1 3 3 1");
1500 
1501     /* Bug which allowed constant factors */
1502     test_derivative("3  5 1 -2/2  10 2", "3  0 -10 -1/3  25 10 1");
1503 
1504     /* Evaluation ************************************************************/
1505 
1506     test_evaluate("1  1/1  2", -2, 3, "1/2");
1507     test_evaluate("3  1 0 1/2  0 1", -1, 2, "-5/2");
1508     test_evaluate("2  3 1/2  -1 1", 1, 1, "P");
1509     test_evaluate("2  3 1/2  -1 1", 2, 3, "-11");
1510     test_evaluate("2  3 1/2  -1 2", 1, 2, "P");
1511     test_evaluate("2  1 1/2  -1 1", 2, 1, "3");
1512 
1513     /* String methods ********************************************************/
1514 
1515     fmpz_poly_q_init(qpoly1);
1516     ans = fmpz_poly_q_set_str(qpoly1, "1  3/xyz");
1517     if ((ans == 0) || !fmpz_poly_q_is_zero(qpoly1))
1518     {
1519         flint_printf("test_set_str: failed\n");
1520         abort();
1521     }
1522     fmpz_poly_q_clear(qpoly1);
1523 
1524     fmpz_poly_q_init(qpoly1);
1525     ans = fmpz_poly_q_set_str(qpoly1, "abc/1  3");
1526     if ((ans == 0) || !fmpz_poly_q_is_zero(qpoly1))
1527     {
1528         flint_printf("test_set_str: failed\n");
1529         abort();
1530     }
1531     fmpz_poly_q_clear(qpoly1);
1532 
1533     fmpz_poly_q_init(qpoly1);
1534     ans = fmpz_poly_q_set_str(qpoly1, "abc/xyz");
1535     if ((ans == 0) || !fmpz_poly_q_is_zero(qpoly1))
1536     {
1537         flint_printf("test_set_str: failed\n");
1538         abort();
1539     }
1540     fmpz_poly_q_clear(qpoly1);
1541 
1542     test_get_str_pretty("1  -3", "-3");
1543     test_get_str_pretty("3  1 2 1", "t^2+2*t+1");
1544     test_get_str_pretty("1  -2/2  1 1", "-2/(t+1)");
1545     test_get_str_pretty("2  1 1/2  -1 1", "(t+1)/(t-1)");
1546     test_get_str_pretty("2  1 1/1  2", "(t+1)/2");
1547     test_get_str_pretty("1  1/1  2", "1/2");
1548 
1549     FLINT_TEST_CLEANUP(state);
1550     flint_printf("PASS\n");
1551     return EXIT_SUCCESS;
1552 }
1553 
1554