uint int for greater and lesser than

pull/5370/head
aniket-engg 5 years ago committed by Aniket
parent f5ee22e0a3
commit b13c3447a2
  1. 15
      libs/remix-tests/sol/tests.sol.ts
  2. 4
      libs/remix-tests/src/assertionEvents.ts
  3. 8
      libs/remix-tests/tests/examples_0/assert_greaterThan_test.sol
  4. 8
      libs/remix-tests/tests/examples_0/assert_lesserThan_test.sol
  5. 24
      libs/remix-tests/tests/testRunner.spec.ts

@ -50,6 +50,13 @@ library Assert {
string expected string expected
); );
event AssertionEventUintInt(
bool passed,
string message,
uint returned,
int expected
);
function ok(bool a, string memory message) public returns (bool result) { function ok(bool a, string memory message) public returns (bool result) {
result = a; result = a;
emit AssertionEvent(result, message); emit AssertionEvent(result, message);
@ -150,14 +157,14 @@ library Assert {
emit AssertionEventInt(result, message, a, b); emit AssertionEventInt(result, message, a, b);
} }
// TODO: safely compare between uint and int // TODO: safely compare between uint and int
function greaterThan(uint a, int b, string memory message) public returns (bool result) { function greaterThan(uint256 a, int256 b, string memory message) public returns (bool result) {
if(b < int(0)) { if(b < int(0)) {
// int is negative uint "a" always greater // int is negative uint "a" always greater
result = true; result = true;
} else { } else {
result = (a > uint(b)); result = (a > uint(b));
} }
emit AssertionEvent(result, message); emit AssertionEventUintInt(result, message, a, b);
} }
function greaterThan(int a, uint b, string memory message) public returns (bool result) { function greaterThan(int a, uint b, string memory message) public returns (bool result) {
if(a < int(0)) { if(a < int(0)) {
@ -179,14 +186,14 @@ library Assert {
emit AssertionEventInt(result, message, a, b); emit AssertionEventInt(result, message, a, b);
} }
// TODO: safely compare between uint and int // TODO: safely compare between uint and int
function lesserThan(uint a, int b, string memory message) public returns (bool result) { function lesserThan(uint256 a, int256 b, string memory message) public returns (bool result) {
if(b < int(0)) { if(b < int(0)) {
// int is negative int "b" always lesser // int is negative int "b" always lesser
result = false; result = false;
} else { } else {
result = (a < uint(b)); result = (a < uint(b));
} }
emit AssertionEvent(result, message); emit AssertionEventUintInt(result, message, a, b);
} }
function lesserThan(int a, uint b, string memory message) public returns (bool result) { function lesserThan(int a, uint b, string memory message) public returns (bool result) {

@ -26,6 +26,10 @@ const assertionEvents = [
{ {
name: 'AssertionEventString', name: 'AssertionEventString',
params: ['bool', 'string', 'string', 'string'] params: ['bool', 'string', 'string', 'string']
},
{
name: 'AssertionEventUintInt',
params: ['bool', 'string', 'uint256', 'int256']
} }
] ]

@ -17,4 +17,12 @@ contract AssertGreaterThanTest {
function greaterThanIntFailTest() public { function greaterThanIntFailTest() public {
Assert.greaterThan(int(-1), int(1), "greaterThanIntFailTest fails"); Assert.greaterThan(int(-1), int(1), "greaterThanIntFailTest fails");
} }
function greaterThanUintIntPassTest() public {
Assert.greaterThan(uint(1), int(-2), "greaterThanUintIntPassTest passes");
}
function greaterThanUintIntFailTest() public {
Assert.greaterThan(uint(1), int(2), "greaterThanUintIntFailTest fails");
}
} }

@ -17,4 +17,12 @@ contract AssertLesserThanTest {
function lesserThanIntFailTest() public { function lesserThanIntFailTest() public {
Assert.lesserThan(int(1), int(-1), "lesserThanIntFailTest fails"); Assert.lesserThan(int(1), int(-1), "lesserThanIntFailTest fails");
} }
function lesserThanUintIntPassTest() public {
Assert.lesserThan(uint(1), int(2), "lesserThanUintIntPassTest passes");
}
function lesserThanUintIntFailTest() public {
Assert.lesserThan(uint(-1), int(-1), "lesserThanUintIntFailTest fails");
}
} }

@ -221,12 +221,12 @@ describe('testRunner', () => {
afterAll(() => { tests = [] }) afterAll(() => { tests = [] })
it('should have 2 passing test', () => { it('should have 3 passing test', () => {
assert.equal(results.passingNum, 2) assert.equal(results.passingNum, 3)
}) })
it('should have 2 failing test', () => { it('should have 3 failing test', () => {
assert.equal(results.failureNum, 2) assert.equal(results.failureNum, 3)
}) })
it('should return', () => { it('should return', () => {
deepEqualExcluding(tests, [ deepEqualExcluding(tests, [
@ -235,7 +235,9 @@ describe('testRunner', () => {
{ type: 'testPass', value: 'Greater than uint pass test', context: 'AssertGreaterThanTest' }, { type: 'testPass', value: 'Greater than uint pass test', context: 'AssertGreaterThanTest' },
{ type: 'testFailure', value: 'Greater than uint fail test', errMsg: 'greaterThanUintFailTest fails', context: 'AssertGreaterThanTest', expected: '4', returned: '1'}, { type: 'testFailure', value: 'Greater than uint fail test', errMsg: 'greaterThanUintFailTest fails', context: 'AssertGreaterThanTest', expected: '4', returned: '1'},
{ type: 'testPass', value: 'Greater than int pass test', context: 'AssertGreaterThanTest' }, { type: 'testPass', value: 'Greater than int pass test', context: 'AssertGreaterThanTest' },
{ type: 'testFailure', value: 'Greater than int fail test', errMsg: 'greaterThanIntFailTest fails', context: 'AssertGreaterThanTest', expected: '1', returned: '-1'} { type: 'testFailure', value: 'Greater than int fail test', errMsg: 'greaterThanIntFailTest fails', context: 'AssertGreaterThanTest', expected: '1', returned: '-1'},
{ type: 'testPass', value: 'Greater than uint int pass test', context: 'AssertGreaterThanTest' },
{ type: 'testFailure', value: 'Greater than uint int fail test', errMsg: 'greaterThanUintIntFailTest fails', context: 'AssertGreaterThanTest', expected: '2', returned: '1'}
], ['time']) ], ['time'])
}) })
}) })
@ -251,12 +253,12 @@ describe('testRunner', () => {
afterAll(() => { tests = [] }) afterAll(() => { tests = [] })
it('should have 2 passing test', () => { it('should have 3 passing test', () => {
assert.equal(results.passingNum, 2) assert.equal(results.passingNum, 3)
}) })
it('should have 2 failing test', () => { it('should have 3 failing test', () => {
assert.equal(results.failureNum, 2) assert.equal(results.failureNum, 3)
}) })
it('should return', () => { it('should return', () => {
@ -266,7 +268,9 @@ describe('testRunner', () => {
{ type: 'testPass', value: 'Lesser than uint pass test', context: 'AssertLesserThanTest' }, { type: 'testPass', value: 'Lesser than uint pass test', context: 'AssertLesserThanTest' },
{ type: 'testFailure', value: 'Lesser than uint fail test', errMsg: 'lesserThanUintFailTest fails', context: 'AssertLesserThanTest', expected: '2', returned: '4'}, { type: 'testFailure', value: 'Lesser than uint fail test', errMsg: 'lesserThanUintFailTest fails', context: 'AssertLesserThanTest', expected: '2', returned: '4'},
{ type: 'testPass', value: 'Lesser than int pass test', context: 'AssertLesserThanTest' }, { type: 'testPass', value: 'Lesser than int pass test', context: 'AssertLesserThanTest' },
{ type: 'testFailure', value: 'Lesser than int fail test', errMsg: 'lesserThanIntFailTest fails', context: 'AssertLesserThanTest', expected: '-1', returned: '1'} { type: 'testFailure', value: 'Lesser than int fail test', errMsg: 'lesserThanIntFailTest fails', context: 'AssertLesserThanTest', expected: '-1', returned: '1'},
{ type: 'testPass', value: 'Lesser than uint int pass test', context: 'AssertLesserThanTest' },
{ type: 'testFailure', value: 'Lesser than uint int fail test', errMsg: 'lesserThanUintIntFailTest fails', context: 'AssertLesserThanTest', expected: '-1', returned: '115792089237316195423570985008687907853269984665640564039457584007913129639935'}
], ['time']) ], ['time'])
}) })
}) })

Loading…
Cancel
Save