parent
c832dfbb7c
commit
0f6eb306ea
|
@ -1,17 +0,0 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "./Utils.sol";
|
||||
|
||||
// exposes public functions for testing
|
||||
contract TestUtils {
|
||||
|
||||
function resize(bytes32[] memory array,
|
||||
uint8 newSize)
|
||||
public
|
||||
pure
|
||||
returns (bytes32[] memory)
|
||||
{
|
||||
return Utils._resize(array, newSize);
|
||||
}
|
||||
}
|
|
@ -6,25 +6,6 @@ import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
|
|||
library Utils {
|
||||
using EnumerableSet for EnumerableSet.Bytes32Set;
|
||||
|
||||
function _resize(bytes32[] memory array, uint8 newSize)
|
||||
internal
|
||||
pure
|
||||
returns (bytes32[] memory)
|
||||
{
|
||||
require(newSize <= array.length, "size out of bounds");
|
||||
|
||||
if (newSize == 0) {
|
||||
bytes32[] memory empty;
|
||||
return empty;
|
||||
} else {
|
||||
bytes32[] memory sized = new bytes32[](newSize);
|
||||
for (uint8 i = 0; i < newSize; i++) {
|
||||
sized[i] = array[i];
|
||||
}
|
||||
return sized;
|
||||
}
|
||||
}
|
||||
|
||||
function filter(
|
||||
EnumerableSet.Bytes32Set storage set,
|
||||
function(bytes32) internal view returns (bool) include
|
||||
|
|
|
@ -1,45 +0,0 @@
|
|||
const { ethers } = require("hardhat")
|
||||
const { expect } = require("chai")
|
||||
const { hexlify, randomBytes } = ethers.utils
|
||||
const { exampleAddress } = require("./examples")
|
||||
const { hexZeroPad } = require("ethers/lib/utils")
|
||||
|
||||
describe("Utils", function () {
|
||||
let contract
|
||||
let value1
|
||||
let value2
|
||||
let value3
|
||||
let value4
|
||||
let value5
|
||||
let array
|
||||
|
||||
describe("resize", function () {
|
||||
beforeEach(async function () {
|
||||
let Utils = await ethers.getContractFactory("TestUtils")
|
||||
contract = await Utils.deploy()
|
||||
value1 = hexlify(randomBytes(32))
|
||||
value2 = hexlify(randomBytes(32))
|
||||
value3 = hexlify(randomBytes(32))
|
||||
value4 = hexZeroPad(0, 32)
|
||||
value5 = hexZeroPad(0, 32)
|
||||
array = [value1, value2, value3, value4, value5]
|
||||
})
|
||||
|
||||
it("resizes to zero length if new size is 0", async function () {
|
||||
await expect(await contract.resize(array, 0)).to.deep.equal([])
|
||||
})
|
||||
|
||||
it("resizes to specified length", async function () {
|
||||
await expect(await contract.resize(array, 3)).to.deep.equal([
|
||||
value1,
|
||||
value2,
|
||||
value3,
|
||||
])
|
||||
})
|
||||
|
||||
it("fails to resize to out of bounds length", async function () {
|
||||
await expect(contract.resize(array, 6))
|
||||
.to.be.revertedWith("size out of bounds")
|
||||
})
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue