File: test_misc.R

package info (click to toggle)
rcpp 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,480 kB
  • sloc: cpp: 27,436; ansic: 7,778; sh: 53; makefile: 2
file content (193 lines) | stat: -rw-r--r-- 7,183 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193

##  Copyright (C) 2010 - 2021  Dirk Eddelbuettel and Romain Francois
##
##  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 <http://www.gnu.org/licenses/>.

if (Sys.getenv("RunAllRcppTests") != "yes") exit_file("Set 'RunAllRcppTests' to 'yes' to run.")

Rcpp::sourceCpp("cpp/misc.cpp")

#   test.Symbol <- function(){
res <- symbol_()
expect_true( res[1L], info = "Symbol creation - SYMSXP " )
expect_true( res[2L], info = "Symbol creation - CHARSXP " )
expect_true( res[3L], info = "Symbol creation - STRSXP " )
expect_true( res[4L], info = "Symbol creation - std::string " )

#    test.Symbol.notcompatible <- function(){
expect_error( symbol_ctor(symbol_ctor), info = "Symbol not compatible with function" )
expect_error( symbol_ctor(asNamespace("Rcpp")), info = "Symbol not compatible with environment" )
expect_error( symbol_ctor(1:10), info = "Symbol not compatible with integer" )
expect_error( symbol_ctor(TRUE), info = "Symbol not compatible with logical" )
expect_error( symbol_ctor(1.3), info = "Symbol not compatible with numeric" )
expect_error( symbol_ctor(as.raw(1) ), info = "Symbol not compatible with raw" )

#    test.Argument <- function(){
expect_equal( Argument_(), list( x = 2L, y = 3L ) , info = "Argument")

#    test.Dimension.const <- function(){
expect_equal( Dimension_const( c(2L, 2L)) , 2L, info = "testing const operator[]" )

#    test.evaluator.error <- function(){
expect_error( evaluator_error(), info = "Rcpp_eval( stop() )" )

#    test.evaluator.ok <- function(){
expect_equal( sort(evaluator_ok(1:10)), 1:10, info = "Rcpp_eval running fine" )

#    test.exceptions <- function(){
can.demangle <- Rcpp:::capabilities()[["demangling"]]

e <- tryCatch(  exceptions_(), "C++Error" = function(e) e )
expect_true( "C++Error" %in% class(e), info = "exception class C++Error" )

if( can.demangle )  expect_true( "std::range_error" %in% class(e), info = "exception class std::range_error" )

expect_equal( e$message, "boom", info = "exception message" )

if( can.demangle ){
    ## same with direct handler
    e <- tryCatch(  exceptions_(), "std::range_error" = function(e) e )
    expect_true( "C++Error" %in% class(e), info = "(direct handler) exception class C++Error" )
    expect_true( "std::range_error" %in% class(e), info = "(direct handler) exception class std::range_error" )
    expect_equal( e$message, "boom", info = "(direct handler) exception message" )
}
f <- function(){
    try( exceptions_(), silent = TRUE)
    "hello world"
}
expect_equal( f(), "hello world", info = "life continues after an exception" )


#    test.has.iterator <- function(){
has_it <- has_iterator_()
expect_true( has_it[1L] , info = "has_iterator< std::vector<int> >" )
expect_true( has_it[2L] , info = "has_iterator< std::ist<int> >" )
expect_true( has_it[3L] , info = "has_iterator< std::deque<int> >" )
expect_true( has_it[4L] , info = "has_iterator< std::set<int> >" )
expect_true( has_it[5L] , info = "has_iterator< std::map<string,int> >" )

expect_true( ! has_it[6L] , info = "has_iterator< std::pair<string,int> >" )
expect_true( ! has_it[7L] , info = "Rcpp::Symbol" )

#    test.AreMacrosDefined <- function(){
expect_true( Rcpp:::areMacrosDefined( "__cplusplus" ) )

#    test.rcout <- function(){
## define test string that is written to two files
teststr <- "First line.\nSecond line."

rcppfile <- tempfile()
rfile <- tempfile()

## write to test_rcpp.txt from Rcpp
test_rcout(rcppfile,  teststr )

## write to test_r.txt from R
cat( teststr, file=rfile, sep='\n' )

## compare whether the two files have the same data
expect_equal( readLines(rcppfile), readLines(rfile), info="Rcout output")

#    test.rcout.complex <- function(){
rcppfile <- tempfile()
rfile <- tempfile()

z <- complex(real=sample(1:10, 1), imaginary=sample(1:10, 1))

## write to test_rcpp.txt from Rcpp
test_rcout_rcomplex(rcppfile,  z )

## write to test_r.txt from R
cat( z, file=rfile, sep='\n' )

## compare whether the two files have the same data
expect_equal( readLines(rcppfile), readLines(rfile), info="Rcout Rcomplex")

#    test.na_proxy <- function(){
expect_equal(
    na_proxy(),
    rep(c(TRUE,TRUE,TRUE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE) , 2),
    info = "Na_Proxy NA == handling"
)

#    test.StretchyList <- function(){
expect_equal(stretchy_list(), pairlist( "foo", 1L, 3.2 ))

#    test.named_StretchyList <- function(){
expect_equal(named_stretchy_list(), pairlist( a = "foo", b = 1L, c = 3.2 ))

#    test.stop.variadic <- function(){
m <- tryCatch( test_stop_variadic(), error = function(e){
    conditionMessage(e)
})
expect_equal( m, "foo 3" )

#    test.NullableForNull <- function() {
M <- matrix(1:4, 2, 2)
expect_true(   testNullableForNull(NULL) )
expect_true( ! testNullableForNull(M) )

#    test.NullableForNotNull <- function() {
M <- matrix(1:4, 2, 2)
expect_true( ! testNullableForNotNull(NULL) )
expect_true(   testNullableForNotNull(M) )

#    test.NullableAccessOperator <- function() {
M <- matrix(1:4, 2, 2)
expect_equal( testNullableOperator(M), M )

#    test.NullableAccessGet <- function() {
M <- matrix(1:4, 2, 2)
expect_equal( testNullableGet(M), M )

#    test.NullableAccessAs <- function() {
M <- matrix(1:4, 2, 2)
expect_equal( testNullableAs(M), M )

#    test.NullableAccessClone <- function() {
M <- matrix(1:4, 2, 2)
expect_equal( testNullableClone(M), M )

#    test.NullableIsUsableTrue <- function() {
M <- matrix(1:4, 2, 2)
expect_equal( testNullableIsUsable(M), M)

#    test.NullableIsUsableFalse <- function() {
expect_true(is.null(testNullableIsUsable(NULL)))

#    test.NullableString <- function() {
expect_equal(testNullableString(), "")
expect_equal(testNullableString("blah"), "blah")

#    test.bib <- function() {
expect_true(nchar(Rcpp:::bib()) > 0, info="bib file")

#    test.getRcppVersion <- function() {
expect_true(inherits(Rcpp::getRcppVersion(), "package_version"), info="package_version object")
expect_true(Rcpp::getRcppVersion(devel=TRUE) >= Rcpp::getRcppVersion(devel=FALSE), info="dev greater equal release")

## if need be it can be useful to fail to test e.g. the Docker setup
## commented out now as we prefer to pass when not debugging ;-)
# expect_true(FALSE, info="oh noes")

## test that a message is output as is, and a suppressedMessage is not
txt <- "ABCdef"
expect_equal(capture.output(messageWrapper(txt), type="message"), txt)
expect_equal(capture.output(suppressMessages(messageWrapper(txt)), type="message"), character())
expect_message(messageWrapper(txt))
## test for message component
msg <- tryCatch(message(txt), message = identity)
expect_equal(msg$message, paste(txt, "\n", sep=""))