如何使用 Scalatest Equality 类型比较 Double 列表?
How to compare lists of Double using Scalatest Equality type?
这是我到目前为止尝试过的方法:
implicit val doubleEq = TolerantNumerics.tolerantDoubleEquality(0.1)
implicit val listEq = new Equivalence[List[Double]] {
override def areEquivalent(a: List[Double], b: List[Double]): Boolean = {
(a, b) match {
case (Nil, Nil) => true
case (x :: xs, y :: ys) => x === y && areEquivalent(xs, ys)
case _ => false
}
}
}
第一个断言成功但第二个断言失败:
assert(1.0 === 1.01)
assert(List(1.0) === List(1.01))
有没有办法让集合也使用我为其元素定义的隐式?
在我的例子中,我通过提供 new Equality[List[Double]]
重新定义了 areEqual
方法,它是 Equivalence[List[Double]]
的子类,考虑到 areEqual
将 Any
作为第二个类型参数。
implicit val listEq = new Equality[List[Double]] {
def areEqual(a: List[Double], b: Any): Boolean = {
def areEqualRec(a: List[Double], b: List[Double]): Boolean = {
(a, b) match {
case (Nil, Nil) => true
case (x :: xs, y :: ys) => x === y && areEquivalent(xs, ys)
case _ => false
}
}
b match {
case daList: List[Double] => areEqualRec(a, daList)
case _ => false
}
}
}
Equality
classes 仅在导入时使用 TypeCheckedTripleEquals
:
Provides === and !== operators that return Boolean, delegate the equality determination to an Equality type class, and require the types of the two values compared to be in a subtype/supertype relationship.
这是基础测试class我用来解决这个问题:
import org.scalactic.{Equivalence, TolerantNumerics, TypeCheckedTripleEquals}
import org.scalatest.FunSuite
abstract class UnitSpec extends FunSuite with TypeCheckedTripleEquals {
implicit val doubleEq = TolerantNumerics.tolerantDoubleEquality(0.001)
implicit val listEq = new Equivalence[List[Double]] {
override def areEquivalent(a: List[Double], b: List[Double]): Boolean = {
(a, b) match {
case (Nil, Nil) => true
case (x :: xs, y :: ys) => x === y && areEquivalent(xs, ys)
case _ => false
}
}
}
}
这是我到目前为止尝试过的方法:
implicit val doubleEq = TolerantNumerics.tolerantDoubleEquality(0.1)
implicit val listEq = new Equivalence[List[Double]] {
override def areEquivalent(a: List[Double], b: List[Double]): Boolean = {
(a, b) match {
case (Nil, Nil) => true
case (x :: xs, y :: ys) => x === y && areEquivalent(xs, ys)
case _ => false
}
}
}
第一个断言成功但第二个断言失败:
assert(1.0 === 1.01)
assert(List(1.0) === List(1.01))
有没有办法让集合也使用我为其元素定义的隐式?
在我的例子中,我通过提供 new Equality[List[Double]]
重新定义了 areEqual
方法,它是 Equivalence[List[Double]]
的子类,考虑到 areEqual
将 Any
作为第二个类型参数。
implicit val listEq = new Equality[List[Double]] {
def areEqual(a: List[Double], b: Any): Boolean = {
def areEqualRec(a: List[Double], b: List[Double]): Boolean = {
(a, b) match {
case (Nil, Nil) => true
case (x :: xs, y :: ys) => x === y && areEquivalent(xs, ys)
case _ => false
}
}
b match {
case daList: List[Double] => areEqualRec(a, daList)
case _ => false
}
}
}
Equality
classes 仅在导入时使用 TypeCheckedTripleEquals
:
Provides === and !== operators that return Boolean, delegate the equality determination to an Equality type class, and require the types of the two values compared to be in a subtype/supertype relationship.
这是基础测试class我用来解决这个问题:
import org.scalactic.{Equivalence, TolerantNumerics, TypeCheckedTripleEquals}
import org.scalatest.FunSuite
abstract class UnitSpec extends FunSuite with TypeCheckedTripleEquals {
implicit val doubleEq = TolerantNumerics.tolerantDoubleEquality(0.001)
implicit val listEq = new Equivalence[List[Double]] {
override def areEquivalent(a: List[Double], b: List[Double]): Boolean = {
(a, b) match {
case (Nil, Nil) => true
case (x :: xs, y :: ys) => x === y && areEquivalent(xs, ys)
case _ => false
}
}
}
}