Skip to content

Add unit tests for cansdo module#49

Merged
jsphuebner merged 2 commits intomasterfrom
copilot/write-tests-for-cansdo-module
Apr 27, 2026
Merged

Add unit tests for cansdo module#49
jsphuebner merged 2 commits intomasterfrom
copilot/write-tests-for-cansdo-module

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 27, 2026

No tests existed for the CanSdo SDO protocol implementation. This adds 26 test cases covering the full request/reply surface.

New files

  • test/test_cansdo.cpp — test suite using the same UnitTest/CanStub pattern as test_canmap.cpp
  • test/test-include/errormessage_prj.h — minimal project header (ERROR_MESSAGE_LIST empty, ERROR_BUF_SIZE=10) required to compile errormessage.cpp in the host test environment

Makefile

Added test_cansdo.o cansdo.o errormessage.o printf.o to OBJS.

Test coverage

Area Tests
Parameter read/write via index 0x2000 value round-trip, out-of-range → SDO_ERR_RANGE, invalid index → SDO_ERR_INVIDX
Parameter access by UID (0x21xx) read/write, unknown UID → abort
CAN map add TX/RX (0x3000/0x3001) full 3-step flow; invalid COB-ID and unknown UID error paths
CAN map read (0x3100/0x3180) COB-ID, signal position, gain/offset; empty map → abort
CAN map delete (0x3100) delete entry, confirm removal
Error objects (0x5003/0x5004) read returns reply; write → abort
User-space SDO unknown index populates pending frame; SendSdoReply clears it
Node ID wrong ID silently ignored; SetNodeId activates new ID
String upload (0x5001) GetPrintRequest() set on read

Include ordering workaround

The libopeninv printf.h declares printf/sprintf without extern "C", which conflicts with system stdio.h (pulled in via <iostream>) on modern host compilers. The test file resolves this by including <cstdio> and forward-declaring IPutChar before cansdo.h, then setting PRINTF_H_INCLUDED to prevent the problematic header from being re-processed:

#include <cstdio>
class IPutChar { public: virtual void PutChar(char c) = 0; };
#define PRINTF_H_INCLUDED

#include "cansdo.h"
// ... remaining includes

RX map verification

Because the test stub stores only the last-registered CanCallback (CanSdo), delivering a raw CAN frame via canStub->HandleRx would not invoke CanMap's receive path. RX mapping acceptance is therefore verified with canMap->FindMap() instead.

Copilot AI and others added 2 commits April 27, 2026 10:03
Agent-Logs-Url: https://github.com/jsphuebner/libopeninv/sessions/64be9f23-b2d8-422e-8274-172a7d783561

Co-authored-by: jsphuebner <3882041+jsphuebner@users.noreply.github.com>
@jsphuebner
Copy link
Copy Markdown
Owner

@davefiddes What do you think about these?

@davefiddes
Copy link
Copy Markdown
Contributor

Not bad. I've not found AI generated tests up till now very good. It usually goes off and generates fragile nonsense. These look OK. Not massively deep but a solid basis for any future regressions.

@jsphuebner jsphuebner marked this pull request as ready for review April 27, 2026 12:15
@jsphuebner jsphuebner merged commit 7949792 into master Apr 27, 2026
2 checks passed
@jsphuebner
Copy link
Copy Markdown
Owner

Alright, thanks for checking!

@jsphuebner jsphuebner deleted the copilot/write-tests-for-cansdo-module branch April 27, 2026 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants