Skip to content

Commit

Permalink
Add for loop testing
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0Vu committed Oct 15, 2020
1 parent b88f2ab commit 8a4b007
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 46 deletions.
8 changes: 4 additions & 4 deletions src/RLP.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use InvalidArgumentException;
use RuntimeException;
use Web3p\RLP\Buffer;
use Web3p\RLP\Types\Str;
use Web3p\RLP\Types\Numeric;

Expand Down Expand Up @@ -64,10 +63,10 @@ class RLP
/**
* Return RLP encoded of the given inputs.
*
* @param array $inputs array of data you want to RLP encode
* @param mixed $inputs mixed type of data you want to RLP encode
* @return string RLP encoded hex string of inputs
*/
public function encode(array $inputs)
public function encode($inputs)
{
$output = '';
if (is_array($inputs)) {
Expand All @@ -80,7 +79,8 @@ public function encode(array $inputs)
$input = $this->encodeInput($inputs);
$length = mb_strlen($input) / 2;

if ($length === 1 && hexdec(mb_substr($input, 0, 2)) < 128) {
// first byte < 0x80
if ($length === 1 && mb_substr($input, 0, 1) < 8) {
return $input;
}
return $this->encodeLength($length, 128) . $input;
Expand Down
101 changes: 59 additions & 42 deletions test/unit/RLPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,45 @@

class RLPTest extends TestCase
{
/**
* $testCases for rlp
*
* @var array
*/
protected $testCases = [
[
"decoded" => ["dog", "god", "cat"],
"encoded" => "cc83646f6783676f6483636174",
"rlpdecoded" => [
"646f67", "676f64", "636174"
]
], [
"decoded" => ["0xabcd", "0xdeff", "0xaaaa"],
"encoded" => "c982abcd82deff82aaaa",
"rlpdecoded" => ["abcd", "deff", "aaaa"]
], [
"decoded" => 0,
"encoded" => "80",
"rlpdecoded" => ""
], [
"decoded" => [],
"encoded" => "c0",
"rlpdecoded" => []
], [
"decoded" => "0x00",
"encoded" => "00",
"rlpdecoded" => "0"
], [
"decoded" => "0x0400",
"encoded" => "820400",
"rlpdecoded" => "0400"
], [
"decoded" => [[], [[]], [ [], [[]] ]],
"encoded" => "c7c0c1c0c3c0c1c0",
"rlpdecoded" => [[], [[]], [ [], [[]] ]]
]
];

/**
* testEncode
*
Expand All @@ -16,13 +55,10 @@ public function testEncode()
{
$rlp = $this->rlp;

$encoded = $rlp->encode(['dog', 'god', 'cat']);
$this->assertEquals('cc83646f6783676f6483636174', $encoded);

$encoded = $rlp->encode(['0xabcd', '0xdeff', '0xaaaa']);
$this->assertEquals('c982abcd82deff82aaaa', $encoded);
$this->assertEquals('00', $rlp->encode(chr(0)));
$this->assertEquals('01', $rlp->encode(chr(1)));
foreach ($this->testCases as $testCase) {
$encoded = $rlp->encode($testCase["decoded"]);
$this->assertEquals($testCase["encoded"], $encoded);
}
}

/**
Expand All @@ -33,29 +69,10 @@ public function testEncode()
public function testDecode()
{
$rlp = $this->rlp;
$encoded = '0x' . $rlp->encode(['dog', 'god', 'cat']);
$decoded = $rlp->decode($encoded);
$this->assertEquals(3, count($decoded));
$this->assertEquals('dog', Str::decodeHex(strtoupper($decoded[0])));
$this->assertEquals('god', Str::decodeHex($decoded[1]));
$this->assertEquals('cat', Str::decodeHex($decoded[2]));

$encoded = '0x' . $rlp->encode(['0xabcd', '0xdeff', '0xaaaa']);
$decoded = $rlp->decode($encoded);
$this->assertEquals(3, count($decoded));
$this->assertEquals('abcd', $decoded[0]);
$this->assertEquals('deff', $decoded[1]);
$this->assertEquals('aaaa', $decoded[2]);

$encoded = '0x' . $rlp->encode([199999, 1]);
$decoded = $rlp->decode($encoded);
$this->assertEquals(2, count($decoded));
$this->assertEquals(199999, hexdec($decoded[0]));
$this->assertEquals(1, hexdec($decoded[1]));

$encoded = '0x' . $rlp->encode('0x25');
$decoded = $rlp->decode($encoded);
$this->assertEquals('25', $decoded);
foreach ($this->testCases as $testCase) {
$decoded = $rlp->decode("0x" . $testCase["encoded"]);
$this->assertEquals($testCase["rlpdecoded"], $decoded);
}
}

/**
Expand All @@ -72,9 +89,9 @@ public function testValidRlp()
$rlptest = json_decode($rlptestJson, true);

foreach ($rlptest as $test) {
$encoded = $rlp->encode($test['in']);
$encoded = $rlp->encode($test["in"]);

$this->assertEquals($test['out'], $encoded);
$this->assertEquals($test["out"], $encoded);
}
}

Expand All @@ -88,14 +105,14 @@ public function testValidRlp()
public function testIssue14()
{
$rlp = $this->rlp;
$this->assertEquals('c0', $rlp->encode([]));
$this->assertEquals('80', $rlp->encode(0));
$this->assertEquals('80', $rlp->encode(0x0));
$this->assertEquals('80', $rlp->encode(-1));
$this->assertEquals('80', $rlp->encode(-2));
$this->assertEquals('30', $rlp->encode('0'));
$this->assertEquals('00', $rlp->encode('0x0'));
$this->assertEquals('80', $rlp->encode(null));
$this->assertEquals("c0", $rlp->encode([]));
$this->assertEquals("80", $rlp->encode(0));
$this->assertEquals("80", $rlp->encode(0x0));
$this->assertEquals("80", $rlp->encode(-1));
$this->assertEquals("80", $rlp->encode(-2));
$this->assertEquals("30", $rlp->encode("0"));
$this->assertEquals("00", $rlp->encode("0x0"));
$this->assertEquals("80", $rlp->encode(null));
}

/**
Expand All @@ -113,9 +130,9 @@ public function testIssue14()
// $invalidrlptest = json_decode($invalidrlptestJson, true);

// foreach ($invalidrlptest as $test) {
// $encoded = $rlp->encode($test['in']);
// $encoded = $rlp->encode($test["in"]);

// $this->assertEquals($test['out'], $encoded);
// $this->assertEquals($test["out"], $encoded);
// }
// }
}

0 comments on commit 8a4b007

Please sign in to comment.