From 5620faf7bf8cac5492900469ddec22811b7fbcdb Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Mon, 2 Jul 2018 16:38:39 +0200 Subject: [PATCH] Add unit test for strmatch() Signed-off-by: Joachim Nilsson --- tests/.gitignore | 1 + tests/Makefile.am | 1 + tests/strmatch.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 tests/strmatch.c diff --git a/tests/.gitignore b/tests/.gitignore index 2178a4f..3ba0413 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -12,6 +12,7 @@ pidfile progress rsync str +strmatch tempfile touch tree diff --git a/tests/Makefile.am b/tests/Makefile.am index 8965fcf..c2b6e18 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -15,6 +15,7 @@ TESTS += str TESTS += touch TESTS += progress TESTS += rsync +TESTS += strmatch TESTS += tempfile TESTS += which diff --git a/tests/strmatch.c b/tests/strmatch.c new file mode 100644 index 0000000..67a9dcd --- /dev/null +++ b/tests/strmatch.c @@ -0,0 +1,44 @@ +#include "check.h" +#define SYSLOG_NAMES +#include + +#define SEVERITY_LEVEL { \ + CHOOSE(SEVERITY_NONE, "none"), \ + CHOOSE(SEVERITY_DEBUG, "debug"), \ + CHOOSE(SEVERITY_INFO, "info"), \ + CHOOSE(SEVERITY_NOTICE, "notice"), \ + CHOOSE(SEVERITY_WARNING, "warning"), \ + CHOOSE(SEVERITY_ERR, "err"), \ + CHOOSE(SEVERITY_CRIT, "crit"), \ + CHOOSE(SEVERITY_ALERT, "alert"), \ + CHOOSE(SEVERITY_EMERG, "emerg") \ +} + +#define CHOOSE(a, b) b +const char *levels[] = SEVERITY_LEVEL; + +#undef CHOOSE +#define CHOOSE(a, b) a +typedef enum SEVERITY_LEVEL severity_level_t; + +int main(void) +{ + size_t i; + int num; + struct { + char *str; + int val; + } t[] = { + { "warn", SEVERITY_WARNING }, + { "alert", SEVERITY_ALERT }, + { NULL, 0 } + }; + + for (i = 0; t[i].str; i++) { + num = strmatch(t[i].str, levels); + fail_unless(num >= 0); + fail_unless(num == t[i].val); + } + + return 0; +}