Skip to content

Commit

Permalink
Add missing destroy()
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager committed Jan 21, 2025
1 parent 9b5002c commit 11c33e2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ b.get(1234)

#### `const bitarray = new Bitarray()`

#### `bitarray.destroy()`

#### `bitarray.page(index, bitfield)`

#### `bitarray.insert(bitfield[, start])`
Expand Down
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ module.exports = exports = class Bitarray {
this._pages.set(index)
}

destroy() {
if (this._handle === null) return

binding.destroy(this._handle)

this._handle = null
}

page(index, bitfield) {
if (typeof index !== 'number') {
throw new TypeError(
Expand Down
13 changes: 13 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { constants } = Bitarray

test('get and set', (t) => {
const b = new Bitarray()
t.teardown(() => b.destroy())

t.is(b.get(100000), false)
t.is(b.set(100000, true), true)
Expand All @@ -13,6 +14,8 @@ test('get and set', (t) => {

test('random set and get', (t) => {
const b = new Bitarray()
t.teardown(() => b.destroy())

const set = new Set()

for (let i = 0; i < 2000; i++) {
Expand Down Expand Up @@ -44,6 +47,7 @@ test('random set and get', (t) => {

test('setBatch', (t) => {
const b = new Bitarray()
t.teardown(() => b.destroy())

t.is(b.setBatch([1, 4, 8], true), true)
t.is(b.get(1), true)
Expand All @@ -53,6 +57,7 @@ test('setBatch', (t) => {

test('count', (t) => {
const b = new Bitarray()
t.teardown(() => b.destroy())

for (const [start, length] of [
[0, 2],
Expand All @@ -71,6 +76,7 @@ test('count', (t) => {

test('find first, all zeroes', (t) => {
const b = new Bitarray()
t.teardown(() => b.destroy())

t.is(b.findFirst(false, 0), 0)
t.is(b.findFirst(true, 0), -1)
Expand All @@ -94,6 +100,7 @@ test('find first, all zeroes', (t) => {

test('find first, all ones', (t) => {
const b = new Bitarray()
t.teardown(() => b.destroy())

b.fill(true, 0, 2 ** 24)

Expand Down Expand Up @@ -121,6 +128,7 @@ test('find first, all ones', (t) => {

test('find last, all zeroes', (t) => {
const b = new Bitarray()
t.teardown(() => b.destroy())

t.is(b.findLast(false, 0), 0)
t.is(b.findLast(true, 0), -1)
Expand All @@ -144,6 +152,7 @@ test('find last, all zeroes', (t) => {

test('find last, all ones', (t) => {
const b = new Bitarray()
t.teardown(() => b.destroy())

b.fill(true, 0, 2 ** 24)

Expand Down Expand Up @@ -171,6 +180,7 @@ test('find last, all ones', (t) => {

test('find first, ones around page boundary', function (t) {
const b = new Bitarray()
t.teardown(() => b.destroy())

b.set(32766, true)
t.is(b.findFirst(false, 32766), 32767)
Expand All @@ -182,6 +192,7 @@ test('find first, ones around page boundary', function (t) {

test('find first, ones around segment boundary', (t) => {
const b = new Bitarray()
t.teardown(() => b.destroy())

b.set(2097150, true)
t.is(b.findFirst(false, 2097150), 2097151)
Expand All @@ -193,6 +204,7 @@ test('find first, ones around segment boundary', (t) => {

test('find last, ones around page boundary', (t) => {
const b = new Bitarray()
t.teardown(() => b.destroy())

b.set(32767, true)
t.is(b.findLast(false, 32767), 32766)
Expand All @@ -215,6 +227,7 @@ test('find last, ones around segment boundary', (t) => {

test('external page', (t) => {
const b = new Bitarray()
t.teardown(() => b.destroy())

const page = Buffer.alloc(constants.BYTES_PER_PAGE)

Expand Down

0 comments on commit 11c33e2

Please sign in to comment.