Skip to content

Conversation

cmaglie
Copy link
Contributor

@cmaglie cmaglie commented Jun 11, 2025

This PR aims to provide unit tests that can be run locally on a PC.

This patch is inspired by Aentiger's (😢) work on unit tests for the ArduinoCore-API.
The Serial connection is mocked with a bidirectional Stream.

Currently, only a single call test is implemented, which is very rough and can be optimized. However, my goal is to create a pilot implementation that works.

It uses CMake for building, and the catch.hpp library to run assertions on tests. It should also produce coverage, but I haven't investigated yet how to extract the data and upload it to codecov.
Further instructions can be found in the README in the PR.

@per1234 per1234 added type: enhancement Proposed improvement topic: infrastructure Related to project infrastructure labels Jun 12, 2025
@eigen-value
Copy link
Collaborator

Hi @cmaglie that's a super useful feature!
I'm experiencing some issues I wanna share, but first of all here's my setup:

  • WSL2 Ubuntu 22.04
  • cmake 4.0.2
  • gcc 11.4.0

I tried to modify the tests a little and:

  • If I try to multiply -2.0*3.0 I get:
/home/lucio/Arduino/libraries/Arduino_RPClite/extras/test/src/RPC/test_RPCClient.cpp:27: FAILED:
  REQUIRE( expected[i] == got[i] )
with expansion:
  '�' == '@'
with messages:
  Test case [RPCClient::call] failed at line 63
  Array size: expected: 27, got: 27
  Index 10: expected 192, got 64
  • If I use the MALFORMED_CALL_ERR (0xFD) as error code instead of 1 in the missing parameter case I get:
/home/lucio/Arduino/libraries/Arduino_RPClite/extras/test/src/RPC/test_RPCClient.cpp:99: FAILED:
  REQUIRE( rpc.lastError.code == 0xFD )
with expansion:
  -3 == 253

It looks like a sign conversion problem maybe related to the typecast the StreamMock class is doing (?)

The library has no issues regarding error codes or float sign

@cmaglie
Copy link
Contributor Author

cmaglie commented Jul 1, 2025

  • If I try to multiply -2.0*3.0 I get:
/home/lucio/Arduino/libraries/Arduino_RPClite/extras/test/src/RPC/test_RPCClient.cpp:27: FAILED:
  REQUIRE( expected[i] == got[i] )
with expansion:
  '�' == '@'
with messages:
  Test case [RPCClient::call] failed at line 63
  Array size: expected: 27, got: 27
  Index 10: expected 192, got 64

This is actually the correct outcome since you changed 2.0 to -2.0 the response obtained is different from the expected.

   Test case [RPCClient::call] failed at line 63
   Array size: expected: 27, got: 27
   Index 10: expected 192, got 64

since line 63 is:

    COMPARE_ARRAYS(expected, got);

The error indicates that the byte at index 10 is 64 instead of 192.
The strangest part is probably:

> /home/lucio/Arduino/libraries/Arduino_RPClite/extras/test/src/RPC/test_RPCClient.cpp:27: FAILED:
>   REQUIRE( expected[i] == got[i] )
> with expansion:
>   '�' == '@'

This is an artifact due to the implementation of the COMPARE_ARRAY part. I admit I sketched this part quickly, but it can be improved, for example changing line 27 from:

    REQUIRE(expected[i] == got[i]);

to

    REQUIRE(int(expected[i]) == int(got[i]));

improves the message:

/home/megabug/Workspace/Arduino_RPClite/extras/test/src/RPC/test_RPCClient.cpp:27: FAILED:
  REQUIRE( int(expected[i]) == int(got[i]) )
with expansion:
  64 == 192
with messages:
  Test case [RPCClient::call] failed at line 63
  Array size: expected: 27, got: 27
  Index 10: expected 64, got 192
  • If I use the MALFORMED_CALL_ERR (0xFD) as error code instead of 1 in the missing parameter case I get:
/home/lucio/Arduino/libraries/Arduino_RPClite/extras/test/src/RPC/test_RPCClient.cpp:99: FAILED:
  REQUIRE( rpc.lastError.code == 0xFD )
with expansion:
  -3 == 253

It looks like a sign conversion problem maybe related to the typecast the StreamMock class is doing (?)

Here you are comparing rpc.lastError.code that is an integer with value -3 with 0xFD that is an integer with value 253.
It is correct that this will trigger an error, because you're trying to compare -3 with the Msgpack encoding of -3 that is the unsigned 0xFD.

The correct comparison should be:

REQUIRE(rpc.lastError.code == -3)

That is exactly what you will write client-side.

@cmaglie
Copy link
Contributor Author

cmaglie commented Jul 1, 2025

I've rebased this branch on top of the latest main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: infrastructure Related to project infrastructure type: enhancement Proposed improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants