MediaWiki  REL1_21
UIDGeneratorTest.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class UIDGeneratorTest extends MediaWikiTestCase {
00007         public function testTimestampedUID( $method, $digitlen, $bits, $tbits, $hostbits ) {
00008                 $id = call_user_func( array( 'UIDGenerator', $method ) );
00009                 $this->assertEquals( true, ctype_digit( $id ), "UID made of digit characters" );
00010                 $this->assertLessThanOrEqual( $digitlen, strlen( $id ),
00011                         "UID has the right number of digits" );
00012                 $this->assertLessThanOrEqual( $bits, strlen( wfBaseConvert( $id, 10, 2 ) ),
00013                         "UID has the right number of bits" );
00014 
00015                 $ids = array();
00016                 for ( $i = 0; $i < 300; $i++ ) {
00017                         $ids[] = call_user_func( array( 'UIDGenerator', $method ) );
00018                 }
00019 
00020                 $lastId = array_shift( $ids );
00021                 if ( $hostbits ) {
00022                         $lastHost = substr( wfBaseConvert( $lastId, 10, 2, $bits ), -$hostbits );
00023                 }
00024 
00025                 $this->assertArrayEquals( array_unique( $ids ), $ids, "All generated IDs are unique." );
00026 
00027                 foreach ( $ids as $id ) {
00028                         $id_bin = wfBaseConvert( $id, 10, 2 );
00029                         $lastId_bin = wfBaseConvert( $lastId, 10, 2 );
00030 
00031                         $this->assertGreaterThanOrEqual(
00032                                 substr( $id_bin, 0, $tbits ),
00033                                 substr( $lastId_bin, 0, $tbits ),
00034                                 "New ID timestamp ($id_bin) >= prior one ($lastId_bin)." );
00035 
00036                         if ( $hostbits ) {
00037                                 $this->assertEquals(
00038                                         substr( $id_bin, 0, -$hostbits ),
00039                                         substr( $lastId_bin, 0, -$hostbits ),
00040                                         "Host ID of ($id_bin) is same as prior one ($lastId_bin)." );
00041                         }
00042 
00043                         $lastId = $id;
00044                 }
00045         }
00046 
00050         public static function provider_testTimestampedUID() {
00051                 return array(
00052                         array( 'newTimestampedUID128', 39, 128, 46, 48 ),
00053                         array( 'newTimestampedUID128', 39, 128, 46, 48 ),
00054                         array( 'newTimestampedUID88', 27, 88, 46, 32 ),
00055                 );
00056         }
00057 
00058         public function testUUIDv4() {
00059                 for ( $i = 0; $i < 100; $i++ ) {
00060                         $id = UIDGenerator::newUUIDv4();
00061                         $this->assertEquals( true,
00062                                 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 ),
00063                                 "UID $id has the right format" );
00064 
00065                         $id = UIDGenerator::newRawUUIDv4();
00066                         $this->assertEquals( true,
00067                                 preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
00068                                 "UID $id has the right format" );
00069 
00070                         $id = UIDGenerator::newRawUUIDv4( UIDGenerator::QUICK_RAND );
00071                         $this->assertEquals( true,
00072                                 preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
00073                                 "UID $id has the right format" );
00074                 }
00075         }
00076 }