3 class UIDGeneratorTest
extends MediaWikiTestCase
{
5 protected function tearDown() {
7 UIDGenerator
::unitTestTearDown();
12 * @dataProvider provider_testTimestampedUID
13 * @covers UIDGenerator::newTimestampedUID128
14 * @covers UIDGenerator::newTimestampedUID88
16 public function testTimestampedUID( $method, $digitlen, $bits, $tbits, $hostbits ) {
17 $id = call_user_func( array( 'UIDGenerator', $method ) );
18 $this->assertEquals( true, ctype_digit( $id ), "UID made of digit characters" );
19 $this->assertLessThanOrEqual( $digitlen, strlen( $id ),
20 "UID has the right number of digits" );
21 $this->assertLessThanOrEqual( $bits, strlen( wfBaseConvert( $id, 10, 2 ) ),
22 "UID has the right number of bits" );
25 for ( $i = 0; $i < 300; $i++
) {
26 $ids[] = call_user_func( array( 'UIDGenerator', $method ) );
29 $lastId = array_shift( $ids );
31 $this->assertArrayEquals( array_unique( $ids ), $ids, "All generated IDs are unique." );
33 foreach ( $ids as $id ) {
34 $id_bin = wfBaseConvert( $id, 10, 2 );
35 $lastId_bin = wfBaseConvert( $lastId, 10, 2 );
37 $this->assertGreaterThanOrEqual(
38 substr( $lastId_bin, 0, $tbits ),
39 substr( $id_bin, 0, $tbits ),
40 "New ID timestamp ($id_bin) >= prior one ($lastId_bin)." );
44 substr( $id_bin, -$hostbits ),
45 substr( $lastId_bin, -$hostbits ),
46 "Host ID of ($id_bin) is same as prior one ($lastId_bin)." );
54 * array( method, length, bits, hostbits )
55 * NOTE: When adding a new method name here please update the covers tags for the tests!
57 public static function provider_testTimestampedUID() {
59 array( 'newTimestampedUID128', 39, 128, 46, 48 ),
60 array( 'newTimestampedUID128', 39, 128, 46, 48 ),
61 array( 'newTimestampedUID88', 27, 88, 46, 32 ),
66 * @covers UIDGenerator::newUUIDv4
68 public function testUUIDv4() {
69 for ( $i = 0; $i < 100; $i++
) {
70 $id = UIDGenerator
::newUUIDv4();
71 $this->assertEquals( true,
72 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 ),
73 "UID $id has the right format" );
78 * @covers UIDGenerator::newRawUUIDv4
80 public function testRawUUIDv4() {
81 for ( $i = 0; $i < 100; $i++
) {
82 $id = UIDGenerator
::newRawUUIDv4();
83 $this->assertEquals( true,
84 preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
85 "UID $id has the right format" );
90 * @covers UIDGenerator::newRawUUIDv4
92 public function testRawUUIDv4QuickRand() {
93 for ( $i = 0; $i < 100; $i++
) {
94 $id = UIDGenerator
::newRawUUIDv4( UIDGenerator
::QUICK_RAND
);
95 $this->assertEquals( true,
96 preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
97 "UID $id has the right format" );
102 * @covers UIDGenerator::newSequentialPerNodeID
104 public function testNewSequentialID() {
105 $id1 = UIDGenerator
::newSequentialPerNodeID( 'test', 32 );
106 $id2 = UIDGenerator
::newSequentialPerNodeID( 'test', 32 );
108 $this->assertType( 'float', $id1, "ID returned as float" );
109 $this->assertType( 'float', $id2, "ID returned as float" );
110 $this->assertGreaterThan( 0, $id1, "ID greater than 1" );
111 $this->assertGreaterThan( $id1, $id2, "IDs increasing in value" );
115 * @covers UIDGenerator::newSequentialPerNodeIDs
117 public function testNewSequentialIDs() {
118 $ids = UIDGenerator
::newSequentialPerNodeIDs( 'test', 32, 5 );
120 foreach ( $ids as $id ) {
121 $this->assertType( 'float', $id, "ID returned as float" );
122 $this->assertGreaterThan( 0, $id, "ID greater than 1" );
124 $this->assertGreaterThan( $lastId, $id, "IDs increasing in value" );