Skip to content

Conversation

@pks-t
Copy link
Member

@pks-t pks-t commented Apr 15, 2025

The macros we have to assert the state of integers are lacking due to
multiple reasons:

  • We explicitly cast the values to int, which causes problems in
    case the values do not fit into an int. Furthermore, this hides
    issues in case one accidentally passes the wrong type to this macro.

  • We only have macros to compare integers for equality. Notably
    lacking are constructs to compare for non-equality, like "less
    than" or "less or equal".

  • We only have macros to compare signed integers, but lack macros to
    check for unsigned macros.

Fix this issue by introducing clar__assert_compare_i() as well as an
equivalent for unsigned types, clar__assert_compare_u(). These macros:

  • Get intmax_t and uintmax_t as input, respectively, which allows
    us to get rid of the explicit casts. Instead, the compiler can now
    verify types for us and print warnings when there is an incompatible
    type.

  • Get an enum as input for the various different comparisons. Like
    this we don't only support equality checks, but also all the other
    checks one would typically expect.

Adapt existing macros to use clar__assert_compare_i(). Furthermore,
introduce new macros that supersede the older variants and which allow
the caller to perform integer comparisons.

@pks-t
Copy link
Member Author

pks-t commented Apr 15, 2025

@ethomson Would you mind doing a review of this MR? 🙏

@pks-t pks-t force-pushed the pks-integer-comparisons branch from 9e99603 to e818e71 Compare April 15, 2025 11:56
@phillipwood
Copy link
Contributor

@pks-t Thanks for working on this, it will be a very welcome improvement. I've left a couple of small comments but this all looks sensible to me.

@pks-t pks-t force-pushed the pks-integer-comparisons branch from e818e71 to eeeeca8 Compare December 3, 2025 20:04
@pks-t
Copy link
Member Author

pks-t commented Dec 3, 2025

@phillipwood Thanks for your review! I've rebased the PR now to fix conflicts and have applied your feedback, except for the fmt part.

pks-t added 3 commits December 3, 2025 21:10
Introduce a portable wrapper for `vsnprintf()`. This function will be
used in subsequent commits.
While we already have `clar_fail()`, this function does not allow the
caller to provide a printf(3p)-style formatting string. This makes it
somewhat hard at times to give a proper explanation _why_ a specific
test has failed.

Introduce `clar_failf()` to plug this gap. Note that the core of this
new functionality is implemented in `clar__failv()`, which receives a
`va_list` as input. This indirection isn't needed right now, but we will
add more callers in subsequent commits.
The macros we have to assert the state of integers are lacking due to
multiple reasons:

  - We explicitly cast the values to `int`, which causes problems in
    case the values do not fit into an `int`. Furthermore, this hides
    issues in case one accidentally passes the wrong type to this macro.

  - We only have macros to compare integers for equality. Notably
    lacking are constructs to compare for non-equality, like "less
    than" or "less or equal".

  - We only have macros to compare _signed_ integers, but lack macros to
    check for _unsigned_ macros.

Fix this issue by introducing `clar__assert_compare_i()` as well as an
equivalent for unsigned types, `clar__assert_compare_u()`. These macros:

  - Get `intmax_t` and `uintmax_t` as input, respectively, which allows
    us to get rid of the explicit casts. Instead, the compiler can now
    verify types for us and print warnings when there is an incompatible
    type.

  - Get an enum as input for the various different comparisons. Like
    this we don't only support equality checks, but also all the other
    checks one would typically expect.

Adapt existing macros to use `clar__assert_compare_i()`. Furthermore,
introduce new macros that supersede the older variants and which allow
the caller to perform integer comparisons.
@pks-t pks-t force-pushed the pks-integer-comparisons branch from eeeeca8 to 06f0c35 Compare December 3, 2025 20:11
@pks-t pks-t requested a review from phillipwood December 3, 2025 20:14
Copy link
Contributor

@phillipwood phillipwood left a comment

Choose a reason for hiding this comment

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

@pks-t The range-diff looks good, thanks again for adding these assertions.

@pks-t
Copy link
Member Author

pks-t commented Dec 4, 2025

Thanks, @phillipwood! Let's merge this then.

@pks-t pks-t merged commit 49a20bf into clar-test:main Dec 4, 2025
7 checks passed
@pks-t pks-t deleted the pks-integer-comparisons branch December 4, 2025 11:31
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