add test coverage. more tests
This commit is contained in:
parent
95ac6e0f55
commit
9536889468
@ -11,6 +11,7 @@
|
|||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "ts-mocha 'test/*.spec.ts'",
|
"test": "ts-mocha 'test/*.spec.ts'",
|
||||||
|
"coverage": "nyc npm run test",
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"clean": "rm -rf lib/",
|
"clean": "rm -rf lib/",
|
||||||
"prepare": "npm run clean && npm run build",
|
"prepare": "npm run clean && npm run build",
|
||||||
|
@ -14,10 +14,6 @@ export default class MerkleTree {
|
|||||||
return this._layers.slice()
|
return this._layers.slice()
|
||||||
}
|
}
|
||||||
|
|
||||||
set layers(value: Array<Element[]>) {
|
|
||||||
this._layers = value
|
|
||||||
}
|
|
||||||
|
|
||||||
levels: number
|
levels: number
|
||||||
capacity: number
|
capacity: number
|
||||||
private _hashFn: HashFunction<Element>
|
private _hashFn: HashFunction<Element>
|
||||||
@ -86,7 +82,7 @@ export default class MerkleTree {
|
|||||||
* Insert multiple elements into the tree.
|
* Insert multiple elements into the tree.
|
||||||
* @param {Array} elements Elements to insert
|
* @param {Array} elements Elements to insert
|
||||||
*/
|
*/
|
||||||
bulkInsert(elements: Element[]) {
|
bulkInsert(elements: Element[]): void {
|
||||||
if (!elements.length) {
|
if (!elements.length) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -172,30 +168,21 @@ export default class MerkleTree {
|
|||||||
* @param comparator A function that checks leaf value equality
|
* @param comparator A function that checks leaf value equality
|
||||||
* @returns {number} Index if element is found, otherwise -1
|
* @returns {number} Index if element is found, otherwise -1
|
||||||
*/
|
*/
|
||||||
indexOf(element: Element, comparator?: <T, R> (arg0: T, arg1: T) => R): number {
|
indexOf(element: Element, comparator?: <T> (arg0: T, arg1: T) => boolean): number {
|
||||||
if (comparator) {
|
if (comparator) {
|
||||||
return this._layers[0].findIndex((el) => comparator<Element, number>(element, el))
|
return this._layers[0].findIndex((el) => comparator<Element>(element, el))
|
||||||
} else {
|
} else {
|
||||||
return this._layers[0].indexOf(element)
|
return this._layers[0].indexOf(element)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getTreeEdge(edgeElement: Element, index?: number): TreeEdge {
|
getTreeEdge(edgeElement: Element): TreeEdge {
|
||||||
if (edgeElement === 'undefined') {
|
const leaves = this._layers[0]
|
||||||
throw new Error('element is required')
|
const edgeIndex = leaves.indexOf(edgeElement)
|
||||||
}
|
if (edgeIndex <= -1) {
|
||||||
let edgeIndex: number
|
|
||||||
if (!Number.isInteger(index)) {
|
|
||||||
index = -1
|
|
||||||
const leaves = this._layers[0]
|
|
||||||
index = leaves.indexOf(edgeElement)
|
|
||||||
edgeIndex = index
|
|
||||||
}
|
|
||||||
|
|
||||||
if (index <= -1) {
|
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
const edgePath = this.path(index)
|
const edgePath = this.path(edgeIndex)
|
||||||
return { edgePath, edgeElement, edgeIndex }
|
return { edgePath, edgeElement, edgeIndex }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,6 +110,12 @@ describe('MerkleTree', () => {
|
|||||||
const call = () => tree.bulkInsert([3, 4, 5])
|
const call = () => tree.bulkInsert([3, 4, 5])
|
||||||
should().throw(call, 'Tree is full')
|
should().throw(call, 'Tree is full')
|
||||||
})
|
})
|
||||||
|
it('should bypass empty elements', () => {
|
||||||
|
const elements = [1, 2, 3, 4]
|
||||||
|
const tree = new MerkleTree(2, elements)
|
||||||
|
tree.bulkInsert([])
|
||||||
|
assert.deepEqual(tree.elements, elements, 'No elements inserted')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('#update', () => {
|
describe('#update', () => {
|
||||||
@ -164,6 +170,11 @@ describe('MerkleTree', () => {
|
|||||||
should().equal(tree.indexOf(3), 2)
|
should().equal(tree.indexOf(3), 2)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should work with comparator', () => {
|
||||||
|
const tree = new MerkleTree(10, [1, 2, 3, 4, 5])
|
||||||
|
should().equal(tree.indexOf(4, (arg0, arg1) => arg0 === arg1), 3)
|
||||||
|
})
|
||||||
|
|
||||||
it('should return -1 for non existent element', () => {
|
it('should return -1 for non existent element', () => {
|
||||||
const tree = new MerkleTree(10, [1, 2, 3, 4, 5])
|
const tree = new MerkleTree(10, [1, 2, 3, 4, 5])
|
||||||
should().equal(tree.indexOf(42), -1)
|
should().equal(tree.indexOf(42), -1)
|
||||||
@ -235,7 +246,12 @@ describe('MerkleTree', () => {
|
|||||||
])
|
])
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
describe('#getTreeEdge', () => {
|
||||||
|
it('should return correct treeEdge', () => {
|
||||||
|
const tree = new MerkleTree(4, [0, 1, 2, 3, 4, 5])
|
||||||
|
console.log(tree.getTreeEdge(4))
|
||||||
|
})
|
||||||
|
})
|
||||||
describe('#getters', () => {
|
describe('#getters', () => {
|
||||||
const elements = [1, 2, 3, 4, 5]
|
const elements = [1, 2, 3, 4, 5]
|
||||||
const layers = [
|
const layers = [
|
@ -1,5 +1,4 @@
|
|||||||
import { MerkleTree, PartialMerkleTree } from '../src'
|
import { MerkleTree, PartialMerkleTree } from '../src'
|
||||||
import { assert, should } from 'chai'
|
|
||||||
import { it } from 'mocha'
|
import { it } from 'mocha'
|
||||||
|
|
||||||
describe('PartialMerkleTree', () => {
|
describe('PartialMerkleTree', () => {
|
||||||
@ -12,7 +11,6 @@ describe('PartialMerkleTree', () => {
|
|||||||
const leavesAfterEdge = leaves.splice(edge.edgeIndex)
|
const leavesAfterEdge = leaves.splice(edge.edgeIndex)
|
||||||
it('should initialize merkle tree', () => {
|
it('should initialize merkle tree', () => {
|
||||||
const partialTree = new PartialMerkleTree(edge, leavesAfterEdge, root)
|
const partialTree = new PartialMerkleTree(edge, leavesAfterEdge, root)
|
||||||
console.log(partialTree)
|
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user