aboutsummaryrefslogtreecommitdiff
path: root/Week7 Polynomials/test/polynomial/PolynomialTest.java
blob: c00f165760d823279acb4eee7fc5df66a3c1bf87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package polynomial;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 * A test class for the polynomial.Polynomial class
 * @author Camil Staps, s4498062
 */
public class PolynomialTest {
    
    public PolynomialTest() {
    }
    
    @BeforeClass
    public static void setUpClass() {
    }
    
    @AfterClass
    public static void tearDownClass() {
    }
    
    @Before
    public void setUp() {
    }
    
    @After
    public void tearDown() {
    }

    /**
     * Test of toString method, of class Polynomial.
     */
    @Test
    public void testToString() {
        System.out.println("toString");
        // Test exp=0, exp=1, exp>1, positive & negative integer & floating point coefficients all at once
        assertEquals("3x^5-4x+2.5-3.4x^-7", new Polynomial("3 5 -4 1 2.5 0 -3.4 -7").toString());
        // Test zero polynomial
        assertEquals("", new Polynomial().toString());
    }

    /**
     * Test of minus method, of class Polynomial.
     */
    @Test
    public void testMinus() {
        System.out.println("minus");
        // Test cancelling out and exponents that don't exist in either of the two polynomials all at once
        Polynomial instance = new Polynomial("7 4 -4 2 3 1 5 0");       // 7x^4 - 4x^2 + 3x + 5
        instance.minus(new Polynomial("5 3 4 2 3 1 10 0"));             // 5x^3 + 4x^2 + 3x + 10
        assertEquals(new Polynomial("7 4 -5 3 -8 2 -5 0"), instance);   // 7x^4 - 5x^3 - 8x^2 - 5
        
        // Test minus = plus (times -1) on many small 2-term polynomials
        for (float c1 = -10; c1 < 10; c1 += 2.3)
            for (int exp1 = 5; exp1 < 10; exp1++)
                for (float c2 = -10; c2 < 10; c2 += 2.3)
                    for (int exp2 = 5; exp2 < 10; exp2++) {
                        Polynomial fst = new Polynomial(c1 + " " + exp1 + " " + c2 + " " + exp2);
                        Polynomial snd = new Polynomial(c1 + " " + exp2 + " " + c2 + " " + exp1);
                        Polynomial fst_2 = new Polynomial(fst);
                        Polynomial snd_2 = new Polynomial(snd);
                        fst.minus(snd);
                        snd_2.times(new Polynomial("-1 0"));
                        fst_2.plus(snd_2);
                        assertEquals(true, fst.equals(fst_2));
                    }
        
        // Test associativity for some random polynomials
        Polynomial a = new Polynomial("5 4 2 5 20 8 4 0");
        Polynomial b = new Polynomial("9 2 48 2 10 8 28 2");
        Polynomial c = new Polynomial("3 8 4 20 8 2 4 5 0 0");
        Polynomial a_bc = new Polynomial(a);
        a_bc.minus(b);
        a_bc.minus(c);
        Polynomial a_cb = new Polynomial(a);
        a_cb.minus(c);
        a_cb.minus(b);
        assertEquals(true, a_bc.equals(a_cb));
    }

    /**
     * Test of times method, of class Polynomial.
     */
    @Test
    public void testTimes() {
        System.out.println("times");
        
        // Simple case
        Polynomial instance = new Polynomial("7 1 6 3");
        instance.times(new Polynomial("5 3 4 2"));
        assertEquals(new Polynomial("30 6 24 5 35 4 28 3"), instance);
        
        // Simple case with zero polynomial
        instance = new Polynomial("5 3");
        instance.times(new Polynomial());
        assertEquals(new Polynomial(), instance);
        
        // Advanced case with coefficient 0 in the result
        instance = new Polynomial("4 4 2 3");                           // 4x^4 + 2x^3
        instance.times(new Polynomial("2 2 -4 3"));                     // 2x^2 - 4x^3
        assertEquals(new Polynomial("4 5 -16 7"), instance);            // 4x^5 - 16x^7 (exponent 6 cancels out)
        
        // Test associativity for some random polynomials
        Polynomial a = new Polynomial("5 4 2 5 20 8 4 0");
        Polynomial b = new Polynomial("9 2 48 2 10 8 28 2");
        Polynomial c = new Polynomial("3 8 4 20 8 2 4 5 0 0");
        Polynomial a_bc = new Polynomial(a);
        a_bc.times(b);
        a_bc.times(c);
        Polynomial a_cb = new Polynomial(a);
        a_cb.times(c);
        a_cb.times(b);
        assertEquals(true, a_bc.equals(a_cb));
        
        // Test commutativity
        Polynomial a_b = new Polynomial(a);
        a_b.times(b);
        Polynomial b_a = new Polynomial(b);
        b_a.times(a);
        assertEquals(true, a_b.equals(b_a));
        
        // Test left distributivity
        Polynomial b_plus_c = new Polynomial(b);
        b_plus_c.plus(c);
        Polynomial a_times_b_plus_c = new Polynomial(a);
        a_times_b_plus_c.times(b_plus_c);
        Polynomial a_times_c = new Polynomial(a);
        a_times_c.times(c);
        Polynomial a_times_b_plus_a_times_c = new Polynomial(a);
        a_times_b_plus_a_times_c.times(b);
        a_times_b_plus_a_times_c.plus(a_times_c);
        assertEquals(true, a_times_b_plus_c.equals(a_times_b_plus_a_times_c));
        
        // Test right distributivity
        Polynomial b_plus_c_times_a = new Polynomial(b);
        b_plus_c_times_a.plus(c);
        b_plus_c_times_a.times(a);
        Polynomial c_times_a = new Polynomial(c);
        c_times_a.times(a);
        Polynomial b_times_a_plus_c_times_a = new Polynomial(b);
        b_times_a_plus_c_times_a.times(a);
        b_times_a_plus_c_times_a.plus(c_times_a);
        assertEquals(true, b_plus_c_times_a.equals(b_times_a_plus_c_times_a));
    }

    /**
     * Test of divide method, of class Polynomial.
     */
    @Test
    public void testDivide() {
        System.out.println("divide");
        Polynomial b = null;
        Polynomial instance = new Polynomial();
        instance.divide(b);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    }

    /**
     * Test of equals method, of class Polynomial.
     */
    @Test
    public void testEquals() {
        System.out.println("equals");
        // Order of terms irrelevant
        assertEquals(true, new Polynomial("4 2 3 5").equals(new Polynomial("3 5 4 2")));
        // Terms with same exponent are added
        assertEquals(true, new Polynomial("5 5 10 5").equals(new Polynomial("15 5")));
        // Simple coefficient 0 removed
        assertEquals(true, new Polynomial("0 7").equals(new Polynomial()));
        // Advanced coefficient 0 removed
        assertEquals(true, new Polynomial("5 10 -5 10").equals(new Polynomial()));
        // Difference in exponent matters
        assertEquals(false, new Polynomial("10 0").equals(new Polynomial("10 1")));
        // Difference in coefficient matters
        assertEquals(false, new Polynomial("10 0").equals(new Polynomial("5 0")));
    }

    /**
     * Test of plus method, of class Polynomial.
     */
    @Test
    public void testPlus_Polynomial() {
        System.out.println("plus");
        // Test cancelling out and exponents that don't exist in either of the two polynomials all at once
        Polynomial instance = new Polynomial("7 4 -4 2 7 1 5 0");       // 7x^4 - 4x^2 + 7x + 5
        instance.plus(new Polynomial("5 3 4 2 3 1 10 0"));              // 5x^3 + 4x^2 + 3x + 10
        assertEquals(new Polynomial("7 4 5 3 10 1 15 0"), instance);    // 7x^4 + 5x^3 + 10x + 15
        
        // Test associativity for some random polynomials
        Polynomial a = new Polynomial("5 4 2 5 20 8 4 0");
        Polynomial b = new Polynomial("9 2 7 5 10 8 28 2");
        Polynomial c = new Polynomial("3 8 4 20 8 2 4 5 0 0");
        Polynomial a_bc = new Polynomial(a);
        a_bc.plus(b);
        a_bc.plus(c);
        Polynomial a_cb = new Polynomial(a);
        a_cb.plus(c);
        a_cb.plus(b);
        assertEquals(true, a_bc.equals(a_cb));
        
        // Test commutativity
        Polynomial a_b = new Polynomial(a);
        a_b.plus(b);
        Polynomial b_a = new Polynomial(b);
        b_a.plus(a);
        assertEquals(true, a_b.equals(b_a));
    }

    /**
     * Test of apply method, of class Polynomial.
     */
    @Test
    public void testApply() {
        System.out.println("apply");
        // Test zero coefficient, floating point coefficient and negative coefficient all at once
        Polynomial instance = new Polynomial("0 7 5 4 0.5 3 9 2 -8 0");
        assertEquals(422654.5, instance.apply(17.0), 0.0);
    }
    
}