Merge "Allow API results to wrap long lines"
[lhc/web/wiklou.git] / tests / phpunit / includes / UIDGeneratorTest.php
1 <?php
2
3 class UIDGeneratorTest extends MediaWikiTestCase {
4 /**
5 * @dataProvider provider_testTimestampedUID
6 */
7 public function testTimestampedUID( $method, $digitlen, $bits, $tbits, $hostbits ) {
8 $id = call_user_func( array( 'UIDGenerator', $method ) );
9 $this->assertEquals( true, ctype_digit( $id ), "UID made of digit characters" );
10 $this->assertLessThanOrEqual( $digitlen, strlen( $id ),
11 "UID has the right number of digits" );
12 $this->assertLessThanOrEqual( $bits, strlen( wfBaseConvert( $id, 10, 2 ) ),
13 "UID has the right number of bits" );
14
15 $ids = array();
16 for ( $i = 0; $i < 300; $i++ ) {
17 $ids[] = call_user_func( array( 'UIDGenerator', $method ) );
18 }
19
20 $lastId = array_shift( $ids );
21 if ( $hostbits ) {
22 $lastHost = substr( wfBaseConvert( $lastId, 10, 2, $bits ), -$hostbits );
23 }
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 */
50 public static function provider_testTimestampedUID() {
51 return array(
52 array( 'newTimestampedUID128', 39, 128, 46, 48 ),
53 array( 'newTimestampedUID128', 39, 128, 46, 48 ),
54 array( 'newTimestampedUID88', 27, 88, 46, 32 ),
55 );
56 }
57
58 public function testUUIDv4() {
59 for ( $i = 0; $i < 100; $i++ ) {
60 $id = UIDGenerator::newUUIDv4();
61 $this->assertEquals( true,
62 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 ),
63 "UID $id has the right format" );
64
65 $id = UIDGenerator::newRawUUIDv4();
66 $this->assertEquals( true,
67 preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
68 "UID $id has the right format" );
69
70 $id = UIDGenerator::newRawUUIDv4( UIDGenerator::QUICK_RAND );
71 $this->assertEquals( true,
72 preg_match( '!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!', $id ),
73 "UID $id has the right format" );
74 }
75 }
76 }