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

Add *_PRED_* tests: A predicate test that uses a provided function for comparing values #146

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,14 @@ UTEST(c, Near) {
}

UTEST(c, Todo) { UTEST_SKIP("Not yet implemented!"); }

int test_func_int(int a, int b)
{
return a<b;
}

UTEST(c, Predicate) {
EXPECT_PRED(1, 2, test_func_int);
ASSERT_PRED(1, 2, test_func_int);
}

6 changes: 6 additions & 0 deletions test/test11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,9 @@ UTEST(cpp11, EnumClass) {
#pragma clang diagnostic pop
#endif
#endif

UTEST(cpp, Predicate) {
EXPECT_PRED(1, 2, [](int a, int b){return a<b;});
EXPECT_PRED(1.0f, 2.0f, [](float a, float b){float res = a-b; return res > -1.00001 && res < -0.99999 ;});
ASSERT_PRED(1, 2, [](int a, int b){return a<b;});
}
77 changes: 77 additions & 0 deletions utest.h
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,83 @@ utest_strncpy_gcc(char *const dst, const char *const src, const size_t size) {
UTEST_EXCEPTION_WITH_MESSAGE(x, exception_type, exception_message, msg, 1)
#endif


#if defined(__clang__)
#define UTEST_PRED(x, y, cond, msg, is_assert) \
UTEST_SURPRESS_WARNING_BEGIN do { \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wlanguage-extension-token\"") \
_Pragma("clang diagnostic ignored \"-Wc++98-compat-pedantic\"") \
_Pragma("clang diagnostic ignored \"-Wfloat-equal\"") \
UTEST_AUTO(x) xEval = (x); \
UTEST_AUTO(y) yEval = (y); \
if (!(cond((xEval),(yEval)))) { \
_Pragma("clang diagnostic pop") \
UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Actual : "); \
utest_type_printer(xEval); \
UTEST_PRINTF(" vs "); \
utest_type_printer(yEval); \
UTEST_PRINTF("\n"); \
if (strlen(msg) > 0) { \
UTEST_PRINTF(" Message : %s\n", msg); \
} \
*utest_result = UTEST_TEST_FAILURE; \
if (is_assert) { \
return; \
} \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#elif defined(__GNUC__) || defined(__TINYC__)
#define UTEST_PRED(x, y, cond, msg, is_assert) \
UTEST_SURPRESS_WARNING_BEGIN do { \
UTEST_AUTO(x) xEval = (x); \
UTEST_AUTO(y) yEval = (y); \
if (!(cond((xEval),(yEval)))) { \
UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Actual : "); \
utest_type_printer(xEval); \
UTEST_PRINTF(" vs "); \
utest_type_printer(yEval); \
UTEST_PRINTF("\n"); \
if (strlen(msg) > 0) { \
UTEST_PRINTF(" Message : %s\n", msg); \
} \
*utest_result = UTEST_TEST_FAILURE; \
if (is_assert) { \
return; \
} \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#else
#define UTEST_PRED(x, y, cond, msg, is_assert) \
UTEST_SURPRESS_WARNING_BEGIN do { \
if (!(cond((x),(y)))) { \
UTEST_PRINTF("%s:%i: Failure", __FILE__, __LINE__); \
if (strlen(msg) > 0) { \
UTEST_PRINTF(" Message : %s", msg); \
} \
UTEST_PRINTF("\n"); \
*utest_result = UTEST_TEST_FAILURE; \
if (is_assert) { \
return; \
} \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#endif

#define EXPECT_PRED(x, y, pred) UTEST_PRED(x, y, pred, "", 0)
#define EXPECT_PRED_MSG(x, y, pred, msg) UTEST_PRED(x, y, pred, msg, 0)
#define ASSERT_PRED(x, y, pred) UTEST_PRED(x, y, pred, "", 1)
#define ASSERT_PRED_MSG(x, y, pred, msg) UTEST_PRED(x, y, pred, msg, 1)


#if defined(__clang__)
#if __has_warning("-Wunsafe-buffer-usage")
#define UTEST_SURPRESS_WARNINGS_BEGIN \
Expand Down
Loading