]> git.ozlabs.org Git - ccan/blob - junkcode/iasoule32@gmail.com-polynomial/test/run.c
tdb2: fix coalesce race #2
[ccan] / junkcode / iasoule32@gmail.com-polynomial / test / run.c
1 /* 
2 ** polynomial_adt_test.c
3 ** Test (minimalistic) for the polynomial module
4  * More of a display of functionality
5  * Copyright (c) 2009 I. Soule
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29 **          iasoule32@gmail.com
30 */
31
32 #include <stdio.h>
33 #include "../polynomial_adt.h"
34 #include "../polynomial_adt.c"
35
36 int main(void)
37 {
38     PolyAdt *p = create_adt(5), *q = create_adt(4);
39     PolyAdt *sum, *diff, *prod;
40     
41     insert_term(p,1,5);
42     insert_term(p,3,4);
43     insert_term(p,1,3);
44     insert_term(p,9,2);
45     insert_term(p,8,1);
46     
47     insert_term(q,2,4);
48     insert_term(q,8,3);
49     insert_term(q,7,2);
50     insert_term(q,6,1);
51     
52     
53     printf("Displaying Polynomials ...\n");
54     display_poly(p);
55     display_poly(q);
56     
57     sum = add(p,q);
58     printf("P(x) + Q(x) = ");
59     display_poly(sum);
60     
61     diff = subtract(p,q);
62     printf("P(x) - Q(x) = ");
63     display_poly(diff);
64     
65     prod = multiply(p,q);
66     printf("P(x)*Q(x) = ");
67     display_poly(prod);
68     
69     PolyAdt *quad = create_adt(2);
70     insert_term(quad, 10, 2);
71     insert_term(quad, 30, 1);
72     insert_term(quad, 2, 0);
73     
74     quadratic_roots(quad, NULL, NULL); //print out the roots
75     
76     float real, cplx;
77     quadratic_roots(quad, &real, &cplx);
78     
79     printf("X1 = %f, X2 = %f\n\n", real, cplx);
80     
81     PolyAdt *deriv, *integral;
82     
83     deriv = derivative(p);
84     printf("The derivitive of p = ");
85     display_poly(deriv);
86     integral = integrate(q);
87     
88     printf("The integral of q = ");
89     display_poly(integral);
90     
91     printf("\n Computing P(x)^3\n");
92     
93     PolyAdt *expo;
94     expo = exponentiate(p, 3);
95     display_poly(expo);
96     printf("\n");
97     
98     printf("Computing Integral[Q(x)^2]\n");
99     expo = exponentiate(q, 2);
100     integral = integrate(expo);
101     display_poly(integral);
102     
103     
104     printf(" Differentiating and Integrating P\n");
105     display_poly(integrate(derivative(p))); 
106     
107     PolyAdt *L, *M;
108     
109     L = create_adt(3), M = create_adt(2);
110     
111     insert_term(L, 4, 3);
112     insert_term(L, 10, 2);
113     insert_term(L, 15, 1);
114     
115     insert_term(M, 4, 2);
116     printf("L = ");
117     display_poly(L);
118     printf("M = ");
119     display_poly(M);
120     
121     
122     printf("Computing composition L(M(X))\n");
123     display_poly(compose(L, M));
124     
125     printf("Freed memory back to heap for allocated polys'\n");
126     destroy_poly(sum);
127     destroy_poly(diff);
128     destroy_poly(prod);
129     destroy_poly(L); destroy_poly(M);
130     destroy_poly(q); destroy_poly(p);
131     
132     return 0;
133 }