I tried to test a sample code below, by writing a test file "test_real_roots.R". Bu the result I am getting is wrong. Actually if you observe carefully all my tests should pass, but I am getting 1 fail cases not sure why. Can anyone help me ?
real_roots.R
real.roots <- function(a, b, c)
{
if (a == 0.)
stop("Leading term cannot be zero")
d = b*b - 4*a*c # discriminant
if (d < 0)
rr = c()
else if (d == 0)
rr = c( -b/(2*a) )
else
rr = c( (-b - sqrt(d))/(2*a),
(-b + sqrt(d))/(2*a) )
return(rr)
}
test_real_roots.R
source("real_roots.R")
library(testthat)
test_that("Distinct roots", {
roots <- real.roots(1, 80, 12)
expect_that( roots, is_a("numeric") )
expect_that( length(roots), equals(2) )
expect_that( roots[1] < roots[2], is_true() )
})