add test coverage. more tests

This commit is contained in:
Sergei SMART 2022-03-01 11:16:52 +10:00
parent 95ac6e0f55
commit 9536889468
5 changed files with 27 additions and 24 deletions

1
.nvmrc Normal file
View File

@ -0,0 +1 @@
14.17

View File

@ -11,6 +11,7 @@
},
"scripts": {
"test": "ts-mocha 'test/*.spec.ts'",
"coverage": "nyc npm run test",
"build": "tsc",
"clean": "rm -rf lib/",
"prepare": "npm run clean && npm run build",

View File

@ -14,10 +14,6 @@ export default class MerkleTree {
return this._layers.slice()
}
set layers(value: Array<Element[]>) {
this._layers = value
}
levels: number
capacity: number
private _hashFn: HashFunction<Element>
@ -86,7 +82,7 @@ export default class MerkleTree {
* Insert multiple elements into the tree.
* @param {Array} elements Elements to insert
*/
bulkInsert(elements: Element[]) {
bulkInsert(elements: Element[]): void {
if (!elements.length) {
return
}
@ -172,30 +168,21 @@ export default class MerkleTree {
* @param comparator A function that checks leaf value equality
* @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) {
return this._layers[0].findIndex((el) => comparator<Element, number>(element, el))
return this._layers[0].findIndex((el) => comparator<Element>(element, el))
} else {
return this._layers[0].indexOf(element)
}
}
getTreeEdge(edgeElement: Element, index?: number): TreeEdge {
if (edgeElement === 'undefined') {
throw new Error('element is required')
}
let edgeIndex: number
if (!Number.isInteger(index)) {
index = -1
const leaves = this._layers[0]
index = leaves.indexOf(edgeElement)
edgeIndex = index
}
if (index <= -1) {
getTreeEdge(edgeElement: Element): TreeEdge {
const leaves = this._layers[0]
const edgeIndex = leaves.indexOf(edgeElement)
if (edgeIndex <= -1) {
return null
}
const edgePath = this.path(index)
const edgePath = this.path(edgeIndex)
return { edgePath, edgeElement, edgeIndex }
}

View File

@ -110,6 +110,12 @@ describe('MerkleTree', () => {
const call = () => tree.bulkInsert([3, 4, 5])
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', () => {
@ -164,6 +170,11 @@ describe('MerkleTree', () => {
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', () => {
const tree = new MerkleTree(10, [1, 2, 3, 4, 5])
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', () => {
const elements = [1, 2, 3, 4, 5]
const layers = [

View File

@ -1,5 +1,4 @@
import { MerkleTree, PartialMerkleTree } from '../src'
import { assert, should } from 'chai'
import { it } from 'mocha'
describe('PartialMerkleTree', () => {
@ -12,7 +11,6 @@ describe('PartialMerkleTree', () => {
const leavesAfterEdge = leaves.splice(edge.edgeIndex)
it('should initialize merkle tree', () => {
const partialTree = new PartialMerkleTree(edge, leavesAfterEdge, root)
console.log(partialTree)
return true
})
})