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

View File

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

View File

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

1102
yarn.lock

File diff suppressed because it is too large Load Diff