我正在通过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。在您的测试
你会如何使用这个类?你不能写一个平等运算符吗?就目前的操作而言,例如,你总是可以检查一个人的前任没有前任,并且以这种方式连锁。 – JETM
为什么你的'Succ'类不是'case class'?它会使一切变得更加容易(除非你还没有听说过)。 –
@JETM这只是一个演示值如何可以是对象。为了更好地理解原理,我开始编写单元测试。是的,这是我的主要想法,要统计有多少前任“零”,但我徘徊,如果有更多的地方*皮亚诺数*。 –