38 Foo foo(
"testfoo", 42, 43);
39 printf(
"setting Foo(%s, %d, %d) in the R environment\n", foo.
name.c_str(), foo.
a, foo.
b);
43 printf(
"got Foo(%s, %d, %d) via getValue\n", foo2.name.c_str(), foo2.a, foo2.b);
46 printf(
"got Foo(%s, %d, %d) via parseEval\n", foo3.name.c_str(), foo3.a, foo3.b);
51 catch (
const std::runtime_error &e) {
52 printf(
"Getting a nonexistent variable failed with message:\n%s\n", e.what());
58 catch (
const std::runtime_error &e) {
59 printf(
"Getting foo as an object of class Bar failed with message:\n%s\n", e.what());
79 std::function<Foo(const std::string &)> loadFoo =
80 [] (
const std::string &name) ->
Foo {
81 return Foo(name, name.length(), 1);
85 std::function<Foo(const Foo &)>
swapFoo =
86 [] (
const Foo &foo) ->
Foo {
87 return Foo(foo.name, foo.b, foo.a);
91 std::function<Bar(int)> loadBar =
93 std::string foo_name = std::string(
"foo_") + std::to_string(
id);
94 return Bar(foo_name,
Foo(foo_name, foo_name.length(), id));
98 std::function<std::vector<float>(float, float,
const std::vector<int> &)> calibrate =
99 [] (
float offset,
float scale,
const std::vector<int> &in) -> std::vector<float> {
100 std::vector<float> out;
101 out.reserve(in.size());
103 out.push_back(offset + (
float) v * scale);
109 printf(
"got Foo(%s, %d, %d) via loadFoo()\n", foo.name.c_str(), foo.a, foo.b);
112 printf(
"got Foo(%s, %d, %d) after swapFoo()\n", foo2.name.c_str(), foo2.a, foo2.b);
115 printf(
"got Bar(%s, Foo(%s, %d, %d))\n", bar.name.c_str(), bar.foo.name.c_str(), bar.foo.a, bar.foo.b);
117 auto vec = R.
parseEval<std::vector<float>>(
"calibrate(1.0, 0.3, c(1,2,3,4,5))");
121 printf(
") from calibrate()\n");
125 printf(
"got Foo(%s, %d, %d) via loadFoo()\n", foo3.name.c_str(), foo3.a, foo3.b);
127 catch (
const std::runtime_error &e) {
128 printf(
"Calling loadFoo() with wrong parameters failed with message:\n%s\n", e.what());
131 printf(
"Calling loadFoo() with wrong parameters lead to an unrecoverable error, ending test\n");
137 printf(
"Got x = %d\n", x);
151 printf(
"Output of the R script:\n%s\n", output.c_str());
165 R.
parseEvalQ(
"plot(c(0,0), type = 'n', xlim=c(0,1), ylim=c(-1,1), xlab = 'x', ylab = 'y', bty='n')");
167 R.
parseEvalQ(
"lines(c(0,0), c(-1,1), col='red', add=TRUE)");
168 R.
parseEvalQ(
"curve(-x, 0, 1, 200, col='blue', add=TRUE)");
169 R.
parseEvalQ(
"curve(0.5+sqrt(1-x^2)/2, 0, 1, 200, col='#00FF00', add=TRUE)");
170 R.
parseEvalQ(
"curve(0.5-sqrt(1-x^2)/2, 0, 1, 200, col='#33EE33', add=TRUE)");
172 printf(
"Got a png from the plot, saving to plot.png\n");
174 std::fstream f(
"plot.png", std::fstream::out | std::fstream::binary | std::fstream::trunc);
196 printf(
"id of environment 1 is: %d, id of environment 2 is: %d\n", id1, id2);
202 printf(
"==========================\nTesting setting and getting:\n==========================\n");
204 printf(
"\n==========================\nTesting callbacks:\n==========================\n");
206 printf(
"\n==========================\nTesting console output:\n==========================\n");
208 printf(
"\n==========================\nTesting plots:\n==========================\n");
210 printf(
"\n==========================\nTesting multiple environments:\n==========================\n");
214 printf(
"Error communicating with the server\nDid you start ./example_server?\n");
static void test_setting_getting()
void setCallback(const std::string &name, std::function< R(Params...)> &callback)
static void test_multiple()
void setValue(const std::string &name, const T &value)
T parseEval(const std::string &code)
static void test_callbacks()
#define ris_socket_address
T getValue(const std::string &name)
void initPlot(uint32_t width=800, uint32_t height=600)
static void test_console_output()
std::string getConsoleOutput()
static BinaryStream connectToUnixSocket(const char *)
void parseEvalQ(const std::string &code)