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

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

4

1 回答 1

3

首先,您甚至从未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)
})
于 2014-12-10T04:23:37.690 回答