Fixed bug with 0-size bitmap (.grow(x))

Add testcase
Add tests script & config
This commit is contained in:
Eugene 2025-04-02 09:08:46 +03:00
parent e73e47a702
commit dd78397385
5 changed files with 1109 additions and 28 deletions

7
jest.config.js Normal file
View File

@ -0,0 +1,7 @@
/** @type {import('ts-jest').JestConfigWithTsJest} **/
export default {
testEnvironment: "node",
transform: {
"^.+\.tsx?$": ["ts-jest",{}],
},
};

View File

@ -21,7 +21,8 @@
"predeploy": "npm run dist", "predeploy": "npm run dist",
"lint": "eslint src --ext .ts", "lint": "eslint src --ext .ts",
"lint:fix": "eslint src --ext .ts --fix", "lint:fix": "eslint src --ext .ts --fix",
"format": "prettier --write ./src/**/*.ts" "format": "prettier --write ./src/**/*.ts",
"test": "jest"
}, },
"devDependencies": { "devDependencies": {
"@jest/globals": "^29.7.0", "@jest/globals": "^29.7.0",
@ -32,8 +33,10 @@
"@typescript-eslint/parser": "^7.15.0", "@typescript-eslint/parser": "^7.15.0",
"eslint": "^9.6.0", "eslint": "^9.6.0",
"eslint-config-prettier": "^9.1.0", "eslint-config-prettier": "^9.1.0",
"jest": "^29.7.0",
"prettier": "^3.3.2", "prettier": "^3.3.2",
"rollup-plugin-typescript-paths": "^1.5.0", "rollup-plugin-typescript-paths": "^1.5.0",
"ts-jest": "^29.3.1",
"tslib": "^2.6.3", "tslib": "^2.6.3",
"typescript": "^5.5.3", "typescript": "^5.5.3",
"vite": "^5.3.3", "vite": "^5.3.3",

View File

@ -2,13 +2,13 @@ import { Bitmap } from "./bitmap";
describe("Bitmap", () => { describe("Bitmap", () => {
test("Set", () => { test("Set", () => {
const x = 2; const bitmap = new Bitmap(0);
const y = 4; bitmap.set(0);
const bitmap = new Bitmap(32); bitmap.set(1);
bitmap.set(x); bitmap.set(2);
bitmap.set(y); expect(bitmap.contains(0)).toBe(true);
expect(bitmap.contains(x)).toBe(true); expect(bitmap.contains(1)).toBe(true);
expect(bitmap.contains(y)).toBe(true); expect(bitmap.contains(2)).toBe(true);
}); });
test("Remove", () => { test("Remove", () => {

View File

@ -12,12 +12,8 @@ export class Bitmap implements IBitmap {
this.bits = new Uint8Array(computeBitsArrayLength(x)); this.bits = new Uint8Array(computeBitsArrayLength(x));
} }
protected getBits(): Uint8Array {
return this.bits;
}
public grow(x: number): void { public grow(x: number): void {
const arrLength = computeBitsArrayLength(x); const arrLength = Math.max(1, computeBitsArrayLength(x));
if (arrLength <= this.bits.length) return; if (arrLength <= this.bits.length) return;
const prev = this.bits; const prev = this.bits;
this.bits = new Uint8Array(arrLength); this.bits = new Uint8Array(arrLength);
@ -55,6 +51,7 @@ export class Bitmap implements IBitmap {
} }
return count; return count;
} }
public and(bitmap: IBitmap): IBitmap { public and(bitmap: IBitmap): IBitmap {
const otherBits = (bitmap as Bitmap).bits; const otherBits = (bitmap as Bitmap).bits;
const result = this.clone() as Bitmap; const result = this.clone() as Bitmap;
@ -116,6 +113,7 @@ export class Bitmap implements IBitmap {
if (!needContinueIterating) break; if (!needContinueIterating) break;
} }
} }
public filter(fn: (x: number) => boolean): void { public filter(fn: (x: number) => boolean): void {
for (let i = 0; i < this.bits.length; i++) { for (let i = 0; i < this.bits.length; i++) {
for (let j = 0; j < 8; j++) { for (let j = 0; j < 8; j++) {
@ -126,6 +124,7 @@ export class Bitmap implements IBitmap {
} }
} }
} }
public clear(): void { public clear(): void {
for (let i = 0; i < this.bits.length; i++) { for (let i = 0; i < this.bits.length; i++) {
this.bits[i] = 0; this.bits[i] = 0;

1102
yarn.lock

File diff suppressed because it is too large Load Diff