public function testReadFromEmptyString()
 {
     $reader = new StringReader('');
     $char = $reader->readCharacter();
     $this->assertNull($char);
     $matcher = new WhitespaceMatcher();
     $match = $reader->readTextMatch($matcher);
     $this->assertNull($match);
     $actualException = null;
     try {
         $reader->skip(1);
     } catch (\Exception $e) {
         $actualException = $e;
     }
     $this->assertNotNull($actualException);
 }
 public function __construct($filename)
 {
     parent::__construct();
     $this->_str = file_get_contents($filename);
     if (false === $this->_str) {
         return false;
     }
     $this->_pos = 0;
 }
 function __construct($filename)
 {
     parent::__construct();
     $this->_str = file_get_contents($filename);
     if (FALSE === $this->_str) {
         return FALSE;
     }
     $this->_pos = 0;
 }
 public function testFindDemandedPace()
 {
     $Reader = new StringReader();
     $this->assertEquals(0, $Reader->setString('No pace here')->findDemandedPace());
     $this->assertEquals(0, $Reader->setString('Wrong pattern for 3:20')->findDemandedPace());
     $this->assertEquals(200, $Reader->setString('Wrong pattern for 3:20')->findDemandedPace(' for '));
     $this->assertEquals(200, $Reader->setString('Correct pattern in 3:20')->findDemandedPace());
     $this->assertEquals(17, $Reader->setString('Whats about 17 seconds?')->findDemandedPace('about '));
     $this->assertEquals(3600 + 23 * 60 + 45, $Reader->setString('And with hours in 1:23:45?')->findDemandedPace());
     $this->assertEquals(200, $Reader->setString('And multiple times in 3:20 and 4:20?')->findDemandedPace());
 }
 public function testRead()
 {
     $value = get_test_string();
     $value_size = strlen($value);
     $stream = string_to_stream($value);
     $buffer = '';
     $buffer_size = 1000;
     $reader = new StringReader($stream, 0, $buffer, $buffer_size);
     $this->assertEmpty($buffer);
     $this->assertSame(0, $reader->getBytes());
     $iterations = 0;
     $bytes = 0;
     while (!feof($stream)) {
         $iterations++;
         $bytes += $reader->read();
     }
     // Make sure we read the same number of bytes
     $this->assertSame($value_size, $bytes);
     $this->assertSame($value_size, $reader->getBytes());
     // Make sure the data is copied to the buffer and is identical
     $this->assertSame(md5($value), md5($buffer), "I/O itegrity check failed");
     $min_iterations = ceil($value_size / $buffer_size);
     $this->assertGreaterThanOrEqual($min_iterations, $iterations, "Finished reading the stream in {$iterations} iterations, but with a buffer_size of {$buffer_size} and {$value_size} bytes of data, it must have taken at least {$min_iterations} iterations");
 }
Exemple #6
0
  function CachedFileReader($filename) {
    parent::StringReader();

    if (file_exists($filename)) {

      $length=filesize($filename);
      $fd = fopen($filename,'rb');

      if (!$fd) {
        $this->error = 3; // Cannot read file, probably permissions
        return false;
      }
      $this->_str = fread($fd, $length);
	  fclose($fd);

    } else {
      $this->error = 2; // File doesn't exist
      return false;
    }
  }
 /**
  * Maps all special chars to the given string
  * @param string $specialChars
  * @param string $mapped 
  */
 private static function AddSpecialCharsToMap($specialChars, $mapped)
 {
     $reader = new StringReader($specialChars);
     while ($ch = $reader->ReadChar()) {
         self::$specialWesternCharMap[$ch] = $mapped;
     }
 }
 public function readAfterReadingAllLines()
 {
     $stream = new StringReader(new MemoryInputStream($line = 'Hello World' . "\n"));
     $this->assertEquals('Hello World', $stream->readLine());
     $this->assertEquals(NULL, $stream->read());
 }