Skip to content

Commit

Permalink
Add append method
Browse files Browse the repository at this point in the history
  • Loading branch information
RobQuistNL committed Apr 23, 2023
1 parent a10bbae commit b40acbd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/ByteArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public static function fromArray(array $array): self
return $self;
}

public function append(ByteArray $array): self
{
foreach ($array->toArray() as $value) {
$this->array[] = $value;
}
return $this;
}

/**
* Create a byte array from string. Assumes every character in the string to be an unsigned char.
*/
Expand Down
21 changes: 21 additions & 0 deletions tests/ByteArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,25 @@ public static function provideString() : array
['🚀', [240, 159, 154, 128]], // This emoji should be 4 bytes (unicode)
];
}

/**
* @dataProvider provideAppend
*/
public function testAppend(ByteArray $current, ByteArray $appended, ByteArray $expected) : void
{
$this->assertEquals($current->append($appended), $expected);
}

public static function provideAppend() : array
{
return [
[ByteArray::fromArray([]), ByteArray::fromArray([]), ByteArray::fromArray([])], // 2x empty = empty
[ByteArray::fromArray([123]), ByteArray::fromArray([]), ByteArray::fromArray([123])],
[ByteArray::fromArray([]), ByteArray::fromArray([234]), ByteArray::fromArray([234])],

[ByteArray::fromArray([123]), ByteArray::fromArray([234]), ByteArray::fromArray([123, 234])],

[ByteArray::fromString('🚀'), ByteArray::fromString('🐸'), ByteArray::fromString('🚀🐸')],
];
}
}

0 comments on commit b40acbd

Please sign in to comment.