Skip to content

Commit ac159da

Browse files
committed
addind tests with testthat, removing some commented lines, more input conditions
1 parent dd15859 commit ac159da

3 files changed

Lines changed: 114 additions & 10 deletions

File tree

R/workflow_rcpp.R

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,8 @@ simulation <- function(flows_matrix
5757
,dist_matrix
5858
,beta_offset = .25
5959
) {
60-
# ,cost_fun = "exp" # develop in a future version the possibility to provide a custom cost function.
61-
# run name is a prefix to include in the name of saved files
6260

6361
# CHECK VARIABLES PROVIDED TO SUBMIT TO CPP
64-
6562
stopifnot(inherits(flows_matrix,"matrix")
6663
,inherits(dist_matrix,"matrix")
6764
,mode(dist_matrix) == "numeric"
@@ -111,12 +108,9 @@ run_model <- function(flows
111108
stopifnot(is.matrix(flows)
112109
,is.matrix(distance) & is.numeric(distance)
113110
,is.numeric(beta) & beta > 0
114-
,all(dim(flows)==dim(distance)))
115-
116-
# if(mode(flows)!='integer'){
117-
# flows <- `mode<-`(flows,'integer')
118-
# cat('Converting flows to integers by retaining the whole part of each value.')
119-
# }
111+
,all(dim(flows)==dim(distance))
112+
,all(flows>=0)
113+
,all(distance>=0))
120114

121115
# if(is.na(as.integer(ncores))) {
122116
# print('Non integer value provided to ncores, using the default values of 1')
@@ -125,7 +119,6 @@ run_model <- function(flows
125119
# print('Value provided to ncores to big, using default of one')
126120
# ncores <<- 1
127121
# }
128-
129122
# print(paste0("Running a model on "
130123
# ,ncores
131124
# ," cores."))
@@ -178,6 +171,10 @@ run_model_single <- function(flows
178171
,is.numeric(distance)
179172
,any(is.numeric(weight),is.null(weight))
180173
,any(length(flows) == dim(distance))
174+
,all(flows>=0)
175+
,all(distance>=0)
176+
,is.numeric(beta) & beta > 0
177+
,any(all(weight > 0),is.null(weight))
181178
)
182179

183180
if(is.null(weight) & length(flows) == dim(distance)[1]) {

tests/testthat.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This file is part of the standard setup for testthat.
2+
# It is recommended that you do not modify it.
3+
#
4+
# Where should you do additional test configuration?
5+
# Learn more about the roles of various files in:
6+
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
7+
# * https://testthat.r-lib.org/articles/special-files.html
8+
9+
library(testthat)
10+
library(cppSim)
11+
12+
test_check("cppSim")

tests/testthat/test-inputs.R

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
2+
test_that("doubly constrained tested", {
3+
4+
## error datas
5+
error_dist <- cbind(cppSim::distance_test,1:ncol(cppSim::distance_test))
6+
7+
error_dist_neg <- cppSim::distance_test
8+
error_dist_neg[100,150] <- -1
9+
10+
error_flow_neg <- cppSim::flows_test
11+
error_flow_neg[543,57] <- -1
12+
# errors
13+
testthat::expect_error({
14+
cppSim::run_model(flows = cppSim::flows_test
15+
,distance = error_dist)
16+
})
17+
18+
testthat::expect_error({
19+
cppSim::run_model(flows = cppSim::flows_test
20+
,distance = cppSim::distance_test
21+
,beta = -1)
22+
})
23+
24+
testthat::expect_error({
25+
cppSim::run_model(flows=cppSim::flows_test
26+
,distance = error_dist_neg#cppSim::distance_test
27+
)
28+
})
29+
30+
testthat::expect_error({
31+
cppSim::run_model(flows= error_flow_neg
32+
,distance = cppSim::distance_test
33+
)
34+
})
35+
36+
37+
#### Correct
38+
39+
testthat::expect_type({
40+
41+
cppSim::run_model(flows = cppSim::flows_test
42+
,distance = cppSim::distance_test)
43+
44+
},"list")
45+
46+
})
47+
48+
49+
50+
test_that("singly constrained tested", {
51+
52+
## error datas
53+
error_dist <- cbind(cppSim::distance_test,1:ncol(cppSim::distance_test))
54+
55+
error_dist_neg <- cppSim::distance_test
56+
error_dist_neg[100,150] <- -1
57+
58+
orig_flow <- error_flow_neg <- apply(cppSim::flows_test,FUN = sum,MARGIN = 1)
59+
60+
error_flow_neg[543] <- -1
61+
62+
# errors
63+
testthat::expect_error({
64+
cppSim::run_model_single(flows = c(orig_flow,1,2)
65+
,distance = error_dist)
66+
})
67+
68+
testthat::expect_error({
69+
cppSim::run_model_single(flows = orig_flow
70+
,distance = cppSim::distance_test
71+
,beta = -1)
72+
})
73+
74+
testthat::expect_error({
75+
cppSim::run_model_single(flows = orig_flow
76+
,distance = error_dist_neg#cppSim::distance_test
77+
)
78+
})
79+
80+
testthat::expect_error({
81+
cppSim::run_model_single(flows = error_flow_neg
82+
,distance = cppSim::distance_test
83+
)
84+
})
85+
86+
#### Correct
87+
88+
testthat::expect_type({
89+
90+
cppSim::run_model_single(flows = orig_flow
91+
,distance = cppSim::distance_test)
92+
93+
},"list")
94+
95+
})

0 commit comments

Comments
 (0)