Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output expected value on assertion #350

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

jimmy-mcelwain
Copy link
Collaborator

@jimmy-mcelwain jimmy-mcelwain commented Dec 30, 2024

In reference to #268.

I am going to make this a draft because I definitely want feedback and expect that I will have to make changes.

The second most recent commit (as of now), 6668a4d had a function for each assertion, but there was a lot of code duplication. 7c68007 changed that by using more macros.

…ification/more detailed printouts, and change currently existing assertions to match.
Add error codes that break up some larger conditions so that the expected values can be logged appropriately
@jimmy-mcelwain jimmy-mcelwain marked this pull request as draft December 30, 2024 22:22
@jimmy-mcelwain jimmy-mcelwain force-pushed the OutputExpectedValueOnAssertion branch from 84e5e92 to 7c68007 Compare December 30, 2024 22:26
FOREVER \
{ \
Ros_Debug_BroadcastMsg("MotoROS2 Assertion Failure: %s (subcode: %d)", motoRos_error_handling_assert_msg, subCodeOnFailure); \
Ros_Debug_BroadcastMsg("Actual: %s == %d", actualName, (actual)); \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need two specialised versions for this?

I'm sort-of hoping/expecting we can use token pasting / concatenation and just build the expected vs actual string part of the msg using the pre-processor. I don't know whether that's true though, I expect you've tried.

Comment on lines +246 to +250
char motoRos_error_handling_assert_msg[ERROR_MSG_MAX_SIZE] = {0}; \
snprintf(motoRos_error_handling_assert_msg, ERROR_MSG_MAX_SIZE, msgFmtOnFailure, ##__VA_ARGS__); \
Ros_Controller_SetIOState(IO_FEEDBACK_FAILURE, TRUE); \
Ros_Controller_SetIOState(IO_FEEDBACK_INITIALIZATION_DONE, FALSE); \
mpSetAlarm(ALARM_ASSERTION_FAIL, motoRos_error_handling_assert_msg, subCodeOnFailure); \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering whether this is what we'd want to do. With the current implementation, we'd be adding both mpSetAlarm(..) and IO state management code to all call-sites of this macro.

It'd be invisible -- because it's part of the macro body -- but it would still be there. At the very least it increases binary size, but it also seems like we should be able to call a function to which we delegate this responsibility -- didn't we have that before?


#define motoRos_ASSERT_GENERIC_COMPARISON_INT_MESSAGE(actual, benchmark, subCodeOnFailure, actualName, benchmarkName, op, msgFmtOnFailure, ...) \
do { \
if (!((actual) op (benchmark))) \
Copy link
Collaborator

@gavanderhoorn gavanderhoorn Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice use of token pasting / concatenation, but I might suggest keeping the actual 'ops' (ie: the conditionals) in the macros that call this one.

That would both make those macros easier to read (as we just see them doing something like if (!(x > y)) assert(my_msg)) and it would reduce the responsibilities of this macro (as it now only needs to log the message and raise an alarm (but see my other comment about this)).

Less responsibilities would simplify the code (less nesting), which is always good I feel.

Copy link
Collaborator

@gavanderhoorn gavanderhoorn Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't immediately find it, but isn't it possible to simplify things a bit by considering the conditional expression a token and evaluate it both as an actual expression as well as use it as part of the message?

So we could end up with something like: motoRos_Assert(x < y, "my message if not true", ...) and x < y would end up in the message as well, despite being an expression. That would reduce the need for specialised macros, while still getting us what we're after.


Edit: something like this:

#define ASSERT_WITH_MSG_(EXPR, msg) \
{ \
  if (!(EXPR)) \
  { \
    printf("ERROR: %s: '%s'", msg, #EXPR ); \
  } \
} while (0)

For ASSERT_WITH_MSG_(1 < 1, "not smaller than") this prints:

ERROR: not smaller than: '1 < 1'

Edit 2: see frameworks like nemequ/munit fi, which do something similar (here fi).


Edit 3: ah, but for expressions which reference variables you'd only get the name, not the value: ASSERT_WITH_MSG_(some_struct.field0 < 3, "...") only prints:

ERROR: ...: 'some_struct.field0 < 3'

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.

2 participants