【发布时间】:2021-05-12 15:33:05
【问题描述】:
我很想知道是否可以使用 Pitest/ScalaCheck 全面测试以下代码
测试方法:
def insertionSort(xs: List[Int]): List[Int] = xs match {
case Nil => Nil
case a :: as => insert(a, insertionSort(as))
}
private def insert(x: Int, xs: List[Int]): List[Int] = xs match {
case Nil => List(x)
case a :: as =>
if (a >= x) x :: xs
else a :: insert(x, as)
}
提供的测试:
@RunWith(classOf[ScalaCheckJUnitPropertiesRunner])
class InsertionSortTests extends Properties("InsertionSortTests") {
private val nonEmptyIntListGen: Gen[List[Int]] = Gen.nonEmptyListOf(Arbitrary.arbitrary[Int])
property("ordered") = forAll(nonEmptyIntListGen) { (xs: List[Int]) =>
val sorted = insertionSort(xs)
xs.nonEmpty ==> xs.indices.tail.forall((i: Int) => sorted(i - 1) <= sorted(i))
}
property("permutation") = forAll { (xs: List[Int]) =>
val sorted = insertionSort(xs)
def count(a: Int, as: List[Int]) = as.count(_ == a)
xs.forall((x: Int) => count(x, xs) == count(x, sorted))
}
}
我得到了全面的报道,除了以下几行:
def insertionSort(xs: List[Int]): List[Int] = xs match {
private def insert(x: Int, xs: List[Int]): List[Int] = xs match {
两行都出现以下错误:
1. removed call to scala/MatchError::<init> → NO_COVERAGE
【问题讨论】: