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); return addressArray.append(addressToAppend);
} }
/// @dev Creates an in-memory copy of `addressArray`, /// @dev Moves the free memory pointer by `freeMemOffset` bytes,
/// moves the free memory pointer by `freeMemOffset` bytes,
/// then performs the append. /// then performs the append.
/// This tests the behavior of the address array being reallocated if /// This tests the behavior of the address array being reallocated if
/// the memory immediately after the old array is claimed. /// the memory immediately after the old array is claimed.
@ -64,22 +63,15 @@ contract TestLibAddressArray {
uint256 newArrayMemStart uint256 newArrayMemStart
) )
{ {
// Create a copy of the array.
result = new address[](addressArray.length);
assembly { assembly {
oldArrayMemStart := result // Remember the original memory address of the array.
let length := mload(addressArray) oldArrayMemStart := 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))))
}
// Move the free memory pointer. // Move the free memory pointer.
mstore(0x40, add(mload(0x40), freeMemOffset)) mstore(0x40, add(mload(0x40), freeMemOffset))
} }
// Call append. // Call append.
result = result.append(addressToAppend); result = addressArray.append(addressToAppend);
// Get the new array memory address. // Get the new array memory address.
assembly { 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 () => { 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 arr = _.times(3, () => addressUtils.generatePseudoRandomAddress());
const addr = addressUtils.generatePseudoRandomAddress(); const addr = addressUtils.generatePseudoRandomAddress();
const freeMemOffset = new BigNumber(-1);
return expectContractCallFailedAsync( return expectContractCallFailedAsync(
lib.testAppendRealloc.callAsync(arr, new BigNumber(-1), addr), lib.testAppendRealloc.callAsync(arr, freeMemOffset, addr),
RevertReason.InvalidFreeMemoryPtr, RevertReason.InvalidFreeMemoryPtr,
); );
}); });
@ -70,10 +71,11 @@ describe('LibAddressArray', () => {
it('should keep the same memory address if free memory pointer does not move', async () => { it('should keep the same memory address if free memory pointer does not move', async () => {
const arr = _.times(3, () => addressUtils.generatePseudoRandomAddress()); const arr = _.times(3, () => addressUtils.generatePseudoRandomAddress());
const addr = addressUtils.generatePseudoRandomAddress(); const addr = addressUtils.generatePseudoRandomAddress();
const freeMemOffset = new BigNumber(0);
const expected = [...arr, addr]; const expected = [...arr, addr];
const [result, oldArrayMemStart, newArrayMemStart] = await lib.testAppendRealloc.callAsync( const [result, oldArrayMemStart, newArrayMemStart] = await lib.testAppendRealloc.callAsync(
arr, arr,
new BigNumber(0), freeMemOffset,
addr, addr,
); );
expect(result).to.deep.equal(expected); expect(result).to.deep.equal(expected);
@ -83,14 +85,19 @@ describe('LibAddressArray', () => {
it('should change memory address if free memory pointer advances', async () => { it('should change memory address if free memory pointer advances', async () => {
const arr = _.times(3, () => addressUtils.generatePseudoRandomAddress()); const arr = _.times(3, () => addressUtils.generatePseudoRandomAddress());
const addr = 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( const [result, oldArrayMemStart, newArrayMemStart] = await lib.testAppendRealloc.callAsync(
arr, arr,
new BigNumber(1), freeMemOffset,
addr, addr,
); );
expect(result).to.deep.equal(expected); // The new location should be the end of the old array + freeMemOffset.
expect(newArrayMemStart).bignumber.to.not.be.equal(oldArrayMemStart); const expectedNewArrayMemStart = oldArrayMemStart
.plus((arr.length + 1) * 32)
.plus(freeMemOffset);
expect(result).to.deep.equal(expectedArray);
expect(newArrayMemStart).bignumber.to.be.equal(expectedNewArrayMemStart);
}); });
}); });