You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
2.0 KiB
63 lines
2.0 KiB
<?php
|
|
|
|
namespace RouterOS\Tests\Helpers;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use RouterOS\Helpers\BinaryStringHelper;
|
|
|
|
/**
|
|
* Limit code coverage to the class
|
|
*
|
|
* @coversDefaultClass \RouterOS\Helpers\BinaryStringHelper
|
|
*/
|
|
class BinaryStringHelperTest extends TestCase
|
|
{
|
|
/**
|
|
* @dataProvider IntegerToNBOBinaryStringProvider
|
|
* @covers ::IntegerToNBOBinaryString
|
|
*/
|
|
public function test__IntegerToNBOBinaryString($value, $expected)
|
|
{
|
|
$this->assertEquals($expected, BinaryStringHelper::IntegerToNBOBinaryString($value));
|
|
}
|
|
|
|
public function IntegerToNBOBinaryStringProvider(): array
|
|
{
|
|
return [
|
|
[0, chr(0)], // lower boundary value
|
|
[0xFFFFFFFF, chr(0xFF) . chr(0xFF) . chr(0xFF) . chr(0xFF)], // 32 bits maximal value
|
|
|
|
// strange behaviour :
|
|
// TypeError: Argument 1 passed to RouterOS\Tests\Helpers\BinaryStringHelperTest::test__IntegerToNBOBinaryString() must be of the type integer, float given
|
|
// Seems that php auto convert to float 0xFFF....
|
|
//
|
|
// [0xFFFFFFFFFFFFFFFF, chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF)],
|
|
|
|
// Let's try random value
|
|
[0x390DDD99, chr(0x39) . chr(0x0D) . chr(0xDD) . chr(0x99)],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider IntegerToNBOBinaryStringProvider64
|
|
* @covers ::IntegerToNBOBinaryString
|
|
*/
|
|
public function test__IntegerToNBOBinaryString64($value, $expected)
|
|
{
|
|
if (PHP_INT_SIZE < 8) {
|
|
$this->markTestSkipped('Available only on x64 CPUs');
|
|
}
|
|
|
|
$this->assertEquals($expected, BinaryStringHelper::IntegerToNBOBinaryString($value));
|
|
}
|
|
|
|
public function IntegerToNBOBinaryStringProvider64(): array
|
|
{
|
|
return [
|
|
// -1 is encoded with 0xFFFFFFF.....
|
|
// 64 bits maximal value (on a 64 bits system only)
|
|
[-1, chr(0xFF) . chr(0xFF) . chr(0xFF) . chr(0xFF) . chr(0xFF) . chr(0xFF) . chr(0xFF) . chr(0xFF)], // 64 bits upper boundary value
|
|
];
|
|
}
|
|
}
|