Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Testthat is showing wrong result

$
0
0

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() )
})

Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>