2
0

murmurhash3_spec.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 _murmurhash = require('../../core/murmurhash3');
  17. describe('MurmurHash3_64', function () {
  18. it('instantiates without seed', function () {
  19. var hash = new _murmurhash.MurmurHash3_64();
  20. expect(hash).toEqual(jasmine.any(_murmurhash.MurmurHash3_64));
  21. });
  22. it('instantiates with seed', function () {
  23. var hash = new _murmurhash.MurmurHash3_64(1);
  24. expect(hash).toEqual(jasmine.any(_murmurhash.MurmurHash3_64));
  25. });
  26. var hexDigestExpected = 'f61cfdbfdae0f65e';
  27. var sourceText = 'test';
  28. var sourceCharCodes = [116, 101, 115, 116];
  29. it('correctly generates a hash from a string', function () {
  30. var hash = new _murmurhash.MurmurHash3_64();
  31. hash.update(sourceText);
  32. expect(hash.hexdigest()).toEqual(hexDigestExpected);
  33. });
  34. it('correctly generates a hash from a Uint8Array', function () {
  35. var hash = new _murmurhash.MurmurHash3_64();
  36. hash.update(new Uint8Array(sourceCharCodes));
  37. expect(hash.hexdigest()).toEqual(hexDigestExpected);
  38. });
  39. it('correctly generates a hash from a Uint32Array', function () {
  40. var hash = new _murmurhash.MurmurHash3_64();
  41. hash.update(new Uint32Array(new Uint8Array(sourceCharCodes).buffer));
  42. expect(hash.hexdigest()).toEqual(hexDigestExpected);
  43. });
  44. it('changes the hash after update without seed', function () {
  45. var hash = new _murmurhash.MurmurHash3_64();
  46. var hexdigest1, hexdigest2;
  47. hash.update(sourceText);
  48. hexdigest1 = hash.hexdigest();
  49. hash.update(sourceText);
  50. hexdigest2 = hash.hexdigest();
  51. expect(hexdigest1).not.toEqual(hexdigest2);
  52. });
  53. it('changes the hash after update with seed', function () {
  54. var hash = new _murmurhash.MurmurHash3_64(1);
  55. var hexdigest1, hexdigest2;
  56. hash.update(sourceText);
  57. hexdigest1 = hash.hexdigest();
  58. hash.update(sourceText);
  59. hexdigest2 = hash.hexdigest();
  60. expect(hexdigest1).not.toEqual(hexdigest2);
  61. });
  62. });