murmurhash3_spec.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. import { MurmurHash3_64 } from "../../src/shared/murmurhash3.js";
  16. describe("MurmurHash3_64", function () {
  17. it("instantiates without seed", function () {
  18. const hash = new MurmurHash3_64();
  19. expect(hash).toEqual(jasmine.any(MurmurHash3_64));
  20. });
  21. it("instantiates with seed", function () {
  22. const hash = new MurmurHash3_64(1);
  23. expect(hash).toEqual(jasmine.any(MurmurHash3_64));
  24. });
  25. const hexDigestExpected = "f61cfdbfdae0f65e";
  26. const sourceText = "test";
  27. const sourceCharCodes = [116, 101, 115, 116]; // 't','e','s','t'
  28. it("correctly generates a hash from a string", function () {
  29. const hash = new MurmurHash3_64();
  30. hash.update(sourceText);
  31. expect(hash.hexdigest()).toEqual(hexDigestExpected);
  32. });
  33. it("correctly generates a hash from a Uint8Array", function () {
  34. const hash = new MurmurHash3_64();
  35. hash.update(new Uint8Array(sourceCharCodes));
  36. expect(hash.hexdigest()).toEqual(hexDigestExpected);
  37. });
  38. it("correctly generates a hash from a Uint32Array", function () {
  39. const hash = new MurmurHash3_64();
  40. hash.update(new Uint32Array(new Uint8Array(sourceCharCodes).buffer));
  41. expect(hash.hexdigest()).toEqual(hexDigestExpected);
  42. });
  43. it("changes the hash after update without seed", function () {
  44. const hash = new MurmurHash3_64();
  45. hash.update(sourceText);
  46. const hexdigest1 = hash.hexdigest();
  47. hash.update(sourceText);
  48. const hexdigest2 = hash.hexdigest();
  49. expect(hexdigest1).not.toEqual(hexdigest2);
  50. });
  51. it("changes the hash after update with seed", function () {
  52. const hash = new MurmurHash3_64(1);
  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(
  60. "generates correct hashes for TypedArrays which share the same " +
  61. "underlying ArrayBuffer (issue 12533)",
  62. function () {
  63. // prettier-ignore
  64. const typedArray = new Uint8Array([
  65. 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
  66. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  67. ]);
  68. const startArray = new Uint8Array(typedArray.buffer, 0, 10);
  69. const endArray = new Uint8Array(typedArray.buffer, 10, 10);
  70. expect(startArray).not.toEqual(endArray);
  71. const startHash = new MurmurHash3_64();
  72. startHash.update(startArray);
  73. const startHexdigest = startHash.hexdigest();
  74. const endHash = new MurmurHash3_64();
  75. endHash.update(endArray);
  76. const endHexdigest = endHash.hexdigest();
  77. // The two hashes *must* be different.
  78. expect(startHexdigest).not.toEqual(endHexdigest);
  79. expect(startHexdigest).toEqual("a49de339cc5b0819");
  80. expect(endHexdigest).toEqual("f81a92d9e214ab35");
  81. }
  82. );
  83. });