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 Concatenation. #454

Merged
merged 1 commit into from
Oct 12, 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ cache
# ignore non repo contract related aritifacts
artifacts/@openzeppelin
artifacts/build-info
artifacts/contracts/**/**.dbg.json
artifacts/contracts

.openzeppelin/unknown-298.json
.env
test-results.*
Expand Down
22 changes: 22 additions & 0 deletions contracts/solidity/concatenation/Concatenation.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;

contract Concatenation {
function byteConcatenation(bytes calldata first, bytes calldata second, bytes calldata third) public pure returns (uint256) {
bytes memory concatenated = bytes.concat(first, second, third);
return concatenated.length;
}

function stringConcatenation(string memory first, string memory second, string memory third) public pure returns (string memory) {
return string.concat(first, second, third);
}

function byteConcatenationEmpty() public pure returns (bytes memory) {
return bytes.concat();
}

function stringConcatenationEmpty() public pure returns (string memory) {
return string.concat();
}

}
1 change: 1 addition & 0 deletions test/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const Contract = {
HRCContract: 'HRCContract',
ExchangeRateMock: 'ExchangeRateMock',
PrngSystemContract: 'PrngSystemContract',
Concatenation: 'Concatenation',
}

const CALL_EXCEPTION = 'CALL_EXCEPTION'
Expand Down
47 changes: 47 additions & 0 deletions test/solidity/concatenation/concatenation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const { expect } = require('chai')
const { ethers } = require('hardhat')
const Constants = require('../../constants')

describe('Concatenation', function () {
let signers;
let contract;
const first = 'first';
const second = 'second';
const third = 'third';

before(async function () {
signers = await ethers.getSigners()

const factory = await ethers.getContractFactory(Constants.Contract.Concatenation)
contract = await factory.deploy()
})

it('byte concatenation', async function () {
let utf8Encode = new TextEncoder();
const bytesFirst = utf8Encode.encode(first);
const bytesSecond = utf8Encode.encode(second);
const bytesThird = utf8Encode.encode(third);
const res = await contract.byteConcatenation(bytesFirst, bytesSecond, bytesThird)

expect((bytesFirst.byteLength + bytesSecond.byteLength + bytesThird.byteLength)).to.equal(res);
})

it('string concatenation', async function () {
const res = await contract.stringConcatenation(first, second, third);

expect((first.length + second.length + third.length)).to.equal(res.length);
expect(first.concat(second, third)).to.equal(res);
})

it('string concatenation Empty', async function () {
const res = await contract.stringConcatenationEmpty()

expect(res.length).to.equal(0)
})

it('string concatenation Empty', async function () {
const res = await contract.stringConcatenationEmpty()

expect(res.length).to.equal(0)
})
})
Loading