[Bps-public-commit] r13032 - in Parse-BooleanLogic: lib/Parse

ruz at bestpractical.com ruz at bestpractical.com
Sat Jun 7 17:32:39 EDT 2008


Author: ruz
Date: Sat Jun  7 17:32:38 2008
New Revision: 13032

Added:
   Parse-BooleanLogic/t/solve.t
Modified:
   Parse-BooleanLogic/lib/Parse/BooleanLogic.pm

Log:
* implement solve method and add tests

Modified: Parse-BooleanLogic/lib/Parse/BooleanLogic.pm
==============================================================================
--- Parse-BooleanLogic/lib/Parse/BooleanLogic.pm	(original)
+++ Parse-BooleanLogic/lib/Parse/BooleanLogic.pm	Sat Jun  7 17:32:38 2008
@@ -327,25 +327,28 @@
 sub solve {
     my ($self, $tree, $cb) = @_;
 
-    my $skip_next = 0;
-
-    my ($res, $ea) = (0, 'OR');
-
+    my ($res, $ea, $skip_next) = (0, 'OR', 0);
     foreach my $entry ( @$tree ) {
         next if $skip_next-- > 0;
         unless ( ref $entry ) {
-
+            $ea = $entry;
+            $skip_next++ if ($res && $ea eq 'OR') || (!$res && $ea eq 'AND');
+            next;
         }
 
+        my $cur;
         if ( ref $entry eq 'ARRAY' ) {
-            my $tmp = $self->solve( $entry, $cb, 1 );
-        } elsif ( ref $entry eq 'HASH' ) {
-            my $tmp = $cb->( $entry );
+            $cur = $self->solve( $entry, $cb, 1 );
+        } else {
+            $cur = $cb->( $entry );
+        }
+        if ( $ea eq 'OR' ) {
+            $res ||= $cur;
         } else {
-            push @res, $entry;
+            $res &&= $cur;
         }
     }
-    return $res;
+    return $res? 1 : 0;
 }
 
 1;

Added: Parse-BooleanLogic/t/solve.t
==============================================================================
--- (empty file)
+++ Parse-BooleanLogic/t/solve.t	Sat Jun  7 17:32:38 2008
@@ -0,0 +1,61 @@
+
+use strict;
+use warnings;
+
+use Test::More tests => 43;
+
+use_ok 'Parse::BooleanLogic';
+
+my $p = new Parse::BooleanLogic;
+
+my $cb = sub { return $_[0]->{'v'} };
+
+ok  $p->solve( [{ v => 1 }], $cb ), "true";
+ok !$p->solve( [{ v => 0 }], $cb ), "false";
+
+ok !$p->solve( [{v => 0}, 'AND', {v => 0}], $cb ), "0 AND 0";
+ok !$p->solve( [{v => 0}, 'AND', {v => 1}], $cb ), "0 AND 1";
+ok !$p->solve( [{v => 1}, 'AND', {v => 0}], $cb ), "1 AND 0";
+ok  $p->solve( [{v => 1}, 'AND', {v => 1}], $cb ), "1 AND 1";
+
+ok !$p->solve( [{v => 0}, 'OR', {v => 0}], $cb ), "0 OR 0";
+ok  $p->solve( [{v => 0}, 'OR', {v => 1}], $cb ), "0 OR 1";
+ok  $p->solve( [{v => 1}, 'OR', {v => 0}], $cb ), "1 OR 0";
+ok  $p->solve( [{v => 1}, 'OR', {v => 1}], $cb ), "1 OR 1";
+
+ok !$p->solve( [{v => 0}, 'AND', {v => 0}, 'OR', {v => 0}], $cb ), "0 AND 0 OR 0";
+ok  $p->solve( [{v => 0}, 'AND', {v => 0}, 'OR', {v => 1}], $cb ), "0 AND 0 OR 1";
+ok !$p->solve( [{v => 0}, 'AND', {v => 1}, 'OR', {v => 0}], $cb ), "0 AND 1 OR 0";
+ok  $p->solve( [{v => 0}, 'AND', {v => 1}, 'OR', {v => 1}], $cb ), "0 AND 1 OR 1";
+ok !$p->solve( [{v => 1}, 'AND', {v => 0}, 'OR', {v => 0}], $cb ), "1 AND 0 OR 0";
+ok  $p->solve( [{v => 1}, 'AND', {v => 0}, 'OR', {v => 1}], $cb ), "1 AND 0 OR 1";
+ok  $p->solve( [{v => 1}, 'AND', {v => 1}, 'OR', {v => 0}], $cb ), "1 AND 1 OR 0";
+ok  $p->solve( [{v => 1}, 'AND', {v => 1}, 'OR', {v => 1}], $cb ), "1 AND 1 OR 1";
+
+ok !$p->solve( [{v => 0}, 'OR', {v => 0}, 'AND', {v => 0}], $cb ), "0 OR 0 AND 0";
+ok !$p->solve( [{v => 0}, 'OR', {v => 0}, 'AND', {v => 1}], $cb ), "0 OR 0 AND 1";
+ok !$p->solve( [{v => 0}, 'OR', {v => 1}, 'AND', {v => 0}], $cb ), "0 OR 1 AND 0";
+ok  $p->solve( [{v => 0}, 'OR', {v => 1}, 'AND', {v => 1}], $cb ), "0 OR 1 AND 1";
+ok !$p->solve( [{v => 1}, 'OR', {v => 0}, 'AND', {v => 0}], $cb ), "1 OR 0 AND 0";
+ok  $p->solve( [{v => 1}, 'OR', {v => 0}, 'AND', {v => 1}], $cb ), "1 OR 0 AND 1";
+ok !$p->solve( [{v => 1}, 'OR', {v => 1}, 'AND', {v => 0}], $cb ), "1 OR 1 AND 0";
+ok  $p->solve( [{v => 1}, 'OR', {v => 1}, 'AND', {v => 1}], $cb ), "1 OR 1 AND 1";
+
+ok !$p->solve( [{v => 0}, 'AND', [ {v => 0}, 'OR', {v => 0}]], $cb ), "0 AND (0 OR 0)";
+ok !$p->solve( [{v => 0}, 'AND', [ {v => 0}, 'OR', {v => 1}]], $cb ), "0 AND (0 OR 1)";
+ok !$p->solve( [{v => 0}, 'AND', [ {v => 1}, 'OR', {v => 0}]], $cb ), "0 AND (1 OR 0)";
+ok !$p->solve( [{v => 0}, 'AND', [ {v => 1}, 'OR', {v => 1}]], $cb ), "0 AND (1 OR 1)";
+ok !$p->solve( [{v => 1}, 'AND', [ {v => 0}, 'OR', {v => 0}]], $cb ), "1 AND (0 OR 0)";
+ok  $p->solve( [{v => 1}, 'AND', [ {v => 0}, 'OR', {v => 1}]], $cb ), "1 AND (0 OR 1)";
+ok  $p->solve( [{v => 1}, 'AND', [ {v => 1}, 'OR', {v => 0}]], $cb ), "1 AND (1 OR 0)";
+ok  $p->solve( [{v => 1}, 'AND', [ {v => 1}, 'OR', {v => 1}]], $cb ), "1 AND (1 OR 1)";
+
+ok !$p->solve( [{v => 0}, 'OR', [ {v => 0}, 'AND', {v => 0}]], $cb ), "0 OR (0 AND 0)";
+ok !$p->solve( [{v => 0}, 'OR', [ {v => 0}, 'AND', {v => 1}]], $cb ), "0 OR (0 AND 1)";
+ok !$p->solve( [{v => 0}, 'OR', [ {v => 1}, 'AND', {v => 0}]], $cb ), "0 OR (1 AND 0)";
+ok  $p->solve( [{v => 0}, 'OR', [ {v => 1}, 'AND', {v => 1}]], $cb ), "0 OR (1 AND 1)";
+ok  $p->solve( [{v => 1}, 'OR', [ {v => 0}, 'AND', {v => 0}]], $cb ), "1 OR (0 AND 0)";
+ok  $p->solve( [{v => 1}, 'OR', [ {v => 0}, 'AND', {v => 1}]], $cb ), "1 OR (0 AND 1)";
+ok  $p->solve( [{v => 1}, 'OR', [ {v => 1}, 'AND', {v => 0}]], $cb ), "1 OR (1 AND 0)";
+ok  $p->solve( [{v => 1}, 'OR', [ {v => 1}, 'AND', {v => 1}]], $cb ), "1 OR (1 AND 1)";
+



More information about the Bps-public-commit mailing list