Skip to content

Commit

Permalink
Adding C++ tests with Catch2
Browse files Browse the repository at this point in the history
  • Loading branch information
programLyrique committed Mar 14, 2023
1 parent 2a0b70b commit 57ab6e5
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 1 deletion.
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ RoxygenNote: 7.2.1
Imports:
pkgload
Suggests:
testthat,
xml2
LinkingTo:
testthat
6 changes: 6 additions & 0 deletions R/catch-routine-registration.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This dummy function definition is included with the package to ensure that
# 'tools::package_native_routine_registration_skeleton()' generates the required
# registration info for the 'run_testthat_tests' symbol.
(function() {
.Call("run_testthat_tests", FALSE, PACKAGE = "sxpdb")
})
5 changes: 4 additions & 1 deletion src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include "sxpdb.h"

extern SEXP run_testthat_tests(SEXP use_xml_sxp); // required for catch2, because we disable dynamic registration

static const R_CallMethodDef callMethods[] = {
/* name casted ptr to function # of args */
// Generic record related
Expand Down Expand Up @@ -51,6 +53,7 @@ static const R_CallMethodDef callMethods[] = {
{"merge_all_dbs", (DL_FUNC) &merge_all_dbs, 3},
{"values_from_origins", (DL_FUNC) &values_from_origins, 3},
{"values_from_calls", (DL_FUNC) &values_from_calls, 3},
{"run_testthat_tests", (DL_FUNC) &run_testthat_tests, 1},


// Must have at the end
Expand All @@ -59,7 +62,7 @@ static const R_CallMethodDef callMethods[] = {

void R_init_sxpdb(DllInfo* dll) {
R_registerRoutines(dll, NULL, callMethods, NULL, NULL);
R_useDynamicSymbols(dll, FALSE);
R_useDynamicSymbols(dll, FALSE);
R_RegisterCCallable("sxpdb", "add_val", (DL_FUNC) &add_val);
R_RegisterCCallable("sxpdb", "add_val_origin_", (DL_FUNC) &add_val_origin_);
R_RegisterCCallable("sxpdb", "add_origin_", (DL_FUNC) &add_origin_);
Expand Down
7 changes: 7 additions & 0 deletions src/test-runner.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* Please do not edit this file -- it ensures that your package will export a
* 'run_testthat_tests()' C routine that can be used to run the Catch unit tests
* available in your package.
*/
#define TESTTHAT_TEST_RUNNER
#include <testthat.h>
26 changes: 26 additions & 0 deletions src/tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* This file uses the Catch unit testing library, alongside
* testthat's simple bindings, to test a C++ function.
*
* For your own packages, ensure that your test files are
* placed within the `src/` folder, and that you include
* `LinkingTo: testthat` within your DESCRIPTION file.
*/

// All test files should include the <testthat.h>
// header file.
#include <testthat.h>
#include "search_index.h"


context("Index tests") {

test_that("find_na on sexp_view") {
Serializer ser(64);

const sexp_view_t sexp_view = Serializer::unserialize_view(ser.serialize(Rf_ScalarReal(NA_REAL)));

expect_true(find_na(sexp_view));
}

}
1 change: 1 addition & 0 deletions tests/testthat/test-cpp.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run_cpp_tests("sxpdb")

0 comments on commit 57ab6e5

Please sign in to comment.