|
| 1 | +pragma solidity 0.4.24; |
| 2 | + |
| 3 | +import "../helpers/Assert.sol"; |
| 4 | +import "../../common/MemoryHelpers.sol"; |
| 5 | + |
| 6 | + |
| 7 | +contract TestMemoryHelpers { |
| 8 | + using MemoryHelpers for bytes; |
| 9 | + |
| 10 | + uint256 constant internal FIRST = uint256(10); |
| 11 | + uint256 constant internal SECOND = uint256(1); |
| 12 | + uint256 constant internal THIRD = uint256(15); |
| 13 | + |
| 14 | + function testBytesArrayCopy() public { |
| 15 | + bytes memory blob = _initializeArbitraryBytesArray(); |
| 16 | + uint256 blobSize = blob.length; |
| 17 | + bytes memory copy = new bytes(blobSize); |
| 18 | + uint256 input; |
| 19 | + uint256 output; |
| 20 | + assembly { |
| 21 | + input := add(blob, 0x20) |
| 22 | + output := add(copy, 0x20) |
| 23 | + } |
| 24 | + MemoryHelpers.memcpy(output, input, blobSize); |
| 25 | + |
| 26 | + Assert.equal(blob.length, copy.length, "should have correct length"); |
| 27 | + |
| 28 | + uint256 firstWord = _assertEqualMemoryWord(blob, copy, 0); |
| 29 | + Assert.equal(firstWord, FIRST, "first value should match"); |
| 30 | + |
| 31 | + uint256 secondWord = _assertEqualMemoryWord(blob, copy, 1); |
| 32 | + Assert.equal(secondWord, SECOND, "second value should match"); |
| 33 | + |
| 34 | + uint256 thirdWord = _assertEqualMemoryWord(blob, copy, 2); |
| 35 | + Assert.equal(thirdWord, THIRD, "third value should match"); |
| 36 | + } |
| 37 | + |
| 38 | + function testAppendAddressToBytesArray() public { |
| 39 | + bytes memory blob = _initializeArbitraryBytesArray(); |
| 40 | + address addr = address(0x000000000000000000000000000000000000dEaD); |
| 41 | + bytes memory result = blob.append(addr); |
| 42 | + |
| 43 | + Assert.equal(blob.length + 32, result.length, "should have correct length"); |
| 44 | + |
| 45 | + uint256 firstWord = _assertEqualMemoryWord(blob, result, 0); |
| 46 | + Assert.equal(firstWord, FIRST, "first value should match"); |
| 47 | + |
| 48 | + uint256 secondWord = _assertEqualMemoryWord(blob, result, 1); |
| 49 | + Assert.equal(secondWord, SECOND, "second value should match"); |
| 50 | + |
| 51 | + uint256 thirdWord = _assertEqualMemoryWord(blob, result, 2); |
| 52 | + Assert.equal(thirdWord, THIRD, "third value should match"); |
| 53 | + |
| 54 | + bytes32 storedAddress; |
| 55 | + assembly { storedAddress := mload(add(result, 0x80))} |
| 56 | + Assert.equal(storedAddress, bytes32(0x000000000000000000000000000000000000000000000000000000000000dEaD), "appended address should match"); |
| 57 | + } |
| 58 | + |
| 59 | + function _assertEqualMemoryWord(bytes _actual, bytes _expected, uint256 _index) private returns (uint256) { |
| 60 | + uint256 actualValue; |
| 61 | + uint256 expectedValue; |
| 62 | + uint256 pos = _index * 32; |
| 63 | + assembly { |
| 64 | + actualValue := mload(add(add(_actual, 0x20), pos)) |
| 65 | + expectedValue := mload(add(add(_expected, 0x20), pos)) |
| 66 | + } |
| 67 | + Assert.equal(actualValue, expectedValue, "memory values should match"); |
| 68 | + return expectedValue; |
| 69 | + } |
| 70 | + |
| 71 | + function _initializeArbitraryBytesArray() private pure returns (bytes memory) { |
| 72 | + bytes memory blob = new bytes(96); |
| 73 | + |
| 74 | + uint256 first = FIRST; |
| 75 | + uint256 second = SECOND; |
| 76 | + uint256 third = THIRD; |
| 77 | + assembly { |
| 78 | + mstore(add(blob, 0x20), first) |
| 79 | + mstore(add(blob, 0x40), second) |
| 80 | + mstore(add(blob, 0x60), third) |
| 81 | + } |
| 82 | + |
| 83 | + return blob; |
| 84 | + } |
| 85 | +} |
0 commit comments