Add tests for R builds

This commit is contained in:
Greg Lin 2022-08-10 17:17:01 -05:00
commit 1de359f3e6
17 changed files with 505 additions and 0 deletions

View file

@ -0,0 +1,2 @@
^.*\.Rproj$
^\.Rproj\.user$

2
test/testpkg/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
.Rproj.user
.Rhistory

12
test/testpkg/DESCRIPTION Normal file
View file

@ -0,0 +1,12 @@
Package: testpkg
Type: Package
Title: A Test Package
Version: 0.1.0
Authors@R: person("Test", "Pkg", email = "test@test", role = c("aut", "cre"))
Description: A test package.
License: MIT
Depends:
R (>= 3.1)
SystemRequirements: C++11
RoxygenNote: 6.1.1
Encoding: UTF-8

6
test/testpkg/NAMESPACE Normal file
View file

@ -0,0 +1,6 @@
# Generated by roxygen2: do not edit by hand
export(add_it)
export(square_it)
export(subtract_it)
useDynLib(testpkg, .registration = TRUE)

32
test/testpkg/R/testpkg.R Normal file
View file

@ -0,0 +1,32 @@
#' @useDynLib testpkg, .registration = TRUE
"_PACKAGE"
#' Add it together
#'
#' @param a Number
#' @param b Number
#' @return Sum of numbers
#' @export
add_it <- function(a, b) {
.Call("add", a, b)
}
#' Subtract it
#'
#' @param a Number
#' @param b Number
#' @return Difference of numbers
#' @export
subtract_it <- function(a, b) {
.Call("subtract", a, b)
}
#' Square it up
#'
#' @param n Integer
#' @return Square
#' @export
square_it <- function(n) {
result <- .Fortran("square", n = as.integer(n), answer = as.integer(1))
result$answer
}

3
test/testpkg/README.md Normal file
View file

@ -0,0 +1,3 @@
# testpkg
Test package with C/C++ and Fortran code, which links against libR, BLAS, LAPACK (see [Makevars](src/Makevars)).

View file

@ -0,0 +1,19 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/testpkg.R
\name{add_it}
\alias{add_it}
\title{Add it together}
\usage{
add_it(a, b)
}
\arguments{
\item{a}{Number}
\item{b}{Number}
}
\value{
Sum of numbers
}
\description{
Add it together
}

View file

@ -0,0 +1,17 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/testpkg.R
\name{square_it}
\alias{square_it}
\title{Square it up}
\usage{
square_it(n)
}
\arguments{
\item{n}{Integer}
}
\value{
Square
}
\description{
Square it up
}

View file

@ -0,0 +1,19 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/testpkg.R
\name{subtract_it}
\alias{subtract_it}
\title{Subtract it}
\usage{
subtract_it(a, b)
}
\arguments{
\item{a}{Number}
\item{b}{Number}
}
\value{
Difference of numbers
}
\description{
Subtract it
}

View file

@ -0,0 +1,14 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/testpkg.R
\docType{package}
\name{testpkg-package}
\alias{testpkg}
\alias{testpkg-package}
\title{testpkg: A Test Package}
\description{
A test package.
}
\author{
\strong{Maintainer}: Test Pkg \email{test@test}
}

View file

@ -0,0 +1,2 @@
CXX_STD=CXX11
PKG_LIBS=$(MAIN_LDFLAGS) $(LDFLAGS) $(LIBR) $(LIBS) $(LAPACK_LIBS) $(BLAS_LIBS) $(SHLIB_OPENMP_CFLAGS)

10
test/testpkg/src/add.c Normal file
View file

@ -0,0 +1,10 @@
#include <R.h>
#include <Rinternals.h>
SEXP add(SEXP a, SEXP b)
{
SEXP result = PROTECT(allocVector(REALSXP, 1));
REAL(result)[0] = asReal(a) + asReal(b);
UNPROTECT(1);
return result;
}

33
test/testpkg/src/init.c Normal file
View file

@ -0,0 +1,33 @@
#include <R.h>
#include <Rinternals.h>
#include <R_ext/Rdynload.h>
#define CALLDEF(name, n) {#name, (DL_FUNC) &name, n}
#define FDEF(name) {#name, (DL_FUNC) &F77_SUB(name), sizeof(name ## _t)/sizeof(name ## _t[0]), name ##_t}
extern SEXP add(SEXP, SEXP);
extern SEXP subtract(SEXP, SEXP);
void F77_SUB(square)(int *n, int *answer);
static R_NativePrimitiveArgType square_t[] = {
INTSXP,
INTSXP
};
static const R_CallMethodDef CallEntries[] = {
CALLDEF(add, 2),
CALLDEF(subtract, 2),
{NULL, NULL, 0}
};
static const R_FortranMethodDef fMethods[] = {
FDEF(square),
{NULL, NULL, 0}
};
void R_init_testpkg(DllInfo *dll)
{
R_registerRoutines(dll, NULL, CallEntries, fMethods, NULL);
R_useDynamicSymbols(dll, FALSE);
}

View file

@ -0,0 +1,5 @@
subroutine square(x,answer)
integer, intent(in) :: x
integer, intent(out) :: answer
answer = x * x
end

View file

@ -0,0 +1,10 @@
#include <R.h>
#include <Rinternals.h>
extern "C" SEXP subtract(SEXP a, SEXP b)
{
SEXP result = PROTECT(allocVector(REALSXP, 1));
REAL(result)[0] = asReal(a) - asReal(b);
UNPROTECT(1);
return result;
}

View file

@ -0,0 +1,19 @@
Version: 1.0
RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: No
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
RnwWeave: knitr
LaTeX: pdfLaTeX
AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
BuildType: Package
PackageRoxygenize: rd,collate,namespace

View file

@ -0,0 +1,5 @@
library(testpkg)
stopifnot(add_it(1, 2) == 3)
stopifnot(subtract_it(1, 2) == -1)
stopifnot(square_it(3) == 9)