3 class UIDGeneratorTest
extends PHPUnit_Framework_TestCase
{
5 use MediaWikiCoversValidator
;
7 protected function tearDown() {
9 UIDGenerator
::unitTestTearDown();
14 * Test that generated UIDs have the expected properties
16 * @dataProvider provider_testTimestampedUID
17 * @covers UIDGenerator::newTimestampedUID128
18 * @covers UIDGenerator::newTimestampedUID88
20 public function testTimestampedUID( $method, $digitlen, $bits, $tbits, $hostbits ) {
21 $id = call_user_func( [ 'UIDGenerator', $method ] );
22 $this->assertEquals( true, ctype_digit( $id ), "UID made of digit characters" );
23 $this->assertLessThanOrEqual( $digitlen, strlen( $id ),
24 "UID has the right number of digits" );
25 $this->assertLessThanOrEqual( $bits, strlen( Wikimedia\base_convert
( $id, 10, 2 ) ),
26 "UID has the right number of bits" );
29 for ( $i = 0; $i < 300; $i++
) {
30 $ids[] = call_user_func( [ 'UIDGenerator', $method ] );
33 $lastId = array_shift( $ids );
35 $this->assertSame( array_unique( $ids ), $ids, "All generated IDs are unique." );
37 foreach ( $ids as $id ) {
38 // Convert string to binary and pad to full length so we can
40 $id_bin = Wikimedia\base_convert
( $id, 10, 2, $bits );
41 $lastId_bin = Wikimedia\base_convert
( $lastId, 10, 2, $bits );
43 $timestamp_bin = substr( $id_bin, 0, $tbits );
44 $last_timestamp_bin = substr( $lastId_bin, 0, $tbits );
46 $this->assertGreaterThanOrEqual(
49 "timestamp ($timestamp_bin) of current ID ($id_bin) >= timestamp ($last_timestamp_bin) " .
50 "of prior one ($lastId_bin)" );
52 $hostbits_bin = substr( $id_bin, -$hostbits );
53 $last_hostbits_bin = substr( $lastId_bin, -$hostbits );
59 "Host ID ($hostbits_bin) of current ID ($id_bin) is same as host ID ($last_hostbits_bin) " .
60 "of prior one ($lastId_bin)." );
68 * array( method, length, bits, hostbits )
69 * NOTE: When adding a new method name here please update the covers tags for the tests!
71 public static function provider_testTimestampedUID() {
73 [ 'newTimestampedUID128', 39, 128, 46, 48 ],
74 [ 'newTimestampedUID128', 39, 128, 46, 48 ],
75 [ 'newTimestampedUID88', 27, 88, 46, 32 ],
80 * @covers UIDGenerator::newUUIDv1
82 public function testUUIDv1() {
84 for ( $i = 0; $i < 100; $i++
) {
85 $id = UIDGenerator
::newUUIDv1();
86 $this->assertEquals( true,
87 preg_match( '!^[0-9a-f]{8}-[0-9a-f]{4}-1[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$!', $id ),
88 "UID $id has the right format" );
91 $id = UIDGenerator
::newRawUUIDv1();
92 $this->assertEquals( true,
93 preg_match( '!^[0-9a-f]{12}1[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
94 "UID $id has the right format" );
96 $id = UIDGenerator
::newRawUUIDv1();
97 $this->assertEquals( true,
98 preg_match( '!^[0-9a-f]{12}1[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
99 "UID $id has the right format" );
102 $this->assertEquals( array_unique( $ids ), $ids, "All generated IDs are unique." );
106 * @covers UIDGenerator::newUUIDv4
108 public function testUUIDv4() {
110 for ( $i = 0; $i < 100; $i++
) {
111 $id = UIDGenerator
::newUUIDv4();
113 $this->assertEquals( true,
114 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 ),
115 "UID $id has the right format" );
118 $this->assertEquals( array_unique( $ids ), $ids, 'All generated IDs are unique.' );
122 * @covers UIDGenerator::newRawUUIDv4
124 public function testRawUUIDv4() {
125 for ( $i = 0; $i < 100; $i++
) {
126 $id = UIDGenerator
::newRawUUIDv4();
127 $this->assertEquals( true,
128 preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
129 "UID $id has the right format" );
134 * @covers UIDGenerator::newRawUUIDv4
136 public function testRawUUIDv4QuickRand() {
137 for ( $i = 0; $i < 100; $i++
) {
138 $id = UIDGenerator
::newRawUUIDv4( UIDGenerator
::QUICK_RAND
);
139 $this->assertEquals( true,
140 preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
141 "UID $id has the right format" );
146 * @covers UIDGenerator::newSequentialPerNodeID
148 public function testNewSequentialID() {
149 $id1 = UIDGenerator
::newSequentialPerNodeID( 'test', 32 );
150 $id2 = UIDGenerator
::newSequentialPerNodeID( 'test', 32 );
152 $this->assertInternalType( 'float', $id1, "ID returned as float" );
153 $this->assertInternalType( 'float', $id2, "ID returned as float" );
154 $this->assertGreaterThan( 0, $id1, "ID greater than 1" );
155 $this->assertGreaterThan( $id1, $id2, "IDs increasing in value" );
159 * @covers UIDGenerator::newSequentialPerNodeIDs
161 public function testNewSequentialIDs() {
162 $ids = UIDGenerator
::newSequentialPerNodeIDs( 'test', 32, 5 );
164 foreach ( $ids as $id ) {
165 $this->assertInternalType( 'float', $id, "ID returned as float" );
166 $this->assertGreaterThan( 0, $id, "ID greater than 1" );
168 $this->assertGreaterThan( $lastId, $id, "IDs increasing in value" );