PackedArray.spec.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. /*
  4. * This is a TypeScript port of the original Java version, which was written by
  5. * Gil Tene as described in
  6. * https://github.com/HdrHistogram/HdrHistogram
  7. * and released to the public domain, as explained at
  8. * http://creativecommons.org/publicdomain/zero/1.0/
  9. */
  10. const PackedArrayContext_1 = require("./PackedArrayContext");
  11. const PackedArray_1 = require("./PackedArray");
  12. const { pow } = Math;
  13. describe("Packed array context", () => {
  14. it("Should initialize array", () => {
  15. const ctx = new PackedArrayContext_1.PackedArrayContext(1024, 128);
  16. expect(ctx.isPacked).toBe(true);
  17. expect(ctx.getPopulatedShortLength()).toBeGreaterThan(0);
  18. });
  19. });
  20. describe("Packed array", () => {
  21. it("Should initialize array", () => {
  22. const array = new PackedArray_1.PackedArray(1024, 128);
  23. expect(array.getPhysicalLength()).toBe(128);
  24. expect(array.length()).toBe(1024);
  25. });
  26. it("Should retrieve data stored in array", () => {
  27. // given
  28. const array = new PackedArray_1.PackedArray(1024, 16);
  29. // when
  30. array.set(16, 1);
  31. array.set(12, 42);
  32. // then
  33. expect(array.get(12)).toBe(42);
  34. expect(array.get(16)).toBe(1);
  35. });
  36. it("Should resize array when storing data", () => {
  37. // given
  38. const array = new PackedArray_1.PackedArray(1024, 16);
  39. // when
  40. array.set(12, 361);
  41. // then
  42. const storedData = array.get(12);
  43. expect(storedData).toBe(361);
  44. });
  45. it("Should retrieve big numbers stored in array", () => {
  46. // given
  47. const array = new PackedArray_1.PackedArray(1024, 16);
  48. // when
  49. array.set(12, Math.pow(2, 16) + 1);
  50. // then
  51. const storedData = array.get(12);
  52. expect(storedData).toBe(Math.pow(2, 16) + 1);
  53. });
  54. it("Should copy data when resizing array", () => {
  55. const array = new PackedArray_1.PackedArray(1024);
  56. for (let value = 1; value <= 272; value++) {
  57. array.set(value, value);
  58. }
  59. expect(array.get(1)).toBe(1);
  60. expect(array.get(255)).toBe(255);
  61. expect(array.get(272)).toBe(272);
  62. });
  63. it("Should increment data stored in array", () => {
  64. // given
  65. const array = new PackedArray_1.PackedArray(1024, 16);
  66. array.set(16, 1);
  67. // when
  68. array.add(16, 41);
  69. // then
  70. expect(array.get(16)).toBe(42);
  71. });
  72. it("Should increment data stored in array with big numbers", () => {
  73. // given
  74. const array = new PackedArray_1.PackedArray(1024, 16);
  75. array.set(16, 42);
  76. // when
  77. array.add(16, pow(2, 33));
  78. // then
  79. expect(array.get(16)).toBe(pow(2, 33) + 42);
  80. });
  81. it("Should increment data stored in array with big numbers when a resize is needed", () => {
  82. // given
  83. const array = new PackedArray_1.PackedArray(10000, 16);
  84. array.set(6144, 243);
  85. array.set(60, 243);
  86. array.set(1160, 243);
  87. // when
  88. array.add(6144, 25);
  89. // then
  90. expect(array.get(6144)).toBe(268);
  91. });
  92. it("Should increment data stored in array with big numbers", () => {
  93. // given
  94. const array = new PackedArray_1.PackedArray(1024, 16);
  95. array.set(16, 42);
  96. // when
  97. array.add(16, pow(2, 33));
  98. // then
  99. expect(array.get(16)).toBe(pow(2, 33) + 42);
  100. });
  101. it("Should clear data stored in array", () => {
  102. // given
  103. const array = new PackedArray_1.PackedArray(1024, 16);
  104. array.set(16, 42);
  105. // when
  106. array.clear();
  107. // then
  108. expect(array.get(16)).toBe(0);
  109. });
  110. it("Should resize array when virtual length change", () => {
  111. // given
  112. const array = new PackedArray_1.PackedArray(16, 16);
  113. array.set(7, 42);
  114. // when
  115. array.setVirtualLength(pow(2, 20));
  116. array.add(pow(2, 19), 42);
  117. // then
  118. expect(array.get(7)).toBe(42);
  119. expect(array.get(pow(2, 19))).toBe(42);
  120. });
  121. it("should handle properly big numbers", () => {
  122. // given
  123. const array = new PackedArray_1.PackedArray(45056, 16);
  124. // when
  125. array.set(32768, 1);
  126. // then
  127. expect(array.get(32768)).toBe(1);
  128. expect(array.get(0)).toBe(0);
  129. });
  130. });
  131. describe("Unpacked array", () => {
  132. it("Should increment data stored in array", () => {
  133. // given
  134. const array = new PackedArray_1.PackedArray(1024, pow(2, 20));
  135. array.set(16, 1);
  136. // when
  137. array.add(16, 41);
  138. // then
  139. expect(array.get(16)).toBe(42);
  140. });
  141. });
  142. //# sourceMappingURL=PackedArray.spec.js.map