From d3be68298f510a4ac20f29a07e459e16dd74f81a Mon Sep 17 00:00:00 2001 From: aniket-engg Date: Tue, 5 Jan 2021 18:35:22 +0530 Subject: [PATCH] safemath tests enabled and updated --- .../tests/examples_4/SafeMath_test.sol | 26 +++++++++++++++---- libs/remix-tests/tests/testRunner.spec.ts | 20 ++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/libs/remix-tests/tests/examples_4/SafeMath_test.sol b/libs/remix-tests/tests/examples_4/SafeMath_test.sol index aa5112dc64..be24d43312 100644 --- a/libs/remix-tests/tests/examples_4/SafeMath_test.sol +++ b/libs/remix-tests/tests/examples_4/SafeMath_test.sol @@ -23,7 +23,7 @@ contract SafeMathTest { function safeMultiplicationShouldRevert() public returns (bool) { uint256 a = 4; uint256 b = 2 ** 256 - 1; - (bool success, bytes memory data) = address(safemathproxy).call.gas(40000).value(0)(abi.encode("mulProxy, [a, b]")); + (bool success, bytes memory data) = address(safemathproxy).call{gas:40000, value:0}(abi.encode("mulProxy, [a, b]")); return Assert.equal( success, false, @@ -34,7 +34,7 @@ contract SafeMathTest { function safeDivisionByZeroShouldRevert() public returns (bool) { uint256 a = 4; uint256 b = 0; - (bool success, bytes memory data) = address(safemathproxy).call.gas(40000).value(0)(abi.encode("divProxy, [a, b]")); + (bool success, bytes memory data) = address(safemathproxy).call{gas:40000, value:0}(abi.encode("divProxy, [a, b]")); return Assert.equal( success, false, @@ -53,7 +53,7 @@ contract SafeMathTest { } function safeSubtractShouldRevert() public returns (bool) { - (bool success, bytes memory data) = address(safemathproxy).call.gas(40000).value(0)(abi.encode("subProxy, [0, 1]")); + (bool success, bytes memory data) = address(safemathproxy).call{gas:40000, value:0}(abi.encode("subProxy, [0, 1]")); return Assert.equal( success, false, @@ -61,6 +61,22 @@ contract SafeMathTest { ); } + function safeSubtractShouldRevertUsingTryCatch() public returns (bool) { + try safemathproxy.subProxy(0, 1) returns ( uint256 res) { + Assert.ok(false, "Should revert"); + } catch (bytes memory /*lowLevelData*/) { + Assert.ok(true, "safe subtract should revert"); + } + } + + function safeSubtractShouldNotRevert() public returns (bool) { + try safemathproxy.subProxy(3, 2) returns ( uint256 res) { + Assert.equal(res, 1, "should be equal to 1"); + } catch (bytes memory /*lowLevelData*/) { + Assert.ok(false, "safe subtract should not revert"); + } + } + function unsafeAdditionShouldOverflow() public returns (bool) { uint256 a = 1; uint256 b = 2 ** 256 - 1; @@ -70,7 +86,7 @@ contract SafeMathTest { function safeAdditionShouldRevert() public returns (bool) { uint256 a = 1; uint256 b = 2 ** 256 - 1; - (bool success, bytes memory data) = address(safemathproxy).call.gas(40000).value(0)(abi.encode("addProxy, [a, b]")); + (bool success, bytes memory data) = address(safemathproxy).call{gas:40000, value:0}(abi.encode("addProxy, [a, b]")); return Assert.equal( success, false, @@ -81,7 +97,7 @@ contract SafeMathTest { function safeModulusShouldRevert() public returns (bool) { uint256 a = 1; uint256 b = 0; - (bool success, bytes memory data) = address(safemathproxy).call.gas(40000).value(0)(abi.encode("modProxy, [a, b]")); + (bool success, bytes memory data) = address(safemathproxy).call{gas:40000, value:0}(abi.encode("modProxy, [a, b]")); return Assert.equal( success, false, diff --git a/libs/remix-tests/tests/testRunner.spec.ts b/libs/remix-tests/tests/testRunner.spec.ts index 593de29e8d..5bfc9312a7 100644 --- a/libs/remix-tests/tests/testRunner.spec.ts +++ b/libs/remix-tests/tests/testRunner.spec.ts @@ -392,6 +392,26 @@ describe('testRunner', () => { }) }) + //Test signed/unsigned integer weight + describe('test SafeMath library', () => { + const filename: string = __dirname + '/examples_4/SafeMath_test.sol' + + beforeAll(done => { + compileAndDeploy(filename, function (_err: Error | null | undefined, compilationData: object, contracts: any, asts: any, accounts: string[]) { + runTest('SafeMathTest', contracts.SafeMathTest, compilationData[filename]['SafeMathTest'], asts[filename], { accounts }, testCallback, resultsCallback(done)) + }) + }) + + afterAll(() => { tests = [] }) + + it('should have 10 passing tests', () => { + assert.equal(results.passingNum, 10) + }) + it('should have 0 failing tests', () => { + assert.equal(results.failureNum, 0) + }) + }) + //Test signed/unsigned integer weight describe('test number weight', () => { const filename: string = __dirname + '/number/number_test.sol'