Remove unnecessary copy operation in TestLibAddressArray.testAppendRealloc().

Test explicitly for newly allocated address in LibAddressArray "append" tests.
This commit is contained in:
Lawrence 2019-03-20 10:19:15 -04:00
parent 78b9a45158
commit 6ed1412bdd
2 changed files with 17 additions and 18 deletions

View File

@ -40,8 +40,7 @@ contract TestLibAddressArray {
return addressArray.append(addressToAppend);
}
/// @dev Creates an in-memory copy of `addressArray`,
/// moves the free memory pointer by `freeMemOffset` bytes,
/// @dev Moves the free memory pointer by `freeMemOffset` bytes,
/// then performs the append.
/// This tests the behavior of the address array being reallocated if
/// the memory immediately after the old array is claimed.
@ -64,22 +63,15 @@ contract TestLibAddressArray {
uint256 newArrayMemStart
)
{
// Create a copy of the array.
result = new address[](addressArray.length);
assembly {
oldArrayMemStart := result
let length := mload(addressArray)
for { let i := 0 } lt(i, length) { i := add(i, 1) } {
mstore(add(result, mul(add(i, 1), 32)),
mload(add(addressArray, mul(add(i, 1), 32))))
}
// Remember the original memory address of the array.
oldArrayMemStart := addressArray
// Move the free memory pointer.
mstore(0x40, add(mload(0x40), freeMemOffset))
}
// Call append.
result = result.append(addressToAppend);
result = addressArray.append(addressToAppend);
// Get the new array memory address.
assembly {

View File

@ -61,8 +61,9 @@ describe('LibAddressArray', () => {
it('should revert if the free memory pointer was moved to before the end of the array', async () => {
const arr = _.times(3, () => addressUtils.generatePseudoRandomAddress());
const addr = addressUtils.generatePseudoRandomAddress();
const freeMemOffset = new BigNumber(-1);
return expectContractCallFailedAsync(
lib.testAppendRealloc.callAsync(arr, new BigNumber(-1), addr),
lib.testAppendRealloc.callAsync(arr, freeMemOffset, addr),
RevertReason.InvalidFreeMemoryPtr,
);
});
@ -70,10 +71,11 @@ describe('LibAddressArray', () => {
it('should keep the same memory address if free memory pointer does not move', async () => {
const arr = _.times(3, () => addressUtils.generatePseudoRandomAddress());
const addr = addressUtils.generatePseudoRandomAddress();
const freeMemOffset = new BigNumber(0);
const expected = [...arr, addr];
const [result, oldArrayMemStart, newArrayMemStart] = await lib.testAppendRealloc.callAsync(
arr,
new BigNumber(0),
freeMemOffset,
addr,
);
expect(result).to.deep.equal(expected);
@ -83,14 +85,19 @@ describe('LibAddressArray', () => {
it('should change memory address if free memory pointer advances', async () => {
const arr = _.times(3, () => addressUtils.generatePseudoRandomAddress());
const addr = addressUtils.generatePseudoRandomAddress();
const expected = [...arr, addr];
const freeMemOffset = new BigNumber(1);
const expectedArray = [...arr, addr];
const [result, oldArrayMemStart, newArrayMemStart] = await lib.testAppendRealloc.callAsync(
arr,
new BigNumber(1),
freeMemOffset,
addr,
);
expect(result).to.deep.equal(expected);
expect(newArrayMemStart).bignumber.to.not.be.equal(oldArrayMemStart);
// The new location should be the end of the old array + freeMemOffset.
const expectedNewArrayMemStart = oldArrayMemStart
.plus((arr.length + 1) * 32)
.plus(freeMemOffset);
expect(result).to.deep.equal(expectedArray);
expect(newArrayMemStart).bignumber.to.be.equal(expectedNewArrayMemStart);
});
});