UUIDTest.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
  3. print "This script must be run from the command line\n";
  4. exit();
  5. }
  6. define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
  7. define('GNUSOCIAL', true);
  8. define('STATUSNET', true); // compatibility
  9. require_once INSTALLDIR . '/lib/common.php';
  10. class UUIDTest extends PHPUnit_Framework_TestCase
  11. {
  12. public function testGenerate()
  13. {
  14. $result = UUID::gen();
  15. $this->assertRegExp('/^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$/',
  16. $result);
  17. // Check version number
  18. $this->assertEquals(0x4000, hexdec(substr($result, 14, 4)) & 0xF000);
  19. $this->assertEquals(0x8000, hexdec(substr($result, 19, 4)) & 0xC000);
  20. }
  21. public function testUnique()
  22. {
  23. $reps = 100;
  24. $ids = array();
  25. for ($i = 0; $i < $reps; $i++) {
  26. $ids[] = UUID::gen();
  27. }
  28. $this->assertEquals(count($ids), count(array_unique($ids)), "UUIDs must be unique");
  29. }
  30. }