MediaWiki  REL1_24
UIDGeneratorTest.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class UIDGeneratorTest extends MediaWikiTestCase {
00004 
00005     protected function tearDown() {
00006         // Bug: 44850
00007         UIDGenerator::unitTestTearDown();
00008         parent::tearDown();
00009     }
00010 
00016     public function testTimestampedUID( $method, $digitlen, $bits, $tbits, $hostbits ) {
00017         $id = call_user_func( array( 'UIDGenerator', $method ) );
00018         $this->assertEquals( true, ctype_digit( $id ), "UID made of digit characters" );
00019         $this->assertLessThanOrEqual( $digitlen, strlen( $id ),
00020             "UID has the right number of digits" );
00021         $this->assertLessThanOrEqual( $bits, strlen( wfBaseConvert( $id, 10, 2 ) ),
00022             "UID has the right number of bits" );
00023 
00024         $ids = array();
00025         for ( $i = 0; $i < 300; $i++ ) {
00026             $ids[] = call_user_func( array( 'UIDGenerator', $method ) );
00027         }
00028 
00029         $lastId = array_shift( $ids );
00030 
00031         $this->assertArrayEquals( array_unique( $ids ), $ids, "All generated IDs are unique." );
00032 
00033         foreach ( $ids as $id ) {
00034             $id_bin = wfBaseConvert( $id, 10, 2 );
00035             $lastId_bin = wfBaseConvert( $lastId, 10, 2 );
00036 
00037             $this->assertGreaterThanOrEqual(
00038                 substr( $id_bin, 0, $tbits ),
00039                 substr( $lastId_bin, 0, $tbits ),
00040                 "New ID timestamp ($id_bin) >= prior one ($lastId_bin)." );
00041 
00042             if ( $hostbits ) {
00043                 $this->assertEquals(
00044                     substr( $id_bin, 0, -$hostbits ),
00045                     substr( $lastId_bin, 0, -$hostbits ),
00046                     "Host ID of ($id_bin) is same as prior one ($lastId_bin)." );
00047             }
00048 
00049             $lastId = $id;
00050         }
00051     }
00052 
00057     public static function provider_testTimestampedUID() {
00058         return array(
00059             array( 'newTimestampedUID128', 39, 128, 46, 48 ),
00060             array( 'newTimestampedUID128', 39, 128, 46, 48 ),
00061             array( 'newTimestampedUID88', 27, 88, 46, 32 ),
00062         );
00063     }
00064 
00068     public function testUUIDv4() {
00069         for ( $i = 0; $i < 100; $i++ ) {
00070             $id = UIDGenerator::newUUIDv4();
00071             $this->assertEquals( true,
00072                 preg_match( '!^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$!', $id ),
00073                 "UID $id has the right format" );
00074         }
00075     }
00076 
00080     public function testRawUUIDv4() {
00081         for ( $i = 0; $i < 100; $i++ ) {
00082             $id = UIDGenerator::newRawUUIDv4();
00083             $this->assertEquals( true,
00084                 preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
00085                 "UID $id has the right format" );
00086         }
00087     }
00088 
00092     public function testRawUUIDv4QuickRand() {
00093         for ( $i = 0; $i < 100; $i++ ) {
00094             $id = UIDGenerator::newRawUUIDv4( UIDGenerator::QUICK_RAND );
00095             $this->assertEquals( true,
00096                 preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
00097                 "UID $id has the right format" );
00098         }
00099     }
00100 
00104     public function testNewSequentialID() {
00105         $id1 = UIDGenerator::newSequentialPerNodeID( 'test', 32 );
00106         $id2 = UIDGenerator::newSequentialPerNodeID( 'test', 32 );
00107 
00108         $this->assertType( 'float', $id1, "ID returned as float" );
00109         $this->assertType( 'float', $id2, "ID returned as float" );
00110         $this->assertGreaterThan( 0, $id1, "ID greater than 1" );
00111         $this->assertGreaterThan( $id1, $id2, "IDs increasing in value" );
00112     }
00113 
00117     public function testNewSequentialIDs() {
00118         $ids = UIDGenerator::newSequentialPerNodeIDs( 'test', 32, 5 );
00119         $lastId = null;
00120         foreach ( $ids as $id ) {
00121             $this->assertType( 'float', $id, "ID returned as float" );
00122             $this->assertGreaterThan( 0, $id, "ID greater than 1" );
00123             if ( $lastId ) {
00124                 $this->assertGreaterThan( $lastId, $id, "IDs increasing in value" );
00125             }
00126             $lastId = $id;
00127         }
00128     }
00129 }