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