Skip to content

Commit

Permalink
Update compare() method
Browse files Browse the repository at this point in the history
  • Loading branch information
wapmorgan committed Feb 6, 2017
1 parent 0173cb3 commit 953483a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/BinaryStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,12 @@ public function saveGroup($name, $fields) {
public function compare($sizeInBytes, $bytes) {
$data = fread($this->fp, $sizeInBytes);
fseek($this->fp, -$sizeInBytes, SEEK_CUR);
if (is_array($bytes)) {
$source = $bytes;
$bytes = null;
foreach ($source as $byte)
$bytes .= is_int($byte) ? chr($byte) : $byte;
}
return ($data === $bytes);
}

Expand Down
11 changes: 11 additions & 0 deletions tests/CompareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,15 @@ public function testCompare() {
$this->assertFalse($s->compare(7, 'Called'));
$this->assertTrue($s->compare(6, 'Called'));
}

public function testCompareBinary() {
$s = new BinaryStream($this->createStream(pack('H*', '3026B27511')));
$this->assertFalse($s->compare(4, array(0x00, 0x00, 0x00, 0x00)));
$this->assertFalse($s->compare(3, array(0x30, 0x26, 0xB2, 0x75)));
$this->assertFalse($s->compare(5, array(0x30, 0x26, 0xB2, 0x75)));
$this->assertTrue($s->compare(4, array(0x30, 0x26, 0xB2, 0x75)));

$s = new BinaryStream($this->createStream(pack('H*', '202020')));
$this->assertTrue($s->compare(3, array(0x20, 32, ' ')));
}
}

0 comments on commit 953483a

Please sign in to comment.