UUIDTest.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. namespace Tests\Unit;
  17. if (!defined('INSTALLDIR')) {
  18. define('INSTALLDIR', dirname(dirname(__DIR__)));
  19. }
  20. if (!defined('GNUSOCIAL')) {
  21. define('GNUSOCIAL', true);
  22. }
  23. if (!defined('STATUSNET')) { // Compatibility
  24. define('STATUSNET', true);
  25. }
  26. use PHPUnit\Framework\TestCase;
  27. use UUID;
  28. require_once INSTALLDIR . '/lib/common.php';
  29. final class UUIDTest extends TestCase
  30. {
  31. public function testGenerate()
  32. {
  33. $result = UUID::gen();
  34. $this->assertRegExp('/^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$/',
  35. $result);
  36. // Check version number
  37. $this->assertEquals(0x4000, hexdec(substr($result, 14, 4)) & 0xF000);
  38. $this->assertEquals(0x8000, hexdec(substr($result, 19, 4)) & 0xC000);
  39. }
  40. public function testUnique()
  41. {
  42. $reps = 100;
  43. $ids = array();
  44. for ($i = 0; $i < $reps; $i++) {
  45. $ids[] = UUID::gen();
  46. }
  47. $this->assertEquals(count($ids), count(array_unique($ids)), "UUIDs must be unique");
  48. }
  49. }