001package hw3.test;
002
003import hw3.*;
004
005import org.junit.Test;
006import org.junit.BeforeClass;
007import static org.junit.Assert.*;
008
009/**
010 * This class contains a set of test cases that can be used to test the
011 * implementation of the RatPolyStack class.
012 * 
013 * Some test methods are relatively longer in order to simulate proper stack functionality.
014 */
015public final class RatPolyStackTest {
016    // create a new poly that is a constant (doesn't depend on x)
017    private RatPoly constantPoly(int constant) {
018        return new RatPoly(constant, 0);
019    }
020
021    // create a new poly that is a constant (doesn't depend on x)
022    // taking a char allows us to represent stacks as strings
023    private RatPoly constantPoly(char constant) {
024        return constantPoly(Integer.valueOf("" + constant).intValue());
025    }
026
027    /** @return a new RatPolyStack instance */
028    private RatPolyStack stack() {
029        return new RatPolyStack();
030    }
031
032    // create stack of single-digit constant polys
033    private RatPolyStack stack(String desc) {
034        RatPolyStack s = new RatPolyStack();
035
036        // go backwards to leave first element in desc on _top_ of stack
037        for (int i = desc.length() - 1; i >= 0; i--) {
038            char c = desc.charAt(i);
039            s.push(constantPoly(c));
040        }
041        return s;
042    }
043
044    // RatPoly equality check
045    // (getting around non-definition of RatPoly.equals)
046    private boolean eqv(RatPoly p1, RatPoly p2) {
047        return p1.toString().equals(p2.toString());
048    }
049
050    // Compares 's' to a string describing its values.
051    // desc MUST be a sequence of decimal number chars.
052    // Example call: assertStackIs(myStack, "123")
053    //
054    // NOTE: THIS CAN FAIL WITH A WORKING STACK IF RatPoly.toString IS BROKEN!
055    private void assertStackIs(RatPolyStack s, String desc) {
056        assertEquals(desc.length(), s.size());
057
058        for (int i = 0; i < desc.length(); i++) {
059            RatPoly p = s.getNthFromTop(i);
060            char c = desc.charAt(i);
061            String asstr
062              = String.format("Elem(%d): %s, Expected %c, (Expected Stack:%s)",
063                              i, p, c, desc);
064
065            assertTrue(asstr, eqv(p, constantPoly(c)));
066        }
067    }
068
069        ///////////////////////////////////////////////////////////////////////////////////////
070        ////    Constructor
071        ///////////////////////////////////////////////////////////////////////////////////////     
072    
073    @Test
074    public void testCtor() {
075        RatPolyStack stk1 = stack();
076        assertEquals(0, stk1.size());
077    }
078
079        ///////////////////////////////////////////////////////////////////////////////////////
080        ////    Push
081        /////////////////////////////////////////////////////////////////////////////////////// 
082    
083    @Test
084    public void testPush() {
085        RatPolyStack stk1 = stack();
086        stk1.push(constantPoly(0));
087
088        assertStackIs(stk1, "0");
089
090        stk1.push(constantPoly(0));
091        assertStackIs(stk1, "00");
092
093        stk1.push(constantPoly(1));
094        assertStackIs(stk1, "100");
095
096        stk1 = stack("3");
097        assertStackIs(stk1, "3");
098
099        stk1 = stack("23");
100        assertStackIs(stk1, "23");
101
102        stk1 = stack("123");
103        assertStackIs(stk1, "123");
104    }
105
106    @Test
107    public void testPushCheckForSharingTwixtStacks() {
108        RatPolyStack stk1 = stack();
109        RatPolyStack stk2 = stack("123");
110        assertStackIs(stk1, "");
111        assertStackIs(stk2, "123");
112
113        stk1.push(constantPoly(0));
114        assertStackIs(stk1, "0");
115        assertStackIs(stk2, "123");
116
117        stk1.push(constantPoly(0));
118        assertStackIs(stk1, "00");
119        assertStackIs(stk2, "123");
120
121        stk1.push(constantPoly(1));
122        assertStackIs(stk1, "100");
123        assertStackIs(stk2, "123");
124
125        stk2.push(constantPoly(8));
126        assertStackIs(stk1, "100");
127        assertStackIs(stk2, "8123");
128    }
129
130        ///////////////////////////////////////////////////////////////////////////////////////
131        ////    Pop
132        ///////////////////////////////////////////////////////////////////////////////////////     
133    
134    @Test
135    public void testPop() {
136        RatPolyStack stk1 = stack("123");
137
138        RatPoly poly = stk1.pop();
139        assertTrue(eqv(poly, constantPoly(1)));
140        assertStackIs(stk1, "23");
141
142        poly = stk1.pop();
143        assertTrue(eqv(poly, constantPoly(2)));
144        assertStackIs(stk1, "3");
145
146        poly = stk1.pop();
147        assertStackIs(stk1, "");
148    }
149
150        ///////////////////////////////////////////////////////////////////////////////////////
151        ////    Duplicate
152        ///////////////////////////////////////////////////////////////////////////////////////  
153    
154    @Test
155    public void testDupWithOneVal() {
156        RatPolyStack stk1 = stack("3");
157        stk1.dup();
158        assertStackIs(stk1, "33");
159        stk1 = stack("123");
160        stk1.dup();
161        assertStackIs(stk1, "1123");
162    }
163    
164    @Test
165    public void testDupWithTwoVal() {
166        RatPolyStack stk1 = stack("23");
167        stk1.dup();
168        assertStackIs(stk1, "223");
169        assertEquals(3, stk1.size());
170        assertTrue(eqv(stk1.getNthFromTop(0), constantPoly(2)));
171        assertTrue(eqv(stk1.getNthFromTop(1), constantPoly(2)));
172        assertTrue(eqv(stk1.getNthFromTop(2), constantPoly(3)));
173    }
174    
175    @Test
176    public void testDupWithMultVal() {
177        RatPolyStack stk1 = stack("123");
178        stk1.dup();
179        assertStackIs(stk1, "1123");
180    }
181
182        ///////////////////////////////////////////////////////////////////////////////////////
183        ////    Swap
184        ///////////////////////////////////////////////////////////////////////////////////////  
185    
186    @Test
187    public void testSwapWithTwoElems() {
188        RatPolyStack stk1 = stack("23");
189        stk1.swap();
190        assertStackIs(stk1, "32");    
191    }
192    
193    @Test
194    public void testSwapWithMultElems() {
195        RatPolyStack stk1 = stack("123");
196        stk1.swap();
197        assertStackIs(stk1, "213");
198    }
199
200    @Test
201    public void testSwapWitSameElems() {
202        RatPolyStack stk1 = stack("112");
203        stk1.swap();
204        assertStackIs(stk1, "112");
205    }
206
207        ///////////////////////////////////////////////////////////////////////////////////////
208        ////    Clear
209        ///////////////////////////////////////////////////////////////////////////////////////    
210    
211    @Test
212    public void testClear() {
213        RatPolyStack stk1 = stack("123");
214        stk1.clear();
215        assertStackIs(stk1, "");
216        RatPolyStack stk2 = stack("112");
217        stk2.clear();
218        assertStackIs(stk2, "");
219    }
220
221        ///////////////////////////////////////////////////////////////////////////////////////
222        ////    Add
223        ///////////////////////////////////////////////////////////////////////////////////////  
224    
225    @Test
226    public void testAddTwoElems() {
227        RatPolyStack stk1 = stack("11");
228        stk1.add();
229        assertStackIs(stk1, "2");
230    }
231    
232    @Test
233    public void testAddMultiElems() {
234        RatPolyStack stk1 = stack("123");
235        stk1.add();
236        assertStackIs(stk1, "33");
237        stk1.add();
238        assertStackIs(stk1, "6");
239
240        stk1 = stack("112");
241        stk1.add();
242        assertStackIs(stk1, "22");
243        stk1.add();
244        assertStackIs(stk1, "4");
245        stk1.push(constantPoly(5));
246        assertStackIs(stk1, "54");
247        stk1.add();
248        assertStackIs(stk1, "9");
249
250    }
251    
252    ///////////////////////////////////////////////////////////////////////////////////////
253    ////        Subtract
254    ///////////////////////////////////////////////////////////////////////////////////////
255    
256    @Test
257    public void testSubTwoElems() {
258        RatPolyStack stk1 = stack("12");
259        stk1.sub();
260        assertStackIs(stk1, "1");
261    }
262
263    @Test
264    public void testSubMultiElems() {
265        RatPolyStack stk1 = stack("123");
266        stk1.sub();
267        assertStackIs(stk1, "13");
268        stk1.sub();
269        assertStackIs(stk1, "2");
270
271        stk1 = stack("5723");
272        stk1.sub();
273        assertStackIs(stk1, "223");
274        stk1.sub();
275        assertStackIs(stk1, "03");
276        stk1.sub();
277        assertStackIs(stk1, "3");
278    }
279
280    ///////////////////////////////////////////////////////////////////////////////////////
281    ////        Multiplication
282    ///////////////////////////////////////////////////////////////////////////////////////    
283    
284    @Test
285    public void testMulTwoElems() {
286        RatPolyStack stk1 = stack("23");
287        stk1.mul();
288        assertStackIs(stk1, "6");
289    }
290    
291    @Test
292    public void testMulMultiElems() {
293        RatPolyStack stk1 = stack("123");
294        stk1.mul();
295        assertStackIs(stk1, "23");
296        stk1.mul();
297        assertStackIs(stk1, "6");
298
299        stk1 = stack("112");
300        stk1.mul();
301        assertStackIs(stk1, "12");
302        stk1.mul();
303        assertStackIs(stk1, "2");
304        stk1.push(constantPoly(4));
305        assertStackIs(stk1, "42");
306        stk1.mul();
307        assertStackIs(stk1, "8");
308    }
309
310    ///////////////////////////////////////////////////////////////////////////////////////
311    ////        Division
312    ///////////////////////////////////////////////////////////////////////////////////////  
313    
314    @Test
315    public void testDivTwoElems() {
316        RatPolyStack stk1 = stack("28");
317        stk1.div();
318        assertStackIs(stk1, "4");
319    }
320    
321    @Test
322    public void testDivMultiElems() {
323        RatPolyStack stk1 = stack("123");
324        stk1.div();
325        assertStackIs(stk1, "23");
326    }
327
328    ///////////////////////////////////////////////////////////////////////////////////////
329    ////        Differentiate
330    ///////////////////////////////////////////////////////////////////////////////////////  
331    
332    @Test
333    public void testDifferentiate() {
334        RatPolyStack stk1 = stack("123");
335        stk1.differentiate();
336        stk1.differentiate();
337        stk1.differentiate();
338        stk1.differentiate();
339        assertEquals("Test if stack size changes", 3, stk1.size());
340        assertStackIs(stk1, "023");
341
342        RatPoly rp1 = new RatPoly(3, 5);
343        RatPoly rp2 = new RatPoly(7, 0);
344        RatPoly rp3 = new RatPoly(4, 1);
345        stk1.push(rp1);
346        stk1.push(rp2);
347        stk1.push(rp3);
348
349        stk1.differentiate();
350        assertEquals("Test simple differentiate1", "4", stk1.pop().toString());
351        stk1.differentiate();
352        assertEquals("Test simple differentiate2", "0", stk1.pop().toString());
353        stk1.differentiate();
354        assertEquals("Test simple differentiate3", "15*x^4", stk1.pop().toString());
355    }
356
357    ///////////////////////////////////////////////////////////////////////////////////////
358    ////        Integrate
359    ///////////////////////////////////////////////////////////////////////////////////////  
360    
361    @Test
362    public void testIntegrate() {
363        RatPolyStack stk1 = stack("123");
364        stk1.integrate();
365        stk1.integrate();
366        stk1.integrate();
367        stk1.integrate();
368        assertEquals("Test if stack size changes", 3, stk1.size());
369        assertEquals("Test simple integrate1", "1/24*x^4", stk1.pop().toString());
370        RatPoly rp1 = new RatPoly(15, 4);
371        RatPoly rp2 = new RatPoly(7, 0);
372        RatPoly rp3 = new RatPoly(4, 0);
373        stk1.push(rp1);
374        stk1.push(rp2);
375        stk1.push(rp3);
376
377        stk1.integrate();
378        assertEquals("Test simple integrate1", "4*x", stk1.pop().toString());
379        stk1.integrate();
380        assertEquals("Test simple integrate2", "7*x", stk1.pop().toString());
381        stk1.integrate();
382        assertEquals("Test simple integrate3", "3*x^5", stk1.pop().toString());
383    }
384
385}