freenetis-github/tests/application/libraries/variable_key_generators/Checksum_Variable_Key_GeneratorTest.php @ 31ca0a32
31ca0a32 | Michal Kliment | <?php
|
|
require_once dirname(__FILE__) . '/../../../../application/libraries/variable_key_generators/Checksum_Variable_Key_Generator.php';
|
|||
/**
|
|||
* Test class for Parity_Variable_Key_Generator.
|
|||
* Generated by PHPUnit on 2013-06-05 at 14:39:59.
|
|||
*/
|
|||
class Checksum_Variable_Key_GeneratorTest extends PHPUnit_Framework_TestCase {
|
|||
/**
|
|||
* @var Parity_Variable_Key_Generator
|
|||
*/
|
|||
protected $object;
|
|||
/**
|
|||
* Sets up the fixture, for example, opens a network connection.
|
|||
* This method is called before a test is executed.
|
|||
*/
|
|||
protected function setUp() {
|
|||
$this->object = new Checksum_Variable_Key_Generator;
|
|||
}
|
|||
/**
|
|||
* Tears down the fixture, for example, closes a network connection.
|
|||
* This method is called after a test is executed.
|
|||
*/
|
|||
protected function tearDown() {
|
|||
}
|
|||
/**
|
|||
* Test uniqe keys for member IDs
|
|||
* @covers Parity_Variable_Key_Generator::generate
|
|||
*/
|
|||
public function testGenerate() {
|
|||
$variable_symbols_bottom = array();
|
|||
for ($i = 0; $i < 29999; $i++)
|
|||
{
|
|||
$vs = $this->object->generate(strval($i));
|
|||
$variable_symbols_bottom[$vs] = true;
|
|||
}
|
|||
$this->assertEquals(count($variable_symbols_bottom), 29999);
|
|||
$variable_symbols_top = array();
|
|||
for ($i = 990000; $i < 999999; $i++)
|
|||
{
|
|||
$vs = $this->object->generate(strval($i));
|
|||
$variable_symbols_top[$vs] = true;
|
|||
}
|
|||
$this->assertEquals(count($variable_symbols_top), 9999);
|
|||
$variable_symbols_middle = array();
|
|||
$seed = rand(30000, 269999);
|
|||
for ($i = $seed; $i < $seed + 9999; $i++)
|
|||
{
|
|||
$vs = $this->object->generate(strval($i));
|
|||
$variable_symbols_middle[$vs] = true;
|
|||
}
|
|||
$this->assertEquals(count($variable_symbols_middle), 9999);
|
|||
$vs = array_merge($variable_symbols_bottom, $variable_symbols_middle, $variable_symbols_top);
|
|||
$this->assertEquals(count($vs), 49997);
|
|||
}
|
|||
/**
|
|||
* Tests one error check
|
|||
* @covers Parity_Variable_Key_Generator::errorCheck
|
|||
*/
|
|||
public function testOneErrorCheck() {
|
|||
$vs = $this->object->generate(rand(1, 999999));
|
|||
// one error in variable symbol
|
|||
for ($i = 0; $i < 10; $i++) // move error position
|
|||
{
|
|||
for ($j = 0; $j < 10; $j++) // change value
|
|||
{
|
|||
$bad_vs = $vs;
|
|||
$bad_vs[$i] = strval($j);
|
|||
// skip no error test
|
|||
if ($bad_vs == $vs)
|
|||
{
|
|||
continue;
|
|||
}
|
|||
$result = $this->object->errorCheck($bad_vs);
|
|||
$this->assertFalse($result);
|
|||
}
|
|||
}
|
|||
}
|
|||
/**
|
|||
* Tests two error check
|
|||
* @covers Parity_Variable_Key_Generator::errorCheck
|
|||
*/
|
|||
public function testTwoErrorsCheck() {
|
|||
$vs = $this->object->generate(rand(1, 999999));
|
|||
// two errors in variable symbol
|
|||
for ($i = 0; $i < 10; $i++) // move first error position
|
|||
{
|
|||
for ($j = 0; $j < 10; $j++) // change first value
|
|||
{
|
|||
for ($k = $i+1; $k < 10; $k++) // move second error position
|
|||
{
|
|||
for ($l = 0; $l < 10; $l++) // change second value
|
|||
{
|
|||
$bad_vs = $vs;
|
|||
$bad_vs[$i] = strval($j);
|
|||
$bad_vs[$k] = strval($l);
|
|||
// skip no error test
|
|||
if ($bad_vs == $vs)
|
|||
{
|
|||
continue;
|
|||
}
|
|||
$result = $this->object->errorCheck($bad_vs);
|
|||
$this->assertFalse($result);
|
|||
}
|
|||
}
|
|||
}
|
|||
}
|
|||
}
|
|||
/**
|
|||
* Tests one error correction
|
|||
* @covers Parity_Variable_Key_Generator::errorCorrection
|
|||
*/
|
|||
public function testOneErrorCorrection() {
|
|||
$vs = $this->object->generate(rand(1, 999999));
|
|||
// one error in variable symbol
|
|||
for ($i = 0; $i < 10; $i++) // move error position
|
|||
{
|
|||
for ($j = 0; $j < 10; $j++) // change value
|
|||
{
|
|||
$bad_vs = $vs;
|
|||
$bad_vs[$i] = strval($j);
|
|||
// skip no error test
|
|||
if ($bad_vs == $vs)
|
|||
{
|
|||
continue;
|
|||
}
|
|||
$result = $this->object->errorCorrection($bad_vs);
|
|||
$this->assertTrue($result['status']);
|
|||
$this->assertEquals($result['corrected_variable_key'], $vs);
|
|||
}
|
|||
}
|
|||
}
|
|||
}
|