aboutsummaryrefslogtreecommitdiff
path: root/challenge-099/duncan-c-white/opt2-separateRunX/TestMatch2.pm
blob: 484019e186ebfb5305637816acb4f1d22580a84e (plain)
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
package TestMatch2;

use strict;
use warnings;
use feature 'say';
use Data::Dumper;
use Function::Parameters;
use Test::More;

use Exporter;
our @ISA = qw(Exporter);

our @EXPORT_OK = qw(match_tests); # Export on demand

use lib qw(.);
use Translate qw(pat2apis);

my $debug = 0;

sub setdebug { my($d) = @_; $debug = $d; Translate::setdebug($d); }


#
# match_tests( $patmatch );
#	Do a load of match tests, using the $patmatch->() function,
#	really using the pattern this time.  we compile each pattern into
#	a api list, then string match the string against the api list.
#
fun match_tests( $patmatch )
{
	# format of each match test: S:P:expmatch:expmt
	my @matchtests = (
		"abcde:abcde:1:",
		"abcdef:abcde:0:",
		"abcde:abcdef:0:",
		"abcde:*:1:abcde",
		"abcde:a*e:1:bcd",
		"abcde:abc*de:1:__empty__",
		"abcde:a*d:0:",
		"abcde:?b*d:0:",
		"abcde:?b*e:1:a,cd",
		"abcde:a*c?e:1:b,d",

		# and here some of my own examples..
		"hellotherehowareyou:*ll*u:1:he,otherehowareyo",
		"hellu:*ll*u:1:he,__empty__",
		"hellou:*ll*u:1:he,o",
		"helloyu:*ll*u:1:he,oy",
		"helloyou:*ll*u:1:he,oyo",
		"hellohellohelloyou:*ll*u:1:he,ohellohelloyo",

		"hlo:h*?l*:0:",
		"hel:h*?l*:1:__empty__,e,__empty__",
		"helo:h*?l*:1:__empty__,e,o",
		"hello:h*?l*:1:__empty__,e,lo",
		"heello:h*?l*:1:e,e,lo",
		"heauellooo:h*?l*:1:eau,e,looo",

		"mississippi:*s*:1:mi,sissippi",
		"mississippi:*ss*:1:mi,issippi",
		"mississippi:*ss*s*:1:mi,i,sippi",
		"mississippi:*ss*ss*:1:mi,i,ippi",
		"mississippi:*ss*ss*p*:1:mi,i,i,pi",
		"mississippi:*ss*ss*pp*:1:mi,i,i,i",
		"mississippi:*ss*ss*p?*:1:mi,i,i,p,i",
		"mississippi:*ss*ss*?p?*:1:mi,i,,i,p,i",
		"mississippi:*is*:1:m,sissippi",
		"mississippi:*mis*:1:,sissippi",
		"mississippi:*mi?*:1:,s,sissippi",
	);

	#say "matchtests=". Dumper(\@matchtests);
	foreach my $test (@matchtests)
	{
		#say "test $test";
		my( $s, $p, $expmatch, $expmts ) = split( /:/, $test );
		$expmatch //= '0';
		my @expectedmt = map { /^__empty__$/ ? '' : $_ }
			split(/,/,$expmts);
		my @api = pat2apis( $p );
		say "apis:" if $debug;
		say join("\n", map { "  $_" } @api ) if $debug;

		my( $match, @mt ) = $patmatch->( $s, @api );
		#say "p=$p, s=$s, match=$match, mt=".Dumper(\@mt);
		is( $match, $expmatch, "match($s,$p)=$expmatch" );
		if( $match )
		{
			my $nmatch = @expectedmt;
			is( scalar(@mt), $nmatch,
				"match($s,$p).#mt==$nmatch" );
			foreach my $i (0..$#mt)
			{
				is( $mt[$i], $expectedmt[$i],
				    "match($s,$p).mt[$i]==$expectedmt[$i]" );
			}
		}
	}
}


1;