|
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 | | - test_path <- file.path(package_path, "inst","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") |
17 | 18 |
|
18 | 19 | # Test directory structure initialization |
19 | | - if(!dir.exists(test_path)) { |
| 20 | + if (!dir.exists(test_path)) { |
20 | 21 | dir.create(test_path, showWarnings=FALSE, recursive=TRUE) |
21 | | - }else{ |
| 22 | + }else { |
22 | 23 | # delete all the existing files except for the harness |
23 | | - for(function_name in list.files(test_path)) { |
| 24 | + for (function_name in list.files(test_path)) { |
24 | 25 | fun_path <- file.path(test_path, function_name) |
25 | | - filename <- paste0(function_name,"_DeepState_TestHarness",".cpp") |
| 26 | + filename <- paste0(function_name, "_DeepState_TestHarness", ".cpp") |
26 | 27 | harness_path <- file.path(fun_path, filename) |
27 | 28 |
|
28 | 29 | # delete all the files and directories except the harness file |
29 | | - if(file.exists(harness_path)){ |
| 30 | + if (file.exists(harness_path)) { |
30 | 31 | delete_content <- setdiff(list.files(fun_path), filename) |
31 | 32 | unlink(file.path(fun_path, delete_content), recursive=TRUE) |
32 | 33 | } |
33 | 34 | } |
34 | 35 | } |
35 | 36 |
|
36 | | - if(!file.exists(file.path(package_path,"src/*.so"))) { |
37 | | - # 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 |
38 | 39 | makevars_file <- file.path(package_path, "src", "Makevars") |
39 | | - if (dir.exists(file.path(package_path, "src"))){ |
| 40 | + if (dir.exists(file.path(package_path, "src"))) { |
40 | 41 | makevars_content <- "PKG_CXXFLAGS += -g " |
41 | 42 | write(makevars_content, makevars_file, append=FALSE) |
42 | 43 | } |
43 | 44 |
|
44 | | - 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) |
45 | 47 | unlink(makevars_file, recursive = FALSE) |
46 | 48 | } |
47 | 49 |
|
48 | | - if(!(file.exists("~/.RcppDeepState/deepstate-master/build/libdeepstate32.a") && |
49 | | - file.exists("~/.RcppDeepState/deepstate-master/build/libdeepstate.a"))) |
50 | | - { |
| 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))) { |
51 | 54 | deepstate_make_run(verbose) |
52 | 55 | } |
| 56 | + |
53 | 57 | Rcpp::compileAttributes(package_path) |
54 | 58 | harness <- list() |
55 | 59 | failed.harness <- list() |
56 | 60 |
|
57 | 61 | functions.list <- deepstate_get_function_body(package_path) |
58 | | - if(!is.null(functions.list) && length(functions.list) > 1){ |
59 | | - 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) |
60 | 65 | match_count <- 0 |
61 | 66 | mismatch_count <- 0 |
62 | 67 |
|
63 | 68 | fun_names <- unique(functions.list$funName) |
64 | | - for(function_name.i in fun_names) { |
65 | | - fun_path <- file.path(test_path, function_name.i) |
66 | | - 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") |
67 | 72 | harness_path <- file.path(fun_path, filename) |
68 | 73 |
|
69 | | - functions.rows <- functions.list [functions.list$funName == function_name.i,] |
| 74 | + functions.rows <- functions.list[functions.list$funName == function_name,] |
70 | 75 | params <- c(functions.rows$argument.type) |
71 | | - filepath <- deepstate_fun_create(package_path,function_name.i) |
| 76 | + filepath <- deepstate_fun_create(package_path, function_name) |
72 | 77 |
|
73 | | - if(!is.na(filepath) && basename(filepath) == filename ){ |
74 | | - match_count = match_count + 1 |
75 | | - harness <- c(harness, filename) |
| 78 | + if (!is.na(filepath) && basename(filepath) == filename) { |
| 79 | + match_count <- match_count + 1 |
| 80 | + harness <- c(harness, filename) |
76 | 81 | }else { |
77 | | - mismatch_count = mismatch_count + 1 |
78 | | - failed.harness <- c(failed.harness,function_name.i) |
| 82 | + mismatch_count <- mismatch_count + 1 |
| 83 | + failed.harness <- c(failed.harness, function_name) |
79 | 84 | } |
80 | 85 |
|
81 | 86 | } |
82 | 87 |
|
83 | | - if(match_count > 0 && match_count == length(fun_names)){ |
84 | | - 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")) |
85 | 93 | return(as.character(harness)) |
86 | 94 | } |
87 | | - else{ |
88 | | - if(mismatch_count < length(fun_names) && length(failed.harness) > 0 && match_count != 0){ |
89 | | - message(sprintf("Failed to create testharness for %d functions in the package - %s\n",mismatch_count,paste(failed.harness, collapse=", "))) |
90 | | - message(sprintf("Testharness created for %d functions in the package\n",match_count)) |
91 | | - return(as.character(harness)) |
92 | | - } |
| 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)) |
93 | 106 | } |
94 | | - if(mismatch_count == length(fun_names)){ |
95 | | - 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") |
96 | 111 | return(as.character(failed.harness)) |
97 | 112 | } |
| 113 | + |
| 114 | + }else { |
| 115 | + stop("No Rcpp function to test in the package") |
98 | 116 | } |
99 | | - else{ |
100 | | - stop("No Rcpp Functions to test in the package") |
101 | | - } |
102 | | -} |
| 117 | +} |
0 commit comments