diff --git a/src/packedarray/PackedArrayContext.fc.spec.ts b/src/packedarray/PackedArrayContext.fc.spec.ts new file mode 100644 index 0000000..9c6a35d --- /dev/null +++ b/src/packedarray/PackedArrayContext.fc.spec.ts @@ -0,0 +1,76 @@ +/* + * This is a TypeScript port of the original Java version, which was written by + * Gil Tene as described in + * https://github.com/HdrHistogram/HdrHistogram + * and released to the public domain, as explained at + * http://creativecommons.org/publicdomain/zero/1.0/ + */ +import * as fc from "fast-check"; +import { bitCount } from "./PackedArrayContext"; + +const runFromStryker = __dirname.includes("stryker"); + +const runnerOptions = { + numRuns: runFromStryker ? 10 : 1000, + verbose: true, +}; + +// Reference population count, one bit at a time on the low 32 bits of each word +const referenceBitCount = (n: number) => { + let bits = 0; + let rest = n; + while (rest > 0) { + const word = rest % 0x100000000; + for (let bit = 0; bit < 32; bit++) { + bits += Math.floor(word / 2 ** bit) % 2; + } + rest = Math.floor(rest / 0x100000000); + } + return bits; +}; + +describe("Packed array context", () => { + it("should count the set bits of a value below 2^53", () => { + fc.assert( + fc.property( + fc.integer({ min: 0, max: Number.MAX_SAFE_INTEGER }), + (n) => bitCount(n) === referenceBitCount(n) + ), + runnerOptions + ); + }); + + it("should count the set bits of the 16 bit slot indicators", () => { + fc.assert( + fc.property( + fc.integer({ min: 0, max: 0xffff }), + (n) => bitCount(n) === referenceBitCount(n) + ), + runnerOptions + ); + }); + + it("should count the set bits of a single word, including bit 31", () => { + fc.assert( + fc.property( + fc.integer({ min: 0, max: 0xffffffff }), + (n) => bitCount(n) === referenceBitCount(n) + ), + runnerOptions + ); + }); + + it("should count the bits at the 2^32 boundaries and above", () => { + expect(bitCount(0)).toBe(0); + expect(bitCount(0xffffffff)).toBe(32); + expect(bitCount(0x100000000)).toBe(1); + expect(bitCount(0x1ffffffff)).toBe(33); + expect(bitCount(Number.MAX_SAFE_INTEGER)).toBe(53); + }); + + it("should reject inputs that are not non-negative safe integers", () => { + [NaN, Infinity, -Infinity, -1, 1.5, 2 ** 53].forEach((n) => { + expect(() => bitCount(n)).toThrow(/non-negative safe integer/); + }); + }); +}); diff --git a/src/packedarray/PackedArrayContext.ts b/src/packedarray/PackedArrayContext.ts index 7c3535f..b3abadf 100644 --- a/src/packedarray/PackedArrayContext.ts +++ b/src/packedarray/PackedArrayContext.ts @@ -38,11 +38,21 @@ const PACKED_ARRAY_GROWTH_FRACTION_POW2 = 4; const { ceil, log2, max } = Math; -const bitCount = (n: number) => { - var bits = 0; +/** + * Population count of a non-negative integer below 2^53. The high words are + * shifted down by whole 32 bit words, so a value below 2^32 takes one pass. + * Any other input (negative, non-integer, NaN, Infinity) would never reach 0 + * and is rejected instead. + * @internal + */ +export const bitCount = (n: number) => { + if (!Number.isSafeInteger(n) || n < 0) { + throw new Error(`bitCount expects a non-negative safe integer, got ${n}`); + } + let bits = 0; while (n !== 0) { bits += bitCount32(n | 0); - n /= 0x100000000; + n = Math.floor(n / 0x100000000); } return bits; };