Skip to content

Commit 482b19b

Browse files
author
Florian Plaza Oñate
committed
Add unit tests.
1 parent bf34e7b commit 482b19b

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

inst/unitTests/cpp/Matrix.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,7 @@ NumericVector runit_const_Matrix_column( const NumericMatrix& m ){
238238
return col1 ;
239239
}
240240

241+
// [[Rcpp::export]]
242+
int access_with_bounds_checking(const IntegerMatrix m, int i, int j) {
243+
return m.at(i, j);
244+
}

inst/unitTests/cpp/Vector.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,3 +777,7 @@ int noprotect_vector( Vector<REALSXP, NoProtectStorage> x){
777777
int noprotect_matrix( Matrix<REALSXP, NoProtectStorage> x){
778778
return x.nrow() ;
779779
}
780+
781+
int access_with_bounds_checking(const IntegerVector x, int index) {
782+
return x.at(index);
783+
}

inst/unitTests/runit.Matrix.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,16 @@ if (.runThisTest) {
171171
checkEquals( m[,1], m[,2] )
172172
}
173173

174+
test.IntegerMatrix.accessor.with.bounds.checking <- function() {
175+
m <- matrix(seq(1L, 12, by=1L), nrow=4L, ncol=3L)
176+
checkEquals(access_with_bounds_checking(m, 0, 0), 1)
177+
checkEquals(access_with_bounds_checking(m, 1, 2), 10)
178+
checkEquals(access_with_bounds_checking(m, 3, 2), 12)
179+
checkException(access_with_bounds_checking(m, 4, 2) , msg = "index out of bounds not detected" )
180+
checkException(access_with_bounds_checking(m, 3, 3) , msg = "index out of bounds not detected" )
181+
checkException(access_with_bounds_checking(m, 3, -1) , msg = "index out of bounds not detected" )
182+
checkException(access_with_bounds_checking(m, -1, 2) , msg = "index out of bounds not detected" )
183+
checkException(access_with_bounds_checking(m, -1, -1) , msg = "index out of bounds not detected" )
184+
}
185+
174186
}

inst/unitTests/runit.Vector.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,15 @@ if (.runThisTest) {
683683
x <- matrix(rnorm(10), nrow=2)
684684
checkIdentical( noprotect_matrix(x), 2L )
685685
}
686+
687+
test.IntegerVector.accessor.with.bounds.checking <- function() {
688+
x <- seq(1L, 5L, by=1L)
689+
checkEquals(access_with_bounds_checking(x, 3), 4)
690+
checkException(access_with_bounds_checking(x, 5) , msg = "index out of bounds not detected" )
691+
checkException(access_with_bounds_checking(x, -1) , msg = "index out of bounds not detected" )
692+
}
693+
}
694+
686695

687696
}
688697

0 commit comments

Comments
 (0)