Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding tests for Arithmetic #481

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions contracts/solidity/cryptomath/Arithmetic.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract Arithmetic {
string public name = "Arithmetic";
uint256 maxUint = type(uint256).max;
uint256 minUint = type(uint256).min;

function checkName() external view returns (string memory){
return name;
}

function add() external {
name = "Arithmetic check if NOT reverted";
maxUint = maxUint + 1;
}

function add2() external {
uint256 tmp = maxUint;
name = "Arithmetic check if NOT reverted";
tmp += 100;
}

function mul() external {
uint8 maxUint8 = type(uint8).max;
name = "Arithmetic check if NOT reverted";
maxUint8 * 2;
}

function dec() external {
// This subtraction will revert on underflow.
name = "Arithmetic check if NOT reverted";
minUint--;
}

function sub() external {
uint256 tmp = minUint;
name = "Arithmetic check if NOT reverted";
tmp -= 1;
}

function negativeHasMoreValues() external {
int tmp;
int x = type(int).min;
name = "Arithmetic check if NOT reverted";
tmp = -x;
}

function uncheckedAdd() external view returns (bool) {
unchecked {
uint256 tmp;
tmp = maxUint + 1;

return true;
}
}

function uncheckedSub() external view returns (bool) {
unchecked {
uint256 tmp = minUint;
tmp -= 1;

return true;
}
}

}
1 change: 1 addition & 0 deletions test/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ const Contract = {
FunctionsChild: 'FunctionsChild',
FunctionsParent: 'FunctionsParent',
Scoping: 'Scoping',
Arithmetic: "Arithmetic"
}

const CALL_EXCEPTION = 'CALL_EXCEPTION'
Expand Down
1 change: 0 additions & 1 deletion test/solidity/cryptomath/CryptoMath.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const Constants = require('../../constants')
describe('@solidityevmequiv1 CryptoMath Test Suite', function () {
let cryptoMathContract, provider, signers;


before(async function () {
signers = await ethers.getSigners();
provider = ethers.getDefaultProvider();
Expand Down
126 changes: 126 additions & 0 deletions test/solidity/cryptomath/arithmetic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*-
*
* Hedera Smart Contracts
*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
const { expect } = require('chai')
const { ethers } = require('hardhat')
const Constants = require('../../constants')

describe('Arithmetic Test Suite', function () {
let contract;

before(async function () {
const factory = await ethers.getContractFactory(Constants.Contract.Arithmetic)
contract = await factory.deploy()
});

it('it should confirm solidity functionality: Arithmetic, checked overflow - confirm revert add', async function () {
let hasError = false;
try {
const res = await contract.add()
await res.wait()
} catch (error) {
hasError = true;
expect(error).to.exist
const name = await contract.checkName()
expect(name).to.equal('Arithmetic')
}
expect(hasError).to.be.true;
});

it('it should confirm solidity functionality: Arithmetic, checked overflow - confirm revert add2', async function () {
stefan-stefanooov marked this conversation as resolved.
Show resolved Hide resolved
let hasError = false;
try {
const res = await contract.add2()
await res.wait()
} catch (error) {
hasError = true;
expect(error).to.exist
const name = await contract.checkName()
expect(name).to.equal('Arithmetic')
}
expect(hasError).to.be.true;
});

it('it should confirm solidity functionality: Arithmetic, checked overflow - confirm revert mul', async function () {
let hasError = false;
try {
const res = await contract.mul()
await res.wait()
} catch (error) {
hasError = true;
expect(error).to.exist
const name = await contract.checkName()
expect(name).to.equal('Arithmetic')
}
expect(hasError).to.be.true;
});

it('it should confirm solidity functionality: Arithmetic, checked underflow - confirm revert sub', async function () {
let hasError = false;
try {
const res = await contract.sub()
await res.wait()
} catch (error) {
hasError = true;
expect(error).to.exist
const name = await contract.checkName()
expect(name).to.equal('Arithmetic')
}
expect(hasError).to.be.true;
});

it('it should confirm solidity functionality: Arithmetic, checked underflow - confirm revert dec', async function () {
let hasError = false;
try {
const res = await contract.dec()
await res.wait()
} catch (error) {
hasError = true;
expect(error).to.exist
const name = await contract.checkName()
expect(name).to.equal('Arithmetic')
}
expect(hasError).to.be.true;
});

it('it should confirm solidity functionality: Arithmetic, checked underflow - confirm revert negativeHasMoreValues', async function () {
let hasError = false;
try {
const res = await contract.negativeHasMoreValues()
await res.wait()
} catch (error) {
hasError = true;
expect(error).to.exist
const name = await contract.checkName()
expect(name).to.equal('Arithmetic')
}
expect(hasError).to.be.true;
});

it('it should confirm solidity functionality: Arithmetic, unchecked overflow - confirm wrap uncheckedAdd', async function () {
const res = await contract.uncheckedAdd()
expect(res).to.be.true
});

it('it should confirm solidity functionality: Arithmetic, unchecked underflow - confirm wrap uncheckedSub', async function () {
const res = await contract.uncheckedSub()
expect(res).to.be.true
});

})
6 changes: 3 additions & 3 deletions utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const pollForNewERC721Owner = async(erc721Contract, tokenId, ownerBefore) => {
return ownerAfter; // Ownership changed
}

delay();
await delay();
}

throw new Error(`Ownership did not change after ${process.env.MAX_RETRY} tries`);
Expand Down Expand Up @@ -132,7 +132,7 @@ const pollForNewERC721HollowWalletOwner = async(erc721Contract, nftTokenAddress,
return ownerAfter; // Ownership changed
}

delay();
await delay();
}

throw new Error(`Ownership did not change after ${process.env.MAX_RETRY} tries`);
Expand Down Expand Up @@ -162,7 +162,7 @@ const pollForNewHollowWalletBalance = async (provider, walletAddress, balanceBef
return balanceAfter; // Balance changed
}

delay();
await delay();
}

throw new Error(`Failed to get a different balance value after ${process.env.MAX_RETRY} tries`);
Expand Down