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