XxHash64Test.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\XxHash64;
  8. use PHPUnit\Framework\TestCase;
  9. use Necklace\XxHash\XxHash;
  10. use Exception;
  11. final class XxHash64Test extends TestCase
  12. {
  13. private $x;
  14. protected function setUp(): void
  15. {
  16. try {
  17. $this->x = new XxHash64;
  18. } catch (Exception $e) {
  19. if($e->getMessage() === 'Too old version of libxxhash. XXH3 requires v0.7.1 or above.')
  20. {
  21. $this->markTestSkipped('Not applicable for version.');
  22. }
  23. }
  24. }
  25. protected function teardown(): void
  26. {
  27. unset($this->x);
  28. }
  29. public function testHash(): void
  30. {
  31. $this->assertEquals('83ff80eedd51d542', $this->x->hash('test'));
  32. }
  33. public function testHashWithSeed(): void
  34. {
  35. $this->assertEquals('7c601857728eb3f6', $this->x->hash('test', 1));
  36. $this->assertEquals('f4673c78b49c1c7a', $this->x->hash('test', 10));
  37. $this->assertEquals('3a81538f866b929a', $this->x->hash('test', 100));
  38. }
  39. public function testHashWithBadSecret(): void
  40. {
  41. $this->expectExceptionMessage('XxHash XXH3 secret is too small.');
  42. $this->x->hash('test', null, 'not long enough secret');
  43. }
  44. public function testHashWithSecret(): void
  45. {
  46. $secret = '9a5593c8403739049889a795e5fa34627250727170a11f6bf6ff6d8cc53163d3f5d2d69cd0876547797c15358310764836bd91275bdfc4cab884537b386e678a1ab82358';
  47. $this->assertEquals('a123f947d57f73e7', $this->x->hash('test', null, $secret));
  48. }
  49. public function testReset(): void
  50. {
  51. $this->assertEquals(true, $this->x->reset());
  52. }
  53. public function testUpdateAndDigest(): void
  54. {
  55. $this->assertEquals(true, $this->x->update('te'));
  56. $this->assertEquals(true, $this->x->update('st'));
  57. $this->assertEquals('83ff80eedd51d542', $this->x->digest());
  58. }
  59. public function testResetWithSeed(): void
  60. {
  61. $this->assertEquals(true, $this->x->reset(1));
  62. $this->assertEquals(true, $this->x->update('te'));
  63. $this->assertEquals(true, $this->x->update('st'));
  64. $this->assertEquals('7c601857728eb3f6', $this->x->digest());
  65. $this->assertEquals(true, $this->x->reset(10));
  66. $this->assertEquals(true, $this->x->update('te'));
  67. $this->assertEquals(true, $this->x->update('st'));
  68. $this->assertEquals('f4673c78b49c1c7a', $this->x->digest());
  69. $this->assertEquals(true, $this->x->reset(100));
  70. $this->assertEquals(true, $this->x->update('te'));
  71. $this->assertEquals(true, $this->x->update('st'));
  72. $this->assertEquals('3a81538f866b929a', $this->x->digest());
  73. }
  74. public function testResetWithBadSecret(): void
  75. {
  76. $this->expectExceptionMessage('XxHash XXH3 secret is too small.');
  77. $this->x->reset(null, 'not long enough secret');
  78. }
  79. public function testResetWithSecret(): void
  80. {
  81. $secret = '9a5593c8403739049889a795e5fa34627250727170a11f6bf6ff6d8cc53163d3f5d2d69cd0876547797c15358310764836bd91275bdfc4cab884537b386e678a1ab82358';
  82. $this->assertEquals(true, $this->x->reset(null, $secret));
  83. $this->assertEquals(true, $this->x->update('te'));
  84. $this->assertEquals(true, $this->x->update('st'));
  85. $this->assertEquals('a123f947d57f73e7', $this->x->digest());
  86. }
  87. public function testHashFile(): void
  88. {
  89. $this->assertEquals('db70939c6397e562', $this->x->hashFile(__DIR__ . '/../bootstrap.php'));
  90. }
  91. public function testHashFileWithSeed(): void
  92. {
  93. $this->assertEquals('c9c52648e7f4a15f', $this->x->hashFile(__DIR__ . '/../bootstrap.php', 1));
  94. }
  95. public function testHashFileWithSecret(): void
  96. {
  97. $secret = '9a5593c8403739049889a795e5fa34627250727170a11f6bf6ff6d8cc53163d3f5d2d69cd0876547797c15358310764836bd91275bdfc4cab884537b386e678a1ab82358';
  98. $this->assertEquals('0983cba538bf950b', $this->x->hashFile(__DIR__ . '/../bootstrap.php', null, $secret));
  99. }
  100. }