-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding tests for Arithmetic --------- Signed-off-by: Stefan Stefanov <[email protected]>
- Loading branch information
1 parent
dd2e502
commit 85a0600
Showing
5 changed files
with
197 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () { | ||
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 | ||
}); | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters