XXH-test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. var assert = require('assert')
  2. var XXH = require('..')
  3. describe('XXH', function () {
  4. var seed = 0
  5. describe('with small input multiple of 4', function () {
  6. var input = 'abcd'
  7. var expected = 'A3643705' // Computed with xxHash C version
  8. it('should return hash in a single step', function (done) {
  9. var h = XXH.h32( input, seed ).toString(16).toUpperCase()
  10. assert.equal( h, expected )
  11. done()
  12. })
  13. it('should return hash in many steps', function (done) {
  14. var H = XXH.h32( seed )
  15. var h = H.update( input ).digest().toString(16).toUpperCase()
  16. assert.equal( h, expected )
  17. done()
  18. })
  19. })
  20. describe('with medium input multiple of 4', function () {
  21. var input = Array(1001).join('abcd')
  22. var expected = 'E18CBEA'
  23. it('should return hash in a single step', function (done) {
  24. var h = XXH.h32( input, seed ).toString(16).toUpperCase()
  25. assert.equal( h, expected )
  26. done()
  27. })
  28. it('should return hash in many steps', function (done) {
  29. var H = XXH.h32( seed )
  30. var h = H.update( input ).digest().toString(16).toUpperCase()
  31. assert.equal( h, expected )
  32. done()
  33. })
  34. })
  35. describe('with small input', function () {
  36. var input = 'abc'
  37. var expected = '32D153FF' // Computed with xxHash C version
  38. it('should return hash in a single step', function (done) {
  39. var h = XXH.h32( input, seed ).toString(16).toUpperCase()
  40. assert.equal( h, expected )
  41. done()
  42. })
  43. it('should return hash in many steps', function (done) {
  44. var H = XXH.h32( seed )
  45. var h = H.update( input ).digest().toString(16).toUpperCase()
  46. assert.equal( h, expected )
  47. done()
  48. })
  49. })
  50. describe('with medium input', function () {
  51. var input = Array(1000).join('abc')
  52. var expected = '89DA9B6E'
  53. it('should return hash in a single step', function (done) {
  54. var h = XXH.h32( input, seed ).toString(16).toUpperCase()
  55. assert.equal( h, expected )
  56. done()
  57. })
  58. it('should return hash in many steps', function (done) {
  59. var H = XXH.h32( seed )
  60. var h = H.update( input ).digest().toString(16).toUpperCase()
  61. assert.equal( h, expected )
  62. done()
  63. })
  64. })
  65. describe('with split medium input', function () {
  66. var input = Array(1000).join('abc')
  67. var expected = '89DA9B6E'
  68. it('should return hash with split input < 16', function (done) {
  69. var H = XXH.h32( seed )
  70. var h = H
  71. .update( input.slice(0, 10) )
  72. .update( input.slice(10) )
  73. .digest().toString(16).toUpperCase()
  74. assert.equal( h, expected )
  75. done()
  76. })
  77. it('should return hash with split input = 16', function (done) {
  78. var H = XXH.h32( seed )
  79. var h = H
  80. .update( input.slice(0, 16) )
  81. .update( input.slice(16) )
  82. .digest().toString(16).toUpperCase()
  83. assert.equal( h, expected )
  84. done()
  85. })
  86. it('should return hash with split input > 16', function (done) {
  87. var H = XXH.h32( seed )
  88. var h = H
  89. .update( input.slice(0, 20) )
  90. .update( input.slice(20) )
  91. .digest().toString(16).toUpperCase()
  92. assert.equal( h, expected )
  93. done()
  94. })
  95. })
  96. describe('with utf-8 strings', function () {
  97. var input = 'heiå'
  98. var expected = 'DB5ABCCC' // Computed with xxHash C version
  99. it('should return hash', function (done) {
  100. var h = XXH.h32( input, seed ).toString(16).toUpperCase()
  101. assert.equal( h, expected )
  102. done()
  103. })
  104. it('should return hash in many steps', function (done) {
  105. var H = XXH.h32( seed )
  106. var h = H.update( input ).digest().toString(16).toUpperCase()
  107. assert.equal( h, expected )
  108. done()
  109. })
  110. })
  111. describe('with utf-8 strings', function () {
  112. var input = 'κόσμε'
  113. var expected = 'D855F606' // Computed with xxHash C version
  114. it('should return hash', function (done) {
  115. var h = XXH.h32( input, seed ).toString(16).toUpperCase()
  116. assert.equal( h, expected )
  117. done()
  118. })
  119. it('should return hash in many steps', function (done) {
  120. var H = XXH.h32( seed )
  121. var h = H.update( input ).digest().toString(16).toUpperCase()
  122. assert.equal( h, expected )
  123. done()
  124. })
  125. })
  126. })