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