From bf34911857f5d34699a7664d86c6bf88f53bf4f8 Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Mon, 6 Aug 2018 15:18:19 -0300 Subject: [PATCH] Remove Math.min64 and Math.max64 (#1156) * remove Math.min64 and Math.max64 * refactor Math tests to use return values * enhance Math coverage --- contracts/math/Math.sol | 12 ++------- contracts/mocks/MathMock.sol | 19 +++----------- test/library/Math.test.js | 51 +++++++++++++++--------------------- 3 files changed, 27 insertions(+), 55 deletions(-) diff --git a/contracts/math/Math.sol b/contracts/math/Math.sol index 3928eb9d5..0292f5180 100644 --- a/contracts/math/Math.sol +++ b/contracts/math/Math.sol @@ -6,19 +6,11 @@ pragma solidity ^0.4.24; * @dev Assorted math operations */ library Math { - function max64(uint64 _a, uint64 _b) internal pure returns (uint64) { + function max(uint256 _a, uint256 _b) internal pure returns (uint256) { return _a >= _b ? _a : _b; } - function min64(uint64 _a, uint64 _b) internal pure returns (uint64) { - return _a < _b ? _a : _b; - } - - function max256(uint256 _a, uint256 _b) internal pure returns (uint256) { - return _a >= _b ? _a : _b; - } - - function min256(uint256 _a, uint256 _b) internal pure returns (uint256) { + function min(uint256 _a, uint256 _b) internal pure returns (uint256) { return _a < _b ? _a : _b; } } diff --git a/contracts/mocks/MathMock.sol b/contracts/mocks/MathMock.sol index a82898d72..087ccf142 100644 --- a/contracts/mocks/MathMock.sol +++ b/contracts/mocks/MathMock.sol @@ -5,22 +5,11 @@ import "../../contracts/math/Math.sol"; contract MathMock { - uint64 public result64; - uint256 public result256; - - function max64(uint64 _a, uint64 _b) public { - result64 = Math.max64(_a, _b); - } - - function min64(uint64 _a, uint64 _b) public { - result64 = Math.min64(_a, _b); - } - - function max256(uint256 _a, uint256 _b) public { - result256 = Math.max256(_a, _b); + function max(uint256 _a, uint256 _b) public pure returns (uint256) { + return Math.max(_a, _b); } - function min256(uint256 _a, uint256 _b) public { - result256 = Math.min256(_a, _b); + function min(uint256 _a, uint256 _b) public pure returns (uint256) { + return Math.min(_a, _b); } } diff --git a/test/library/Math.test.js b/test/library/Math.test.js index d079a848f..999b685e9 100644 --- a/test/library/Math.test.js +++ b/test/library/Math.test.js @@ -1,43 +1,34 @@ const MathMock = artifacts.require('MathMock'); contract('Math', function () { - let math; + const min = 1234; + const max = 5678; beforeEach(async function () { - math = await MathMock.new(); + this.math = await MathMock.new(); }); - it('returns max64 correctly', async function () { - const a = 5678; - const b = 1234; - await math.max64(a, b); - const result = await math.result64(); - assert.equal(result, a); - }); - - it('returns min64 correctly', async function () { - const a = 5678; - const b = 1234; - await math.min64(a, b); - const result = await math.result64(); - - assert.equal(result, b); - }); + describe('max', function () { + it('is correctly detected in first argument position', async function () { + const result = await this.math.max(max, min); + assert.equal(result, max); + }); - it('returns max256 correctly', async function () { - const a = 5678; - const b = 1234; - await math.max256(a, b); - const result = await math.result256(); - assert.equal(result, a); + it('is correctly detected in second argument position', async function () { + const result = await this.math.max(min, max); + assert.equal(result, max); + }); }); - it('returns min256 correctly', async function () { - const a = 5678; - const b = 1234; - await math.min256(a, b); - const result = await math.result256(); + describe('min', function () { + it('is correctly detected in first argument position', async function () { + const result = await this.math.min(min, max); + assert.equal(result, min); + }); - assert.equal(result, b); + it('is correctly detected in second argument position', async function () { + const result = await this.math.min(max, min); + assert.equal(result, min); + }); }); });