我试图通过编写测试文件“ test_real_roots.R”来测试下面的示例代码。但是我得到的结果是错误的。实际上,如果您仔细观察,我所有的测试都应该通过,但是我收到1个失败案例,不确定为什么。谁能帮我吗?
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() )
})