File: strmatch.c

package info (click to toggle)
fisg 0.3.8-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 640 kB
  • ctags: 284
  • sloc: ansic: 2,020; makefile: 52; sh: 16
file content (131 lines) | stat: -rw-r--r-- 2,614 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Test suite for th_str*match() functions
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "th_util.h"

typedef struct {
	char *testStr;
	BOOL testResult;
} t_test;

char	*testStr1 = "foo.foo.bar..bar.baz";

t_test testList1[] = {
	{ "*.baz",	TRUE },
	{ "*b*.BAZ",	TRUE },
	{ "*b*.baz",	TRUE },
	{ "*A*",	TRUE },
	{ "f*",		TRUE },
	{ "f*z",	TRUE },
	{ "*",		TRUE },
	{ "?*",		TRUE },
	{ "*?",		TRUE },
	{ "*a?",	TRUE },
	{ "*z?",	FALSE },
	{ "*az",	TRUE },
	{ "*ar?",	FALSE },
	{ "*bar*",	TRUE },
	{ "*b?r*",	TRUE },
	{ "*.baz*",	TRUE },
	{ "*foo*foo*bar*", TRUE },
	{ "*foo*bar.baz", TRUE },
	{ "f*z*", 	TRUE },
	{ "*z*", 	TRUE },
	{ "?*z",	TRUE },
};
const int ntestList1 = (sizeof(testList1) / sizeof(t_test));

t_test testList2[] = {
	{ "*.baz",	TRUE },
	{ "*b*.BAZ",	FALSE },
	{ "*b*.baz",	TRUE },
	{ "*Ar*",	FALSE },
};
const int ntestList2 = (sizeof(testList2) / sizeof(t_test));


int nTest = 0, nFailed = 0, nPassed = 0;

void TST(char *str, char *pattern, BOOL testResult)
{
 BOOL iResult = th_strcasematch(str, pattern);

 printf("-------------------------\n");
 printf("TEST #%i: [\"%s\" -- \"%s\"], RESULT: %s\n",
 	nTest++,
 	str,
 	pattern,
	(testResult == iResult) ? "PASSED" : "FAILED"
	);

 if (iResult == testResult)
 	nPassed++;
 	else
 	nFailed++;
}


void doTestList(t_test testList[], int ntestList, char *testStr)
{
 int i;
 BOOL iResult;

 for (i = 0; i < ntestList; i++)
	{
	printf("-------------------------\n");
	printf("TEST #%i: [\"%s\" -- \"%s\"]\n",
		nTest,
	 	testStr,
	 	testList[i].testStr);


	iResult = th_strcasematch(testStr, testList[i].testStr);

	printf("TEST #%i RESULT: %s\n",
		nTest++,
		(testList[i].testResult == iResult) ? "PASSED" : "FAILED"
		);

	if (testList[i].testResult == iResult)
		nPassed++;
		else
		nFailed++;
	}
}


int main(void)
{
 doTestList(testList1, ntestList1, testStr1);

 TST("cras",	"*cras*",	TRUE);
 TST("cras",	"*cras",	TRUE);
 TST("cras",	"cras*",	TRUE);
 TST("cras",	"cras",		TRUE);
 TST(" cras",	"cras",		FALSE);
 TST("cras",	" cras",	FALSE);
 TST(" cras",	" cras ",	FALSE);
 TST(" cras ",	" cras",	FALSE);

 TST("cras_",	"*cras*",	TRUE);
 TST("cra",	"*cras*",	FALSE);
 TST("cras",	"cra*",		TRUE);
 TST("cras",	"*r*",		TRUE);
 TST("cras",	"*ra*",		TRUE);
 TST("cras",	"c*s",		TRUE);
 TST("cras",	"c*a",		FALSE);
 TST("cras",	"*a?",		TRUE);
 TST("cras",	"*r?*",		TRUE);
 TST("cras",	"*r??*",	TRUE);
 TST("cras",	"**",		TRUE);
 TST("cras",	"*_*",		FALSE);
 TST("cras_",	"*_*",		TRUE);
 TST("cras_",	"*_",		TRUE);
 
 printf("Tests passed: %i, failed: %i\n", nPassed, nFailed);

 return (nFailed > 0) ? -1 : 0;
}