首页 > 解决方案 > 测试显示错误结果

问题描述

我尝试通过编写测试文件“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() )
})

标签: r

解决方案


推荐阅读