murmurhash3_spec.js 2.4 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 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 = [116, 101, 115, 116];
  30. it('correctly generates a hash from a string', function () {
  31. var hash = new MurmurHash3_64();
  32. hash.update(sourceText);
  33. expect(hash.hexdigest()).toEqual(hexDigestExpected);
  34. });
  35. it('correctly generates a hash from a Uint8Array', function () {
  36. var hash = new MurmurHash3_64();
  37. hash.update(new Uint8Array(sourceCharCodes));
  38. expect(hash.hexdigest()).toEqual(hexDigestExpected);
  39. });
  40. it('correctly generates a hash from a Uint32Array', function () {
  41. var hash = new MurmurHash3_64();
  42. hash.update(new Uint32Array(new Uint8Array(sourceCharCodes).buffer));
  43. expect(hash.hexdigest()).toEqual(hexDigestExpected);
  44. });
  45. it('changes the hash after update without seed', function () {
  46. var hash = new MurmurHash3_64();
  47. var hexdigest1, hexdigest2;
  48. hash.update(sourceText);
  49. hexdigest1 = hash.hexdigest();
  50. hash.update(sourceText);
  51. hexdigest2 = hash.hexdigest();
  52. expect(hexdigest1).not.toEqual(hexdigest2);
  53. });
  54. it('changes the hash after update with seed', function () {
  55. var hash = new MurmurHash3_64(1);
  56. var hexdigest1, hexdigest2;
  57. hash.update(sourceText);
  58. hexdigest1 = hash.hexdigest();
  59. hash.update(sourceText);
  60. hexdigest2 = hash.hexdigest();
  61. expect(hexdigest1).not.toEqual(hexdigest2);
  62. });
  63. });