]> Dogcows Code - chaz/p5-Acme-Test-LogicalEquivalence/blob - bin/test-logical-equivalence
Version 0.001
[chaz/p5-Acme-Test-LogicalEquivalence] / bin / test-logical-equivalence
1 #!perl
2 # ABSTRACT: Test if expressions are logically equivalent
3 # PODNAME: test-logical-equivalence
4
5
6 use 5.008;
7 use warnings FATAL => 'all';
8 use strict;
9
10 use Test::More;
11 use Acme::Test::LogicalEquivalence qw(is_logically_equivalent);
12
13 sub usage {
14 print <<END;
15 $0 EXPR1 EXPR2
16 Test if two simple expressions are logically equivalent.
17
18 Exactly two expressions must be given.
19
20 Example: $0 '\$a || \$b' '\$b || \$a'
21 Remember that you may need to escape special characters like "\$" from your shell.
22 END
23 exit 1;
24 }
25
26 sub find_num_vars {
27 my $expr = shift;
28
29 my $highest = -1;
30
31 # check for scalars that look like $a or $b
32 for my $varname ($expr =~ /\$([a-z])/g) {
33 my $num = ord($varname) - 97;
34 $highest = $num if $highest < $num;
35 }
36
37 # check for scalars that index $_
38 for my $varname ($expr =~ /\$_\[(\d+)\]/g) {
39 my $num = $varname;
40 $highest = $num if $highest < $num;
41 }
42
43 return $highest + 1;
44 }
45
46 my $expr1 = shift or usage;
47 my $expr2 = shift or usage;
48
49 my $numvars1 = find_num_vars($expr1);
50 my $numvars2 = find_num_vars($expr2);
51 my $num = $numvars1 > $numvars2 ? $numvars1 : $numvars2;
52
53 if ($num < 1) {
54 print STDERR 'No variables detected. Variables should be one or more of $a, $b, ..., $z', "\n";
55 exit 2;
56 }
57
58 # convert $a-style vars to $_[0]-style to support more than just $a and $b
59 $expr1 =~ s/\$([a-z])/'$_['.(ord($1) - 97).']'/ge;
60 $expr2 =~ s/\$([a-z])/'$_['.(ord($1) - 97).']'/ge;
61
62 my $sub1 = eval "sub { $expr1 }" or die "Expression 1: $@\n"; ## no critic (ProhibitStringyEval)
63 my $sub2 = eval "sub { $expr2 }" or die "Expression 2: $@\n"; ## no critic (ProhibitStringyEval)
64
65 plan tests => (2 ** $num);
66 note "Testing for logical equivalence of two expressions with $num variable(s)...";
67
68 my $equivalence = is_logically_equivalent($num, $sub1, $sub2);
69 note $equivalence ? 'Logical equivalence proved!' : 'Bummer...';
70
71 __END__
72
73 =pod
74
75 =encoding UTF-8
76
77 =head1 NAME
78
79 test-logical-equivalence - Test if expressions are logically equivalent
80
81 =head1 VERSION
82
83 version 0.001
84
85 =head1 SYNOPSIS
86
87 # be sure to escape $ and other special characters from your shell
88 test-logical-equivalence '$a && $b' '$b && $a'
89
90 =head1 SEE ALSO
91
92 =over 4
93
94 =item *
95
96 L<Acme::Test::LogicalEquivalence>
97
98 =back
99
100 =head1 AUTHOR
101
102 Charles McGarvey <chazmcgarvey@brokenzipper.com>
103
104 =head1 COPYRIGHT AND LICENSE
105
106 This software is copyright (c) 2016 by Charles McGarvey.
107
108 This is free software; you can redistribute it and/or modify it under
109 the same terms as the Perl 5 programming language system itself.
110
111 =cut
This page took 0.038869 seconds and 4 git commands to generate.