Skip to content

Commit

Permalink
Adds tests for parameter restructuring and assignment (#498)
Browse files Browse the repository at this point in the history
* 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
konstantinabl authored Nov 6, 2023
1 parent 271fa2e commit 0481257
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
24 changes: 24 additions & 0 deletions contracts/solidity/assignments/AssignmentReferenceTypes.sol
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;
}
}
15 changes: 15 additions & 0 deletions contracts/solidity/assignments/DestructuringReturns.sol
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);
}
}
2 changes: 2 additions & 0 deletions test/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ const Contract = {
Inheritance: 'Inheritance',
ControlStructures: 'ControlStructures',
Defaults: 'Defaults',
AssignmentReferenceTypes: 'AssignmentReferenceTypes',
DestructuringReturns: 'DestructuringReturns',
}

const CALL_EXCEPTION = 'CALL_EXCEPTION'
Expand Down
49 changes: 49 additions & 0 deletions test/solidity/assignments/assignments.js
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)])
})
})

0 comments on commit 0481257

Please sign in to comment.