在讀取和寫入文件的函數上使用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'?顯示錯誤的代碼是怎樣的?我們只是簡單地丟失了導致錯誤的任何代碼。 –