【问题标题】:Basic Iterator class, and string traversal基本迭代器类和字符串遍历
【发布时间】:2013-01-24 21:21:30
【问题描述】:

我知道这很简单,但我就是不知道顺序错在哪里。

<?php

class StringIterator implements \Iterator
{
private $_string;
private $_length;   
private $_position;


public function __construct($string)
{
    if(empty($string))
    {
        throw new \InvalidArgumentException(sprintf('The specified string is empty'));
    }//end if

        $this->_string = $string;
        $this->_length = strlen($this->_string);
        $this->rewind(); //setting the initial position instead of having it all over the place.

}//end func 

public function current()
{
 return $this->_string[$this->_position];

}//end func

public function key()
{
    return $this->_position;
}//end func



public function rewind()
{
    $this-> _position = -1;
}//end func


public function next()
{
    $this-> _position++;
}//end func


public function valid()
{   //why is it that your doing this instead of...
/*
*   return isset(this->_string[this->_position]);
*
*
*/
    return $this->_position < $this->_length;
}//end func

}//结束类

TEST CLASS
<?php

require_once __DIR__. '/../src/Iterators/StringIterator.php';

class StringIteratorTest extends PHPUnit_Framework_TestCase
{
    //each method must begin with TEST
public function testInitializing()
{

    $iterator = new \Iterators\StringIterator("Hello World");

    $this->assertEquals(true,$iterator->valid()); 


}//end func

/**
*   @expectedException InvalidArgumentException
*/
public function testInitException()
{

$iterator = new \Iterators\StringIterator("");

}//end func


public function testTraverse()
{
    $string ="Hello World";
    $iterator = new \Iterators\StringIterator($string);
    $count =0;

    $iterator->rewind();
    //test to make sure the next() runs.


    //The iterator interface defines the method Key key()= $key
    //Iterator::current() = $char (gets the current value at the position)
    foreach($iterator as $key=>$char)
    {
        $this->assertEquals($count,$key);
        $this->assertEquals($string[$count],$char);
        ++$count;
         $this->next();
    }//end 4e

}//end func

//tests that the internal pointer (it) is at a valid position in that container that is being iterated
public function testValid()
{
$iterator = new \Iterators\StringIterator($string);

}//end func

//tests the rewind method back to the start of the container.

public function testRewind()
{
$string="Bye";

$iterator = new \Iterators\StringIterator($string); 


for( $i = 0; $i< strlen($string) + 1; ++$i){

$iterator->next();

}//end for
$this->assertEquals(false,$iterator->valid());
$iterator->rewind();
$this->assertEquals(true,$iterator->valid());

}


}

有问题的问题:当我运行测试 (phpunit test) 时,它指出实际测试组件的 current() (return line)foreach 循环中存在错误

foreach($iterator as $key=>$char)
{
    $this->assertEquals($count,$key);
    $this->assertEquals($string[$count],$char);
    ++$count;
    $this->next();
}//end 4e

根据我的研究,我知道这与我在 foreach 循环中调用 next 的顺序有关,我可以准确地弄清楚它需要什么......

【问题讨论】:

  • 我不确定是什么问题。你说“有问题的问题是......”你的实际问题在哪里?您可以从示例中删除不相关的部分代码吗?
  • 如果您使用的是 foreach,则无需致电 next,它会为您调用...
  • 您自己的倒带函数将初始位置索引设置为-1;虽然我不确定您的问题是什么,但您可能需要将其设置为 0。
  • 您的编码风格不断变化。到最后真是太可怕了。让我想起了stackoverflow.com/questions/1732348/…
  • @KaosAkroma:但这不是迭代器的工作方式……您在初始化时将其设置为0 并倒带,因为current 在内部next 之前被调用,而key 应该始终返回当前位置。

标签: php string iterator


【解决方案1】:

这里似乎有很多错误:

  • 您在StringIteratorTest::testTraverse() 内呼叫$this-&gt;next()。这个方法没有这样的next() 方法。该方法属于StringIterator 类。这应该是一个致命错误。
  • 即使此代码StringIterator 类中运行,您仍然不需要在foreach 循环中调用next()foreach 调用所有 Iterator 定义的方法本身。这就是重点。在foreach 中调用next() 只会产生跳跃位置的效果。
  • 您的rewind() 方法不正确。它将位置设置为负值。字符串中没有负数。如果您要为此调用current(),则会收到错误消息,因为它正在尝试调用不存在的$_string[-1]
  • 您的valid() 方法也不正确,因为它只检查位置是否超出上限,而不是下限。这就是为什么valid() 会返回TRUE,即使您的rewind() 方法将位置设置为无效状态。
  • 您的testValid() 方法应该已经捕捉到这一点,但该函数实际上并未测试valid() 方法。它只是创建一个新对象并且什么都不做。
  • testRewind() 方法中的测试方法很糟糕。而不是检查valid(),您应该调用current() 并检查它是否返回“B”,这是字符串中的第一个字符。 rewind() 方法的主要功能是将对象的内部指针重置回起点,因此您应该明确地对此进行测试。

【讨论】:

    猜你喜欢
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 2020-08-13
    • 1970-01-01
    相关资源
    最近更新 更多