XxHash128Test.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * @copyright Nichlas Severinsen
  4. * @license https://opensource.org/licenses/BSD-2-Clause
  5. */
  6. namespace Xxh3;
  7. use Necklace\XxHash\Xxh3\XxHash128;
  8. use PHPUnit\Framework\TestCase;
  9. use Exception;
  10. final class XxHash128Test extends TestCase
  11. {
  12. private $x;
  13. protected function setUp(): void
  14. {
  15. try {
  16. $this->x = new XxHash128;
  17. } catch (Exception $e) {
  18. if($e->getMessage() === 'Too old version of libxxhash. XXH3 requires v0.7.1 or above.')
  19. {
  20. $this->markTestSkipped('Not applicable for version.');
  21. }
  22. }
  23. }
  24. protected function teardown(): void
  25. {
  26. unset($this->x);
  27. }
  28. public function testHash(): void
  29. {
  30. $this->assertEquals('2e790d069a07b05183ff80eedd51d542', $this->x->hash('test'));
  31. }
  32. public function testHashWithSeed(): void
  33. {
  34. $this->assertEquals('2409be6c8b4ea1577c601857728eb3f6', $this->x->hash('test', 1));
  35. $this->assertEquals('7a4aef0e7f4f099ff4673c78b49c1c7a', $this->x->hash('test', 10));
  36. $this->assertEquals('0668c111a593fea43a81538f866b929a', $this->x->hash('test', 100));
  37. }
  38. public function testHashWithBadSecret(): void
  39. {
  40. $this->expectExceptionMessage('XxHash XXH3 secret is too small.');
  41. $this->x->hash('test', null, 'not long enough secret');
  42. }
  43. public function testHashWithSecret(): void
  44. {
  45. $secret = '9a5593c8403739049889a795e5fa34627250727170a11f6bf6ff6d8cc53163d3f5d2d69cd0876547797c15358310764836bd91275bdfc4cab884537b386e678a1ab82358';
  46. $this->assertEquals('d08f5a62eab1c1f7a123f947d57f73e7', $this->x->hash('test', null, $secret));
  47. }
  48. public function testReset(): void
  49. {
  50. $this->assertEquals(true, $this->x->reset());
  51. }
  52. public function testUpdateAndDigest(): void
  53. {
  54. $this->assertEquals(true, $this->x->update('te'));
  55. $this->assertEquals(true, $this->x->update('st'));
  56. $this->assertEquals('2e790d069a07b05183ff80eedd51d542', $this->x->digest());
  57. }
  58. public function testResetWithSeed(): void
  59. {
  60. $this->assertEquals(true, $this->x->reset(1));
  61. $this->assertEquals(true, $this->x->update('te'));
  62. $this->assertEquals(true, $this->x->update('st'));
  63. $this->assertEquals('2409be6c8b4ea1577c601857728eb3f6', $this->x->digest());
  64. $this->assertEquals(true, $this->x->reset(10));
  65. $this->assertEquals(true, $this->x->update('te'));
  66. $this->assertEquals(true, $this->x->update('st'));
  67. $this->assertEquals('7a4aef0e7f4f099ff4673c78b49c1c7a', $this->x->digest());
  68. $this->assertEquals(true, $this->x->reset(100));
  69. $this->assertEquals(true, $this->x->update('te'));
  70. $this->assertEquals(true, $this->x->update('st'));
  71. $this->assertEquals('0668c111a593fea43a81538f866b929a', $this->x->digest());
  72. }
  73. public function testResetWithBadSecret(): void
  74. {
  75. $this->expectExceptionMessage('XxHash XXH3 secret is too small.');
  76. $this->x->reset(null, 'not long enough secret');
  77. }
  78. public function testResetWithSecret(): void
  79. {
  80. $secret = '9a5593c8403739049889a795e5fa34627250727170a11f6bf6ff6d8cc53163d3f5d2d69cd0876547797c15358310764836bd91275bdfc4cab884537b386e678a1ab82358';
  81. $this->assertEquals(true, $this->x->reset(null, $secret));
  82. $this->assertEquals(true, $this->x->update('te'));
  83. $this->assertEquals(true, $this->x->update('st'));
  84. $this->assertEquals('d08f5a62eab1c1f7a123f947d57f73e7', $this->x->digest());
  85. }
  86. public function testHashFile(): void
  87. {
  88. if($this->x->version() === '0.7.1')
  89. {
  90. $this->assertEquals('f755027331532f81e4e3f124d220bf33', $this->x->hashFile(__DIR__ . '/../bootstrap.php'));
  91. }
  92. else
  93. {
  94. $this->assertEquals('c3ead5206079114dce23b624b8f2199f', $this->x->hashFile(__DIR__ . '/../bootstrap.php'));
  95. }
  96. }
  97. public function testHashFileWithSeed(): void
  98. {
  99. if($this->x->version() === '0.7.1')
  100. {
  101. $this->assertEquals('45e093f9eedf912cfad4e793fbb576ec', $this->x->hashFile(__DIR__ . '/../bootstrap.php', 1));
  102. }
  103. else
  104. {
  105. $this->assertEquals('3aaea55fc456c7b07e30e17f0a3ee498', $this->x->hashFile(__DIR__ . '/../bootstrap.php', 1));
  106. }
  107. }
  108. public function testHashFileWithSecret(): void
  109. {
  110. $secret = '9a5593c8403739049889a795e5fa34627250727170a11f6bf6ff6d8cc53163d3f5d2d69cd0876547797c15358310764836bd91275bdfc4cab884537b386e678a1ab82358';
  111. if($this->x->version() === '0.7.1')
  112. {
  113. $this->assertEquals('38e756e132a79fffcc7323cea1fecc42', $this->x->hashFile(__DIR__ . '/../bootstrap.php', null, $secret));
  114. }
  115. else
  116. {
  117. $this->assertEquals('2ac5da808cdb875c3c2a9a08b449fa51', $this->x->hashFile(__DIR__ . '/../bootstrap.php', null, $secret));
  118. }
  119. }
  120. }