ThrowTheSwitch / Unity

Simple Unit Testing for C
ThrowTheSwitch.org
MIT License
4k stars 967 forks source link

Feature Request: Support asymmetric range assertions #740

Open jefflongo opened 2 months ago

jefflongo commented 2 months ago

Currently there is no mechanism to assert a number is between two other numbers aside from using TEST_ASSERT_X_WITHIN supports only symmetric ranges. I propose an assertion like TEST_ASSERT_X_WITHIN_RANGE(lower, upper, actual).

mvandervoord commented 2 months ago

memo to myself: There is already greater than, less than, etc... therefore no additional actual functions are needed. A macro could cover this with back to back existing calls.

jefflongo commented 2 months ago

FYI, this is how I implemented it for my own uses:

#define _TEST_ASSERT_WITHIN_RANGE_MESSAGE(type, lower, upper, actual, message)                     \
    do {                                                                                           \
        TEST_ASSERT_GREATER_OR_EQUAL_##type##_MESSAGE(lower, actual, message);                     \
        TEST_ASSERT_LESS_OR_EQUAL_##type##_MESSAGE(upper, actual, message);                        \
    } while (0)

#define TEST_ASSERT_WITHIN_RANGE_INT8_MESSAGE(lower, upper, actual, message)                       \
    _TEST_ASSERT_WITHIN_RANGE_MESSAGE(INT8, lower, upper, actual, message)