summaryrefslogtreecommitdiffstats
path: root/src/test/test-macro.c
diff options
context:
space:
mode:
authorJan Janssen <medhefgo@web.de>2021-11-05 12:53:09 +0100
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2021-11-05 16:27:41 +0100
commitd874a13efc51fe49c8870542fa1c9a64ca7e5e93 (patch)
treef2be84f384bb05c9b284f3ff8bb5f7cc79a95cb8 /src/test/test-macro.c
parentMerge pull request #20056 from calestyo/split-up-sysusers.d-systemd.conf.in (diff)
downloadsystemd-d874a13efc51fe49c8870542fa1c9a64ca7e5e93.tar.xz
systemd-d874a13efc51fe49c8870542fa1c9a64ca7e5e93.zip
test: Add test for flag macros
Diffstat (limited to '')
-rw-r--r--src/test/test-macro.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/test/test-macro.c b/src/test/test-macro.c
index 495221c1d5..6db06d80c6 100644
--- a/src/test/test-macro.c
+++ b/src/test/test-macro.c
@@ -336,6 +336,75 @@ static void test_align_to(void) {
assert_cc(__builtin_types_compatible_p(typeof(CONST_ALIGN_TO(SIZE_MAX, 512)), void));
}
+static void test_flags(void) {
+ enum {
+ F1 = 1 << 0,
+ F2 = 1 << 1,
+ F3 = 1 << 2,
+ F_ALL = F1 | F2 | F3
+ };
+ unsigned n, f;
+
+ log_info("/* %s */", __func__);
+
+ assert_se(FLAGS_SET(0, 0));
+ assert_se(FLAGS_SET(F1, F1));
+ assert_se(FLAGS_SET(F1 | F2, F1));
+ assert_se(FLAGS_SET(F1 | F3, F1 | F3));
+ assert_se(FLAGS_SET(F1 | F2 | F3, F_ALL));
+ assert_se(!FLAGS_SET(0, F1));
+ assert_se(!FLAGS_SET(F2, F1));
+ assert_se(!FLAGS_SET(F1 | F2, F3));
+ assert_se(!FLAGS_SET(F1 | F2, F1 | F3));
+ assert_se(!FLAGS_SET(F1 | F2 | F3, ~F_ALL));
+
+ // Check for no double eval.
+ n = F2;
+ f = F1;
+ assert_se(!FLAGS_SET(--n, ++f));
+ assert_se(n == F1);
+ assert_se(f == F2);
+
+ SET_FLAG(n, F3, true);
+ assert_se(n == (F1 | F3));
+ SET_FLAG(n, F2, false);
+ assert_se(n == (F1 | F3));
+ SET_FLAG(n, F3, false);
+ assert_se(n == F1);
+ SET_FLAG(n, F1, true);
+ assert_se(n == F1);
+ SET_FLAG(n, F1 | F3, true);
+ assert_se(n == (F1 | F3));
+ SET_FLAG(n, F_ALL, false);
+ assert_se(n == 0);
+
+ assert_se(UPDATE_FLAG(0, 0, true) == 0);
+ assert_se(UPDATE_FLAG(0, F1, true) == F1);
+ assert_se(UPDATE_FLAG(0, F1 | F2, true) == (F1 | F2));
+ assert_se(UPDATE_FLAG(F1, 0, true) == F1);
+ assert_se(UPDATE_FLAG(F1, F1, true) == F1);
+ assert_se(UPDATE_FLAG(F1, F3, true) == (F1 | F3));
+ assert_se(UPDATE_FLAG(F1, F1 | F3, true) == (F1 | F3));
+ assert_se(UPDATE_FLAG(F1, F_ALL, true) == F_ALL);
+ assert_se(UPDATE_FLAG(0, 0, false) == 0);
+ assert_se(UPDATE_FLAG(0, F1, false) == 0);
+ assert_se(UPDATE_FLAG(0, F1 | F2, false) == 0);
+ assert_se(UPDATE_FLAG(F1, 0, false) == F1);
+ assert_se(UPDATE_FLAG(F1, F1, false) == 0);
+ assert_se(UPDATE_FLAG(F1, F3, false) == F1);
+ assert_se(UPDATE_FLAG(F1, F1 | F3, false) == 0);
+ assert_se(UPDATE_FLAG(F1, F2 | F3, false) == F1);
+ assert_se(UPDATE_FLAG(F1, F_ALL, false) == 0);
+ assert_se(UPDATE_FLAG(F_ALL, F_ALL, false) == 0);
+
+ // Check for no double eval.
+ n = F2;
+ f = F1;
+ assert_se(UPDATE_FLAG(--n, ++f, true) == (F1 | F2));
+ assert_se(n == F1);
+ assert_se(f == F2);
+}
+
int main(int argc, char *argv[]) {
test_setup_logging(LOG_INFO);
@@ -347,6 +416,7 @@ int main(int argc, char *argv[]) {
test_foreach_pointer();
test_ptr_to_int();
test_align_to();
+ test_flags();
return 0;
}