Skip to content

Commit

Permalink
Rigorous RPC call tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfjlaros committed Dec 27, 2018
1 parent ee0070e commit 1fc4a7c
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 50 deletions.
2 changes: 1 addition & 1 deletion tests/arduino-serial-fixture
166 changes: 117 additions & 49 deletions tests/test_rpcCall.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,145 @@

TEST_CASE("RPC call function", "[call]") {
struct S {
static void f(int, char) {}
static short int g(int i, char c) {
return i + c + 1;
static void v_0(void) {}
static void v_1(int) {}
static void v_2(int, char) {}
static void v_3(int, char, float) {}
static void v_c_1(char *, int) {}
static void v_cc_1(const char *, int) {}
static void v_c_2(int, char *) {}
static void v_cc_2(int, const char *) {}
static short int s_0(void) {
return 1;
}
static short int s_1(int) {
return 2;
}
static short int s_2(int, char) {
return 3;
}
static float f_0(void) {
return 1.0F;
}
static void h(int, char *) {}
static void i(int, const char *) {}
static void j(char *, int) {}
static void k(const char *, int) {}
static char *l(void) {
return (char *)"xxx";
static float f_1(int) {
return 2.0F;
}
static const char *m(void) {
return "xxx";
static float f_2(int, char) {
return 3.0F;
}
static char *c_0(void) {
return (char *)"xyz";
}
static const char *cc_0(void) {
return "xyz";
}
};

// Void function.
// Void function, no parameters.
Serial.reset();
rpcCall(S::f);
REQUIRE(Serial.rx == sizeof(int) + sizeof(char));
rpcCall(S::v_0);
REQUIRE(Serial.rx == 0);
REQUIRE(Serial.tx == 0);

// Non-void function.
// Void function, one parameter.
Serial.reset();
rpcCall(S::v_1);
REQUIRE(Serial.rx == sizeof(int));
REQUIRE(Serial.tx == 0);

// Void function, two parameters.
Serial.reset();
Serial.prepare(12, '\3');
rpcCall(S::g);
rpcCall(S::v_2);
REQUIRE(Serial.rx == sizeof(int) + sizeof(char));
REQUIRE(Serial.tx == sizeof(short int));
REQUIRE(Serial.inspect<short int>() == 16);
REQUIRE(Serial.tx == 0);

// Void function, three parameters.
Serial.reset();
rpcCall(S::v_3);
REQUIRE(Serial.rx == sizeof(int) + sizeof(char) + sizeof(float));
REQUIRE(Serial.tx == 0);

// Void function, first parameter is of type char *.
Serial.reset();
Serial.prepare("xyz", 1234);
rpcCall(S::v_c_1);
REQUIRE(Serial.rx == 4 + sizeof(int));
REQUIRE(Serial.tx == 0);

// Void function, first parameter is of type const char *.
Serial.reset();
Serial.prepare("xyz", 1234);
rpcCall(S::v_cc_1);
REQUIRE(Serial.rx == 4 + sizeof(int));
REQUIRE(Serial.tx == 0);

// Second parameter is of type char *.
// Void function, second parameter is of type char *.
Serial.reset();
Serial.prepare(1234, "xxx");
rpcCall(S::h);
rpcCall(S::v_c_2);
REQUIRE(Serial.rx == sizeof(int) + 4);
REQUIRE(Serial.tx == 0);

// Second parameter is of type const char *.
// Void function, second parameter is of type const char *.
Serial.reset();
Serial.prepare(1234, "xxx");
rpcCall(S::i);
rpcCall(S::v_cc_2);
REQUIRE(Serial.rx == sizeof(int) + 4);
REQUIRE(Serial.tx == 0);

// First parameter is of type char *.
// Function of type short int, zero parameters.
Serial.reset();
Serial.prepare("xxx", 1234);
rpcCall(S::j);
REQUIRE(Serial.rx == 4 + sizeof(int));
REQUIRE(Serial.tx == 0);
rpcCall(S::s_0);
REQUIRE(Serial.inspect<short int>() == 1);
REQUIRE(Serial.rx == 0);
REQUIRE(Serial.tx == sizeof(short int));

// First parameter is of type const char *.
// Function of type short int, one parameter.
Serial.reset();
Serial.prepare("xxx", 1234);
rpcCall(S::k);
REQUIRE(Serial.rx == 4 + sizeof(int));
REQUIRE(Serial.tx == 0);
rpcCall(S::s_1);
REQUIRE(Serial.inspect<short int>() == 2);
REQUIRE(Serial.rx == sizeof(int));
REQUIRE(Serial.tx == sizeof(short int));

// Return type char *.
// Function of type short int, two parameters.
Serial.reset();
rpcCall(S::l);
rpcCall(S::s_2);
REQUIRE(Serial.inspect<short int>() == 3);
REQUIRE(Serial.rx == sizeof(int) + sizeof(char));
REQUIRE(Serial.tx == sizeof(short int));

REQUIRE(Serial.inspect<String>() == "xxx");
// Function of type float, zero parameters.
Serial.reset();
rpcCall(S::f_0);
REQUIRE(Serial.inspect<float>() == 1.0F);
REQUIRE(Serial.rx == 0);
REQUIRE(Serial.tx == 4); // FIXME: off by one.
REQUIRE(Serial.tx == sizeof(float));

// Return type const char *.
// Function of type float, one parameter.
Serial.reset();
rpcCall(S::m);
rpcCall(S::f_1);
REQUIRE(Serial.inspect<float>() == 2.0F);
REQUIRE(Serial.rx == sizeof(int));
REQUIRE(Serial.tx == sizeof(float));

REQUIRE(Serial.inspect<String>() == "xxx");
// Function of type float, two parameters.
Serial.reset();
rpcCall(S::f_2);
REQUIRE(Serial.inspect<float>() == 3.0F);
REQUIRE(Serial.rx == sizeof(int) + sizeof(char));
REQUIRE(Serial.tx == sizeof(float));

// Function of type char *.
Serial.reset();
rpcCall(S::c_0);
REQUIRE(Serial.inspect<String>() == "xyz");
REQUIRE(Serial.rx == 0);
REQUIRE(Serial.tx == 4);

// Function of type const char *.
Serial.reset();
rpcCall(S::cc_0);
REQUIRE(Serial.inspect<String>() == "xyz");
REQUIRE(Serial.rx == 0);
REQUIRE(Serial.tx == 4);
}
Expand All @@ -85,7 +153,7 @@ TEST_CASE("RPC call class member functions", "[call]") {
public:
void f(int, char) {}
short int g(int, char) {
return 0;
return 1;
}
};

Expand All @@ -94,27 +162,27 @@ TEST_CASE("RPC call class member functions", "[call]") {
// Void function.
Serial.reset();
rpcCall(pack(&c, &C::f));

REQUIRE(Serial.rx == sizeof(int) + sizeof(char));
REQUIRE(Serial.tx == 0);

// Non-void function.
Serial.reset();
rpcCall(pack(&c, &C::g));

REQUIRE(Serial.inspect<short int>() == 1);
REQUIRE(Serial.rx == sizeof(int) + sizeof(char));
REQUIRE(Serial.tx == sizeof(short int));
}

short int add(int i, char c) {
return i + c + 1;
}
TEST_CASE("Executing a function", "[call]") {
struct S {
static short int f(int i, char c) {
return i + c + 1;
}
};

TEST_CASE("Input and output", "[call]") {
Serial.reset();
Serial.prepare(1234, '\3');
rpcCall(add);

rpcCall(S::f);
REQUIRE(Serial.rx == sizeof(int) + sizeof(char));
REQUIRE(Serial.tx == sizeof(short int));
REQUIRE(Serial.inspect<short int>() == 1238);
Expand Down

0 comments on commit 1fc4a7c

Please sign in to comment.