-1

助けが必要です。私はテスト駆動開発を行っています。これらはテストです:

@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))); }

これは、「AndrejGajduk」の助けを借りたこれまでの私の追加方法です。

public Term add(Term that) throws CoefficientOverflow, IncompatibleTerms {
    if (this.expo != that.expo) throw new IncompatibleTerms();
    return new Term (this.coef + that.coef,expo);
}

これらは失敗するテストです=2、5、8、12、16、19、21。しかし、私はテスト17-23について疑問を持っています。私はそれらのいくつかが合格することを知っていますが、それはx + y = y + xのようなものであるため、そうすべきではないと思いますが、それを実装する方法がわかりません。

このテストが失敗する理由について、ヘルプ/ガイダンスが必要です。

テスト8と16では、以下のコードを試しました。それは仕事をしますが、それから他のテストでより多くのエラーを作成します。したがって、すべて一緒に失敗は2、5、8、12、16、19、21 + 20、23になります(2つの追加の失敗)

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();
4

2 に答える 2

0

このリンクは、オーバーフローを検出する別の方法を示しています:http: //mindprod.com/jgloss/gotchas.html#OVERFLOW

これは、coefが0の場合、またはステートメント全体がゼロであると想定されるため、これを確認して、ゼロの代替を使用する必要があるためだと思います。

if(this.coef ==0)
{//return Term t = new Term(that.coef, that.expo);}

else
{//return Term t = new Term(this.coef, this.expo);}
}

これがお役に立てば幸いです

于 2012-11-17T20:56:03.843 に答える
0

あなたのequals方法は正しくありません。以下のように更新してください。

@Override
public boolean equals(Object that) {
    if (that == null) {
        return false;
    }else if (this.getClass() != that.getClass()) {
        return false;
    }else if (this == that) {
        return true;
    }else{
        Term term = (Term) that;
        if ((term.coef) == (this.coef) && (term.expo) == (this.expo)) {
            return true;
        }
    }
    return false;
}

ほとんどのテストケースに合格するには、次のようにaddメソッドを更新します。

public Term add(Term that) throws IncompatibleTerms, CoefficientOverflow{
    //handle addition with term zero
        if(this.equals(Term.Zero)){
        return that;
    }else if(Term.Zero.equals(that)){
        return this;
    }

    if (this.expo != that.expo) throw new IncompatibleTerms();
    if((this.coef>= 0 && that.coef >= 0 && this.coef + that.coef <0) ||
            (this.coef<= 0 && that.coef <= 0 && this.coef + that.coef >0)){
                         throw new CoefficientOverflow();
        }    
    return new Term (this.coef + that.coef,expo);
}
于 2012-11-17T20:58:08.783 に答える