We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

UUIDTest.php 1.03 KB
Newer Older
Evan Prodromou's avatar
Evan Prodromou committed
1 2 3 4 5 6 7 8
<?php

if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
    print "This script must be run from the command line\n";
    exit();
}

define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
9 10
define('GNUSOCIAL', true);
define('STATUSNET', true);  // compatibility
Evan Prodromou's avatar
Evan Prodromou committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24

require_once INSTALLDIR . '/lib/common.php';

class UUIDTest extends PHPUnit_Framework_TestCase
{
    public function testGenerate()
    {
        $result = UUID::gen();
        $this->assertRegExp('/^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$/',
                            $result);
        // Check version number
        $this->assertEquals(0x4000, hexdec(substr($result, 14, 4)) & 0xF000);
        $this->assertEquals(0x8000, hexdec(substr($result, 19, 4)) & 0xC000);
    }
25 26 27 28 29 30 31 32 33 34 35 36

    public function testUnique()
    {
        $reps = 100;
        $ids = array();

        for ($i = 0; $i < $reps; $i++) {
            $ids[] = UUID::gen();
        }

        $this->assertEquals(count($ids), count(array_unique($ids)), "UUIDs must be unique");
    }
Evan Prodromou's avatar
Evan Prodromou committed
37 38
}