Merge "Clean up ExportDemoTest"
[lhc/web/wiklou.git] / tests / phpunit / includes / utils / UIDGeneratorTest.php
1 <?php
2
3 class UIDGeneratorTest extends MediaWikiTestCase {
4
5 /**
6 * @dataProvider provider_testTimestampedUID
7 * @covers UIDGenerator::newTimestampedUID128
8 * @covers UIDGenerator::newTimestampedUID88
9 */
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" );
17
18 $ids = array();
19 for ( $i = 0; $i < 300; $i++ ) {
20 $ids[] = call_user_func( array( 'UIDGenerator', $method ) );
21 }
22
23 $lastId = array_shift( $ids );
24
25 $this->assertArrayEquals( array_unique( $ids ), $ids, "All generated IDs are unique." );
26
27 foreach ( $ids as $id ) {
28 $id_bin = wfBaseConvert( $id, 10, 2 );
29 $lastId_bin = wfBaseConvert( $lastId, 10, 2 );
30
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)." );
35
36 if ( $hostbits ) {
37 $this->assertEquals(
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)." );
41 }
42
43 $lastId = $id;
44 }
45 }
46
47 /**
48 * array( method, length, bits, hostbits )
49 * NOTE: When adding a new method name here please update the covers tags for the tests!
50 */
51 public static function provider_testTimestampedUID() {
52 return array(
53 array( 'newTimestampedUID128', 39, 128, 46, 48 ),
54 array( 'newTimestampedUID128', 39, 128, 46, 48 ),
55 array( 'newTimestampedUID88', 27, 88, 46, 32 ),
56 );
57 }
58
59 /**
60 * @covers UIDGenerator::newUUIDv4
61 */
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" );
68 }
69 }
70
71 /**
72 * @covers UIDGenerator::newRawUUIDv4
73 */
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" );
80 }
81 }
82
83 /**
84 * @covers UIDGenerator::newRawUUIDv4
85 */
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" );
92 }
93 }
94
95 /**
96 * @covers UIDGenerator::newSequentialPerNodeID
97 */
98 public function testNewSequentialID() {
99 $id1 = UIDGenerator::newSequentialPerNodeID( 'test', 32 );
100 $id2 = UIDGenerator::newSequentialPerNodeID( 'test', 32 );
101
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" );
106 }
107
108 /**
109 * @covers UIDGenerator::newSequentialPerNodeIDs
110 */
111 public function testNewSequentialIDs() {
112 $ids = UIDGenerator::newSequentialPerNodeIDs( 'test', 32, 5 );
113 $lastId = null;
114 foreach ( $ids as $id ) {
115 $this->assertType( 'float', $id, "ID returned as float" );
116 $this->assertGreaterThan( 0, $id, "ID greater than 1" );
117 if ( $lastId ) {
118 $this->assertGreaterThan( $lastId, $id, "IDs increasing in value" );
119 }
120 $lastId = $id;
121 }
122 }
123 }