scala - ScalaTest - check for "almost equal" for floats and objects containing floats -


while writing tests operations floats or objects containing floats (like vectors or matrices), want test not equality, "almost equal" (difference allowed epsilon).

when using scalatest funsuite, 1 writes assert(xxx == yyy). floats , likes can write assert(math.abs(xxx - yyy)<epsilon), not nice feature of scalatest assert macro of being reported compared values part of failure message.

how can perform testing of float "almost equality" in scalatest, when test fails, values written part of failure message?

test example:

import org.scalatest.funsuite  class floattest extends funsuite {   test("testing computations") {     import math._     assert(sin(pi/4)==sqrt(0.5))     assert(sin(pi)==0)   } } 

it can done using tolerantnumerics , using === instead of ==.

import org.scalactic.tolerantnumerics import org.scalatest.funsuite  class floattest extends funsuite {    val epsilon = 1e-4f    implicit val doubleeq = tolerantnumerics.tolerantdoubleequality(epsilon)    test("testing computations") {     import math._     assert(sin(pi / 4) === sqrt(0.5))     assert(sin(pi) === 0.0)   } } 

for own types can define own subclasses of equality[t].


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -