Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- # Checks that the given regexp matches everything except the given string.
- # Tests with all strings up to 8 characters in length that are composed of the
- # characters the string, plus '#', which is assumed not to occur in the string.
- use strict;
- use warnings;
- my $string = "MOOSE";
- my $regexp = qr/^(.{0,4}$|.{6}|[^M]|.[^O]|..[^O]|...[^S]|....[^E])/;
- my %char;
- for (split //, $string) {
- $char{$_}++;
- }
- $char{'#'}++;
- my @chars = sort keys %char;
- print "@chars\n";
- sub testit {
- local ($_) = @_;
- my $got = /$regexp/ ? "true" : "false";
- my $want = $_ ne $string ? "true" : "false";
- if ($got ne $want) {
- die "regexp('$_')=$got; want $want\n";
- }
- }
- for my $length (1..8) {
- allStringsOfLength($length, [@chars], \&testit, "")
- }
- print "OK\n";
- sub allStringsOfLength {
- my ($length, $chars, $func, $prefix) = @_;
- if (length($prefix) == $length) {
- $func->($prefix);
- return;
- }
- foreach my $char (@$chars) {
- allStringsOfLength($length, $chars, $func, $prefix.$char);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement