gnu-social/tests/Core/UUIDTest.php

63 lines
1.9 KiB
PHP
Raw Permalink Normal View History

2019-07-13 00:31:14 +09:00
<?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
namespace Tests\Unit;
if (!defined('INSTALLDIR')) {
define('INSTALLDIR', dirname(dirname(__DIR__)));
}
2019-07-25 09:29:20 +09:00
if (!defined('PUBLICDIR')) {
define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
}
2019-07-13 00:31:14 +09:00
if (!defined('GNUSOCIAL')) {
define('GNUSOCIAL', true);
}
if (!defined('STATUSNET')) { // Compatibility
define('STATUSNET', true);
}
use PHPUnit\Framework\TestCase;
use UUID;
2019-08-23 21:36:02 +09:00
require_once INSTALLDIR . '/lib/util/common.php';
2019-07-13 00:31:14 +09:00
final class UUIDTest extends TestCase
{
public function testGenerate()
{
$result = UUID::gen();
2020-06-24 21:08:11 +09:00
static::assertRegExp(
'/^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$/',
$result
);
2019-07-13 00:31:14 +09:00
// Check version number
2020-06-24 21:08:11 +09:00
static::assertSame(0x4000, hexdec(substr($result, 14, 4)) & 0xF000);
static::assertSame(0x8000, hexdec(substr($result, 19, 4)) & 0xC000);
2019-07-13 00:31:14 +09:00
}
public function testUnique()
{
$reps = 100;
2020-06-24 21:08:11 +09:00
$ids = [];
2019-07-13 00:31:14 +09:00
2020-06-24 21:08:11 +09:00
for ($i = 0; $i < $reps; ++$i) {
2019-07-13 00:31:14 +09:00
$ids[] = UUID::gen();
}
2020-06-24 21:08:11 +09:00
static::assertSame(count($ids), count(array_unique($ids)), 'UUIDs must be unique');
2019-07-13 00:31:14 +09:00
}
}