Merge "Introduce top level service locator."
[lhc/web/wiklou.git] / tests / phpunit / includes / utils / UIDGeneratorTest.php
1 <?php
2
3 class UIDGeneratorTest extends PHPUnit_Framework_TestCase {
4
5 protected function tearDown() {
6 // Bug: 44850
7 UIDGenerator::unitTestTearDown();
8 parent::tearDown();
9 }
10
11 /**
12 * Flaky test (T131549).
13 *
14 * @group Broken
15 * @dataProvider provider_testTimestampedUID
16 * @covers UIDGenerator::newTimestampedUID128
17 * @covers UIDGenerator::newTimestampedUID88
18 */
19 public function testTimestampedUID( $method, $digitlen, $bits, $tbits, $hostbits ) {
20 $id = call_user_func( [ 'UIDGenerator', $method ] );
21 $this->assertEquals( true, ctype_digit( $id ), "UID made of digit characters" );
22 $this->assertLessThanOrEqual( $digitlen, strlen( $id ),
23 "UID has the right number of digits" );
24 $this->assertLessThanOrEqual( $bits, strlen( Wikimedia\base_convert( $id, 10, 2 ) ),
25 "UID has the right number of bits" );
26
27 $ids = [];
28 for ( $i = 0; $i < 300; $i++ ) {
29 $ids[] = call_user_func( [ 'UIDGenerator', $method ] );
30 }
31
32 $lastId = array_shift( $ids );
33
34 $this->assertSame( array_unique( $ids ), $ids, "All generated IDs are unique." );
35
36 foreach ( $ids as $id ) {
37 $id_bin = Wikimedia\base_convert( $id, 10, 2 );
38 $lastId_bin = Wikimedia\base_convert( $lastId, 10, 2 );
39
40 $this->assertGreaterThanOrEqual(
41 substr( $lastId_bin, 0, $tbits ),
42 substr( $id_bin, 0, $tbits ),
43 "New ID timestamp ($id_bin) >= prior one ($lastId_bin)." );
44
45 if ( $hostbits ) {
46 $this->assertEquals(
47 substr( $id_bin, -$hostbits ),
48 substr( $lastId_bin, -$hostbits ),
49 "Host ID of ($id_bin) is same as prior one ($lastId_bin)." );
50 }
51
52 $lastId = $id;
53 }
54 }
55
56 /**
57 * array( method, length, bits, hostbits )
58 * NOTE: When adding a new method name here please update the covers tags for the tests!
59 */
60 public static function provider_testTimestampedUID() {
61 return [
62 [ 'newTimestampedUID128', 39, 128, 46, 48 ],
63 [ 'newTimestampedUID128', 39, 128, 46, 48 ],
64 [ 'newTimestampedUID88', 27, 88, 46, 32 ],
65 ];
66 }
67
68 /**
69 * @covers UIDGenerator::newUUIDv1
70 */
71 public function testUUIDv1() {
72 $ids = [];
73 for ( $i = 0; $i < 100; $i++ ) {
74 $id = UIDGenerator::newUUIDv1();
75 $this->assertEquals( true,
76 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 ),
77 "UID $id has the right format" );
78 $ids[] = $id;
79
80 $id = UIDGenerator::newRawUUIDv1();
81 $this->assertEquals( true,
82 preg_match( '!^[0-9a-f]{12}1[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
83 "UID $id has the right format" );
84
85 $id = UIDGenerator::newRawUUIDv1();
86 $this->assertEquals( true,
87 preg_match( '!^[0-9a-f]{12}1[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
88 "UID $id has the right format" );
89 }
90
91 $this->assertEquals( array_unique( $ids ), $ids, "All generated IDs are unique." );
92 }
93
94 /**
95 * @covers UIDGenerator::newUUIDv4
96 */
97 public function testUUIDv4() {
98 $ids = [];
99 for ( $i = 0; $i < 100; $i++ ) {
100 $id = UIDGenerator::newUUIDv4();
101 $ids[] = $id;
102 $this->assertEquals( true,
103 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 ),
104 "UID $id has the right format" );
105 }
106
107 $this->assertEquals( array_unique( $ids ), $ids, 'All generated IDs are unique.' );
108 }
109
110 /**
111 * @covers UIDGenerator::newRawUUIDv4
112 */
113 public function testRawUUIDv4() {
114 for ( $i = 0; $i < 100; $i++ ) {
115 $id = UIDGenerator::newRawUUIDv4();
116 $this->assertEquals( true,
117 preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
118 "UID $id has the right format" );
119 }
120 }
121
122 /**
123 * @covers UIDGenerator::newRawUUIDv4
124 */
125 public function testRawUUIDv4QuickRand() {
126 for ( $i = 0; $i < 100; $i++ ) {
127 $id = UIDGenerator::newRawUUIDv4( UIDGenerator::QUICK_RAND );
128 $this->assertEquals( true,
129 preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
130 "UID $id has the right format" );
131 }
132 }
133
134 /**
135 * @covers UIDGenerator::newSequentialPerNodeID
136 */
137 public function testNewSequentialID() {
138 $id1 = UIDGenerator::newSequentialPerNodeID( 'test', 32 );
139 $id2 = UIDGenerator::newSequentialPerNodeID( 'test', 32 );
140
141 $this->assertInternalType( 'float', $id1, "ID returned as float" );
142 $this->assertInternalType( 'float', $id2, "ID returned as float" );
143 $this->assertGreaterThan( 0, $id1, "ID greater than 1" );
144 $this->assertGreaterThan( $id1, $id2, "IDs increasing in value" );
145 }
146
147 /**
148 * @covers UIDGenerator::newSequentialPerNodeIDs
149 */
150 public function testNewSequentialIDs() {
151 $ids = UIDGenerator::newSequentialPerNodeIDs( 'test', 32, 5 );
152 $lastId = null;
153 foreach ( $ids as $id ) {
154 $this->assertInternalType( 'float', $id, "ID returned as float" );
155 $this->assertGreaterThan( 0, $id, "ID greater than 1" );
156 if ( $lastId ) {
157 $this->assertGreaterThan( $lastId, $id, "IDs increasing in value" );
158 }
159 $lastId = $id;
160 }
161 }
162 }