aboutsummaryrefslogtreecommitdiffstats
path: root/Iterator
diff options
context:
space:
mode:
authorIvan Enderlin <ivan.enderlin@hoa-project.net>2013-05-13 17:25:17 +0200
committerIvan Enderlin <ivan.enderlin@hoa-project.net>2013-05-13 17:29:06 +0200
commitbc23a6364e28ff27b2f021b7c1057de9c0cf96d7 (patch)
treef4be5e67d445240b5b9c524c689991b65dba1929 /Iterator
parentb229c04d6119d390ad727e5a70732b2e3bcbd176 (diff)
downloadPraspel-bc23a6364e28ff27b2f021b7c1057de9c0cf96d7.zip
Praspel-bc23a6364e28ff27b2f021b7c1057de9c0cf96d7.tar.gz
Praspel-bc23a6364e28ff27b2f021b7c1057de9c0cf96d7.tar.bz2
Add Hoa\Praspel\Iterator\Coverage global iterator
The Coverage iterator allows to make structural or structural+domain coverage. To do this, the Structural iterator now implements the Hoa\Iterator\Recursive interface. This is a first draft, it may be optimized or updated.
Diffstat (limited to 'Iterator')
-rw-r--r--Iterator/Coverage/Coverage.php141
-rw-r--r--Iterator/Coverage/Structural.php97
2 files changed, 216 insertions, 22 deletions
diff --git a/Iterator/Coverage/Coverage.php b/Iterator/Coverage/Coverage.php
new file mode 100644
index 0000000..49b50e8
--- /dev/null
+++ b/Iterator/Coverage/Coverage.php
@@ -0,0 +1,141 @@
+<?php
+
+/**
+ * Hoa
+ *
+ *
+ * @license
+ *
+ * New BSD License
+ *
+ * Copyright © 2007-2013, Ivan Enderlin. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of the Hoa nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+namespace {
+
+from('Hoa')
+
+/**
+ * \Hoa\Praspel\Iterator\Coverage\Structural
+ */
+-> import('Praspel.Iterator.Coverage.Structural')
+
+/**
+ * \Hoa\Iterator\Aggregate
+ */
+-> import('Iterator.Aggregate')
+
+/**
+ * \Hoa\Iterator\Recursive\Iterator
+ */
+-> import('Iterator.Recursive.Iterator');
+
+}
+
+namespace Hoa\Praspel\Iterator\Coverage {
+
+/**
+ * Class \Hoa\Praspel\Iterator\Coverage.
+ *
+ * Coverage recursive iterator.
+ *
+ * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
+ * @copyright Copyright © 2007-2013 Ivan Enderlin.
+ * @license New BSD License
+ */
+
+class Coverage implements \Hoa\Iterator\Aggregate {
+
+ /**
+ * Criteria: normal (@requires and @ensures).
+ *
+ * @const int
+ */
+ const CRITERIA_NORMAL = 1;
+
+ /**
+ * Criteria: exceptional (@requires and @throwable).
+ *
+ * @const int
+ */
+ const CRITERIA_EXCEPTIONAL = 2;
+
+ /**
+ * Criteria: domain (all disjunctions).
+ *
+ * @const int
+ */
+ const CRITERIA_DOMAIN = 4;
+
+ /**
+ * Iterator (of the specification to cover).
+ *
+ * @var \Hoa\Praspel\Iterator\Coverage\Structural object
+ */
+ protected $_iterator = null;
+
+
+
+ /**
+ * Constructor.
+ *
+ * @access public
+ * @param \Hoa\Praspel\Model\Specification $specification Specification.
+ * @return void
+ */
+ public function __construct ( \Hoa\Praspel\Model\Specification $specification ) {
+
+ $this->_iterator = new Structural($specification);
+
+ return;
+ }
+
+ /**
+ * Set coverage criteria.
+ *
+ * @access public
+ * @param int $criteria Criteria (please, see self::CRITERIA_*
+ * constants).
+ * @return int
+ */
+ public function setCriteria ( $criteria ) {
+
+ return $this->_iterator->setCriteria($criteria);
+ }
+
+ /**
+ * Get iterator.
+ *
+ * @access public
+ * @return \Hoa\Iterator\Recursive\Iterator
+ */
+ public function getIterator ( ) {
+
+ return new \Hoa\Iterator\Recursive\Iterator($this->_iterator);
+ }
+}
+
+}
diff --git a/Iterator/Coverage/Structural.php b/Iterator/Coverage/Structural.php
index dd6fb87..eba44ec 100644
--- a/Iterator/Coverage/Structural.php
+++ b/Iterator/Coverage/Structural.php
@@ -44,9 +44,34 @@ from('Hoa')
-> import('Praspel.Iterator.WeakStack')
/**
- * \Hoa\Iterator
+ * \Hoa\Praspel\Iterator\Coverage
*/
--> import('Iterator.~');
+-> import('Praspel.Iterator.Coverage.~')
+
+/**
+ * \Hoa\Praspel\Iterator\Coverage\Domain
+ */
+-> import('Praspel.Iterator.Coverage.Domain')
+
+/**
+ * \Hoa\Iterator\Recursive
+ */
+-> import('Iterator.Recursive.~')
+
+/**
+ * \Hoa\Iterator\Map
+ */
+-> import('Iterator.Map')
+
+/**
+ * \Hoa\Iterator\Multiple
+ */
+-> import('Iterator.Multiple')
+
+/**
+ * \Hoa\Iterator\Recursive\Mock
+ */
+-> import('Iterator.Recursive.Mock');
}
@@ -62,21 +87,7 @@ namespace Hoa\Praspel\Iterator\Coverage {
* @license New BSD License
*/
-class Structural implements \Hoa\Iterator {
-
- /**
- * Criteria: normal (@requires and @ensures).
- *
- * @const int
- */
- const CRITERIA_NORMAL = 1;
-
- /**
- * Criteria: exceptional (@requires and @throwable).
- *
- * @const int
- */
- const CRITERIA_EXCEPTIONAL = 2;
+class Structural implements \Hoa\Iterator\Recursive {
/**
* Specification to cover.
@@ -90,7 +101,8 @@ class Structural implements \Hoa\Iterator {
*
* @var \Hoa\Praspel\Iterator\Coverage\Structural int
*/
- protected $_criteria = 3; // CRITERIA_NORMAL | CRITERIA_EXCEPTIONAL
+ protected $_criteria = 3; // Coverage::CRITERIA_NORMAL
+ // | Coverage::CRITERIA_EXCEPTIONAL
/**
* Stack (to manage backtracks, yields, etc.)
@@ -141,7 +153,7 @@ class Structural implements \Hoa\Iterator {
$this->_specification = $specification;
$this->setCriteria(
- static::CRITERIA_NORMAL | static::CRITERIA_EXCEPTIONAL
+ Coverage::CRITERIA_NORMAL | Coverage::CRITERIA_EXCEPTIONAL
);
return;
@@ -184,7 +196,7 @@ class Structural implements \Hoa\Iterator {
if(true === $this->_up) {
if( 'ensures' === $this->_post
- && 0 !== (static::CRITERIA_EXCEPTIONAL & $this->getCriteria())) {
+ && 0 !== (Coverage::CRITERIA_EXCEPTIONAL & $this->getCriteria())) {
$this->_up = false;
$this->_post = 'throwable';
@@ -276,7 +288,7 @@ class Structural implements \Hoa\Iterator {
$this->_stack = new \SplStack();
$this->_stack->push($this->_specification);
- $this->_post = 0 !== (static::CRITERIA_NORMAL & $this->getCriteria())
+ $this->_post = 0 !== (Coverage::CRITERIA_NORMAL & $this->getCriteria())
? 'ensures'
: 'throwable';
@@ -348,7 +360,7 @@ class Structural implements \Hoa\Iterator {
* Set coverage criteria.
*
* @access public
- * @param int $criteria Criteria (please, see self::CRITERIA_*
+ * @param int $criteria Criteria (please, see Coverage::CRITERIA_*
* constants).
* @return int
*/
@@ -370,6 +382,47 @@ class Structural implements \Hoa\Iterator {
return $this->_criteria;
}
+
+ /**
+ * Check if we can go deeper (structural to domain coverage).
+ *
+ * @access public
+ * @return bool
+ */
+ public function hasChildren ( ) {
+
+ return 0 !== (Coverage::CRITERIA_DOMAIN & $this->getCriteria());
+ }
+
+ /**
+ * Get the domain coverage iterator from the current variables from pre and
+ * post clauses.
+ *
+ * @access public
+ * @return \Hoa\Iterator\Recursive
+ */
+ public function getChildren ( ) {
+
+ $iterator = new \Hoa\Iterator\Multiple(
+ \Hoa\Iterator\Multiple::MIT_NEED_ANY
+ | \Hoa\Iterator\Multiple::MIT_KEYS_ASSOC
+ );
+ $pre = array();
+ $post = array();
+
+ foreach($this->_current['pre'] as $clause)
+ foreach($clause->getLocalVariables() as $variable)
+ $pre[] = $variable;
+
+ foreach($this->_current['post'] as $clause)
+ foreach($clause->getLocalVariables() as $variable)
+ $post[] = $variable;
+
+ $iterator->attachIterator(new Domain($pre), 'pre');
+ $iterator->attachIterator(new Domain($post), 'post');
+
+ return new \Hoa\Iterator\Recursive\Mock($iterator);
+ }
}
}