【问题标题】:test that function in r does not work测试 r 中的功能不起作用
【发布时间】:2014-12-11 01:04:19
【问题描述】:
library(Rcpp)
cppFunction("
    int fib(int n) 
    {
        if (n < 2) 
            return(n);
        return( fib(n-1) + fib(n-2) );
    }
")

我的任务是编写几个测试来显示案例是否错误。

但是,错误信息如下。

Error during wrapup: Test failed: 'Test cppFunction'
* Not expected: 3 not equal to equals(2)
Modes of target, current: function, numeric
target, current do not match when deparsed.
* Not expected: 5 not equal to equals(5)
Modes of target, current: function, numeric
target, current do not match when deparsed.
* Not expected: 10 not equal to equals(55)
Modes of target, current: function, numeric
target, current do not match when deparsed.
* Not expected: 8 code did not generate an error.
* Not expected: 6 code did not generate an error.
* Not expected: 9 code did not generate an error.
###test that###
library(testthat)
context("Test cppFunction")

##do not know why??
test_that("Test cppFunction",{
  expect_equal(3,equals(2))
  expect_equal(5,equals(5))
  expect_equal(10,equals(55))
  expect_error(8,equals(20))
  expect_error(6,equals(7))
  expect_error(9,equals(25))
})

我无法弄清楚为什么测试不起作用。

【问题讨论】:

  • 什么不起作用,你想达到什么目的?
  • 检查函数及其计算结果 - 不是固定值
  • 检查 fib(1) == 1 和 fib(3) == 2 等等。

标签: r testthat


【解决方案1】:

首先,在测试中,您甚至从未调用过fib 函数。你应该有类似的东西

test_that("Test cppFunction",{
  expect_equal(fib(3),2)
  expect_equal(fib(5),5)
  expect_equal(fib(10),55)
})

expect_error 的使用也是错误的,因为 fib 函数不应该产生错误,因为它现在实现了。我怀疑你想测试不等式。但这没有意义,如果函数没有产生您期望的错误结果,并不意味着该函数是正确的。我建议只写更多expect_equal 测试。如果您仍然想这样做,只需编写类似

的内容
expect_false(fib(10) == 22)

最后你的测试应该是这样的

test_that("Test cppFunction",{
  expect_equal(fib(3),2)
  expect_equal(fib(5),5)
  expect_equal(fib(10),55)
  expect_false(fib(8) == 20)
  expect_false(fib(6) == 7)
  expect_false(fib(9) == 25)
})

【讨论】:

  • 她让你做她的功课,你想要功劳吗?
猜你喜欢
  • 2013-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
相关资源
最近更新 更多