Skip to content

Commit caef856

Browse files
committed
Move automata test to runtest.c
1 parent c59df16 commit caef856

9 files changed

+248
-343
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ runxmlconf.log
9696
stamp-h1
9797
tags
9898
test.out
99-
testAutomata
10099
testModule
101100
testThreads
102101
testapi

CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,6 @@ if(LIBXML2_WITH_TESTS)
505505
runxmlconf
506506
runsuite
507507
testapi
508-
testAutomata
509508
testchar
510509
testdict
511510
testModule

Makefile.am

-26
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ check_PROGRAMS = \
1515
runsuite \
1616
runtest \
1717
runxmlconf \
18-
testAutomata \
1918
testModule \
2019
testThreads \
2120
testapi \
@@ -156,10 +155,6 @@ testThreads_CFLAGS = $(AM_CFLAGS) $(THREAD_CFLAGS)
156155
testThreads_DEPENDENCIES = $(DEPS)
157156
testThreads_LDADD= $(BASE_THREAD_LIBS) $(THREAD_LIBS) $(LDADDS)
158157

159-
testAutomata_SOURCES=testAutomata.c
160-
testAutomata_DEPENDENCIES = $(DEPS)
161-
testAutomata_LDADD= $(LDADDS)
162-
163158
testModule_SOURCES=testModule.c
164159
testModule_DEPENDENCIES = $(DEPS)
165160
testModule_LDADD= $(LDADDS)
@@ -219,9 +214,6 @@ endif
219214
if WITH_DEBUG_SOURCES
220215
OLD_TESTS += Scripttests
221216
endif
222-
if WITH_REGEXPS_SOURCES
223-
OLD_TESTS += Automatatests
224-
endif
225217
if WITH_SCHEMAS_SOURCES
226218
if WITH_PYTHON
227219
OLD_TESTS += RelaxNGPythonTests SchemasPythonTests
@@ -311,24 +303,6 @@ Catatests : xmlcatalog$(EXEEXT)
311303
grep "MORY ALLO" .memdump | grep -v "MEMORY ALLOCATED : 0"; \
312304
rm -f $(srcdir)/result/catalogs/mycatalog)
313305

314-
Automatatests: testAutomata$(EXEEXT)
315-
@(echo > .memdump)
316-
@echo "## Automata regression tests"
317-
-@(for i in $(srcdir)/test/automata/* ; do \
318-
name=`basename $$i`; \
319-
if [ ! -d $$i ] ; then \
320-
if [ ! -f $(srcdir)/result/automata/$$name ] ; then \
321-
echo New test file $$name ; \
322-
$(CHECKER) $(top_builddir)/testAutomata $$i > $(srcdir)/result/automata/$$name; \
323-
grep "MORY ALLO" .memdump | grep -v "MEMORY ALLOCATED : 0";\
324-
else \
325-
log=`$(CHECKER) $(top_builddir)/testAutomata $$i 2>&1 > result.$$name ; \
326-
grep "MORY ALLO" .memdump | grep -v "MEMORY ALLOCATED : 0";\
327-
diff $(srcdir)/result/automata/$$name result.$$name` ; \
328-
if [ -n "$$log" ] ; then echo $$name result ; echo "$$log" ; fi ; \
329-
rm result.$$name ; \
330-
fi ; fi ; done)
331-
332306
dba100000.xml: dbgenattr.pl
333307
@echo "## generating dba100000.xml"
334308
@($(PERL) $(top_srcdir)/dbgenattr.pl 100000 > dba100000.xml)

doc/apibuild.py

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"testOOMlib.h": "out of memory tester",
3030
"testOOMlib.c": "out of memory tester",
3131
"rngparser.c": "not yet integrated",
32-
"testAutomata.c": "test tool",
3332
"testModule.c": "test tool",
3433
"testThreads.c": "test tool",
3534
"testapi.c": "generated regression tests",

runtest.c

+248-2
Original file line numberDiff line numberDiff line change
@@ -4323,7 +4323,9 @@ regexpTest(const char *filename, const char *result, const char *err,
43234323
char expression[5000];
43244324
int len, ret, res = 0;
43254325

4326-
input = fopen(filename, "r");
4326+
nb_tests++;
4327+
4328+
input = fopen(filename, "rb");
43274329
if (input == NULL) {
43284330
xmlGenericError(xmlGenericErrorContext,
43294331
"Cannot open %s for reading\n", filename);
@@ -4399,7 +4401,246 @@ regexpTest(const char *filename, const char *result, const char *err,
43994401
return(res);
44004402
}
44014403

4402-
#endif
4404+
#endif /* LIBXML_REGEXPS_ENABLED */
4405+
4406+
#ifdef LIBXML_AUTOMATA_ENABLED
4407+
/************************************************************************
4408+
* *
4409+
* Automata tests *
4410+
* *
4411+
************************************************************************/
4412+
4413+
static int scanNumber(char **ptr) {
4414+
int ret = 0;
4415+
char *cur;
4416+
4417+
cur = *ptr;
4418+
while ((*cur >= '0') && (*cur <= '9')) {
4419+
ret = ret * 10 + (*cur - '0');
4420+
cur++;
4421+
}
4422+
*ptr = cur;
4423+
return(ret);
4424+
}
4425+
4426+
static int
4427+
automataTest(const char *filename, const char *result,
4428+
const char *err ATTRIBUTE_UNUSED, int options ATTRIBUTE_UNUSED) {
4429+
FILE *input, *output;
4430+
char *temp;
4431+
char expr[5000];
4432+
int len;
4433+
int ret;
4434+
int i;
4435+
int res = 0;
4436+
xmlAutomataPtr am;
4437+
xmlAutomataStatePtr states[1000];
4438+
xmlRegexpPtr regexp = NULL;
4439+
xmlRegExecCtxtPtr exec = NULL;
4440+
4441+
nb_tests++;
4442+
4443+
for (i = 0;i<1000;i++)
4444+
states[i] = NULL;
4445+
4446+
input = fopen(filename, "rb");
4447+
if (input == NULL) {
4448+
xmlGenericError(xmlGenericErrorContext,
4449+
"Cannot open %s for reading\n", filename);
4450+
return(-1);
4451+
}
4452+
temp = resultFilename(filename, "", ".res");
4453+
if (temp == NULL) {
4454+
fprintf(stderr, "Out of memory\n");
4455+
fatalError();
4456+
}
4457+
output = fopen(temp, "wb");
4458+
if (output == NULL) {
4459+
fprintf(stderr, "failed to open output file %s\n", temp);
4460+
free(temp);
4461+
return(-1);
4462+
}
4463+
4464+
am = xmlNewAutomata();
4465+
if (am == NULL) {
4466+
xmlGenericError(xmlGenericErrorContext,
4467+
"Cannot create automata\n");
4468+
fclose(input);
4469+
return(-1);
4470+
}
4471+
states[0] = xmlAutomataGetInitState(am);
4472+
if (states[0] == NULL) {
4473+
xmlGenericError(xmlGenericErrorContext,
4474+
"Cannot get start state\n");
4475+
xmlFreeAutomata(am);
4476+
fclose(input);
4477+
return(-1);
4478+
}
4479+
ret = 0;
4480+
4481+
while (fgets(expr, 4500, input) != NULL) {
4482+
if (expr[0] == '#')
4483+
continue;
4484+
len = strlen(expr);
4485+
len--;
4486+
while ((len >= 0) &&
4487+
((expr[len] == '\n') || (expr[len] == '\t') ||
4488+
(expr[len] == '\r') || (expr[len] == ' '))) len--;
4489+
expr[len + 1] = 0;
4490+
if (len >= 0) {
4491+
if ((am != NULL) && (expr[0] == 't') && (expr[1] == ' ')) {
4492+
char *ptr = &expr[2];
4493+
int from, to;
4494+
4495+
from = scanNumber(&ptr);
4496+
if (*ptr != ' ') {
4497+
xmlGenericError(xmlGenericErrorContext,
4498+
"Bad line %s\n", expr);
4499+
break;
4500+
}
4501+
if (states[from] == NULL)
4502+
states[from] = xmlAutomataNewState(am);
4503+
ptr++;
4504+
to = scanNumber(&ptr);
4505+
if (*ptr != ' ') {
4506+
xmlGenericError(xmlGenericErrorContext,
4507+
"Bad line %s\n", expr);
4508+
break;
4509+
}
4510+
if (states[to] == NULL)
4511+
states[to] = xmlAutomataNewState(am);
4512+
ptr++;
4513+
xmlAutomataNewTransition(am, states[from], states[to],
4514+
BAD_CAST ptr, NULL);
4515+
} else if ((am != NULL) && (expr[0] == 'e') && (expr[1] == ' ')) {
4516+
char *ptr = &expr[2];
4517+
int from, to;
4518+
4519+
from = scanNumber(&ptr);
4520+
if (*ptr != ' ') {
4521+
xmlGenericError(xmlGenericErrorContext,
4522+
"Bad line %s\n", expr);
4523+
break;
4524+
}
4525+
if (states[from] == NULL)
4526+
states[from] = xmlAutomataNewState(am);
4527+
ptr++;
4528+
to = scanNumber(&ptr);
4529+
if (states[to] == NULL)
4530+
states[to] = xmlAutomataNewState(am);
4531+
xmlAutomataNewEpsilon(am, states[from], states[to]);
4532+
} else if ((am != NULL) && (expr[0] == 'f') && (expr[1] == ' ')) {
4533+
char *ptr = &expr[2];
4534+
int state;
4535+
4536+
state = scanNumber(&ptr);
4537+
if (states[state] == NULL) {
4538+
xmlGenericError(xmlGenericErrorContext,
4539+
"Bad state %d : %s\n", state, expr);
4540+
break;
4541+
}
4542+
xmlAutomataSetFinalState(am, states[state]);
4543+
} else if ((am != NULL) && (expr[0] == 'c') && (expr[1] == ' ')) {
4544+
char *ptr = &expr[2];
4545+
int from, to;
4546+
int min, max;
4547+
4548+
from = scanNumber(&ptr);
4549+
if (*ptr != ' ') {
4550+
xmlGenericError(xmlGenericErrorContext,
4551+
"Bad line %s\n", expr);
4552+
break;
4553+
}
4554+
if (states[from] == NULL)
4555+
states[from] = xmlAutomataNewState(am);
4556+
ptr++;
4557+
to = scanNumber(&ptr);
4558+
if (*ptr != ' ') {
4559+
xmlGenericError(xmlGenericErrorContext,
4560+
"Bad line %s\n", expr);
4561+
break;
4562+
}
4563+
if (states[to] == NULL)
4564+
states[to] = xmlAutomataNewState(am);
4565+
ptr++;
4566+
min = scanNumber(&ptr);
4567+
if (*ptr != ' ') {
4568+
xmlGenericError(xmlGenericErrorContext,
4569+
"Bad line %s\n", expr);
4570+
break;
4571+
}
4572+
ptr++;
4573+
max = scanNumber(&ptr);
4574+
if (*ptr != ' ') {
4575+
xmlGenericError(xmlGenericErrorContext,
4576+
"Bad line %s\n", expr);
4577+
break;
4578+
}
4579+
ptr++;
4580+
xmlAutomataNewCountTrans(am, states[from], states[to],
4581+
BAD_CAST ptr, min, max, NULL);
4582+
} else if ((am != NULL) && (expr[0] == '-') && (expr[1] == '-')) {
4583+
/* end of the automata */
4584+
regexp = xmlAutomataCompile(am);
4585+
xmlFreeAutomata(am);
4586+
am = NULL;
4587+
if (regexp == NULL) {
4588+
xmlGenericError(xmlGenericErrorContext,
4589+
"Failed to compile the automata");
4590+
break;
4591+
}
4592+
} else if ((expr[0] == '=') && (expr[1] == '>')) {
4593+
if (regexp == NULL) {
4594+
fprintf(output, "=> failed not compiled\n");
4595+
} else {
4596+
if (exec == NULL)
4597+
exec = xmlRegNewExecCtxt(regexp, NULL, NULL);
4598+
if (ret == 0) {
4599+
ret = xmlRegExecPushString(exec, NULL, NULL);
4600+
}
4601+
if (ret == 1)
4602+
fprintf(output, "=> Passed\n");
4603+
else if ((ret == 0) || (ret == -1))
4604+
fprintf(output, "=> Failed\n");
4605+
else if (ret < 0)
4606+
fprintf(output, "=> Error\n");
4607+
xmlRegFreeExecCtxt(exec);
4608+
exec = NULL;
4609+
}
4610+
ret = 0;
4611+
} else if (regexp != NULL) {
4612+
if (exec == NULL)
4613+
exec = xmlRegNewExecCtxt(regexp, NULL, NULL);
4614+
ret = xmlRegExecPushString(exec, BAD_CAST expr, NULL);
4615+
} else {
4616+
xmlGenericError(xmlGenericErrorContext,
4617+
"Unexpected line %s\n", expr);
4618+
}
4619+
}
4620+
}
4621+
fclose(output);
4622+
fclose(input);
4623+
if (regexp != NULL)
4624+
xmlRegFreeRegexp(regexp);
4625+
if (exec != NULL)
4626+
xmlRegFreeExecCtxt(exec);
4627+
if (am != NULL)
4628+
xmlFreeAutomata(am);
4629+
4630+
ret = compareFiles(temp, result);
4631+
if (ret) {
4632+
fprintf(stderr, "Result for %s failed in %s\n", filename, result);
4633+
res = 1;
4634+
}
4635+
if (temp != NULL) {
4636+
unlink(temp);
4637+
free(temp);
4638+
}
4639+
4640+
return(res);
4641+
}
4642+
4643+
#endif /* LIBXML_AUTOMATA_ENABLED */
44034644

44044645
/************************************************************************
44054646
* *
@@ -4607,6 +4848,11 @@ testDesc testDescriptions[] = {
46074848
{ "Regexp regression tests" ,
46084849
regexpTest, "./test/regexp/*", "result/regexp/", "", ".err",
46094850
0 },
4851+
#endif
4852+
#if defined(LIBXML_AUTOMATA_ENABLED)
4853+
{ "Automata regression tests" ,
4854+
automataTest, "./test/automata/*", "result/automata/", "", NULL,
4855+
0 },
46104856
#endif
46114857
{NULL, NULL, NULL, NULL, NULL, NULL, 0}
46124858
};

0 commit comments

Comments
 (0)