murmurhash3_spec.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. var coreMurmurHash3 = require('../../core/murmurhash3.js');
  17. var MurmurHash3_64 = coreMurmurHash3.MurmurHash3_64;
  18. describe('MurmurHash3_64', function () {
  19. it('instantiates without seed', function () {
  20. var hash = new MurmurHash3_64();
  21. expect(hash).toEqual(jasmine.any(MurmurHash3_64));
  22. });
  23. it('instantiates with seed', function () {
  24. var hash = new MurmurHash3_64(1);
  25. expect(hash).toEqual(jasmine.any(MurmurHash3_64));
  26. });
  27. var hexDigestExpected = 'f61cfdbfdae0f65e';
  28. var sourceText = 'test';
  29. var sourceCharCodes = [
  30. 116,
  31. 101,
  32. 115,
  33. 116
  34. ];
  35. it('correctly generates a hash from a string', function () {
  36. var hash = new MurmurHash3_64();
  37. hash.update(sourceText);
  38. expect(hash.hexdigest()).toEqual(hexDigestExpected);
  39. });
  40. it('correctly generates a hash from a Uint8Array', function () {
  41. var hash = new MurmurHash3_64();
  42. hash.update(new Uint8Array(sourceCharCodes));
  43. expect(hash.hexdigest()).toEqual(hexDigestExpected);
  44. });
  45. it('correctly generates a hash from a Uint32Array', function () {
  46. var hash = new MurmurHash3_64();
  47. hash.update(new Uint32Array(sourceCharCodes));
  48. expect(hash.hexdigest()).toEqual(hexDigestExpected);
  49. });
  50. it('changes the hash after update without seed', function () {
  51. var hash = new MurmurHash3_64();
  52. var hexdigest1, hexdigest2;
  53. hash.update(sourceText);
  54. hexdigest1 = hash.hexdigest();
  55. hash.update(sourceText);
  56. hexdigest2 = hash.hexdigest();
  57. expect(hexdigest1).not.toEqual(hexdigest2);
  58. });
  59. it('changes the hash after update with seed', function () {
  60. var hash = new MurmurHash3_64(1);
  61. var hexdigest1, hexdigest2;
  62. hash.update(sourceText);
  63. hexdigest1 = hash.hexdigest();
  64. hash.update(sourceText);
  65. hexdigest2 = hash.hexdigest();
  66. expect(hexdigest1).not.toEqual(hexdigest2);
  67. });
  68. });