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