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