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 $this->assertArrayEquals( array_unique( $ids ), $ids, "All generated IDs are unique." );
27 foreach ( $ids as $id ) {
28 $id_bin = wfBaseConvert( $id, 10, 2 );
29 $lastId_bin = wfBaseConvert( $lastId, 10, 2 );
31 $this->assertGreaterThanOrEqual(
32 substr( $id_bin, 0, $tbits ),
33 substr( $lastId_bin, 0, $tbits ),
34 "New ID timestamp ($id_bin) >= prior one ($lastId_bin)." );
38 substr( $id_bin, 0, -$hostbits ),
39 substr( $lastId_bin, 0, -$hostbits ),
40 "Host ID of ($id_bin) is same as prior one ($lastId_bin)." );
48 * array( method, length, bits, hostbits )
49 * NOTE: When adding a new method name here please update the covers tags for the tests!
51 public static function provider_testTimestampedUID() {
53 array( 'newTimestampedUID128', 39, 128, 46, 48 ),
54 array( 'newTimestampedUID128', 39, 128, 46, 48 ),
55 array( 'newTimestampedUID88', 27, 88, 46, 32 ),
60 * @covers UIDGenerator::newUUIDv4
62 public function testUUIDv4() {
63 for ( $i = 0; $i < 100; $i++
) {
64 $id = UIDGenerator
::newUUIDv4();
65 $this->assertEquals( true,
66 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 ),
67 "UID $id has the right format" );
72 * @covers UIDGenerator::newRawUUIDv4
74 public function testRawUUIDv4() {
75 for ( $i = 0; $i < 100; $i++
) {
76 $id = UIDGenerator
::newRawUUIDv4();
77 $this->assertEquals( true,
78 preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
79 "UID $id has the right format" );
84 * @covers UIDGenerator::newRawUUIDv4
86 public function testRawUUIDv4QuickRand() {
87 for ( $i = 0; $i < 100; $i++
) {
88 $id = UIDGenerator
::newRawUUIDv4( UIDGenerator
::QUICK_RAND
);
89 $this->assertEquals( true,
90 preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
91 "UID $id has the right format" );
96 * @covers UIDGenerator::newSequentialPerNodeID
98 public function testNewSequentialID() {
99 $id1 = UIDGenerator
::newSequentialPerNodeID( 'test', 32 );
100 $id2 = UIDGenerator
::newSequentialPerNodeID( 'test', 32 );
102 $this->assertType( 'float', $id1, "ID returned as float" );
103 $this->assertType( 'float', $id2, "ID returned as float" );
104 $this->assertGreaterThan( 0, $id1, "ID greater than 1" );
105 $this->assertGreaterThan( $id1, $id2, "IDs increasing in value" );
109 * @covers UIDGenerator::newSequentialPerNodeIDs
111 public function testNewSequentialIDs() {
112 $ids = UIDGenerator
::newSequentialPerNodeIDs( 'test', 32, 5 );
114 foreach ( $ids as $id ) {
115 $this->assertType( 'float', $id, "ID returned as float" );
116 $this->assertGreaterThan( 0, $id, "ID greater than 1" );
118 $this->assertGreaterThan( $lastId, $id, "IDs increasing in value" );