2
0

murmurhash3_spec.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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("../../shared/murmurhash3.js");
  24. describe("MurmurHash3_64", function () {
  25. it("instantiates without seed", function () {
  26. const hash = new _murmurhash.MurmurHash3_64();
  27. expect(hash).toEqual(jasmine.any(_murmurhash.MurmurHash3_64));
  28. });
  29. it("instantiates with seed", function () {
  30. const hash = new _murmurhash.MurmurHash3_64(1);
  31. expect(hash).toEqual(jasmine.any(_murmurhash.MurmurHash3_64));
  32. });
  33. const hexDigestExpected = "f61cfdbfdae0f65e";
  34. const sourceText = "test";
  35. const sourceCharCodes = [116, 101, 115, 116];
  36. it("correctly generates a hash from a string", function () {
  37. const 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. const 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. const 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. const hash = new _murmurhash.MurmurHash3_64();
  53. hash.update(sourceText);
  54. const hexdigest1 = hash.hexdigest();
  55. hash.update(sourceText);
  56. const hexdigest2 = hash.hexdigest();
  57. expect(hexdigest1).not.toEqual(hexdigest2);
  58. });
  59. it("changes the hash after update with seed", function () {
  60. const hash = new _murmurhash.MurmurHash3_64(1);
  61. hash.update(sourceText);
  62. const hexdigest1 = hash.hexdigest();
  63. hash.update(sourceText);
  64. const hexdigest2 = hash.hexdigest();
  65. expect(hexdigest1).not.toEqual(hexdigest2);
  66. });
  67. it("generates correct hashes for TypedArrays which share the same " + "underlying ArrayBuffer (issue 12533)", function () {
  68. const typedArray = new Uint8Array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
  69. const startArray = new Uint8Array(typedArray.buffer, 0, 10);
  70. const endArray = new Uint8Array(typedArray.buffer, 10, 10);
  71. expect(startArray).not.toEqual(endArray);
  72. const startHash = new _murmurhash.MurmurHash3_64();
  73. startHash.update(startArray);
  74. const startHexdigest = startHash.hexdigest();
  75. const endHash = new _murmurhash.MurmurHash3_64();
  76. endHash.update(endArray);
  77. const endHexdigest = endHash.hexdigest();
  78. expect(startHexdigest).not.toEqual(endHexdigest);
  79. expect(startHexdigest).toEqual("a49de339cc5b0819");
  80. expect(endHexdigest).toEqual("f81a92d9e214ab35");
  81. });
  82. });