Skip to content

Commit 5c07a37

Browse files
committed
new unit tests for Sugar mean()
1 parent 26e8502 commit 5c07a37

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

inst/unitTests/cpp/sugar.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// sugar.cpp: Rcpp R/C++ interface class library -- sugar unit tests
44
//
5-
// Copyright (C) 2012 - 2013 Dirk Eddelbuettel and Romain Francois
5+
// Copyright (C) 2012 - 2015 Dirk Eddelbuettel and Romain Francois
66
//
77
// This file is part of Rcpp.
88
//
@@ -643,3 +643,18 @@ List vector_vector_logical( NumericVector xx, NumericVector yy){
643643
LogicalVector y6 = xx != yy;
644644
return List::create(y1, y2, y3, y4, y5, y6);
645645
}
646+
647+
// Additions made 1 Jan 2015
648+
649+
// [[Rcpp::export]]
650+
double meanInteger(Rcpp::IntegerVector x) { return Rcpp::mean(x); }
651+
652+
// [[Rcpp::export]]
653+
double meanNumeric(Rcpp::NumericVector x) { return(Rcpp::mean(x)); }
654+
655+
// [[Rcpp::export]]
656+
double meanLogical(Rcpp::LogicalVector x) { return(Rcpp::mean(x)); }
657+
658+
// [[Rcpp::export]]
659+
Rcomplex meanComplex(Rcpp::ComplexVector x) { return(Rcpp::mean(x)); }
660+

inst/unitTests/runit.sugar.R

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,4 +675,38 @@ if (.runThisTest) {
675675
"sugar vector vector operations")
676676
}
677677

678+
## Additions made 1 Jan 2015
679+
680+
test.mean.integer <- function() {
681+
v1 <- seq(-100L, 100L)
682+
v2 <- c(v1, NA)
683+
checkEquals(mean(v1), meanInteger(v1), "mean of integer vector")
684+
checkEquals(mean(v2), meanInteger(v2), "mean of integer vector with NA")
685+
}
686+
687+
test.mean.numeric <- function() {
688+
v1 <- seq(-100, 100)
689+
v2 <- c(v1, NA)
690+
v3 <- c(v1, Inf)
691+
checkEquals(mean(v1), meanNumeric(v1), "mean of numeric vector")
692+
checkEquals(mean(v2), meanNumeric(v2), "mean of numeric vector with NA")
693+
checkEquals(mean(v3), meanNumeric(v3), "mean of numeric vector with Inf")
694+
}
695+
696+
test.mean.complex <- function() {
697+
v1 <- seq(-100, 100) + 1.0i
698+
v2 <- c(v1, NA)
699+
v3 <- c(v1, Inf)
700+
checkEquals(mean(v1), meanComplex(v1), "mean of complex vector")
701+
checkEquals(mean(v2), meanComplex(v2), "mean of complex vector with NA")
702+
checkEquals(mean(v3), meanComplex(v3), "mean of complex vector with Inf")
703+
}
704+
705+
test.mean.logical <- function() {
706+
v1 <- c(rep(TRUE, 50), rep(FALSE, 25))
707+
v2 <- c(v1, NA)
708+
checkEquals(mean(v1), meanLogical(v1), "mean of logical vector")
709+
checkEquals(mean(v2), meanLogical(v2), "mean of logical vector with NA")
710+
}
711+
678712
}

0 commit comments

Comments
 (0)