8f78ae512612b0f034ed5d0306e0075f2e584b03
3 class UIDGeneratorTest
extends MediaWikiTestCase
{
6 * @dataProvider provider_testTimestampedUID
7 * @covers UIDGenerator::newTimestampedUID128
8 * @covers UIDGenerator::newTimestampedUID88
10 public function testTimestampedUID( $method, $digitlen, $bits, $tbits, $hostbits ) {
11 $id = call_user_func( array( 'UIDGenerator', $method ) );
12 $this->assertEquals( true, ctype_digit( $id ), "UID made of digit characters" );
13 $this->assertLessThanOrEqual( $digitlen, strlen( $id ),
14 "UID has the right number of digits" );
15 $this->assertLessThanOrEqual( $bits, strlen( wfBaseConvert( $id, 10, 2 ) ),
16 "UID has the right number of bits" );
19 for ( $i = 0; $i < 300; $i++
) {
20 $ids[] = call_user_func( array( 'UIDGenerator', $method ) );
23 $lastId = array_shift( $ids );
25 $lastHost = substr( wfBaseConvert( $lastId, 10, 2, $bits ), -$hostbits );
28 $this->assertArrayEquals( array_unique( $ids ), $ids, "All generated IDs are unique." );
30 foreach ( $ids as $id ) {
31 $id_bin = wfBaseConvert( $id, 10, 2 );
32 $lastId_bin = wfBaseConvert( $lastId, 10, 2 );
34 $this->assertGreaterThanOrEqual(
35 substr( $id_bin, 0, $tbits ),
36 substr( $lastId_bin, 0, $tbits ),
37 "New ID timestamp ($id_bin) >= prior one ($lastId_bin)." );
41 substr( $id_bin, 0, -$hostbits ),
42 substr( $lastId_bin, 0, -$hostbits ),
43 "Host ID of ($id_bin) is same as prior one ($lastId_bin)." );
51 * array( method, length, bits, hostbits )
52 * NOTE: When adding a new method name here please update the covers tags for the tests!
54 public static function provider_testTimestampedUID() {
56 array( 'newTimestampedUID128', 39, 128, 46, 48 ),
57 array( 'newTimestampedUID128', 39, 128, 46, 48 ),
58 array( 'newTimestampedUID88', 27, 88, 46, 32 ),
63 * @covers UIDGenerator::newUUIDv4
65 public function testUUIDv4() {
66 for ( $i = 0; $i < 100; $i++
) {
67 $id = UIDGenerator
::newUUIDv4();
68 $this->assertEquals( true,
69 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 ),
70 "UID $id has the right format" );
75 * @covers UIDGenerator::newRawUUIDv4
77 public function testRawUUIDv4() {
78 for ( $i = 0; $i < 100; $i++
) {
79 $id = UIDGenerator
::newRawUUIDv4();
80 $this->assertEquals( true,
81 preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
82 "UID $id has the right format" );
87 * @covers UIDGenerator::newRawUUIDv4
89 public function testRawUUIDv4QuickRand() {
90 for ( $i = 0; $i < 100; $i++
) {
91 $id = UIDGenerator
::newRawUUIDv4( UIDGenerator
::QUICK_RAND
);
92 $this->assertEquals( true,
93 preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
94 "UID $id has the right format" );