Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: move bitfield-rle tests to node:test #636

Merged
merged 1 commit into from
May 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 64 additions & 35 deletions tests/bitfield-rle.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,48 @@
import test from 'brittle'
import test from 'node:test'
import assert from 'node:assert/strict'
import Bitfield from 'bitfield'
import * as rle from '../src/core-manager/bitfield-rle.js'

test('encodes and decodes', function (t) {
test('encodes and decodes', function () {
var bits = new Bitfield(1024)
var deflated = rle.encode(toUint32Array(bits.buffer))
t.ok(deflated.length < bits.buffer.length, 'is smaller')
assert(deflated.length < bits.buffer.length, 'is smaller')
var inflated = rle.decode(deflated)
t.alike(inflated, toUint32Array(bits.buffer), 'decodes to same buffer')
assert.deepEqual(
inflated,
toUint32Array(bits.buffer),
'decodes to same buffer'
)
})

test('encodingLength', function (t) {
test('encodingLength', function () {
var bits = new Bitfield(1024)
var len = rle.encodingLength(bits.buffer)
t.ok(len < bits.buffer.length, 'is smaller')
assert(len < bits.buffer.length, 'is smaller')
var deflated = rle.encode(bits.buffer)
t.alike(
assert.deepEqual(
len,
deflated.length,
'encoding length is similar to encoded buffers length'
)
})

test('encodes and decodes with all bits set', function (t) {
test('encodes and decodes with all bits set', function () {
var bits = new Bitfield(1024)

for (var i = 0; i < 1024; i++) bits.set(i, true)

var deflated = rle.encode(toUint32Array(bits.buffer))
t.ok(deflated.length < bits.buffer.length, 'is smaller')
assert(deflated.length < bits.buffer.length, 'is smaller')
var inflated = rle.decode(deflated)
t.alike(inflated, toUint32Array(bits.buffer), 'decodes to same buffer')
assert.deepEqual(
inflated,
toUint32Array(bits.buffer),
'decodes to same buffer'
)
})

test('encodes and decodes with some bits set', function (t) {
test('encodes and decodes with some bits set', function () {
var bits = new Bitfield(1024)

bits.set(500, true)
Expand All @@ -45,62 +54,82 @@ test('encodes and decodes with some bits set', function (t) {
bits.set(0, true)

var deflated = rle.encode(toUint32Array(bits.buffer))
t.ok(deflated.length < bits.buffer.length, 'is smaller')
assert(deflated.length < bits.buffer.length, 'is smaller')
var inflated = rle.decode(deflated)
t.alike(inflated, toUint32Array(bits.buffer), 'decodes to same buffer')
assert.deepEqual(
inflated,
toUint32Array(bits.buffer),
'decodes to same buffer'
)
})

test('encodes and decodes with random bits set', function (t) {
test('encodes and decodes with random bits set', function () {
var bits = new Bitfield(8 * 1024)

for (var i = 0; i < 512; i++) {
bits.set(Math.floor(Math.random() * 8 * 1024), true)
}

var deflated = rle.encode(toUint32Array(bits.buffer))
t.ok(deflated.length < bits.buffer.length, 'is smaller')
assert(deflated.length < bits.buffer.length, 'is smaller')
var inflated = rle.decode(deflated)
t.alike(inflated, toUint32Array(bits.buffer), 'decodes to same buffer')
assert.deepEqual(
inflated,
toUint32Array(bits.buffer),
'decodes to same buffer'
)
})

test('encodes and decodes with random bits set (not power of two)', function (t) {
test('encodes and decodes with random bits set (not power of two)', function () {
var bits = new Bitfield(8 * 1024)

for (var i = 0; i < 313; i++) {
bits.set(Math.floor(Math.random() * 8 * 1024), true)
}

var deflated = rle.encode(toUint32Array(bits.buffer))
t.ok(deflated.length < bits.buffer.length, 'is smaller')
assert(deflated.length < bits.buffer.length, 'is smaller')
var inflated = rle.decode(deflated)
t.alike(inflated, toUint32Array(bits.buffer), 'decodes to same buffer')
assert.deepEqual(
inflated,
toUint32Array(bits.buffer),
'decodes to same buffer'
)
})

test('encodes empty bitfield', function (t) {
test('encodes empty bitfield', function () {
var deflated = rle.encode(new Uint32Array())
var inflated = rle.decode(deflated)
t.alike(inflated, new Uint32Array(), 'still empty')
assert.deepEqual(inflated, new Uint32Array(), 'still empty')
})

test('throws on bad input', function (t) {
t.exception(function () {
rle.decode(toUint32Array([100, 0, 0, 0]))
}, 'invalid delta count')
test('throws on bad input', function () {
assert.throws(
function () {
rle.decode(toUint32Array([100, 0, 0, 0]))
},
undefined,
'invalid delta count'
)
// t.exception.all also catches RangeErrors, which is what we expect from this
t.exception.all(function () {
rle.decode(
toUint32Array([
10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0,
10, 0,
])
)
}, 'missing delta')
assert.throws(
function () {
rle.decode(
toUint32Array([
10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0,
10, 0,
])
)
},
undefined,
'missing delta'
)
})

test('not power of two', function (t) {
test('not power of two', function () {
var deflated = rle.encode(toUint32Array([255, 255, 255, 240]))
var inflated = rle.decode(deflated)
t.alike(
assert.deepEqual(
inflated,
toUint32Array([255, 255, 255, 240]),
'output equal to input'
Expand Down
Loading