murmurhash3_spec.js 2.6 KB

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