3 class UIDGeneratorTest
extends PHPUnit_Framework_TestCase
{
5 protected function tearDown() {
7 UIDGenerator
::unitTestTearDown();
12 * Flaky test (T131549).
15 * @dataProvider provider_testTimestampedUID
16 * @covers UIDGenerator::newTimestampedUID128
17 * @covers UIDGenerator::newTimestampedUID88
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" );
28 for ( $i = 0; $i < 300; $i++
) {
29 $ids[] = call_user_func( [ 'UIDGenerator', $method ] );
32 $lastId = array_shift( $ids );
34 $this->assertSame( array_unique( $ids ), $ids, "All generated IDs are unique." );
36 foreach ( $ids as $id ) {
37 $id_bin = Wikimedia\base_convert
( $id, 10, 2 );
38 $lastId_bin = Wikimedia\base_convert
( $lastId, 10, 2 );
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)." );
47 substr( $id_bin, -$hostbits ),
48 substr( $lastId_bin, -$hostbits ),
49 "Host ID of ($id_bin) is same as prior one ($lastId_bin)." );
57 * array( method, length, bits, hostbits )
58 * NOTE: When adding a new method name here please update the covers tags for the tests!
60 public static function provider_testTimestampedUID() {
62 [ 'newTimestampedUID128', 39, 128, 46, 48 ],
63 [ 'newTimestampedUID128', 39, 128, 46, 48 ],
64 [ 'newTimestampedUID88', 27, 88, 46, 32 ],
69 * @covers UIDGenerator::newUUIDv1
71 public function testUUIDv1() {
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" );
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" );
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" );
91 $this->assertEquals( array_unique( $ids ), $ids, "All generated IDs are unique." );
95 * @covers UIDGenerator::newUUIDv4
97 public function testUUIDv4() {
99 for ( $i = 0; $i < 100; $i++
) {
100 $id = UIDGenerator
::newUUIDv4();
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" );
107 $this->assertEquals( array_unique( $ids ), $ids, 'All generated IDs are unique.' );
111 * @covers UIDGenerator::newRawUUIDv4
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" );
123 * @covers UIDGenerator::newRawUUIDv4
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" );
135 * @covers UIDGenerator::newSequentialPerNodeID
137 public function testNewSequentialID() {
138 $id1 = UIDGenerator
::newSequentialPerNodeID( 'test', 32 );
139 $id2 = UIDGenerator
::newSequentialPerNodeID( 'test', 32 );
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" );
148 * @covers UIDGenerator::newSequentialPerNodeIDs
150 public function testNewSequentialIDs() {
151 $ids = UIDGenerator
::newSequentialPerNodeIDs( 'test', 32, 5 );
153 foreach ( $ids as $id ) {
154 $this->assertInternalType( 'float', $id, "ID returned as float" );
155 $this->assertGreaterThan( 0, $id, "ID greater than 1" );
157 $this->assertGreaterThan( $lastId, $id, "IDs increasing in value" );