@@ -4323,7 +4323,9 @@ regexpTest(const char *filename, const char *result, const char *err,
4323
4323
char expression [5000 ];
4324
4324
int len , ret , res = 0 ;
4325
4325
4326
- input = fopen (filename , "r" );
4326
+ nb_tests ++ ;
4327
+
4328
+ input = fopen (filename , "rb" );
4327
4329
if (input == NULL ) {
4328
4330
xmlGenericError (xmlGenericErrorContext ,
4329
4331
"Cannot open %s for reading\n" , filename );
@@ -4399,7 +4401,246 @@ regexpTest(const char *filename, const char *result, const char *err,
4399
4401
return (res );
4400
4402
}
4401
4403
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 */
4403
4644
4404
4645
/************************************************************************
4405
4646
* *
@@ -4607,6 +4848,11 @@ testDesc testDescriptions[] = {
4607
4848
{ "Regexp regression tests" ,
4608
4849
regexpTest , "./test/regexp/*" , "result/regexp/" , "" , ".err" ,
4609
4850
0 },
4851
+ #endif
4852
+ #if defined(LIBXML_AUTOMATA_ENABLED )
4853
+ { "Automata regression tests" ,
4854
+ automataTest , "./test/automata/*" , "result/automata/" , "" , NULL ,
4855
+ 0 },
4610
4856
#endif
4611
4857
{NULL , NULL , NULL , NULL , NULL , NULL , 0 }
4612
4858
};
0 commit comments