From 83e9a49847248358b590ea80a88c2208dbd69f57 Mon Sep 17 00:00:00 2001 From: Ed Kellett Date: Fri, 30 Oct 2020 00:55:21 +0000 Subject: [PATCH] Add (some failing) tests for mask matching --- tests/Makefile.am | 2 ++ tests/TESTS | 1 + tests/match1.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 tests/match1.c diff --git a/tests/Makefile.am b/tests/Makefile.am index f4754f63..5c6c67d1 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,4 +1,5 @@ check_PROGRAMS = runtests \ + match1 \ msgbuf_parse1 \ msgbuf_unparse1 \ hostmask1 \ @@ -23,6 +24,7 @@ check_LIBRARIES = tap/libtap.a tap_libtap_a_SOURCES = tap/basic.c tap/basic.h \ tap/float.c tap/float.h tap/macros.h +match1_SOURCES = match1.c msgbuf_parse1_SOURCES = msgbuf_parse1.c msgbuf_unparse1_SOURCES = msgbuf_unparse1.c hostmask1_SOURCES = hostmask1.c diff --git a/tests/TESTS b/tests/TESTS index 31cc388c..a7627c17 100644 --- a/tests/TESTS +++ b/tests/TESTS @@ -1,3 +1,4 @@ +match1 msgbuf_parse1 msgbuf_unparse1 hostmask1 diff --git a/tests/match1.c b/tests/match1.c new file mode 100644 index 00000000..9d118ed6 --- /dev/null +++ b/tests/match1.c @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2020 Ed Kellett + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + * USA + */ +#include +#include +#include +#include +#include "tap/basic.h" + +#include "stdinc.h" +#include "client.h" +#include "match.h" + +#define MSG "%s:%d (%s)", __FILE__, __LINE__, __FUNCTION__ + +struct Client me; + +static void test_match(void) +{ + is_int(0, match("*foo*", "bar"), MSG); + is_int(1, match("*foo*", "foo"), MSG); + + is_int(1, match("*foo*", "xfofoo"), MSG); +} + +static void test_mask_match(void) +{ + is_int(0, mask_match("*foo*", "bar"), MSG); + is_int(1, mask_match("*foo*", "foo"), MSG); + + is_int(1, mask_match("*foo*", "xfofoo"), MSG); + + is_int(1, mask_match("*", "*foo*"), MSG); + is_int(0, mask_match("*foo*", "*"), MSG); + is_int(1, mask_match("*", "*"), MSG); + is_int(0, mask_match("?", "*"), MSG); + is_int(1, mask_match("*?", "*?"), MSG); + is_int(1, mask_match("?*", "*?"), MSG); + is_int(1, mask_match("*?*?*?*", "*?????*"), MSG); + is_int(0, mask_match("*??*??*??*", "*?????*"), MSG); + + is_int(1, mask_match("?*", "*a"), MSG); + is_int(1, mask_match("???*", "*a*a*a"), MSG); + is_int(0, mask_match("???*", "*a*a*"), MSG); + + is_int(0, mask_match("??", "a"), MSG); + is_int(1, mask_match("??", "aa"), MSG); + is_int(0, mask_match("??", "aaa"), MSG); +} + +int main(int argc, char *argv[]) +{ + plan_lazy(); + + test_match(); + test_mask_match(); + + return 0; +}