|
1 | 1 | ##' @title TestHarness for the package |
2 | 2 | ##' @param package_path path to the test package |
3 | 3 | ##' @param verbose used to deliver more in depth information |
4 | | -##' @description Creates Testharness for all the functions in the package that you want to test |
| 4 | +##' @description Creates Testharness for all the functions in the package that |
| 5 | +##' you want to test |
5 | 6 | ##' using RcppDeepState. |
6 | | -##' @examples |
| 7 | +##' @examples |
7 | 8 | ##' path <- system.file("testpkgs/testSAN", package = "RcppDeepState") |
8 | 9 | ##' harness.list <- deepstate_pkg_create(path) |
9 | 10 | ##' print(harness.list) |
10 | 11 | ##' @import RcppArmadillo |
11 | 12 | ##' @return A character vector of TestHarness files that are generated |
12 | 13 | ##' @export |
13 | | -deepstate_pkg_create<-function(package_path, verbose=getOption("verbose")){ |
14 | | - package_path <-normalizePath(package_path, mustWork=TRUE) |
15 | | - package_path <- sub("/$","",package_path) |
16 | | - inst_path <- file.path(package_path, "inst") |
17 | | - test_path <- file.path(inst_path,"testfiles") |
| 14 | +deepstate_pkg_create <- function(package_path, verbose=getOption("verbose")) { |
| 15 | + package_path <- normalizePath(package_path, mustWork=TRUE) |
| 16 | + package_path <- sub("/$", "", package_path) |
| 17 | + test_path <- file.path(package_path, "inst", "testfiles") |
18 | 18 |
|
19 | 19 | # Test directory structure initialization |
20 | | - unlink(test_path, recursive = TRUE) |
21 | | - if(!dir.exists(inst_path)) { |
22 | | - dir.create(inst_path) |
| 20 | + if (!dir.exists(test_path)) { |
| 21 | + dir.create(test_path, showWarnings=FALSE, recursive=TRUE) |
| 22 | + }else { |
| 23 | + # delete all the existing files except for the harness |
| 24 | + for (function_name in list.files(test_path)) { |
| 25 | + fun_path <- file.path(test_path, function_name) |
| 26 | + filename <- paste0(function_name, "_DeepState_TestHarness", ".cpp") |
| 27 | + harness_path <- file.path(fun_path, filename) |
| 28 | + |
| 29 | + # delete all the files and directories except the harness file |
| 30 | + if (file.exists(harness_path)) { |
| 31 | + delete_content <- setdiff(list.files(fun_path), filename) |
| 32 | + unlink(file.path(fun_path, delete_content), recursive=TRUE) |
| 33 | + } |
| 34 | + } |
23 | 35 | } |
24 | | - dir.create(test_path,showWarnings = FALSE) |
25 | 36 |
|
26 | | - if(!file.exists(file.path(package_path,"src/*.so"))) { |
27 | | - # ensure that the debugging symbols are embedded in the resulting shared object |
| 37 | + if (!file.exists(file.path(package_path, "src/*.so"))) { |
| 38 | + # ensure that the debugging symbols are embedded in the shared object |
28 | 39 | makevars_file <- file.path(package_path, "src", "Makevars") |
29 | | - if (dir.exists(file.path(package_path, "src"))){ |
| 40 | + if (dir.exists(file.path(package_path, "src"))) { |
30 | 41 | makevars_content <- "PKG_CXXFLAGS += -g " |
31 | 42 | write(makevars_content, makevars_file, append=FALSE) |
32 | 43 | } |
33 | 44 |
|
34 | | - system(paste0("R CMD INSTALL ",package_path),intern = FALSE, ignore.stdout=!verbose, ignore.stderr=!verbose) |
| 45 | + system(paste0("R CMD INSTALL ", package_path), intern=FALSE, |
| 46 | + ignore.stdout=!verbose, ignore.stderr=!verbose) |
35 | 47 | unlink(makevars_file, recursive = FALSE) |
36 | 48 | } |
37 | 49 |
|
38 | | - if(!(file.exists("~/.RcppDeepState/deepstate-master/build/libdeepstate32.a") && |
39 | | - file.exists("~/.RcppDeepState/deepstate-master/build/libdeepstate.a"))) |
40 | | - { |
| 50 | + # download and build deepstate |
| 51 | + libdeepstate32 <- "~/.RcppDeepState/deepstate-master/build/libdeepstate32.a" |
| 52 | + libdeepstate <- "~/.RcppDeepState/deepstate-master/build/libdeepstate.a" |
| 53 | + if (!(file.exists(libdeepstate32) && file.exists(libdeepstate))) { |
41 | 54 | deepstate_make_run(verbose) |
42 | 55 | } |
| 56 | + |
43 | 57 | Rcpp::compileAttributes(package_path) |
44 | 58 | harness <- list() |
45 | 59 | failed.harness <- list() |
46 | 60 |
|
47 | 61 | functions.list <- deepstate_get_function_body(package_path) |
48 | | - if(!is.null(functions.list) && length(functions.list) > 1){ |
49 | | - functions.list$argument.type<-gsub("Rcpp::","",functions.list$argument.type) |
| 62 | + if (!is.null(functions.list) && length(functions.list) > 1) { |
| 63 | + functions.list$argument.type <- gsub("Rcpp::", "", |
| 64 | + functions.list$argument.type) |
50 | 65 | match_count <- 0 |
51 | 66 | mismatch_count <- 0 |
52 | 67 |
|
53 | 68 | fun_names <- unique(functions.list$funName) |
54 | | - for(function_name.i in fun_names) { |
55 | | - functions.rows <- functions.list [functions.list$funName == function_name.i,] |
56 | | - params <- c(functions.rows$argument.type) |
57 | | - filepath <-deepstate_fun_create(package_path,function_name.i) |
58 | | - filename <- paste0(function_name.i,"_DeepState_TestHarness",".cpp") |
| 69 | + for (function_name in fun_names) { |
| 70 | + fun_path <- file.path(test_path, function_name) |
| 71 | + filename <- paste0(function_name, "_DeepState_TestHarness", ".cpp") |
| 72 | + harness_path <- file.path(fun_path, filename) |
59 | 73 |
|
60 | | - if(!is.na(filepath) && basename(filepath) == filename ){ |
61 | | - match_count = match_count + 1 |
62 | | - harness <- c(harness,filename) |
| 74 | + functions.rows <- functions.list[functions.list$funName == function_name,] |
| 75 | + params <- c(functions.rows$argument.type) |
| 76 | + filepath <- deepstate_fun_create(package_path, function_name) |
| 77 | + |
| 78 | + if (!is.na(filepath) && basename(filepath) == filename) { |
| 79 | + match_count <- match_count + 1 |
| 80 | + harness <- c(harness, filename) |
63 | 81 | }else { |
64 | | - mismatch_count = mismatch_count + 1 |
65 | | - failed.harness <- c(failed.harness,function_name.i) |
| 82 | + mismatch_count <- mismatch_count + 1 |
| 83 | + failed.harness <- c(failed.harness, function_name) |
66 | 84 | } |
67 | 85 |
|
68 | 86 | } |
69 | 87 |
|
70 | | - if(match_count > 0 && match_count == length(fun_names)){ |
71 | | - message(sprintf("Testharness created for %d functions in the package\n",match_count)) |
| 88 | + |
| 89 | + # harness generated for all of the functions |
| 90 | + if (match_count > 0 && match_count == length(fun_names)) { |
| 91 | + message(paste0("Testharness created for ", match_count, |
| 92 | + " functions in the package\n")) |
72 | 93 | return(as.character(harness)) |
73 | 94 | } |
74 | | - else{ |
75 | | - if(mismatch_count < length(fun_names) && length(failed.harness) > 0 && match_count != 0){ |
76 | | - message(sprintf("Failed to create testharness for %d functions in the package - %s\n",mismatch_count,paste(failed.harness, collapse=", "))) |
77 | | - message(sprintf("Testharness created for %d functions in the package\n",match_count)) |
78 | | - return(as.character(harness)) |
79 | | - } |
| 95 | + |
| 96 | + # harness generated for some function |
| 97 | + if (mismatch_count < length(fun_names) && length(failed.harness) > 0 |
| 98 | + && match_count != 0) { |
| 99 | + failed_str <- paste(failed.harness, collapse=", ") |
| 100 | + message(paste0("Failed to create testharness for ", mismatch_count, |
| 101 | + " functions in the package - ", failed_str)) |
| 102 | + |
| 103 | + message(paste0("Testharness created for ", match_count, |
| 104 | + " functions in the package\n")) |
| 105 | + return(as.character(harness)) |
80 | 106 | } |
81 | | - if(mismatch_count == length(fun_names)){ |
82 | | - stop("Testharnesses cannot be created for the package - datatypes fall out of specified list!!") |
| 107 | + |
| 108 | + # harness not generated for any function |
| 109 | + if (mismatch_count == length(fun_names)) { |
| 110 | + stop("Testharnesses cannot be created for the package") |
83 | 111 | return(as.character(failed.harness)) |
84 | 112 | } |
| 113 | + |
| 114 | + }else { |
| 115 | + stop("No Rcpp function to test in the package") |
85 | 116 | } |
86 | | - else{ |
87 | | - stop("No Rcpp Functions to test in the package") |
88 | | - } |
89 | | -} |
| 117 | +} |
0 commit comments