UNPKG

bitcore-node

Version:

A blockchain indexing node with extended capabilities using bitcore

57 lines 2.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const mocha_1 = require("mocha"); const utils_1 = require("../../../src/utils"); const unit_1 = require("../../helpers/unit"); (0, mocha_1.describe)('Partition', () => { before(unit_1.unitBeforeHelper); after(unit_1.unitAfterHelper); it('should split an array of 5 to 5 arrays', () => { let testArr = [1, 2, 3, 4, 5]; let partitioned = (0, utils_1.partition)(testArr, 5); (0, chai_1.expect)(partitioned).to.deep.equal([[1, 2, 3, 4, 5]]); (0, chai_1.expect)(testArr).to.deep.equal([1, 2, 3, 4, 5]); }); it('should handle 0', () => { let testArr = [1, 2, 3, 4, 5]; let partitioned = (0, utils_1.partition)(testArr, 0); (0, chai_1.expect)(partitioned).to.deep.equal([[1], [2], [3], [4], [5]]); (0, chai_1.expect)(testArr).to.deep.equal([1, 2, 3, 4, 5]); }); it('should handle one', () => { let testArr = [1, 2, 3, 4, 5]; let partitioned = (0, utils_1.partition)(testArr, 1); (0, chai_1.expect)(partitioned).to.deep.equal([[1], [2], [3], [4], [5]]); (0, chai_1.expect)(testArr).to.deep.equal([1, 2, 3, 4, 5]); }); it('should handle two', () => { let testArr = [1, 2, 3, 4, 5]; let partitioned = (0, utils_1.partition)(testArr, 2); (0, chai_1.expect)(partitioned).to.deep.equal([[1, 2], [3, 4], [5]]); (0, chai_1.expect)(testArr).to.deep.equal([1, 2, 3, 4, 5]); }); it('should handle between one and zero', () => { let testArr = [1, 2, 3, 4, 5]; let partitioned = (0, utils_1.partition)(testArr, 0.15); (0, chai_1.expect)(partitioned).to.deep.equal([[1], [2], [3], [4], [5]]); (0, chai_1.expect)(testArr).to.deep.equal([1, 2, 3, 4, 5]); }); it('should handle different sizes of arrays', () => { for (let i = 0; i < 1000; i++) { const randomLen = Math.floor(Math.random() * 100) + 1; let adjustment = 0; let randomArr = new Array(randomLen).fill(1).map(num => num + adjustment++); let partitioned = (0, utils_1.partition)(randomArr, 3); const amountInBatches = partitioned.reduce((sum, arr) => sum + arr.length, 0); (0, chai_1.expect)(amountInBatches).to.equal(randomLen); (0, chai_1.expect)(partitioned.length).to.be.gte(Math.floor(randomLen / 3)); let lastBatch = partitioned[partitioned.length - 1]; if (!lastBatch) { console.error('Array partition fails with length', randomLen); } (0, chai_1.expect)(randomArr.length).to.deep.equal(randomLen); } }); }); //# sourceMappingURL=partition.unit.js.map