diff options
-rw-r--r-- | src/shared/macro.h | 13 | ||||
-rw-r--r-- | src/test/test-util.c | 11 |
2 files changed, 24 insertions, 0 deletions
diff --git a/src/shared/macro.h b/src/shared/macro.h index 9f5f51cb13..54b641be23 100644 --- a/src/shared/macro.h +++ b/src/shared/macro.h @@ -285,4 +285,17 @@ do { \ #define SET_FLAG(v, flag, b) \ (v) = (b) ? ((v) | (flag)) : ((v) & ~(flag)) +#define IN_SET(x, ...) ({ \ + typeof(x) _x = (x); \ + unsigned _i; \ + bool _found = false; \ + for (_i = 0; _i < sizeof((typeof(_x)[]) { __VA_ARGS__ })/sizeof(typeof(_x)); _i++) \ + if (((typeof(_x)[]) { __VA_ARGS__ })[_i] == _x) { \ + _found = true; \ + break; \ + } \ + _found; \ + }) + + #include "log.h" diff --git a/src/test/test-util.c b/src/test/test-util.c index afc3247ffd..05b2294e24 100644 --- a/src/test/test-util.c +++ b/src/test/test-util.c @@ -568,6 +568,16 @@ static void test_get_files_in_directory(void) { assert_se(get_files_in_directory(".", NULL) >= 0); } +static void test_in_set(void) { + assert_se(IN_SET(1, 1)); + assert_se(IN_SET(1, 1, 2, 3, 4)); + assert_se(IN_SET(2, 1, 2, 3, 4)); + assert_se(IN_SET(3, 1, 2, 3, 4)); + assert_se(IN_SET(4, 1, 2, 3, 4)); + assert_se(!IN_SET(0, 1)); + assert_se(!IN_SET(0, 1, 2, 3, 4)); +} + int main(int argc, char *argv[]) { test_streq_ptr(); test_first_word(); @@ -604,6 +614,7 @@ int main(int argc, char *argv[]) { test_split_pair(); test_fstab_node_to_udev_node(); test_get_files_in_directory(); + test_in_set(); return 0; } |