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
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(improper_ctypes)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
#[cfg(test)]
mod tests {
use super::*;
use std::os::raw;
macro_rules! cstr {
($s: expr) => {
concat!($s, "\0").as_ptr() as *const raw::c_char
};
}
macro_rules! cstr_mut {
($s: expr) => {
concat!($s, "\0").as_ptr() as *mut raw::c_char
};
}
fn start_R() {
unsafe {
if std::env::var("R_HOME").is_err() {
std::env::set_var("R_HOME", env!("R_HOME"));
}
if cfg!(target_os = "windows") && cfg!(target_arch = "x86") {
Rf_initialize_R(
4,
[cstr_mut!("R"), cstr_mut!("--arch=i386"), cstr_mut!("--slave"), cstr_mut!("--no-save")].as_mut_ptr(),
);
} else {
Rf_initialize_R(
3,
[cstr_mut!("R"), cstr_mut!("--slave"), cstr_mut!("--no-save")].as_mut_ptr(),
);
}
if cfg!(not(target_os = "windows")) {
R_CStackLimit = usize::max_value();
}
setup_Rmainloop();
}
}
#[test]
fn test_eval() {
start_R();
unsafe {
let s = Rf_protect(Rf_mkString(cstr!("1")));
let mut status: ParseStatus = 0;
let status_ptr = &mut status as *mut ParseStatus;
let ps = Rf_protect(R_ParseVector(s, -1, status_ptr, R_NilValue));
let val = Rf_eval(VECTOR_ELT(ps, 0), R_GlobalEnv);
Rf_PrintValue(val);
assert_eq!(TYPEOF(val) as u32, REALSXP);
assert_eq!(*REAL(val), 1.);
Rf_unprotect(2);
}
}
}