public function testMoreRopeOperations()
 {
     $rope = new PhutilRope();
     $rope->append('aaa');
     $rope->append('bbb');
     $rope->append('ccc');
     $rope->append('rrrrddddddddd');
     $rope->removeBytesFromHead(4);
     $string = $rope->getAsString();
     $this->assertEqual('bbcccrrrrddddddddd', $string);
     $this->assertEqual(strlen($string), $rope->getByteLength());
     $rope = new PhutilRope();
     $rope->append('aaa');
     $rope->append('bbb');
     $rope->removeBytesFromHead(6);
     $string = $rope->getAsString();
     $this->assertEqual('', $string);
     $this->assertEqual(0, $rope->getByteLength());
     $rope = new PhutilRope();
     $rope->append('a');
     $rope->append('b');
     $rope->append('c');
     $rope->removeBytesFromHead(1024);
     $string = $rope->getAsString();
     $this->assertEqual('', $string);
     $this->assertEqual(0, $rope->getByteLength());
 }
 public function testRopeOperations()
 {
     $rope = new PhutilRope();
     $rope->append('aaa');
     $rope->append('bbb');
     $this->assertEqual(6, $rope->getByteLength());
     $this->assertEqual('aaabbb', $rope->getAsString());
     $rope->removeBytesFromHead(2);
     $this->assertEqual(4, $rope->getByteLength());
     $this->assertEqual('abbb', $rope->getAsString());
     $rope->removeBytesFromHead(4);
     $this->assertEqual(0, $rope->getByteLength());
     $this->assertEqual('', $rope->getAsString());
 }
 public function testHashingIterator()
 {
     $path = dirname(__FILE__) . '/hashingiterator/mostlyprime.txt';
     $stream = fopen($path, 'rb');
     $algorithm = 'sha256';
     $stream_iterator = new PhutilStreamIterator($stream);
     $hashing_iterator = id(new PhutilHashingIterator($stream_iterator))->setAlgorithm($algorithm);
     $rope = new PhutilRope();
     foreach ($hashing_iterator as $block) {
         $rope->append($block);
     }
     $expect_data = Filesystem::readFile($path);
     $actual_data = $rope->getAsString();
     $this->assertEqual($expect_data, $actual_data, pht('Data should be read correctly.'));
     $expect_hash = 'd50af03db6c8b63084664f352d12bffb8f58022bdd67e3aba21f01e11c3b8939';
     $single_hash = hash($algorithm, $expect_data, $raw_output = false);
     $actual_hash = $hashing_iterator->getHash();
     $this->assertEqual($expect_hash, $single_hash, pht('Expected hash should match freshly computed hash.'));
     $this->assertEqual($expect_hash, $actual_hash, pht('Expected hash should also match iterator actual hash.'));
 }
 private function processLog($mode)
 {
     $chunks = $this->newChunkIterator();
     // NOTE: Because we're going to insert new chunks, we need to stop the
     // iterator once it hits the final chunk which currently exists. Otherwise,
     // it may start consuming chunks we just wrote and run forever.
     $last = $this->loadLastChunkInfo();
     if ($last) {
         $chunks->setRange(null, $last['id']);
     }
     $byte_limit = self::CHUNK_BYTE_LIMIT;
     $rope = new PhutilRope();
     $this->openTransaction();
     foreach ($chunks as $chunk) {
         $rope->append($chunk->getChunkDisplayText());
         $chunk->delete();
         while ($rope->getByteLength() > $byte_limit) {
             $this->writeEncodedChunk($rope, $byte_limit, $mode);
         }
     }
     while ($rope->getByteLength()) {
         $this->writeEncodedChunk($rope, $byte_limit, $mode);
     }
     $this->saveTransaction();
 }