Skip to content

Commit

Permalink
chore: restore bitCount32 test (#489)
Browse files Browse the repository at this point in the history
bitCount32's test is skipped because it is slow, and it's slow because
it iterates over all integers between 0 and 2^32.

This keeps that test skipped but adds a new test that we can run, which
only tests a few inputs. It also speeds up the test-only comparison
function by about 9x in my testing.
  • Loading branch information
EvanHahn authored Feb 25, 2024
1 parent 964cb80 commit ee5233f
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions tests/sync/core-sync-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,27 @@ test('CoreReplicationState', async (t) => {
}
})

test('bitCount32', (t) => {
const testCases = new Set([0, 2 ** 32 - 1])
for (let i = 0; i < 32; i++) {
testCases.add(2 ** i)
testCases.add(2 ** i - 1)
}
for (let i = 0; i < 100; i++) {
testCases.add(Math.floor(Math.random() * 2 ** 32))
}

for (const n of testCases) {
const actual = bitCount32(n)
const expected = slowBitCount(n)
t.is(actual, expected, `${n.toString(2)} has ${expected} bit(s) set`)
}
})

// This takes several hours to run on my M2 Macbook Pro (it's the slowBitCount
// that takes a long time - bitCount32 takes about 23 seconds), so not running
// this by default. The test did pass when I ran it though.
test.skip('bitCount32', (t) => {
test.skip('bitCount32 (full test)', (t) => {
for (let n = 0; n < 2 ** 32; n++) {
if (n % 2 ** 28 === 0) console.log(n)
const bitCount = bitCount32(n)
Expand All @@ -362,7 +379,12 @@ async function createCore(key) {
* @param {number} n
*/
function slowBitCount(n) {
return n.toString(2).replace(/0/g, '').length
let result = 0
for (let i = 0, mask = 1; i < 32; i++) {
if (n & mask) result++
mask <<= 1
}
return result
}

/**
Expand Down

0 comments on commit ee5233f

Please sign in to comment.