aboutsummaryrefslogtreecommitdiff
path: root/challenge-099/duncan-c-white/opt3-rewrite/testextract.pl
blob: 16885dd1b69a58b34bbb921d5d3aca06f3f532c5 (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
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
132
133
134
135
136
137
138
#!/usr/bin/perl
#
# scaffolding test program: test Pattern Match's
# extractmatchedtext
#

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

our $debug=0;
our $test=0;

use lib qw(.);
use Tuple qw(tuple);
use PatternMatch qw(extractmatchedtext);

die "Usage: textextract [--debug]\n"
	unless GetOptions( "debug" => \$debug ) && @ARGV==0;
PatternMatch::setdebug( $debug );

if( 0 )
{
	my $word = "mississippi";
	my @is = ( tuple('ss',2), tuple('s?p',6) );
	say "is $_" for @is;
	say "$word:";
	my @matchtext = extractmatchedtext( $word, 0, 0, \@is );

	foreach my $i (0..$#matchtext)
	{
		say " match text $i: $matchtext[$i]";
	}
}

dotests();

done_testing();

exit 0;


#
# dotests();
#	Do a load of tests.
#
sub dotests
{
	# format of each match test:
	# str:pat:islands:startposes:firststar:laststar:expmts
	my @matchtests = (
		"abcde:abcde:abcde:0:f:f:",
		"abcde:*:::t:t:abcde",
		"abcde:a*e:a,e:0,4:f:f:bcd",
		"abcde:abc*de:abc,de:0,3:f:f:__empty__",
		"abcde:?b*e:?b,e:0,4:f:f:a,cd",
		"abcde:a*c?e:a,c?e:0,2:f:f:b,d",

		# and here some of my own examples..
		"hellotherehowareyou:*ll*u:ll,u:2,18:t:f:he,otherehowareyo",
		"hellu:*ll*u:ll,u:2,4:t:f:he,__empty__",
		"hellou:*ll*u:ll,u:2,5:t:f:he,o",
		"helloyu:*ll*u:ll,u:2,6:t:f:he,oy",
		"helloyou:*ll*u:ll,u:2,7:t:f:he,oyo",
		"hellohellohelloyou:*ll*u:ll,u:2,17:t:f:he,ohellohelloyo",

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

		"mississippi:*s*:s:2:t:t:mi,sissippi",
		"mississippi:*ss*:ss:2:t:t:mi,issippi",
		"mississippi:*ss*s*:ss,s:2,5:t:t:mi,i,sippi",
		"mississippi:*ss*ss*:ss,ss:2,5:t:t:mi,i,ippi",
		"mississippi:*ss*ss*p*:ss,ss,p:2,5,8:t:t:mi,i,i,pi",
		"mississippi:*ss*ss*pp*:ss,ss,pp:2,5,8:t:t:mi,i,i,i",
		"mississippi:*ss*ss*p?*:ss,ss,p?:2,5,8:t:t:mi,i,i,p,i",
		"mississippi:*ss*ss*?p?*:ss,ss,?p?:2,5,7:t:t:mi,i,,i,p,i",
		"mississippi:*is*:is:1:t:t:m,sissippi",
		"mississippi:*mis*:mis:0:t:t:,sissippi",
		"mississippi:*mi?*:mi?:0:t:t:,s,sissippi",
	);

	#say "matchtests=". Dumper(\@matchtests);
	foreach my $test (@matchtests)
	{
		#say "test $test";
		# format of each match test:
		# str:pat:islands:startposes:firststar:laststar:expmts
		my( $s, $p, $islands, $sps, $firststar, $laststar, $expmts ) =
			split( /:/, $test );
		my @islands = split(/,/,$islands);
		my @sp = split(/,/,$sps);
		my @expectedmt = map { /^__empty__$/ ? '' : $_ }
			split(/,/,$expmts);

		$firststar = ($firststar eq 't'?1:0);
		$laststar = ($laststar eq 't'?1:0);

		say "p=$p, s=$s, islands=".Dumper(\@islands)."sp=".Dumper(\@sp)."firststar=$firststar, laststar=$laststar" if $debug;

		my $nislands = @islands;
		my $nsp = @sp;
		die "input error, nislands=$nislands, nsp=$nsp\n"
			unless $nsp == $nislands;

		# build integrated @is array of (island,sp) tuples
		my @is = map { tuple($islands[$_],$sp[$_]) } 0..$nsp-1;

		say "is=".Dumper(\@is) if $debug;

		my @mt = extractmatchedtext( $s, $firststar, $laststar, @is );

		#say "p=$p, s=$s, island=$islands, sps=$sps, mt=".Dumper(\@mt);
		my $nmatch = @expectedmt;
		is( scalar(@mt), $nmatch,
			"match($s,$p,$islands,$sps).#mt==$nmatch" );
		foreach my $i (0..$#mt)
		{
			is( $mt[$i], $expectedmt[$i],
			    "match($s,$p,$islands,$sps).mt[$i]==$expectedmt[$i]" );
		}
		if(0)
		{
			done_testing();
			exit 0;
		}
	}
}


1;