Skip to content

Commit 0323c60

Browse files
committed
add many more tests
1 parent 036aec7 commit 0323c60

File tree

20 files changed

+448
-52
lines changed

20 files changed

+448
-52
lines changed

.github/workflows/test-coverage.yaml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,33 @@ jobs:
3434
run: |
3535
options(warn = 2)
3636
pak::local_install("./", dependencies = TRUE)
37+
38+
# Override Makevars to avoid _FORTIFY_SOURCE warning with -O0
39+
# covr adds --coverage which needs -O0, but _FORTIFY_SOURCE needs -O
40+
makevars_content <- paste(
41+
"PKG_CXXFLAGS = $(SHLIB_OPENMP_CXXFLAGS)",
42+
"PKG_LIBS = $(SHLIB_OPENMP_CXXFLAGS) $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)"
43+
sep = "\n"
44+
)
45+
writeLines(makevars_content, "./extended-tests/cpp4rtest/src/Makevars")
46+
3747
pak::local_install("./extended-tests/cpp4rtest", dependencies = TRUE)
3848
shell: Rscript {0}
3949

4050
- name: Test coverage
4151
run: |
42-
coverage <- covr::package_coverage("./extended-tests/cpp4rtest", quiet = FALSE)
43-
percent <- round(covr::percent_coverage(coverage))
44-
cat("Coverage:", percent, "%\n")
52+
coverage <- covr::package_coverage(
53+
"./extended-tests/cpp4rtest",
54+
type = "all",
55+
quiet = FALSE
56+
)
57+
58+
# Debug: show coverage breakdown
59+
print(coverage)
60+
61+
percent_exact <- covr::percent_coverage(coverage)
62+
percent <- floor(percent_exact) # Use floor for conservative estimate
63+
cat("\nCoverage:", percent_exact, "% (rounded down to", percent, "%)\n")
4564
4665
# Create badge color based on coverage
4766
if (percent >= 90) {

extended-tests/cpp4rtest/NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@ export(roxcpp3)
77
export(roxcpp4)
88
export(roxcpp5)
99
export(roxcpp7)
10-
export(run_tests)
1110
exportPattern("_$")
1211
useDynLib(cpp4rtest, .registration = TRUE)

extended-tests/cpp4rtest/R/test.R

Lines changed: 0 additions & 26 deletions
This file was deleted.

extended-tests/cpp4rtest/man/run_tests.Rd

Lines changed: 0 additions & 18 deletions
This file was deleted.

extended-tests/cpp4rtest/src/Makevars.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ PKG_LIBS = $(SHLIB_OPENMP_CXXFLAGS) $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
99
# PKG_CPPFLAGS = -UDEBUG -g -I vendor/
1010

1111
# uncomment to disable compiler optimizations
12-
PKG_CXXFLAGS = -Wall -O0 -pedantic
12+
# PKG_CXXFLAGS = -Wall -O0 -pedantic
13+
14+
# for local test coverage
15+
# PKG_CXXFLAGS = -Wall -O2 -pedantic

extended-tests/cpp4rtest/src/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <cpp4r.hpp>
22

3+
#include <R.h> // RNG state functions
34
#include <Rmath.h> // for Rf_rgamma, Rf_rnorm
45
#include <deque> // for std::deque
56
#include <numeric> // for std::accumulate

extended-tests/cpp4rtest/src/matrix.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
[[cpp4r::register]] SEXP gibbs_cpp(int N, int thin) {
22
cpp4r::writable::doubles_matrix<> mat(N, 2);
33
double x = 0, y = 0;
4+
GetRNGstate();
45
for (int i = 0; i < N; i++) {
56
for (int j = 0; j < thin; j++) {
67
x = Rf_rgamma(3., 1. / double(y * y + 4));
78
y = Rf_rnorm(1. / (x + 1.), 1. / sqrt(2. * (x + 1.)));
89
// REprintf("x: %f y: %f\n", x, y);
910
}
10-
mat[i][0] = x;
11-
mat[i][1] = y;
11+
mat(i, 0) = x;
12+
mat(i, 1) = y;
1213
}
14+
PutRNGstate();
1315
return mat;
1416
}
1517

1618
[[cpp4r::register]] cpp4r::doubles_matrix<> gibbs_cpp2(int N, int thin) {
1719
cpp4r::writable::doubles_matrix<> mat(N, 2);
1820
double x = 0, y = 0;
21+
GetRNGstate();
1922
for (int i = 0; i < N; i++) {
2023
for (int j = 0; j < thin; j++) {
2124
x = Rf_rgamma(3., 1. / double(y * y + 4));
@@ -24,6 +27,7 @@
2427
mat(i, 0) = x;
2528
mat(i, 1) = y;
2629
}
30+
PutRNGstate();
2731
return mat;
2832
}
2933

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Tests for add.h functions
2+
3+
test_that("cpp4r_add_vec_for_ works", {
4+
x <- c(1.0, 2.0, 3.0)
5+
result <- cpp4r_add_vec_for_(x, 10.0)
6+
expect_equal(as.numeric(result), c(11.0, 12.0, 13.0))
7+
})
8+
9+
test_that("cpp4r_add_vec_for_ handles empty vector", {
10+
x <- numeric(0)
11+
result <- cpp4r_add_vec_for_(x, 5.0)
12+
expect_length(result, 0)
13+
})
14+
15+
test_that("cpp4r_add_vec_for_ handles negative numbers", {
16+
x <- c(-1.0, 0.0, 1.0)
17+
result <- cpp4r_add_vec_for_(x, -5.0)
18+
expect_equal(as.numeric(result), c(-6.0, -5.0, -4.0))
19+
})
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
1-
run_cpp_tests("cpp4rtest")
1+
# Run C++ unit tests
2+
test_that("C++ unit tests run without unexpected errors", {
3+
# https://stackoverflow.com/a/79565018/3720258
4+
expect_warnings <- function(object, warnings, ...) {
5+
wrns <- testthat::capture_warnings({
6+
act <- testthat::quasi_label(rlang::enquo(object), arg = "object")
7+
})
8+
res <- vapply(warnings, function(wrn) any(grepl(wrn, wrns)), FUN.VALUE = logical(1))
9+
if (all(res)) {
10+
testthat::succeed()
11+
return(invisible(act$val))
12+
} else {
13+
msg <- sprintf("%s did not produce one or more warnings: %s", act$lab, toString(names(res)[!res]))
14+
testthat::fail(msg)
15+
}
16+
}
17+
18+
expect_warnings(run_cpp_tests("cpp4rtest"), rep("test", 3))
19+
})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Tests for data_frame.h functions
2+
3+
test_that("data_frame_ works", {
4+
result <- data_frame_()
5+
expect_s3_class(result, "data.frame")
6+
expect_equal(nrow(result), 3)
7+
expect_equal(ncol(result), 2)
8+
expect_equal(result$nums, c(1, 2, 3))
9+
expect_equal(result$letters, c("x", "y", "z"))
10+
})

0 commit comments

Comments
 (0)