-
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.
Signed-off-by: Konstantina Blazhukova <[email protected]>
- Loading branch information
1 parent
b7f9d1e
commit 5ab0685
Showing
3 changed files
with
145 additions
and
0 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,87 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.20; | ||
|
||
contract TestContract { | ||
|
||
constructor() {} | ||
|
||
function myFunc(uint x) public pure returns (string memory) { | ||
require(x != 0, "require failed"); | ||
return "my func was called"; | ||
} | ||
} | ||
|
||
|
||
contract ControlStructures { | ||
TestContract public test; | ||
|
||
constructor() { | ||
test = new TestContract(); | ||
} | ||
|
||
function testIfElse(bool condition) external pure returns(bool) { | ||
if(condition) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
function testWhile(uint256 total) external pure returns(uint256) { | ||
uint256 it = 0; | ||
while(++it < total) {} | ||
|
||
return it; | ||
} | ||
|
||
function testDoWhile(uint256 total) external pure returns(uint256) { | ||
uint256 it = 0; | ||
do { | ||
it++; | ||
} while(it < total); | ||
|
||
return it; | ||
} | ||
|
||
function testBreak(uint256 total, uint256 interception) external pure returns(uint256) { | ||
uint256 it = 0; | ||
while(it++ < total) { | ||
if(it == interception) break; | ||
} | ||
|
||
return it; | ||
} | ||
|
||
function testContinue(uint256 total, uint256 interception) external pure returns(uint256) { | ||
uint256 iterableSteps = 0; | ||
|
||
for(uint i=0; i < total; i++) { | ||
if(interception < i) continue; | ||
iterableSteps++; | ||
} | ||
|
||
return iterableSteps; | ||
} | ||
|
||
function testFor(uint256 total) external pure returns(uint256) { | ||
uint256 it = 0; | ||
for(uint i=0; i < total; i++) { | ||
it = i; | ||
} | ||
|
||
return it; | ||
} | ||
|
||
function myFunc(uint x) internal pure returns (string memory) { | ||
require(x != 0, "require failed"); | ||
return "my func was called"; | ||
} | ||
|
||
function testTryCatch(uint256 condition) external view returns(bool) { | ||
try test.myFunc(condition) { | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
} |
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,57 @@ | ||
const { expect } = require('chai') | ||
const { ethers } = require('hardhat') | ||
const Constants = require('../../constants') | ||
|
||
describe('Control Structures', function () { | ||
let contract; | ||
|
||
before(async function () { | ||
const factory = await ethers.getContractFactory(Constants.Contract.ControlStructures) | ||
contract = await factory.deploy() | ||
}) | ||
|
||
it('should verify is is working correctly', async function () { | ||
const res = await contract.testIfElse(false) | ||
expect(res).to.equal(false) | ||
}) | ||
|
||
it('should verify else is working correctly', async function () { | ||
const res = await contract.testIfElse(true) | ||
expect(res).to.equal(true) | ||
}) | ||
|
||
it('should verify while is working correctly', async function () { | ||
const res = await contract.testWhile(5) | ||
expect(res).to.equal(5) | ||
}) | ||
|
||
it('should verify do is working correctly', async function () { | ||
const res = await contract.testDoWhile(5) | ||
expect(res).to.equal(5) | ||
}) | ||
|
||
it('should verify break is working correctly', async function () { | ||
const res = await contract.testBreak(5, 3) | ||
expect(res).to.equal(3) | ||
}) | ||
|
||
it('should verify continue is working correctly', async function () { | ||
const res = await contract.testContinue(5, 3) | ||
expect(res).to.equal(4) | ||
}) | ||
|
||
it('should verify for is working correctly', async function () { | ||
const res = await contract.testFor(5) | ||
expect(res).to.equal(4) | ||
}) | ||
|
||
it('should verify catch is working correctly', async function () { | ||
const res = await contract.testTryCatch(0) | ||
expect(res).to.equal(false) | ||
}) | ||
|
||
it('should verify try is working correctly', async function () { | ||
const res = await contract.testTryCatch(1) | ||
expect(res).to.equal(true) | ||
}) | ||
}) |