diff --git a/test/unit/helpers/RoundChanges.js b/test/unit/helpers/RoundChanges.js index ba51bc62b48..8a55f5e064c 100644 --- a/test/unit/helpers/RoundChanges.js +++ b/test/unit/helpers/RoundChanges.js @@ -9,188 +9,109 @@ var RoundChanges = require('../../../helpers/RoundChanges.js'); describe('RoundChanges', function () { + var validScope; + + beforeEach(function () { + validScope = { + round: 1, + __private: { + feesByRound: { + 1: 500 + }, + unFeesByRound: {}, + rewardsByRound: { + 1: [0, 0, 100, 10] + }, + unRewardsByRound: {} + } + }; + }); + describe('constructor', function () { - it('should accept valid round fees', function () { - var round = 1; - var fees = 500; - var rewards = [0, 0, 100, 10]; - - var scope = { - round: round, - __private: { - feesByRound: {}, - rewardsByRound: {} - } - }; - scope.__private.feesByRound[round] = fees; - scope.__private.rewardsByRound[round] = rewards; - - var roundChanges = new RoundChanges(scope); - - node.expect(roundChanges.roundFees).equal(fees); - node.expect(_.isEqual(roundChanges.roundRewards, rewards)).to.be.ok; - }); + it('should accept valid scope', function () { + var roundChanges = new RoundChanges(validScope); - it('should take floor from fees value', function () { - var round = 1; - var fees = 50.9999999999999; + node.expect(roundChanges.roundFees).equal(validScope.__private.feesByRound[1]); + node.expect(_.isEqual(roundChanges.roundRewards, validScope.__private.rewardsByRound[1])).to.be.ok; + }); - var scope = { - round: round, - __private: { - feesByRound: {}, - rewardsByRound: {} - } - }; - scope.__private.feesByRound[round] = fees; + it('should floor fees value', function () { + validScope.__private.feesByRound[1] = 50.9999999999999; // Float - var roundChanges = new RoundChanges(scope); + var roundChanges = new RoundChanges(validScope); node.expect(roundChanges.roundFees).equal(50); }); - it('should take rounded number after exceeding precision', function () { - var round = 1; - var fees = 50.999999999999999; // Exceeded precision - node.expect(fees).equals(51); - var scope = { - round: round, - __private: { - feesByRound: {}, - rewardsByRound: {} - } - }; - scope.__private.feesByRound[round] = fees; - - var roundChanges = new RoundChanges(scope); + it('should round up fees after exceeding precision', function () { + validScope.__private.feesByRound[1] = 50.999999999999999; // Exceeded precision + + var roundChanges = new RoundChanges(validScope); node.expect(roundChanges.roundFees).equal(51); }); - it('should work properly for Infinity', function () { - var round = 1; - var fees = Number.MAX_VALUE; - var scope = { - round: round, - __private: { - feesByRound: {}, - rewardsByRound: {} - } - }; - scope.__private.feesByRound[round] = fees * 2; + it('should accept Infinite fees as expected', function () { + validScope.__private.feesByRound[1] = Number.MAX_VALUE * 2; // Infinity - var roundChanges = new RoundChanges(scope); + var roundChanges = new RoundChanges(validScope); node.expect(roundChanges.roundFees).equal(Infinity); }); - it('should accept accept backwards values if present', function () { - var round = 1; - var fees = 500; - var rewards = [0, 0, 100, 10]; - var unRewards = rewards.map(function (reward) { + it('should accept backwards values when present', function () { + validScope.__private.unRewardsByRound[1] = validScope.__private.rewardsByRound[1].map(function (reward) { return reward + 1; }); - var unFees = fees + 1; - - var scope = { - round: round, - backwards: true, - __private: { - feesByRound: {}, - rewardsByRound: {}, - unFeesByRound: {}, - unRewardsByRound: {} - } - }; - scope.__private.feesByRound[round] = fees; - scope.__private.rewardsByRound[round] = rewards; - scope.__private.unFeesByRound[round] = unFees; - scope.__private.unRewardsByRound[round] = unRewards; - - var roundChanges = new RoundChanges(scope); - - node.expect(roundChanges.roundFees).equal(unFees); - node.expect(_.isEqual(roundChanges.roundRewards, unRewards)).to.be.ok; + validScope.__private.unFeesByRound[1] = validScope.__private.feesByRound[1] + 1; + validScope.backwards = true; + + var roundChanges = new RoundChanges(validScope); + + node.expect(roundChanges.roundFees).equal(validScope.__private.unFeesByRound[1]); + node.expect(_.isEqual(roundChanges.roundRewards, validScope.__private.unRewardsByRound[1])).to.be.ok; }); }); describe('at', function () { - it('should calculate round changes from mocked data', function () { - var round = 1; - var fees = 500; - var rewards = [0, 0, 100, 10]; - - var scope = { - round: round, - __private: { - feesByRound: {}, - rewardsByRound: {} - } - }; - scope.__private.feesByRound[round] = fees; - scope.__private.rewardsByRound[round] = rewards; - - var roundChanges = new RoundChanges(scope); + it('should calculate round changes from valid scope', function () { + var roundChanges = new RoundChanges(validScope); var rewardsAt = 2; var res = roundChanges.at(rewardsAt); node.expect(res.fees).equal(4); node.expect(res.feesRemaining).equal(96); - node.expect(res.rewards).equal(rewards[rewardsAt]); // 100 + node.expect(res.rewards).equal(validScope.__private.rewardsByRound[1][rewardsAt]); // 100 node.expect(res.balance).equal(104); }); it('should calculate round changes from Infinite fees', function () { - var round = 1; - var fees = Infinity; - var rewards = [0, 0, 100, 10]; - - var scope = { - round: round, - __private: { - feesByRound: {}, - rewardsByRound: {} - } - }; - scope.__private.feesByRound[round] = fees; - scope.__private.rewardsByRound[round] = rewards; - - var roundChanges = new RoundChanges(scope); + validScope.__private.feesByRound[1] = Infinity; + + var roundChanges = new RoundChanges(validScope); var rewardsAt = 2; var res = roundChanges.at(rewardsAt); node.expect(res.fees).equal(Infinity); node.expect(res.feesRemaining).to.be.NaN; - node.expect(res.rewards).equal(rewards[rewardsAt]); // 100 + node.expect(res.rewards).equal(validScope.__private.rewardsByRound[1][rewardsAt]); // 100 node.expect(res.balance).equal(Infinity); }); it('should calculate round changes from Number.MAX_VALUE fees', function () { - var round = 1; - var fees = Number.MAX_VALUE; // 1.7976931348623157e+308 - - var rewards = [0, 0, 100, 10]; - - var scope = { - round: round, - __private: { - feesByRound: {}, - rewardsByRound: {} - } - }; - scope.__private.feesByRound[round] = fees; - scope.__private.rewardsByRound[round] = rewards; - - var roundChanges = new RoundChanges(scope); + validScope.__private.feesByRound[1] = Number.MAX_VALUE; // 1.7976931348623157e+308 + + var roundChanges = new RoundChanges(validScope); var rewardsAt = 2; var res = roundChanges.at(rewardsAt); var expectedFees = 1779894192932990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099; // 1.7976931348623157e+308 / 101 (delegates num) + node.expect(res.fees).equal(expectedFees); - node.expect(res.rewards).equal(rewards[rewardsAt]); // 100 + node.expect(res.rewards).equal(validScope.__private.rewardsByRound[1][rewardsAt]); // 100 node.expect(res.feesRemaining).equal(1); + var expectedBalance = 1779894192932990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990099009900990199; // 1.7976931348623157e+308 / 101 (delegates num) + 100 node.expect(res.balance).equal(expectedBalance); });