-
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.
Adds tests for parameter restructuring and assignment (#498)
* Adds tests for parameter restructuring and assignment Signed-off-by: Konstantina Blazhukova <[email protected]> * Fixes small comments Signed-off-by: Konstantina Blazhukova <[email protected]> * Fixes some comments Signed-off-by: Konstantina Blazhukova <[email protected]> * Removes unused vars Signed-off-by: Konstantina Blazhukova <[email protected]> * Adds license header to assignments.js Signed-off-by: Konstantina Blazhukova <[email protected]> --------- Signed-off-by: Konstantina Blazhukova <[email protected]>
- Loading branch information
1 parent
271fa2e
commit 0481257
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
contracts/solidity/assignments/AssignmentReferenceTypes.sol
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,24 @@ | ||
//SPDX-License-Identifier: Apache-2.0 | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
contract AssignmentReferenceTypes { | ||
uint[5] private someArray = [1, 2, 3, 4, 5]; | ||
|
||
function testAssignmentOfReferenceTypes() external { | ||
testChangeCopy(someArray); | ||
testChangeReference(someArray); | ||
} | ||
|
||
function testChangeCopy(uint[5] memory y) internal pure { | ||
y[2] = 8; | ||
} | ||
|
||
function testChangeReference(uint[5] storage y) internal { | ||
y[3] = 10; | ||
} | ||
|
||
function getSomeArray() external view returns(uint[5] memory) { | ||
return someArray; | ||
} | ||
} |
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,15 @@ | ||
//SPDX-License-Identifier: Apache-2.0 | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
contract DestructuringReturns { | ||
function f() public pure returns (uint, bool, uint) { | ||
return (7, true, 2); | ||
} | ||
|
||
function testDestructuredReturnParams() external pure returns(uint, bool, uint) { | ||
(uint x, bool y, uint z) = f(); | ||
|
||
return (x, y, z); | ||
} | ||
} |
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,49 @@ | ||
/*- | ||
* | ||
* 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('@solidityequiv1 Test assignments', function () { | ||
|
||
before(async function () { | ||
const factoryDestructuring = await ethers.getContractFactory(Constants.Contract.DestructuringReturns) | ||
contractDesctructuring = await factoryDestructuring.deploy() | ||
|
||
const factoryReferenceTypes = await ethers.getContractFactory(Constants.Contract.AssignmentReferenceTypes) | ||
contractReferenceTypes = await factoryReferenceTypes.deploy() | ||
}) | ||
|
||
it('should verify destructuring works', async function () { | ||
const result = await contractDesctructuring.testDestructuredReturnParams(); | ||
expect(result).to.deep.equal([ethers.BigNumber.from(7), true, ethers.BigNumber.from(2)]) | ||
}) | ||
|
||
it('should verify assignment of reference types', async function () { | ||
// here we are testing that if a parameter is assigned to memory a copy will be created | ||
// and the original object wont be changed | ||
// while if it is in storage and only referenced we expect it to change | ||
await contractReferenceTypes.testAssignmentOfReferenceTypes(); | ||
const result = await contractReferenceTypes.getSomeArray(); | ||
expect(result).to.deep.equal([ethers.BigNumber.from(1), ethers.BigNumber.from(2), ethers.BigNumber.from(3), | ||
ethers.BigNumber.from(10), ethers.BigNumber.from(5)]) | ||
}) | ||
}) |