]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - tests/Core/UUIDTest.php
[CORE] Make tests great gain
[quix0rs-gnu-social.git] / tests / Core / UUIDTest.php
1 <?php
2 // This file is part of GNU social - https://www.gnu.org/software/social
3 //
4 // GNU social is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // GNU social is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with GNU social.  If not, see <http://www.gnu.org/licenses/>.
16
17 namespace Tests\Unit;
18
19 if (!defined('INSTALLDIR')) {
20     define('INSTALLDIR', dirname(dirname(__DIR__)));
21 }
22 if (!defined('GNUSOCIAL')) {
23     define('GNUSOCIAL', true);
24 }
25 if (!defined('STATUSNET')) { // Compatibility
26     define('STATUSNET', true);
27 }
28
29 use PHPUnit\Framework\TestCase;
30 use UUID;
31
32 require_once INSTALLDIR . '/lib/common.php';
33
34 final class UUIDTest extends TestCase
35 {
36     public function testGenerate()
37     {
38         $result = UUID::gen();
39         $this->assertRegExp('/^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$/',
40             $result);
41         // Check version number
42         $this->assertEquals(0x4000, hexdec(substr($result, 14, 4)) & 0xF000);
43         $this->assertEquals(0x8000, hexdec(substr($result, 19, 4)) & 0xC000);
44     }
45
46     public function testUnique()
47     {
48         $reps = 100;
49         $ids = array();
50
51         for ($i = 0; $i < $reps; $i++) {
52             $ids[] = UUID::gen();
53         }
54
55         $this->assertEquals(count($ids), count(array_unique($ids)), "UUIDs must be unique");
56     }
57 }
58