2017-05-15 14 views
0

我正在通过Coursera上的course。有实现它是这样的Peano numbers的例子:如何测试Peano号码

abstract class Nat { 
    def isZero: Boolean 
    def predecessor: Nat 
    def successor: Nat = new Succ(this) 
    def +(that: Nat): Nat 
    def -(that: Nat): Nat 
} 

object Zero extends Nat { 
    def isZero = true 
    def predecessor: Nat = throw new Error("0.predecessor") 
    def +(that: Nat): Nat = that 
    def -(that: Nat): Nat = if (that.isZero) this else throw new Error("negative number") 
} 

class Succ(n: Nat) extends Nat { 
    def isZero: Boolean = false 
    def predecessor: Nat = n 
    def +(that: Nat): Nat = new Succ(n + that) 
    def -(that: Nat): Nat = if (that.isZero) this else n - that.predecessor 
} 

我写几个单元测试。大多数的传球,但下面的人 - 写在天真方式 - 从显而易见的原因失败(不同情况下的比较):

trait Fixture { 
    val one = new Succ(Zero) 
    val two = new Succ(one) 
} 

test("successor of zero is one") { 
    new Fixture { 
    assert(Zero.successor == one) 
    } 
} 

test("successor of one is two") { 
    new Fixture { 
    assert(one.successor == two) 
    } 
} 

test("one plus zero is one") { 
    new Fixture { 
    assert((one + Zero) === one) 
    } 
} 

test("one plus one is two") { 
    new Fixture { 
    assert((one + one) === two) 
    } 
} 

我的问题是:单元测试应该如何实施,成功地测试+和 - 操作peano数字?

为了以防万一,在这里您可以找到remaining unit tests。在您的测试

+0

你会如何使用这个类?你不能写一个平等运算符吗?就目前的操作而言,例如,你总是可以检查一个人的前任没有前任,并且以这种方式连锁。 – JETM

+1

为什么你的'Succ'类不是'case class'?它会使一切变得更加容易(除非你还没有听说过)。 –

+0

@JETM这只是一个演示值如何可以是对象。为了更好地理解原理,我开始编写单元测试。是的,这是我的主要想法,要统计有多少前任“零”,但我徘徊,如果有更多的地方*皮亚诺数*。 –

回答

1

感谢来自Cyrille Corpet暗示,在我看来,它是优雅的使用case class“的结构进行比较,而不是引用”。所有的单元测试现在都没有任何改变。

case class Succ(n: Nat) extends Nat { 
    def isZero: Boolean = false 
    def predecessor: Nat = n 
    def +(that: Nat): Nat = new Succ(n + that) 
    def -(that: Nat): Nat = if (that.isZero) this else n - that.predecessor 
} 
-1

看,似乎要测试平等的条件下,你应该写一个函数,该函数:

def eq(i: Nat, j: Nat): Boolean = 
    if (i.isZero | j.isZero) 
    i.isZero == j.isZero 
    else 
    eq(i.predecessor, j.predecessor) 

通过调用替换您的=== & ==eq。你也可以考虑重写平等的方法,而不是把它作为外部(测试)函数。