| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\User\TempUser; |
| 4 |
|
| 5 |
use MediaWiki\User\TempUser\ScrambleMapping; |
| 6 |
use OutOfBoundsException; |
| 7 |
use PHPUnit\Framework\TestCase; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class ScrambleMappingTest extends TestCase { |
| 13 |
public function testMap() { |
| 14 |
if ( !extension_loaded( 'gmp' ) && !extension_loaded( 'bcmath' ) ) { |
| 15 |
$this->markTestSkipped( 'need extension gmp or bcmath' ); |
| 16 |
} |
| 17 |
$map = new ScrambleMapping( [] ); |
| 18 |
$duplicates = 0; |
| 19 |
|
| 20 |
$n = 200; |
| 21 |
|
| 22 |
$bitArray = str_repeat( "\0", $n * 10 / 8 ); |
| 23 |
for ( $i = 0; $i < $n; $i++ ) { |
| 24 |
$value = (int)$map->getSerialIdForIndex( $i ); |
| 25 |
$minor = $value % 8; |
| 26 |
$major = ( $value - $minor ) / 8; |
| 27 |
$prevBits = ord( $bitArray[$major] ); |
| 28 |
$prevStatus = ( $prevBits & ( 1 << $minor ) ); |
| 29 |
$duplicates += ( $prevStatus ? 1 : 0 ); |
| 30 |
$newBits = $prevBits | ( 1 << $minor ); |
| 31 |
$bitArray[$major] = chr( $newBits ); |
| 32 |
} |
| 33 |
$this->assertSame( 0, $duplicates, 'duplicate detected' ); |
| 34 |
} |
| 35 |
|
| 36 |
public function testOffsetTooLarge() { |
| 37 |
$map = new ScrambleMapping( [ 'offset' => 10 ] ); |
| 38 |
$this->expectException( OutOfBoundsException::class ); |
| 39 |
$map->getSerialIdForIndex( 1 ); |
| 40 |
} |
| 41 |
} |