Skip to content

Commit 0b6cba1

Browse files
code refactoring
1 parent f7059f3 commit 0b6cba1

File tree

1 file changed

+59
-44
lines changed

1 file changed

+59
-44
lines changed

R/pkg_harness_create.R

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,117 @@
11
##' @title TestHarness for the package
22
##' @param package_path path to the test package
33
##' @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
56
##' using RcppDeepState.
6-
##' @examples
7+
##' @examples
78
##' path <- system.file("testpkgs/testSAN", package = "RcppDeepState")
89
##' harness.list <- deepstate_pkg_create(path)
910
##' print(harness.list)
1011
##' @import RcppArmadillo
1112
##' @return A character vector of TestHarness files that are generated
1213
##' @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")
1718

1819
# Test directory structure initialization
19-
if(!dir.exists(test_path)) {
20+
if (!dir.exists(test_path)) {
2021
dir.create(test_path, showWarnings=FALSE, recursive=TRUE)
21-
}else{
22+
}else {
2223
# 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)) {
2425
fun_path <- file.path(test_path, function_name)
25-
filename <- paste0(function_name,"_DeepState_TestHarness",".cpp")
26+
filename <- paste0(function_name, "_DeepState_TestHarness", ".cpp")
2627
harness_path <- file.path(fun_path, filename)
2728

2829
# delete all the files and directories except the harness file
29-
if(file.exists(harness_path)){
30+
if (file.exists(harness_path)) {
3031
delete_content <- setdiff(list.files(fun_path), filename)
3132
unlink(file.path(fun_path, delete_content), recursive=TRUE)
3233
}
3334
}
3435
}
3536

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
3839
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"))) {
4041
makevars_content <- "PKG_CXXFLAGS += -g "
4142
write(makevars_content, makevars_file, append=FALSE)
4243
}
4344

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)
4547
unlink(makevars_file, recursive = FALSE)
4648
}
4749

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))) {
5154
deepstate_make_run(verbose)
5255
}
56+
5357
Rcpp::compileAttributes(package_path)
5458
harness <- list()
5559
failed.harness <- list()
5660

5761
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)
6065
match_count <- 0
6166
mismatch_count <- 0
6267

6368
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")
6772
harness_path <- file.path(fun_path, filename)
6873

69-
functions.rows <- functions.list [functions.list$funName == function_name.i,]
74+
functions.rows <- functions.list[functions.list$funName == function_name,]
7075
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)
7277

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)
7681
}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)
7984
}
8085

8186
}
8287

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"))
8593
return(as.character(harness))
8694
}
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))
93106
}
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")
96111
return(as.character(failed.harness))
97112
}
113+
114+
}else {
115+
stop("No Rcpp function to test in the package")
98116
}
99-
else{
100-
stop("No Rcpp Functions to test in the package")
101-
}
102-
}
117+
}

0 commit comments

Comments
 (0)