Skip to content

Commit

Permalink
feat: Add coverage for:
Browse files Browse the repository at this point in the history
* User defined number of bytes
* Not found element
  • Loading branch information
rodolfopietro97 committed Jul 4, 2023
1 parent b35914b commit 8cf8ffe
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions tests/bloom.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from 'chai'
import { Bloom } from '../src'
import { Buffer } from 'buffer'

describe('bloom', () => {
it('estimate k', () => {
Expand All @@ -11,18 +12,33 @@ describe('bloom', () => {
expect(Bloom.estimateK(500)).equal(3)
})
it('bloom add', () => {
const b = new Bloom(14)
b.add(Buffer.from('hello world', 'utf8'))
expect(b.bits.toString('hex')).equal('00000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000004000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000001000000000000020000000000000000000000000008000000000000000000000000000000080000000100000000000000000000040020000000000080000000000000000000080000000000000000000000000')
// Bloom filter with standard number of bites (definde in Bloom class)
const bloomWithStandardNumberOfBites = new Bloom(14)

// Bloom filter with user defined number of bites (2048 / 8 = 256)
const bloomWithUserDefinedNumberOfBites = new Bloom(14, Buffer.alloc(Bloom.BITS_LENGTH / 8))

bloomWithStandardNumberOfBites.add(Buffer.from('hello world', 'utf8'))
bloomWithUserDefinedNumberOfBites.add(Buffer.from('hello world', 'utf8'))

// Expect the same result because the number of bites is the same and the same data was added
expect(bloomWithStandardNumberOfBites.bits.toString('hex')).equal('00000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000004000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000001000000000000020000000000000000000000000008000000000000000000000000000000080000000100000000000000000000040020000000000080000000000000000000080000000000000000000000000')
expect(bloomWithUserDefinedNumberOfBites.bits.toString('hex')).equal('00000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000004000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000001000000000000020000000000000000000000000008000000000000000000000000000000080000000100000000000000000000040020000000000080000000000000000000080000000000000000000000000')

})
it('bloom test', () => {
// Initialize bloom filter with 14 hash functions and add number from 0 to 99
const b = new Bloom(14)
for (let i = 0; i < 100; i++) {
b.add(Buffer.from(i + '', 'utf8'))
}

// Positive case (number from 0 to 99 must be present in the bloom filter)
for (let i = 0; i < 100; i++) {
expect(b.test(Buffer.from(i + '', 'utf8'))).equal(true)
}

// Negative case (number from 100 must not be present in the bloom filter)
expect(b.test(Buffer.from('100', 'utf8'))).equal(false)
})
})

0 comments on commit 8cf8ffe

Please sign in to comment.