#!/usr/bin/r -t
# -*- mode: R; tab-width: 4; -*-
#
# Copyright (C) 2009 - 2014 Romain Francois and Dirk Eddelbuettel
#
# This file is part of Rcpp.
#
# Rcpp is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Rcpp is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Rcpp. If not, see .
.runThisTest <- Sys.getenv("RunAllRcppTests") == "yes"
if (.runThisTest) {
.setUp <- Rcpp:::unitTestSetup("RObject.cpp")
test.RObject.asDouble <- function(){
checkEquals( asDouble(2.123), 4.246, msg = "as( REALSXP ) " )
checkEquals( asDouble(2L), 4.0, msg = "as( INTSXP ) " )
checkEquals( asDouble(as.raw(2L)), 4.0, msg = "as( RAWSXP )" )
checkException( asDouble('2'), msg = "as( STRSXP ) -> exception" )
checkException( asDouble(2:3), msg = "as expects the vector to be of length 1" )
}
test.RObject.asInt <- function(){
checkEquals( asInt(2.123), 4L, msg = "as( REALSXP )" )
checkEquals( asInt(2), 4L, msg = "as( REALSXP )" )
checkEquals( asInt(2L), 4.0, msg = "as( INTSXP )" )
checkEquals( asInt(as.raw(2L)), 4.0, msg = "as( RAWSXP )" )
checkException( asInt( '2'), msg = "as can not convert character" )
checkException( asInt( 2:3), msg = "as expects the vector to be of length 1" )
}
test.RObject.asStdString <- function(){
checkEquals( asStdString("abc"), "abcabc", msg = "as" )
checkException( asStdString(NULL), msg = "as expects character vector" )
checkException( asStdString(0L), msg = "as expects character vector" )
checkException( asStdString(0.1), msg = "as expects character vector" )
checkException( asStdString(as.raw(0L)), msg = "as expects character vector" )
checkException( asStdString(letters), msg = "as expects single string" )
}
test.RObject.asRaw <- function(){
checkEquals( asRaw(1L), as.raw(2L), msg = "as(integer)" )
checkEquals( asRaw(1.3), as.raw(2L), msg = "as(numeric)" )
checkEquals( asRaw(as.raw(1)), as.raw(2L), msg = "as(raw)" )
checkException( asRaw(NULL) , msg = "as(NULL) -> exception" )
checkException( asRaw("foo") , msg = "as(character) -> exception" )
checkException( asRaw(1:2), msg = "as(>1 integer) -> exception" )
checkException( asRaw(as.numeric(1:2)), msg = "as(>1 numeric) -> exception" )
checkException( asRaw(as.raw(1:3)), msg = "as(>1 raw) -> exception" )
checkException( asRaw(integer(0)), msg = "as(0 integer) -> exception" )
checkException( asRaw(numeric(0)), msg = "as(0 numeric) -> exception" )
checkException( asRaw(raw(0)), msg = "as(0 raw) -> exception" )
}
test.RObject.asLogical <- function(){
checkTrue( !asLogical(TRUE), msg = "as(TRUE) -> true" )
checkTrue( asLogical(FALSE), msg = "as(FALSE) -> false" )
checkTrue( !asLogical(1L), msg = "as(1L) -> true" )
checkTrue( asLogical(0L), msg = "as(0L) -> false" )
checkTrue( !asLogical(1.0), msg = "as(1.0) -> true" )
checkTrue( asLogical(0.0), msg = "as0.0) -> false" )
checkTrue( !asLogical(as.raw(1)), msg = "as(aw.raw(1)) -> true" )
checkTrue( asLogical(as.raw(0)), msg = "as(as.raw(0)) -> false" )
checkException( asLogical(NULL), msg = "as(NULL) -> exception" )
checkException( asLogical(c(TRUE,FALSE)), msg = "as(>1 logical) -> exception" )
checkException( asLogical(1:2), msg = "as(>1 integer) -> exception" )
checkException( asLogical(1:2+.1), msg = "as(>1 numeric) -> exception" )
checkException( asLogical(as.raw(1:2)), msg = "as(>1 raw) -> exception" )
checkException( asLogical(integer(0)), msg = "as(0 integer) -> exception" )
checkException( asLogical(numeric(0)), msg = "as(0 numeric) -> exception" )
checkException( asLogical(raw(0)), msg = "as(0 raw) -> exception" )
}
test.RObject.asStdVectorInt <- function(){
checkEquals( asStdVectorInt(x=2:5), 2:5*2L, msg = "as< std::vector >(integer)" )
checkEquals( asStdVectorInt(x=2:5+.1), 2:5*2L, msg = "as< std::vector >(numeric)" )
checkEquals( asStdVectorInt(x=as.raw(2:5)), 2:5*2L, msg = "as< std::vector >(raw)" )
checkException( asStdVectorInt("foo"), msg = "as< std::vector >(character) -> exception" )
checkException( asStdVectorInt(NULL), msg = "as< std::vector >(NULL) -> exception" )
}
test.RObject.asStdVectorDouble <- function(){
checkEquals( asStdVectorDouble(x=0.1+2:5), 2*(0.1+2:5), msg = "as< std::vector >( numeric )" )
checkEquals( asStdVectorDouble(x=2:5), 2*(2:5), msg = "as< std::vector >(integer)" )
checkEquals( asStdVectorDouble(x=as.raw(2:5)), 2*(2:5), msg = "as< std::vector >(raw)" )
checkException( asStdVectorDouble("foo"), msg = "as< std::vector >(character) -> exception" )
checkException( asStdVectorDouble(NULL), msg = "as< std::vector >(NULL) -> exception" )
}
test.RObject.asStdVectorRaw <- function(){
checkEquals( asStdVectorRaw(x=as.raw(0:9)), as.raw(2*(0:9)), msg = "as< std::vector >(raw)" )
checkEquals( asStdVectorRaw(x=0:9), as.raw(2*(0:9)), msg = "as< std::vector >( integer )" )
checkEquals( asStdVectorRaw(x=as.numeric(0:9)), as.raw(2*(0:9)), msg = "as< std::vector >(numeric)" )
checkException( asStdVectorRaw("foo"), msg = "as< std::vector >(character) -> exception" )
checkException( asStdVectorRaw(NULL), msg = "as< std::vector >(NULL) -> exception" )
}
test.RObject.asStdVectorBool <- function(){
checkEquals( asStdVectorBool(x=c(TRUE,FALSE)), c(FALSE, TRUE), msg = "as< std::vector >(logical)" )
checkEquals( asStdVectorBool(x=c(1L, 0L)), c(FALSE, TRUE), msg = "as< std::vector >(integer)" )
checkEquals( asStdVectorBool(x=c(1.0, 0.0)), c(FALSE, TRUE), msg = "as< std::vector >(numeric)" )
checkEquals( asStdVectorBool(x=as.raw(c(1,0))), c(FALSE, TRUE), msg = "as< std::vector >(raw)" )
checkException( asStdVectorBool("foo"), msg = "as< std::vector >(character) -> exception" )
checkException( asStdVectorBool(NULL), msg = "as< std::vector >(NULL) -> exception" )
}
test.RObject.asStdVectorString <- function(){
checkEquals( asStdVectorString(c("foo", "bar")), c("foofoo", "barbar"), msg = "as< std::vector >(character)" )
checkException( asStdVectorString(1L), msg = "as< std::vector >(integer) -> exception" )
checkException( asStdVectorString(1.0), msg = "as< std::vector >(numeric) -> exception" )
checkException( asStdVectorString(as.raw(1)), msg = "as< std::vector >(raw) -> exception" )
checkException( asStdVectorString(TRUE), msg = "as< std::vector >(logical) -> exception" )
checkException( asStdVectorString(NULL), msg = "as< std::vector >(NULL) -> exception" )
}
test.RObject.stdsetint <- function(){
checkEquals( stdsetint(), c(0L, 1L), msg = "wrap( set )" )
}
test.RObject.stdsetdouble <- function(){
checkEquals( stdsetdouble(), as.numeric(0:1), msg = "wrap( set" )
}
test.RObject.stdsetraw <- function(){
checkEquals( stdsetraw(), as.raw(0:1), msg = "wrap(set)" )
}
test.RObject.stdsetstring <- function(){
checkEquals( stdsetstring(), c("bar", "foo"), msg = "wrap(set)" )
}
test.RObject.attributeNames <- function(){
df <- data.frame( x = 1:10, y = 1:10 )
checkTrue( all( c("names","row.names","class") %in% attributeNames(df)), msg = "RObject.attributeNames" )
}
test.RObject.hasAttribute <- function(){
df <- data.frame( x = 1:10 )
checkTrue( hasAttribute( df ), msg = "RObject.hasAttribute" )
}
test.RObject.attr <- function(){
df <- data.frame( x = 1:150 )
rownames(df) <- 1:150
checkEquals( attr_( iris ), 1:150, msg = "RObject.attr" )
}
test.RObject.attr.set <- function(){
checkEquals( attr(attr_set(), "foo"), 10L, msg = "RObject.attr() = " )
}
test.RObject.isNULL <- function(){
df <- data.frame( x = 1:10 )
checkTrue( !isNULL( df ), msg = "RObject.isNULL(data frame) -> false" )
checkTrue( !isNULL(1L), msg = "RObject.isNULL(integer) -> false" )
checkTrue( !isNULL(1.0), msg = "RObject.isNULL(numeric) -> false" )
checkTrue( !isNULL(as.raw(1)), msg = "RObject.isNULL(raw) -> false" )
checkTrue( !isNULL(letters), msg = "RObject.isNULL(character) -> false")
checkTrue( !isNULL(test.RObject.isNULL), msg = "RObject.isNULL(function) -> false" )
checkTrue( !isNULL(.GlobalEnv), msg = "RObject.isNULL(environment) -> false" )
checkTrue( isNULL(NULL), msg = "RObject.isNULL(NULL) -> true" )
}
test.RObject.inherits <- function(){
x <- 1:10
checkTrue( !inherits_(x) )
class(x) <- "foo"
checkTrue( inherits_(x) )
class(x) <- c("foo", "bar" )
checkTrue( inherits_(x) )
}
}