From de202987925346b2f9bb263cc9c75300ad8e31bb Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Sun, 25 Jan 2026 16:14:07 +0000 Subject: feat: add _STR_ variant of _EQ and _NEQ asserts bump to 1.3 --- ls_test.h | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/ls_test.h b/ls_test.h index 557d22c..2077477 100644 --- a/ls_test.h +++ b/ls_test.h @@ -1,6 +1,6 @@ /* Lion's Standard (LS) test harness. * - * Version: 1.2 + * Version: 1.3 * Website: https://libls.org * Repo: https://github.com/libls/test * SPDX-License-Identifier: MIT @@ -20,6 +20,8 @@ * you use asserts other than `ASSERT` (e.g. ASSERT_EQ), and the constructor * attribute __attribute__((destructor)) for automatic test registration. * + * Supports string comparisons using ASSERT_STR_EQ and ASSERT_STR_NEQ. + * * ==== 2. HOW TO USE ==== * * 1. Copy this file into your project and include it: @@ -96,6 +98,8 @@ _func += 6; \ fprintf(stderr, "%s: FAILED: %s (%s:%d)\n", _func, #cond, \ __FILE__, __LINE__); \ + ++lst_fail; \ + return 1; \ } \ } while (0) @@ -117,6 +121,38 @@ } \ } while (0) +#define ASSERT_STR_EQ(a, b) \ + do { \ + __typeof__(a) _a = (a); \ + __typeof__(b) _b = (b); \ + if (strcmp(_a, _b) != 0) { \ + const char* _func = __func__; \ + if (strncmp(_func, "lst_t_", 6) == 0) \ + _func += 6; \ + fprintf(stderr, \ + "%s: FAILED: %s == %s (actual: \"%s\" != \"%s\") (%s:%d)\n", \ + _func, #a, #b, _a, _b, __FILE__, __LINE__); \ + ++lst_fail; \ + return 1; \ + } \ + } while (0) + +#define ASSERT_STR_NEQ(a, b) \ + do { \ + __typeof__(a) _a = (a); \ + __typeof__(b) _b = (b); \ + if (strcmp(_a, _b) == 0) { \ + const char* _func = __func__; \ + if (strncmp(_func, "lst_t_", 6) == 0) \ + _func += 6; \ + fprintf(stderr, \ + "%s: FAILED: %s != %s (actual: \"%s\" == \"%s\") (%s:%d)\n", \ + _func, #a, #b, _a, _b, __FILE__, __LINE__); \ + ++lst_fail; \ + return 1; \ + } \ + } while (0) + #define ASSERT_NEQ(a, b, fmt) \ do { \ __typeof__(a) _a = (a); \ @@ -268,8 +304,8 @@ static int ls_test_main(int argc, char** argv) { } end: - fprintf(stderr, "%d succeeded, %d failed, %d total\n", lst_n - lst_fail, lst_fail, - lst_n); + fprintf(stderr, "%d succeeded, %d failed, %d total\n", lst_n - lst_fail, + lst_fail, lst_n); free(lst_funcs); if (lst_fail > 0) { -- cgit