1 2## Copyright (C) 2015 - 2019 Wush Wu 3## 4## This file is part of Rcpp. 5## 6## Rcpp is free software: you can redistribute it and/or modify it 7## under the terms of the GNU General Public License as published by 8## the Free Software Foundation, either version 2 of the License, or 9## (at your option) any later version. 10## 11## Rcpp is distributed in the hope that it will be useful, but 12## WITHOUT ANY WARRANTY; without even the implied warranty of 13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14## GNU General Public License for more details. 15## 16## You should have received a copy of the GNU General Public License 17## along with Rcpp. If not, see <http://www.gnu.org/licenses/>. 18 19if (Sys.getenv("RunAllRcppTests") != "yes") exit_file("Set 'RunAllRcppTests' to 'yes' to run.") 20 21# test.Sugar.var <- function() { 22fNumeric <- Rcpp::cppFunction('double myVar(NumericVector x) { return(var(x)); }') 23fInteger <- Rcpp::cppFunction('double myVar(IntegerVector x) { return(var(x)); }') 24fComplex <- Rcpp::cppFunction('double myVar(ComplexVector x) { return(var(x)); }') 25fLogical <- Rcpp::cppFunction('double myVar(LogicalVector x) { return(var(x)); }') 26test_data_real <- 1:10 27expect_equal(fNumeric(test_data_real * 1.1), var(test_data_real * 1.1)) 28expect_equal(fInteger(test_data_real), var(test_data_real)) 29test_data_complex_1 <- complex(real = 5:1, imag = 2:6) 30test_data_complex_2 <- complex(real = 1:5, imag = 6:10) 31test_data_complex_1_known_var <- 5 32test_data_complex_2_known_var <- 5 33expect_equal(fComplex(test_data_complex_1), test_data_complex_1_known_var) 34expect_equal(fComplex(test_data_complex_2), test_data_complex_2_known_var) 35test_data_logical <- c(TRUE, FALSE, TRUE, FALSE, TRUE) 36expect_equal(fLogical(test_data_logical), var(test_data_logical)) 37