Discussion:
Funtional Tests - Project 3
(too old to reply)
Tiuley Alguindigue
2004-03-03 18:18:17 UTC
Permalink
Hello,

Here is JUNIT class that you can use to do some functionality tests on
your PolynomialAsLinkedList class.

It assumes that the toString() function returns the following format:

-2X3+3X2+X+1.0

Also, toString returns 0.0 for the 0 polynomial.

You can modify the assert statements to fit your own implementation of
toString().

These test cases are similar to the ones used by the evaluation software
but they are not the same.

The instructions to run this JUNIT class are in this news group, under
the topic Functional Tests - Rational Class.

The test class for project 3 is attached.
Jack
2004-03-04 01:18:27 UTC
Permalink
For the computer marking...
is it accepting coefficient of 1?

e.g. 1.0x, -1.0x ??
Post by Tiuley Alguindigue
Hello,
Here is JUNIT class that you can use to do some functionality tests on
your PolynomialAsLinkedList class.
-2X3+3X2+X+1.0
Also, toString returns 0.0 for the 0 polynomial.
You can modify the assert statements to fit your own implementation of
toString().
These test cases are similar to the ones used by the evaluation software
but they are not the same.
The instructions to run this JUNIT class are in this news group, under
the topic Functional Tests - Rational Class.
The test class for project 3 is attached.
----------------------------------------------------------------------------
----
Post by Tiuley Alguindigue
import junit.framework.TestCase;
/*
* Created on Feb 3, 2004
*
* To change the template for this generated file go to
*/
/**
*
* To change the template for this generated type comment go to
*/
public class TestProject3 extends TestCase {
/**
* Constructor for TestProject2.
*/
public TestProject3(String arg0) {
super(arg0);
}
/*
*/
protected void setUp() throws Exception {
super.setUp();
}
/*
*/
protected void tearDown() throws Exception {
super.tearDown();
}
public void testCreate() {
/*
*/
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(0,0);
assertEquals("Degree not 0",0,p.getDegree());
p.setCoefficient(2,2);
assertEquals("Degree not 2",2,p.getDegree());
p.setCoefficient(4,4);
assertEquals("Degree not 4",4,p.getDegree());
p.setCoefficient(4,0);
assertEquals("Degree not 2",2,p.getDegree());
// Final result
assertEquals("Polynomial is not as
expected","2.0x^2",(p.toString()).trim().toLowerCase());
Post by Tiuley Alguindigue
}
public void testGetDegree() {
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(0,0);
assertEquals("Degree not 0",0,p.getDegree());
Polynomial p1 = new PolynomialAsLinkedList();
p1.setCoefficient(7,5.0);
p1.setCoefficient(5,1.0);
p1.setCoefficient(2,1.0);
p1.setCoefficient(0,3.0);
assertEquals("Degree not 7",7,p1.getDegree());
assertEquals("Polynomial is not as expected
","5.0x^7+x^5+x^2+3.0",(p1.toString()).trim().toLowerCase());
Post by Tiuley Alguindigue
p1.setCoefficient(7,0.0);
assertEquals("Degree not 5",5,p1.getDegree());
assertEquals("Polynomial is not as expected- 7 -
","x^5+x^2+3.0",(p1.toString()).trim().toLowerCase());
Post by Tiuley Alguindigue
}
public void testAssign() {
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(15,6.0);
p.setCoefficient(10,5.0);
p.setCoefficient(7,5.0);
p.setCoefficient(5,1.0);
p.setCoefficient(2,1.0);
p.setCoefficient(0,3.0);
Polynomial q = new PolynomialAsLinkedList();
q.setCoefficient(16,8.0);
q.setCoefficient(8,2.0);
q.setCoefficient(4,8.0);
q.setCoefficient(2,1.0);
q.setCoefficient(10,2.0);
/*Test assign to non-zero polynomial, p.assign(q) */
p.assign(q);
assertEquals("Polynomial is not as
expected","8.0x^16+2.0x^10+2.0x^8+8.0x^4+x^2",(p.toString()).trim().toLowerC
ase());
Post by Tiuley Alguindigue
}
public void testGetDerivative() {
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(10,5.0);
p.setCoefficient(7,5.0);
p.setCoefficient(5,1.0);
p.setCoefficient(2,1.0);
Polynomial q = new PolynomialAsLinkedList();
q = p.getDerivative();
assertEquals("Polynomial is not as
expected","50.0x^9+35.0x^6+5.0x^4+2.0x",(q.toString()).trim().toLowerCase())
;
Post by Tiuley Alguindigue
}
public void testToString() {
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(100,-1);
p.setCoefficient(50,2.0);
p.setCoefficient(25,-2.0);
p.setCoefficient(0,3.0);
assertEquals("Polynomial is not as
expected","-x^100+2.0x^50-2.0x^25+3.0",(p.toString()).trim().toLowerCase());
Post by Tiuley Alguindigue
}
public void testEval() {
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(4,8.0);
p.setCoefficient(2,1.0);
p.setCoefficient(1,7.0);
p.setCoefficient(0,2.0);
assertEquals(4.0,4.0,p.eval(-1.0));
assertEquals(2.0,2.0,p.eval(0.0));
assertEquals(148.0,148.0,p.eval(2.0));
}
public void testPlus1() {
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(10,5.0);
p.setCoefficient(7,5.0);
p.setCoefficient(2,1.0);
p.setCoefficient(0,3.0);
Polynomial q = new PolynomialAsLinkedList();
q.setCoefficient(10,5.0);
q.setCoefficient(7,5.0);
q.setCoefficient(2,1.0);
q.setCoefficient(0,3.0);
assertEquals("Polynomial is not as
expected","10.0x^10+10.0x^7+2.0x^2+6.0",((p.plus(q)).toString()).trim().toLo
werCase());
Post by Tiuley Alguindigue
}
public void testPlus2() {
/* Test adding the same degree of this polynomial (reducing degree),
p.plus(q) */
Post by Tiuley Alguindigue
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(15,6.0);
p.setCoefficient(10,5.0);
p.setCoefficient(7,5.0);
Polynomial q = new PolynomialAsLinkedList();
q.setCoefficient(15,-6.0);
q.setCoefficient(10,-7.0);
q.setCoefficient(7,-7.0);
assertEquals("Polynomial is not as
expected","-2.0x^10-2.0x^7",((p.plus(q)).toString()).trim().toLowerCase());
Post by Tiuley Alguindigue
}
public void testPlus3() {
/* Test adding the same degree of this polynomial (negative), p.plus(q) */
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(15,6.0);
p.setCoefficient(10,5.0);
Polynomial q = new PolynomialAsLinkedList();
q.setCoefficient(15,-6.0);
q.setCoefficient(10,-5.0);
assertEquals("Polynomial is not as
expected","0.0",((p.plus(q)).toString()).trim().toLowerCase());
Post by Tiuley Alguindigue
}
public void testPlus4() {
/* x^2 + x + 1 + 0 */
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(2,1.0);
p.setCoefficient(1,1.0);
p.setCoefficient(0,1.0);
Polynomial q = new PolynomialAsLinkedList();
q.setCoefficient(0,0.0);
assertEquals("Polynomial is not as
expected","x^2+x+1.0",((p.plus(q)).toString()).trim().toLowerCase());
Post by Tiuley Alguindigue
}
public void testMinus1() {
/*
Test subtracting the same degree of this polynomial (reducing degree)
*/
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(15,6.0);
p.setCoefficient(10,5.0);
p.setCoefficient(7,5.0);
Polynomial q = new PolynomialAsLinkedList();
q.setCoefficient(15,6.0);
q.setCoefficient(10,7.0);
q.setCoefficient(7,7.0);
assertEquals("Polynomial is not as
expected","-2.0x^10-2.0x^7",((p.minus(q)).toString()).trim().toLowerCase());
Post by Tiuley Alguindigue
}
public void testMinus2() {
/* Test subtracting bigger degree of this polynomial, p.minus(q) */
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(15,6.0);
p.setCoefficient(10,5.0);
p.setCoefficient(7,5.0);
Polynomial q = new PolynomialAsLinkedList();
q.setCoefficient(17,8.0);
q.setCoefficient(8,2.0);
assertEquals("Polynomial is not as
expected","-8.0x^17+6.0x^15+5.0x^10-2.0x^8+5.0x^7",((p.minus(q)).toString())
.trim().toLowerCase());
Post by Tiuley Alguindigue
}
public void testMinus3() {
/* Test subtracting smaller degree of this polynomial, p.minus(q) */
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(17,8.0);
p.setCoefficient(8,2.0);
p.setCoefficient(4,8.0);
Polynomial q = new PolynomialAsLinkedList();
q.setCoefficient(15,6.0);
q.setCoefficient(10,5.0);
q.setCoefficient(7,5.0);
assertEquals("Polynomial is not as
expected","8.0x^17-6.0x^15-5.0x^10+2.0x^8-5.0x^7+8.0x^4",((p.minus(q)).toStr
ing()).trim().toLowerCase());
Post by Tiuley Alguindigue
}
public void testTimes1() {
/*
* p= 6.0x^15+5.0x^10+5.0x^7+x^5+x^2+3.0
q= 8.0x^16+2.0x^8+8.0x^4+x^2+2.0x^10.0
Test p.times(q) - OK [+1.0]
*/
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(15,6.0);
p.setCoefficient(10,5.0);
p.setCoefficient(7,5.0);
p.setCoefficient(5,1.0);
p.setCoefficient(2,1.0);
p.setCoefficient(0,3.0);
Polynomial q = new PolynomialAsLinkedList();
q.setCoefficient(16,8.0);
q.setCoefficient(8,2.0);
q.setCoefficient(4,8.0);
q.setCoefficient(2,1.0);
q.setCoefficient(10,2.0);
assertEquals("Polynomial is not as
expected","48.0x^31+40.0x^26+12.0x^25+52.0x^23+8.0x^21+10.0x^20+48.0x^19+18.
0x^18+16.0x^17+24.0x^16+12.0x^15+40.0x^14+2.0x^13+7.0x^12+40.0x^11+8.0x^10+1
3.0x^9+6.0x^8+x^7+8.0x^6+25.0x^4+3.0x^2",((p.times(q)).toString()).trim().to
LowerCase());
Post by Tiuley Alguindigue
}
public void testTimes2() {
/*x^2 - 1 * -x^2 - 1 */
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(2,1.0);
p.setCoefficient(0,-1.0);
Polynomial q = new PolynomialAsLinkedList();
q.setCoefficient(2,-1.0);
q.setCoefficient(0,-1.0);
/*-x^4+1.0 */
assertEquals("Polynomial is not as
expected","-x^4+1.0",((p.times(q)).toString()).trim().toLowerCase());
Post by Tiuley Alguindigue
}
public void testRobustness() {
Test getCoefficient(-1) - OK [+0.5] - since RuntimeException
java.lang.IllegalArgumentException is thrown
Post by Tiuley Alguindigue
Test setCoefficient(-1) - OK [+0.5] - since RuntimeException
java.lang.IllegalArgumentException is thrown
Post by Tiuley Alguindigue
*/
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(15,6.0);
p.setCoefficient(10,2.0);
p.setCoefficient(4,5.0);
p.setCoefficient(5,1.0);
p.setCoefficient(2,1.0);
p.setCoefficient(0,3.0);
try {
p.setCoefficient(-1,-1.0);
}
catch (IllegalArgumentException iae)
{}
try {
p.getCoefficient(-1);
}
catch (IllegalArgumentException iae)
{}
}
}
Igor Ivkovic
2004-03-04 07:38:12 UTC
Permalink
Post by Jack
For the computer marking...
is it accepting coefficient of 1?
e.g. 1.0x, -1.0x ??
Both x and 1.0x should work fine with the AE. This will only come up when
testing toString. Other tests use the coefficient value for comparison.

Igor
Post by Jack
Post by Tiuley Alguindigue
Hello,
Here is JUNIT class that you can use to do some functionality tests on
your PolynomialAsLinkedList class.
-2X3+3X2+X+1.0
Also, toString returns 0.0 for the 0 polynomial.
You can modify the assert statements to fit your own implementation of
toString().
These test cases are similar to the ones used by the evaluation software
but they are not the same.
The instructions to run this JUNIT class are in this news group, under
the topic Functional Tests - Rational Class.
The test class for project 3 is attached.
----------------------------------------------------------------------------
----
Post by Tiuley Alguindigue
import junit.framework.TestCase;
/*
* Created on Feb 3, 2004
*
* To change the template for this generated file go to
*/
/**
*
* To change the template for this generated type comment go to
*/
public class TestProject3 extends TestCase {
/**
* Constructor for TestProject2.
*/
public TestProject3(String arg0) {
super(arg0);
}
/*
*/
protected void setUp() throws Exception {
super.setUp();
}
/*
*/
protected void tearDown() throws Exception {
super.tearDown();
}
public void testCreate() {
/*
*/
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(0,0);
assertEquals("Degree not 0",0,p.getDegree());
p.setCoefficient(2,2);
assertEquals("Degree not 2",2,p.getDegree());
p.setCoefficient(4,4);
assertEquals("Degree not 4",4,p.getDegree());
p.setCoefficient(4,0);
assertEquals("Degree not 2",2,p.getDegree());
// Final result
assertEquals("Polynomial is not as
expected","2.0x^2",(p.toString()).trim().toLowerCase());
Post by Tiuley Alguindigue
}
public void testGetDegree() {
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(0,0);
assertEquals("Degree not 0",0,p.getDegree());
Polynomial p1 = new PolynomialAsLinkedList();
p1.setCoefficient(7,5.0);
p1.setCoefficient(5,1.0);
p1.setCoefficient(2,1.0);
p1.setCoefficient(0,3.0);
assertEquals("Degree not 7",7,p1.getDegree());
assertEquals("Polynomial is not as expected
","5.0x^7+x^5+x^2+3.0",(p1.toString()).trim().toLowerCase());
Post by Tiuley Alguindigue
p1.setCoefficient(7,0.0);
assertEquals("Degree not 5",5,p1.getDegree());
assertEquals("Polynomial is not as expected- 7 -
","x^5+x^2+3.0",(p1.toString()).trim().toLowerCase());
Post by Tiuley Alguindigue
}
public void testAssign() {
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(15,6.0);
p.setCoefficient(10,5.0);
p.setCoefficient(7,5.0);
p.setCoefficient(5,1.0);
p.setCoefficient(2,1.0);
p.setCoefficient(0,3.0);
Polynomial q = new PolynomialAsLinkedList();
q.setCoefficient(16,8.0);
q.setCoefficient(8,2.0);
q.setCoefficient(4,8.0);
q.setCoefficient(2,1.0);
q.setCoefficient(10,2.0);
/*Test assign to non-zero polynomial, p.assign(q) */
p.assign(q);
assertEquals("Polynomial is not as
expected","8.0x^16+2.0x^10+2.0x^8+8.0x^4+x^2",(p.toString()).trim().toLowerC
ase());
Post by Tiuley Alguindigue
}
public void testGetDerivative() {
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(10,5.0);
p.setCoefficient(7,5.0);
p.setCoefficient(5,1.0);
p.setCoefficient(2,1.0);
Polynomial q = new PolynomialAsLinkedList();
q = p.getDerivative();
assertEquals("Polynomial is not as
expected","50.0x^9+35.0x^6+5.0x^4+2.0x",(q.toString()).trim().toLowerCase())
;
Post by Tiuley Alguindigue
}
public void testToString() {
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(100,-1);
p.setCoefficient(50,2.0);
p.setCoefficient(25,-2.0);
p.setCoefficient(0,3.0);
assertEquals("Polynomial is not as
expected","-x^100+2.0x^50-2.0x^25+3.0",(p.toString()).trim().toLowerCase());
Post by Tiuley Alguindigue
}
public void testEval() {
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(4,8.0);
p.setCoefficient(2,1.0);
p.setCoefficient(1,7.0);
p.setCoefficient(0,2.0);
assertEquals(4.0,4.0,p.eval(-1.0));
assertEquals(2.0,2.0,p.eval(0.0));
assertEquals(148.0,148.0,p.eval(2.0));
}
public void testPlus1() {
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(10,5.0);
p.setCoefficient(7,5.0);
p.setCoefficient(2,1.0);
p.setCoefficient(0,3.0);
Polynomial q = new PolynomialAsLinkedList();
q.setCoefficient(10,5.0);
q.setCoefficient(7,5.0);
q.setCoefficient(2,1.0);
q.setCoefficient(0,3.0);
assertEquals("Polynomial is not as
expected","10.0x^10+10.0x^7+2.0x^2+6.0",((p.plus(q)).toString()).trim().toLo
werCase());
Post by Tiuley Alguindigue
}
public void testPlus2() {
/* Test adding the same degree of this polynomial (reducing degree),
p.plus(q) */
Post by Tiuley Alguindigue
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(15,6.0);
p.setCoefficient(10,5.0);
p.setCoefficient(7,5.0);
Polynomial q = new PolynomialAsLinkedList();
q.setCoefficient(15,-6.0);
q.setCoefficient(10,-7.0);
q.setCoefficient(7,-7.0);
assertEquals("Polynomial is not as
expected","-2.0x^10-2.0x^7",((p.plus(q)).toString()).trim().toLowerCase());
Post by Tiuley Alguindigue
}
public void testPlus3() {
/* Test adding the same degree of this polynomial (negative), p.plus(q) */
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(15,6.0);
p.setCoefficient(10,5.0);
Polynomial q = new PolynomialAsLinkedList();
q.setCoefficient(15,-6.0);
q.setCoefficient(10,-5.0);
assertEquals("Polynomial is not as
expected","0.0",((p.plus(q)).toString()).trim().toLowerCase());
Post by Tiuley Alguindigue
}
public void testPlus4() {
/* x^2 + x + 1 + 0 */
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(2,1.0);
p.setCoefficient(1,1.0);
p.setCoefficient(0,1.0);
Polynomial q = new PolynomialAsLinkedList();
q.setCoefficient(0,0.0);
assertEquals("Polynomial is not as
expected","x^2+x+1.0",((p.plus(q)).toString()).trim().toLowerCase());
Post by Tiuley Alguindigue
}
public void testMinus1() {
/*
Test subtracting the same degree of this polynomial (reducing degree)
*/
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(15,6.0);
p.setCoefficient(10,5.0);
p.setCoefficient(7,5.0);
Polynomial q = new PolynomialAsLinkedList();
q.setCoefficient(15,6.0);
q.setCoefficient(10,7.0);
q.setCoefficient(7,7.0);
assertEquals("Polynomial is not as
expected","-2.0x^10-2.0x^7",((p.minus(q)).toString()).trim().toLowerCase());
Post by Tiuley Alguindigue
}
public void testMinus2() {
/* Test subtracting bigger degree of this polynomial, p.minus(q) */
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(15,6.0);
p.setCoefficient(10,5.0);
p.setCoefficient(7,5.0);
Polynomial q = new PolynomialAsLinkedList();
q.setCoefficient(17,8.0);
q.setCoefficient(8,2.0);
assertEquals("Polynomial is not as
expected","-8.0x^17+6.0x^15+5.0x^10-2.0x^8+5.0x^7",((p.minus(q)).toString())
.trim().toLowerCase());
Post by Tiuley Alguindigue
}
public void testMinus3() {
/* Test subtracting smaller degree of this polynomial, p.minus(q) */
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(17,8.0);
p.setCoefficient(8,2.0);
p.setCoefficient(4,8.0);
Polynomial q = new PolynomialAsLinkedList();
q.setCoefficient(15,6.0);
q.setCoefficient(10,5.0);
q.setCoefficient(7,5.0);
assertEquals("Polynomial is not as
expected","8.0x^17-6.0x^15-5.0x^10+2.0x^8-5.0x^7+8.0x^4",((p.minus(q)).toStr
ing()).trim().toLowerCase());
Post by Tiuley Alguindigue
}
public void testTimes1() {
/*
* p= 6.0x^15+5.0x^10+5.0x^7+x^5+x^2+3.0
q= 8.0x^16+2.0x^8+8.0x^4+x^2+2.0x^10.0
Test p.times(q) - OK [+1.0]
*/
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(15,6.0);
p.setCoefficient(10,5.0);
p.setCoefficient(7,5.0);
p.setCoefficient(5,1.0);
p.setCoefficient(2,1.0);
p.setCoefficient(0,3.0);
Polynomial q = new PolynomialAsLinkedList();
q.setCoefficient(16,8.0);
q.setCoefficient(8,2.0);
q.setCoefficient(4,8.0);
q.setCoefficient(2,1.0);
q.setCoefficient(10,2.0);
assertEquals("Polynomial is not as
expected","48.0x^31+40.0x^26+12.0x^25+52.0x^23+8.0x^21+10.0x^20+48.0x^19+18.
0x^18+16.0x^17+24.0x^16+12.0x^15+40.0x^14+2.0x^13+7.0x^12+40.0x^11+8.0x^10+1
3.0x^9+6.0x^8+x^7+8.0x^6+25.0x^4+3.0x^2",((p.times(q)).toString()).trim().to
LowerCase());
Post by Tiuley Alguindigue
}
public void testTimes2() {
/*x^2 - 1 * -x^2 - 1 */
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(2,1.0);
p.setCoefficient(0,-1.0);
Polynomial q = new PolynomialAsLinkedList();
q.setCoefficient(2,-1.0);
q.setCoefficient(0,-1.0);
/*-x^4+1.0 */
assertEquals("Polynomial is not as
expected","-x^4+1.0",((p.times(q)).toString()).trim().toLowerCase());
Post by Tiuley Alguindigue
}
public void testRobustness() {
Test getCoefficient(-1) - OK [+0.5] - since RuntimeException
java.lang.IllegalArgumentException is thrown
Post by Tiuley Alguindigue
Test setCoefficient(-1) - OK [+0.5] - since RuntimeException
java.lang.IllegalArgumentException is thrown
Post by Tiuley Alguindigue
*/
Polynomial p = new PolynomialAsLinkedList();
p.setCoefficient(15,6.0);
p.setCoefficient(10,2.0);
p.setCoefficient(4,5.0);
p.setCoefficient(5,1.0);
p.setCoefficient(2,1.0);
p.setCoefficient(0,3.0);
try {
p.setCoefficient(-1,-1.0);
}
catch (IllegalArgumentException iae)
{}
try {
p.getCoefficient(-1);
}
catch (IllegalArgumentException iae)
{}
}
}
Loading...