From 662997251d4fc43f4155784f9e7df827f193305e Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Thu, 12 Sep 2013 17:42:10 +0200 Subject: Improve interface of np_expect_match() function Replace the three boolean parameters of lib/utils_tcp.c's np_expect_match() function with a single "flags" parameter. --- lib/tests/test_tcp.c | 16 ++++++++-------- lib/utils_tcp.c | 19 ++++++++++--------- lib/utils_tcp.h | 6 +++++- 3 files changed, 23 insertions(+), 18 deletions(-) (limited to 'lib') diff --git a/lib/tests/test_tcp.c b/lib/tests/test_tcp.c index 6cf93946..8e9d43c8 100644 --- a/lib/tests/test_tcp.c +++ b/lib/tests/test_tcp.c @@ -33,21 +33,21 @@ main (int argc, char **argv) server_expect[1] = strdup("bb"); server_expect[2] = strdup("CC"); - ok(np_expect_match("AA bb CC XX", server_expect, server_expect_count, FALSE, TRUE, FALSE) == TRUE, + ok(np_expect_match("AA bb CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == TRUE, "Test matching any string at the beginning (first expect string)"); - ok(np_expect_match("bb AA CC XX", server_expect, server_expect_count, FALSE, TRUE, FALSE) == TRUE, + ok(np_expect_match("bb AA CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == TRUE, "Test matching any string at the beginning (second expect string)"); - ok(np_expect_match("XX bb AA CC XX", server_expect, server_expect_count, FALSE, TRUE, FALSE) == FALSE, + ok(np_expect_match("XX bb AA CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == FALSE, "Test with strings not matching at the beginning"); - ok(np_expect_match("XX CC XX", server_expect, server_expect_count, FALSE, TRUE, FALSE) == FALSE, + ok(np_expect_match("XX CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == FALSE, "Test matching any string"); - ok(np_expect_match("XX", server_expect, server_expect_count, FALSE, FALSE, FALSE) == FALSE, + ok(np_expect_match("XX", server_expect, server_expect_count, 0) == FALSE, "Test not matching any string"); - ok(np_expect_match("XX AA bb CC XX", server_expect, server_expect_count, TRUE, FALSE, FALSE) == TRUE, + ok(np_expect_match("XX AA bb CC XX", server_expect, server_expect_count, NP_MATCH_ALL) == TRUE, "Test matching all strings"); - ok(np_expect_match("XX bb CC XX", server_expect, server_expect_count, TRUE, FALSE, FALSE) == FALSE, + ok(np_expect_match("XX bb CC XX", server_expect, server_expect_count, NP_MATCH_ALL) == FALSE, "Test not matching all strings"); - ok(np_expect_match("XX XX", server_expect, server_expect_count, TRUE, FALSE, FALSE) == FALSE, + ok(np_expect_match("XX XX", server_expect, server_expect_count, NP_MATCH_ALL) == FALSE, "Test not matching any string (testing all)"); diff --git a/lib/utils_tcp.c b/lib/utils_tcp.c index 8589ce67..cf67b116 100644 --- a/lib/utils_tcp.c +++ b/lib/utils_tcp.c @@ -30,26 +30,27 @@ #include "utils_tcp.h" int -np_expect_match(char* status, char** server_expect, int expect_count, int all, int exact_match, int verbose) +np_expect_match(char* status, char** server_expect, int expect_count, int flags) { int match = 0; int i; for (i = 0; i < expect_count; i++) { - if (verbose) + if (flags & NP_MATCH_VERBOSE) printf ("looking for [%s] %s [%s]\n", server_expect[i], - (exact_match) ? "in beginning of" : "anywhere in", + (flags & NP_MATCH_EXACT) ? "in beginning of" : "anywhere in", status); - if ((exact_match && !strncmp(status, server_expect[i], strlen(server_expect[i]))) || - (! exact_match && strstr(status, server_expect[i]))) + if ((flags & NP_MATCH_EXACT && + !strncmp(status, server_expect[i], strlen(server_expect[i]))) || + (!(flags & NP_MATCH_EXACT) && strstr(status, server_expect[i]))) { - if(verbose) puts("found it"); + if(flags & NP_MATCH_VERBOSE) puts("found it"); match += 1; } else - if(verbose) puts("couldn't find it"); + if(flags & NP_MATCH_VERBOSE) puts("couldn't find it"); } - if ((all == TRUE && match == expect_count) || - (! all && match >= 1)) { + if ((flags & NP_MATCH_ALL && match == expect_count) || + (!(flags & NP_MATCH_ALL) && match >= 1)) { return TRUE; } else return FALSE; diff --git a/lib/utils_tcp.h b/lib/utils_tcp.h index b0eb8bee..34b771d6 100644 --- a/lib/utils_tcp.h +++ b/lib/utils_tcp.h @@ -1,4 +1,8 @@ /* Header file for utils_tcp */ +#define NP_MATCH_ALL 0x1 +#define NP_MATCH_EXACT 0x2 +#define NP_MATCH_VERBOSE 0x4 + int np_expect_match(char* status, char** server_expect, int server_expect_count, - int all, int exact_match, int verbose); + int flags); -- cgit v1.2.3