diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-05-13 14:20:04 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-05-15 14:43:58 -0600 |
commit | b47070eef9384bf4463e8886902beb1d6b3f053a (patch) | |
tree | 6baf07e1620567927df203c945af2b45a226943e /libmisc/tests | |
parent | a8c1de352ec10694b9b971bf111257ac5c9f0b71 (diff) |
libmisc: macro.h: Add LM_FOREACH_PARAM
Diffstat (limited to 'libmisc/tests')
-rw-r--r-- | libmisc/tests/test_macro.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/libmisc/tests/test_macro.c b/libmisc/tests/test_macro.c index 39a8b40..5157820 100644 --- a/libmisc/tests/test_macro.c +++ b/libmisc/tests/test_macro.c @@ -82,6 +82,13 @@ int main() { test_assert(LM_IF(LM_IS_TUPLE( (a) ))(1)(0)); test_assert(LM_IF(LM_IS_TUPLE( (a, b) ))(1)(0)); + test_assert(LM_IF(LM_IS_EMPTY_TUPLE( () ))(1)(0)); + test_assert(LM_IF(LM_IS_EMPTY_TUPLE( 9 ))(0)(1)); + test_assert(LM_IF(LM_IS_EMPTY_TUPLE( a ))(0)(1)); + test_assert(LM_IF(LM_IS_EMPTY_TUPLE( (9) ))(0)(1)); + test_assert(LM_IF(LM_IS_EMPTY_TUPLE( (a) ))(0)(1)); + test_assert(LM_IF(LM_IS_EMPTY_TUPLE( (a, b) ))(0)(1)); + printf("== LM_TUPLES ==============================================\n"); test_assert(LM_IF(LM_TUPLES_IS_NONEMPTY( ))(0)(1)); test_assert(LM_IF(LM_TUPLES_IS_NONEMPTY( () ))(1)(0)); @@ -89,6 +96,47 @@ int main() { test_assert(LM_IF(LM_TUPLES_IS_NONEMPTY( (a)(b) ))(1)(0)); test_assert(LM_IF(LM_TUPLES_IS_NONEMPTY( (a)(b)(c) ))(1)(0)); + printf("== LM_FOREACH_PARAM =======================================\n"); + /* Basic test. */ + { + #define FN(A, B) A "-" #B + const char *str = LM_FOREACH_PARAM(FN, (" "), a, (b), c); + #undef FN + test_assert(strcmp(str, " -a -(b) -c") == 0); + } + + /* Test that it works with the documented limit of params. */ + { + #define X(n) , n + #define FN(n) #n "\n" + const char *str = LM_FOREACH_PARAM_(FN, () XUNDER); + #undef FN + #undef X + #define X(n) #n "\n" + test_assert(strcmp(str, XUNDER) == 0); + #undef X + } + + /* Test that it breaks at documented_limit+1 tuples. */ + { + #define X(n) , n + #define FN(n) n + const char *str = LM_STR_(LM_FOREACH_PARAM_(FN, () XOVER)); + #undef FN + #undef X + /* This comparison is a little extra complicated in + * order to not be sensitive to whitespace in the + * suffix. */ + #define X(n) #n " " + const char *exp_prefix = XUNDER; + #undef X + const char *exp_suffix = "FN(" LM_STR_(OVER) ")_LM_FOREACH_PARAM_ITEM_indirect()(FN,(),())"; + test_assert(strlen(exp_prefix) < strlen(str) && memcmp(exp_prefix, str, strlen(exp_prefix)) == 0); + char *act_suffix = without_spaces(&str[strlen(exp_prefix)]); + test_assert(strcmp(act_suffix, exp_suffix) == 0); + free(act_suffix); + } + printf("== LM_FOREACH_TUPLE =======================================\n"); /* Basic test. */ { |