I need some help. I am doing test-driven development. These are the tests:
@Test public void add01() throws TError { assertEquals(new Term(10,5), new Term(4,5).add(new Term(6,5))); }
@Test public void add02() throws TError { assertEquals(new Term(6,5), new Term(0,5).add(new Term(6,5))); }
@Test public void add03() throws TError { assertEquals(new Term(2,5), new Term(-4,5).add(new Term(6,5))); }
@Test public void add04() throws TError { assertEquals(new Term(10,0), new Term(4,0).add(new Term(6,0))); }
@Test public void add05() throws TError { assertEquals(new Term(4,5), new Term(4,5).add(new Term(0,5))); }
@Test public void add06() throws TError { assertEquals(new Term(-2,5), new Term(4,5).add(new Term(-6,5))); }
@Test (expected = IncompatibleTerms.class)
public void add07() throws TError { t = new Term(4,5).add(new Term(6,0)); }
@Test (expected = CoefficientOverflow.class)
public void add08() throws TError { t = new Term(min,4).add(new Term(-1,4)); }
@Test public void add09() throws TError { assertEquals(new Term(min,4), new Term(min+1,4).add(new Term(-1,4))); }
@Test public void add10() throws TError { assertEquals(new Term(-11,4), new Term(-10,4).add(new Term(-1,4))); }
@Test public void add11() throws TError { assertEquals(new Term(-2,4), new Term(-1,4).add(new Term(-1,4))); }
@Test public void add12() throws TError { assertEquals(new Term(-1,4), new Term(0,4).add(new Term(-1,4))); }
@Test public void add13() throws TError { assertEquals(Term.Zero, new Term(1,4).add(new Term(-1,4))); }
@Test public void add14() throws TError { assertEquals(new Term(9,4), new Term(10,4).add(new Term(-1,4))); }
@Test public void add15() throws TError { assertEquals(new Term(max,4), new Term(max-1,4).add(new Term(1,4))); }
@Test (expected = CoefficientOverflow.class)
public void add16() throws TError { t = new Term(max,4).add(new Term(1,4)); }
//Using domain-specific knowledge: addition should be commutative
@Test public void add17() throws TError { assertEquals(new Term(-1,2).add(new Term(1,2)), new Term(1,2).add(new Term(-1,2))); }
@Test public void add18() throws TError { assertEquals(new Term(min+1,2).add(new Term(-1,2)), new Term(-1,2).add(new Term(min+1,2))); }
@Test public void add19() throws TError { assertEquals(new Term(min,2).add(Term.Zero), Term.Zero.add(new Term(min,2))); }
@Test public void add20() throws TError { assertEquals(new Term(min,0).add(Term.Unit), Term.Unit.add(new Term(min,0))); }
@Test public void add21() throws TError { assertEquals(new Term(max,0).add(Term.Zero), Term.Zero.add(new Term(max,0))); }
@Test public void add22() throws TError { assertEquals(new Term(max-1,2).add(new Term(1,2)), new Term(1,2).add(new Term(max-1,2))); }
@Test public void add23() throws TError { assertEquals(new Term(max,2).add(new Term(min,2)), new Term(min,2).add(new Term(max,2))); }
This is my add method so far with the help of "Andrej Gajduk".
public Term add(Term that) throws CoefficientOverflow, IncompatibleTerms {
if (this.expo != that.expo) throw new IncompatibleTerms();
return new Term (this.coef + that.coef,expo);
}
These are the tests that fail = 2, 5, 8, 12, 16, 19, 21. But I have doubts about tests 17-23; I know some of them pass but I don't think they should because that is like x + y = y + x, but I am not sure how to implement it.
I need some help/guidance on why this tests are failing.
For tests 8 and 16 I have tried the code below. It does the job but then it creates more errors on the other tests. So then all together the fails become 2, 5, 8, 12, 16, 19, 21 + 20, 23 (two extra fails)
if (this.coef == Integer.MIN_VALUE || that.coef == Integer.MIN_VALUE) throw new CoefficientOverflow();
if (this.coef == Integer.MAX_VALUE || that.coef == Integer.MAX_VALUE) throw new CoefficientOverflow();