|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.4; |
| 3 | + |
| 4 | +import {MemoryUtils} from "../../../libs/utils/MemoryUtils.sol"; |
| 5 | + |
| 6 | +contract MemoryUtilsMock { |
| 7 | + using MemoryUtils for *; |
| 8 | + |
| 9 | + function testStringMemoryCopy(string memory data_) external view { |
| 10 | + string memory someString = new string(bytes(data_).length); |
| 11 | + |
| 12 | + require( |
| 13 | + keccak256(bytes(data_)) != keccak256(bytes(someString)), |
| 14 | + "MemoryUtilsMock: testStringMemoryCopy failed. Initial data and someString are equal" |
| 15 | + ); |
| 16 | + |
| 17 | + someString = data_.copy(); |
| 18 | + |
| 19 | + require( |
| 20 | + keccak256(bytes(data_)) == keccak256(bytes(someString)), |
| 21 | + "MemoryUtilsMock: testStringMemoryCopy failed. Initial data and someString are not equal" |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + function testBytesMemoryCopy(bytes memory data_) external view { |
| 26 | + bytes memory someBytes = new bytes(data_.length); |
| 27 | + |
| 28 | + require( |
| 29 | + keccak256(data_) != keccak256(someBytes), |
| 30 | + "MemoryUtilsMock: testBytesMemoryCopy failed. Initial data and someBytes are equal" |
| 31 | + ); |
| 32 | + |
| 33 | + someBytes = data_.copy(); |
| 34 | + |
| 35 | + require( |
| 36 | + keccak256(data_) == keccak256(someBytes), |
| 37 | + "MemoryUtilsMock: testBytesMemoryCopy failed. Initial data and someBytes are not equal" |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + function testUnsafeMemoryCopy(bytes memory data_) external view { |
| 42 | + bytes memory someBytes = new bytes(data_.length); |
| 43 | + |
| 44 | + require( |
| 45 | + keccak256(data_) != keccak256(someBytes), |
| 46 | + "MemoryUtilsMock: testBigMemory failed. Initial data and someBytes are equal" |
| 47 | + ); |
| 48 | + |
| 49 | + MemoryUtils.unsafeMemoryCopy( |
| 50 | + MemoryUtils.getPointer(someBytes), |
| 51 | + MemoryUtils.getPointer(data_), |
| 52 | + someBytes.length |
| 53 | + ); |
| 54 | + |
| 55 | + require( |
| 56 | + keccak256(data_) == keccak256(someBytes), |
| 57 | + "MemoryUtilsMock: testBigMemory failed. Initial data and someBytes are not equal" |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + function testPartialCopy(bytes memory data_) external view { |
| 62 | + bytes memory someBytes = new bytes(data_.length / 2); |
| 63 | + |
| 64 | + require( |
| 65 | + keccak256(data_) != keccak256(someBytes), |
| 66 | + "MemoryUtilsMock: testPartialCopy failed. Initial data and someBytes are equal" |
| 67 | + ); |
| 68 | + |
| 69 | + MemoryUtils.unsafeMemoryCopy( |
| 70 | + MemoryUtils.getPointer(someBytes), |
| 71 | + MemoryUtils.getPointer(data_), |
| 72 | + someBytes.length |
| 73 | + ); |
| 74 | + |
| 75 | + for (uint256 i = 0; i < someBytes.length; i++) { |
| 76 | + require( |
| 77 | + someBytes[i] == data_[i], |
| 78 | + "MemoryUtilsMock: testPartialCopy failed. Initial data and someBytes are not equal" |
| 79 | + ); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments