在读取和写入文件的函数上使用testthat
进行单元测试的最佳方法是什么?使用`testthat`对写入/读取文件的函数或Fortran错误进行单元测试
对于问复杂问题抱歉,但我不确定这里有什么问题。
我在Fortran中实现了一些读取和写入文件的函数。它们被编译成R软件包。编写R Extension手册。我的单元测试使用testthat
生成随机内容,使用tempfile()写入临时文件。在我的本地Windows机器上运行R软件包的R CMD检查。
但是,使用R-devel运行会失败,因为它无法检测R-3.5.0(devel)的Rtools。所以我提交给赢家。然而
http://win-builder.r-project.org/失败,错误如下:
At line 9 of file auxil.f95
Fortran runtime error: Actual string length is shorter than the declared one for dummy argument 'fn' (96/255)
与对应Fortran源:
subroutine get_nlines(fn, nlines, stat) !line 9
implicit none
!! Arguments
character(255), intent(in) :: fn
integer, intent(out) :: nlines, stat
!! Local variables
character(len=1) :: one
nlines = 0
open(40, file=fn, status='OLD')
do
read(40, *, iostat=stat) one
if (stat /= 0) exit
nlines = nlines + 1
end do
close(40)
end subroutine
的Fortran代码被存储在src
子目录将R包,并且被称为具有
get_nlines <- function(fn) {
stopifnot(file.exists(fn))
res <- .Fortran('get_nlines', fn=as.character(fn), nlines=integer(1), stat=integer(1))
if (res$nlines == 0 & res$stat != 0) {
warning(paste0('get_nlines did not read lines; IOSTAT error ', res$stat, '.'))
return(structure(NA, code=res$stat))
}
res$nlines
}
所以它就是这样。 我不知道我的Fortran代码是否错误,或者是否是win-builder服务器上发生的错误。
Fortran代码在哪里?你怎样称这个子程序?你展示的东西根本不完整。 –
好的,你作为'fn'传递给那个R代码是什么?请,我们需要看看这些东西来自哪里。真的,这是*不够*。我们只知道'fn'必须有255个字符。 –
你怎么称呼'testthat'?显示错误的代码是怎样的?我们只是简单地丢失了导致错误的任何代码。 –