Skip to content

cr_assert(): Paste condition as a string cause error #550

@MrZLeo

Description

@MrZLeo

In cr_assert(), criterion paste condition as a string directly, preventing use a simple bool expression:

In file included from ../subprojects/criterion/include/criterion/new/assert.h:464,
                 from ../test/rbtree.c:4:
../test/rbtree.c: In functionrb_insert_color_rr_inbalance_impl’:
../subprojects/criterion/include/criterion/new/../internal/new_asserts.h:54:20: error: pasting "CRI_ASSERT_TEST_SPECIFIER_" and "!" does not give a valid preprocessing token
   54 |     CRI_IF_DEFINED(CRI_ASSERT_TEST_SPECIFIER_ ## Spec,  \
      |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~
../subprojects/criterion/include/criterion/new/../internal/new_asserts.h:82:23: note: in expansion of macroCRI_ASSERT_SPECIFIER82 |         cri_cond_un = CRI_ASSERT_SPECIFIER(Condition);                          \
      |                       ^~~~~~~~~~~~~~~~~~~~
../subprojects/criterion/include/criterion/new/../internal/new_asserts.h:173:5: note: in expansion of macroCRI_ASSERT_CALL173 |     CRI_ASSERT_CALL(File, Line, Fail, CR_VA_HEAD(__VA_ARGS__), , CR_VA_TAIL(__VA_ARGS__))
      |     ^~~~~~~~~~~~~~~
../subprojects/criterion/include/criterion/new/../internal/new_asserts.h:179:27: note: in expansion of macrocr_assert_user179 | #define cr_assert(...)    cr_assert_user(__FILE__, __LINE__, criterion_abort_test, __VA_ARGS__)
      |                           ^~~~~~~~~~~~~~
../test/rbtree.c:104:5: note: in expansion of macrocr_assert104 |     cr_assert(!!rb_is_black(&(node2->rb)), "Root node should be black");
      |     ^~~~~~~~~

Here what I am doing is call rb_is_black() and assert it should be true. but in this case, compile error evoked. The reason is clear that we paste the whole condition as a string together with CRI_ASSERT_TEST_SPECIFIER_, and ! is not a valid token.

An ugly workaround is:

    cr_assert(
        eq(i32, true, rb_is_black(&(node2->rb))),
        "Root node should be black"
    );

which makes it a comparison between my function and the true.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions