-
Notifications
You must be signed in to change notification settings - Fork 791
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
API Tests for the bloom module #330
Changes from all commits
27dd338
6700f21
219644d
fa9a89e
2a63762
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const tape = require('tape') | ||
const Bloom = require('../../lib/bloom') | ||
const utils = require('ethereumjs-util') | ||
|
||
const byteSize = 256 | ||
|
||
tape('bloom', (t) => { | ||
t.test('should initialize without params', (st) => { | ||
const b = new Bloom() | ||
st.deepEqual(b.bitvector, utils.zeros(byteSize), 'should be empty') | ||
st.end() | ||
}) | ||
|
||
t.test('shouldnt initialize with invalid bitvector', (st) => { | ||
st.throws(() => new Bloom('invalid'), /AssertionError/, 'should fail for invalid type') | ||
st.throws(() => new Bloom(utils.zeros(byteSize / 2), /AssertionError/), 'should fail for invalid length') | ||
st.end() | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice, looks good. |
||
|
||
t.test('should contain values of hardcoded bitvector', (st) => { | ||
const hex = '00000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000' | ||
const vector = Buffer.from(hex, 'hex') | ||
|
||
const b = new Bloom(vector) | ||
st.true(b.check('value 1'), 'should contain string "value 1"') | ||
st.true(b.check('value 2'), 'should contain string "value 2"') | ||
st.end() | ||
}) | ||
|
||
t.test('check shouldnt be tautology', (st) => { | ||
const b = new Bloom() | ||
st.false(b.check('random value'), 'should not contain string "random value"') | ||
st.end() | ||
}) | ||
|
||
t.test('should correctly add value', (st) => { | ||
const b = new Bloom() | ||
b.add('value') | ||
let found = b.check('value') | ||
st.true(found, 'should contain added value') | ||
st.end() | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. |
||
|
||
t.test('should check multiple values', (st) => { | ||
const b = new Bloom() | ||
b.add('value 1') | ||
b.add('value 2') | ||
let found = b.multiCheck(['value 1', 'value 2']) | ||
st.true(found, 'should contain both values') | ||
st.end() | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Phew, the whole usage of types (string vs Buffer) in the Bloom implementation respectively the corresponding documentation is really a mess: but right, this is exactly what the tests you have written are for to discover! 😄 I've opened an according issue #332. Within the current implementation the Buffer conversion in the Re-architecturing whole parts of the library definitely doesn't have to be part of the scope of the work you are doing here, it's already great that we've discovered this now. If you want you can just comment out this test, then we can re-add once the issue from above is fixed. I would then approve this part of your PR series. |
||
|
||
t.test('should or two filters', (st) => { | ||
const b1 = new Bloom() | ||
b1.add('value 1') | ||
const b2 = new Bloom() | ||
b2.add('value 2') | ||
|
||
b1.or(b2) | ||
st.true(b1.check('value 2'), 'should contain "value 2" after or') | ||
st.end() | ||
}) | ||
|
||
t.test('should generate the correct bloom filter value', (st) => { | ||
let bloom = new Bloom() | ||
bloom.add(Buffer.from('1d7022f5b17d2f8b695918fb48fa1089c9f85401', 'hex')) | ||
bloom.add(Buffer.from('8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925', 'hex')) | ||
bloom.add(Buffer.from('0000000000000000000000005409ed021d9299bf6814279a6a1411a7e866a631', 'hex')) | ||
bloom.add(Buffer.from('0000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c48', 'hex')) | ||
st.equal( | ||
bloom.bitvector.toString('hex'), | ||
'00000000000000000000080000800000000000000000000000000000000000000000000000000000000000000000000000000000000004000000080000200000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000010008000000000000000002000000000000000000000000008000000000000' | ||
) | ||
st.end() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. |
||
}) | ||
}) |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good.