Merge "Add .gitignore to the /skins directory"
[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 if ( $hostbits ) {
25 $lastHost = substr( wfBaseConvert( $lastId, 10, 2, $bits ), -$hostbits );
26 }
27
28 $this->assertArrayEquals( array_unique( $ids ), $ids, "All generated IDs are unique." );
29
30 foreach ( $ids as $id ) {
31 $id_bin = wfBaseConvert( $id, 10, 2 );
32 $lastId_bin = wfBaseConvert( $lastId, 10, 2 );
33
34 $this->assertGreaterThanOrEqual(
35 substr( $id_bin, 0, $tbits ),
36 substr( $lastId_bin, 0, $tbits ),
37 "New ID timestamp ($id_bin) >= prior one ($lastId_bin)." );
38
39 if ( $hostbits ) {
40 $this->assertEquals(
41 substr( $id_bin, 0, -$hostbits ),
42 substr( $lastId_bin, 0, -$hostbits ),
43 "Host ID of ($id_bin) is same as prior one ($lastId_bin)." );
44 }
45
46 $lastId = $id;
47 }
48 }
49
50 /**
51 * array( method, length, bits, hostbits )
52 * NOTE: When adding a new method name here please update the covers tags for the tests!
53 */
54 public static function provider_testTimestampedUID() {
55 return array(
56 array( 'newTimestampedUID128', 39, 128, 46, 48 ),
57 array( 'newTimestampedUID128', 39, 128, 46, 48 ),
58 array( 'newTimestampedUID88', 27, 88, 46, 32 ),
59 );
60 }
61
62 /**
63 * @covers UIDGenerator::newUUIDv4
64 */
65 public function testUUIDv4() {
66 for ( $i = 0; $i < 100; $i++ ) {
67 $id = UIDGenerator::newUUIDv4();
68 $this->assertEquals( true,
69 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 ),
70 "UID $id has the right format" );
71 }
72 }
73
74 /**
75 * @covers UIDGenerator::newRawUUIDv4
76 */
77 public function testRawUUIDv4() {
78 for ( $i = 0; $i < 100; $i++ ) {
79 $id = UIDGenerator::newRawUUIDv4();
80 $this->assertEquals( true,
81 preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
82 "UID $id has the right format" );
83 }
84 }
85
86 /**
87 * @covers UIDGenerator::newRawUUIDv4
88 */
89 public function testRawUUIDv4QuickRand() {
90 for ( $i = 0; $i < 100; $i++ ) {
91 $id = UIDGenerator::newRawUUIDv4( UIDGenerator::QUICK_RAND );
92 $this->assertEquals( true,
93 preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
94 "UID $id has the right format" );
95 }
96 }
97
98 }