Skip to content

Commit 58b63fb

Browse files
committed
add tests
1 parent d4d4e4c commit 58b63fb

File tree

3 files changed

+117
-4
lines changed

3 files changed

+117
-4
lines changed

inst/include/Rcpp/traits/result_of.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// result_of.h: Rcpp R/C++ interface class library -- traits to help wrap
55
//
66
// Copyright (C) 2010 - 2024 Dirk Eddelbuettel and Romain Francois
7-
// Copyright (C) 2025 Dirk Eddelbuettel, Romain Francois, Iñaki Ucar
7+
// Copyright (C) 2025 Dirk Eddelbuettel, Romain Francois and Iñaki Ucar
88
//
99
// This file is part of Rcpp.
1010
//

inst/tinytest/cpp/sugar.cpp

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

22
// sugar.cpp: Rcpp R/C++ interface class library -- sugar unit tests
33
//
4-
// Copyright (C) 2012 - 2022 Dirk Eddelbuettel and Romain Francois
4+
// Copyright (C) 2012 - 2024 Dirk Eddelbuettel and Romain Francois
5+
// Copyright (C) 2025 Dirk Eddelbuettel, Romain Francois and Iñaki Ucar
56
//
67
// This file is part of Rcpp.
78
//
@@ -226,11 +227,58 @@ NumericVector runit_na_omit( NumericVector xx ){
226227
}
227228

228229
// [[Rcpp::export]]
229-
List runit_lapply( IntegerVector xx){
230+
List runit_lapply( NumericVector xx ){
231+
List res = lapply( xx, square<double>() );
232+
return res ;
233+
}
234+
235+
// [[Rcpp::export]]
236+
List runit_lapply_rawfun( NumericVector xx){
237+
List res = lapply( xx, raw_square );
238+
return res ;
239+
}
240+
241+
// [[Rcpp::export]]
242+
List runit_lapply_lambda(NumericVector xx){
243+
List res = lapply(xx, [](double x) { return x*x; });
244+
return res;
245+
}
246+
247+
// [[Rcpp::export]]
248+
List runit_lapply_seq( IntegerVector xx){
230249
List res = lapply( xx, seq_len );
231250
return res ;
232251
}
233252

253+
// [[Rcpp::export]]
254+
NumericVector runit_mapply2(NumericVector xx, NumericVector yy){
255+
NumericVector res = mapply(xx, yy, std::plus<double>());
256+
return res;
257+
}
258+
259+
// [[Rcpp::export]]
260+
NumericVector runit_mapply2_lambda(NumericVector xx, NumericVector yy){
261+
NumericVector res = mapply(xx, yy, [](double x, double y) { return x+y; });
262+
return res;
263+
}
264+
265+
// [[Rcpp::export]]
266+
NumericVector runit_mapply3_lambda(NumericVector xx, NumericVector yy, NumericVector zz){
267+
NumericVector res = mapply(xx, yy, zz, [](double x, double y, double z) { return x+y+z; });
268+
return res;
269+
}
270+
271+
// [[Rcpp::export]]
272+
LogicalVector runit_mapply2_logical(NumericVector xx, NumericVector yy){
273+
return all(mapply(xx, yy, std::plus<double>()) < 100.0);
274+
}
275+
276+
// [[Rcpp::export]]
277+
List runit_mapply2_list(IntegerVector xx, IntegerVector yy){
278+
List res = mapply(xx, yy, seq);
279+
return res ;
280+
}
281+
234282
// [[Rcpp::export]]
235283
List runit_minus( IntegerVector xx ){
236284
return List::create(
@@ -331,6 +379,12 @@ NumericVector runit_sapply_rawfun( NumericVector xx){
331379
return res ;
332380
}
333381

382+
// [[Rcpp::export]]
383+
NumericVector runit_sapply_lambda(NumericVector xx){
384+
NumericVector res = sapply(xx, [](double x) { return x*x; });
385+
return res;
386+
}
387+
334388
// [[Rcpp::export]]
335389
LogicalVector runit_sapply_square( NumericVector xx){
336390
return all( sapply( xx * xx , square<double>() ) < 10.0 );
@@ -448,6 +502,12 @@ NumericMatrix runit_outer( NumericVector xx, NumericVector yy){
448502
return m ;
449503
}
450504

505+
// [[Rcpp::export]]
506+
NumericMatrix runit_outer_lambda(NumericVector xx, NumericVector yy){
507+
NumericMatrix m = outer(xx, yy, [](double x, double y) { return x + y; });
508+
return m ;
509+
}
510+
451511
// [[Rcpp::export]]
452512
List runit_row( NumericMatrix xx ){
453513
return List::create(

inst/tinytest/test_sugar.R

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

2-
## Copyright (C) 2010 - 2023 Dirk Eddelbuettel and Romain Francois
2+
## Copyright (C) 2010 - 2024 Dirk Eddelbuettel and Romain Francois
3+
## Copyright (C) 2025 Dirk Eddelbuettel, Romain Francois and Iñaki Ucar
34
##
45
## This file is part of Rcpp.
56
##
@@ -302,9 +303,49 @@ expect_equal( fx( c(1:10) ), fx( c(1:10) ) )
302303

303304
# test.sugar.lapply <- function( ){
304305
fx <- runit_lapply
306+
expect_equal( fx( 1:10 ), as.list((1:10)^2) )
307+
308+
309+
# test.sugar.lapply.rawfun <- function( ){
310+
fx <- runit_lapply_rawfun
311+
expect_equal( fx( 1:10 ), as.list((1:10)^2) )
312+
313+
314+
# test.sugar.lapply.lambda <- function( ){
315+
fx <- runit_lapply_lambda
316+
expect_equal( fx( 1:10 ), as.list((1:10)^2) )
317+
318+
319+
# test.sugar.lapply.seq <- function( ){
320+
fx <- runit_lapply_seq
305321
expect_equal( fx( 1:10 ), lapply( 1:10, seq_len ) )
306322

307323

324+
# test.sugar.mapply2 <- function( ){
325+
fx <- runit_mapply2
326+
expect_equal( fx(1:10, 1:10) , 1:10+1:10 )
327+
328+
329+
# test.sugar.mapply2.lambda <- function( ){
330+
fx <- runit_mapply2_lambda
331+
expect_equal( fx(1:10, 1:10) , 1:10+1:10 )
332+
333+
334+
# test.sugar.mapply3.lambda <- function( ){
335+
fx <- runit_mapply3_lambda
336+
expect_equal( fx(1:10, 1:10, 1:10) , 1:10+1:10+1:10 )
337+
338+
339+
# test.sugar.mapply2.logical <- function( ){
340+
fx <- runit_mapply2_logical
341+
expect_true( fx(1:10, 1:10) )
342+
343+
344+
# test.sugar.mapply2.list <- function( ){
345+
fx <- runit_mapply2_list
346+
expect_equal( fx(1:10, 1:10*2) , mapply(seq, 1:10, 1:10*2) )
347+
348+
308349
# test.sugar.minus <- function( ){
309350
fx <- runit_minus
310351
expect_equal(fx(1:10) ,
@@ -380,6 +421,11 @@ fx <- runit_sapply_rawfun
380421
expect_equal( fx(1:10) , (1:10)^2 )
381422

382423

424+
# test.sugar.sapply.lambda <- function( ){
425+
fx <- runit_sapply_lambda
426+
expect_equal( fx(1:10) , (1:10)^2 )
427+
428+
383429
# test.sugar.sapply.square <- function( ){
384430
fx <- runit_sapply_square
385431
expect_true( ! fx(1:10) )
@@ -492,6 +538,13 @@ y <- 1:5
492538
expect_equal( fx(x,y) , outer(x,y,"+") )
493539

494540

541+
# test.sugar.matrix.outer.lambda <- function( ){
542+
fx <- runit_outer_lambda
543+
x <- 1:2
544+
y <- 1:5
545+
expect_equal( fx(x,y) , outer(x,y,"+") )
546+
547+
495548
# test.sugar.matrix.row <- function( ){
496549
fx <- runit_row
497550
m <- matrix( 1:16, nc = 4 )

0 commit comments

Comments
 (0)