File: test_cxxfunction.R

package info (click to toggle)
r-cran-inline 0.3.21-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 188 kB
  • sloc: makefile: 2
file content (22 lines) | stat: -rw-r--r-- 751 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

library(inline)

## basic examples from manual page
fx <- cxxfunction(signature(x = "integer", y = "numeric"),
                  "return Rf_ScalarReal(INTEGER(x)[0] * REAL(y)[0]);")
expect_true(is(fx, "CFunc"))
expect_equal(fx(2L, 5), 10)

if (!requireNamespace("Rcpp", quietly=TRUE)) exit_file("Need Rcpp for remainder of tests")

fx <- cxxfunction(signature(x = "integer", y = "numeric"),
                  "return wrap(as<int>(x) * as<double>(y));",
                  plugin = "Rcpp")
expect_true(is(fx, "CFunc"))
expect_equal(fx(2L, 5), 10)

## equivalent shorter form using rcpp()
fx <- rcpp(signature(x = "integer", y = "numeric"),
           "return wrap(as<int>(x) * as<double>(y));")
expect_true(is(fx, "CFunc"))
expect_equal(fx(2L, 5), 10)