Skip to content

Commit

Permalink
✅ Add tests for gmp
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssegers committed Jan 4, 2019
1 parent f79940b commit d243505
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tests/OptimusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,46 @@ public function getBitLengthTestData(): array
];
}

/**
* @dataProvider getBitLengthTestData
*/
public function testEncodeDecodeRandomNumbersWithGmp(int $bitLength)
{
$maxInt = (2 ** $bitLength) - 1;
list($prime, $inverse, $xor) = Energon::generate(null, $bitLength);

$optimus = new Optimus($prime, $inverse, $xor, $bitLength);
$optimus->setMode(Optimus::MODE_GMP);

for ($i = 0; $i < 1000; $i++) {
$id = random_int(0, $maxInt);

$encoded = $optimus->encode($id);
$decoded = $optimus->decode($encoded);

$assertMsgDetails = sprintf(
'Prime: %s, Inverse: %s, Xor: %s, Bit length: %s, Value: %s',
$prime,
$inverse,
$xor,
$bitLength,
$id
);

$this->assertEquals(
$id,
$decoded,
"Encoded value $encoded has not decoded back to $id. ($assertMsgDetails)"
);

$this->assertNotEquals(
$id,
$encoded,
"Encoded value $encoded matches the original value."
);
}
}

public function testEncodeStrings()
{
list($prime, $inverse, $xor) = Energon::generate();
Expand Down

0 comments on commit d243505

Please sign in to comment.