@@ -4,7 +4,6 @@ pragma solidity ^0.8.20;
4
4
5
5
import {Test} from "forge-std/Test.sol " ;
6
6
import {Memory} from "@openzeppelin/contracts/utils/Memory.sol " ;
7
- import {Bytes} from "@openzeppelin/contracts/utils/Bytes.sol " ;
8
7
9
8
contract MemoryTest is Test {
10
9
using Memory for * ;
@@ -25,28 +24,44 @@ contract MemoryTest is Test {
25
24
assertEq (ptr.asBytes ().contentPointer ().asBytes32 (), ptr.addOffset (32 ).asBytes32 ());
26
25
}
27
26
28
- // function testCopy(bytes memory data, uint256 destSeed) public pure {
29
- // uint256 upperPtr = data.asPointer().asUint256() + data.length;
30
- // Memory.Pointer destPtr = bytes32(bound(destSeed, upperPtr, upperPtr + 100)).asPointer();
31
- // Memory.copy(data.asPointer(), destPtr, data.length + 32);
32
- // for (uint256 i = 0; i < data.length; i++) {
33
- // assertEq(data[i], destPtr.asBytes()[i]);
34
- // }
35
- // }
27
+ function testCopy (bytes memory data , uint256 destSeed ) public pure {
28
+ uint256 minDestPtr = Memory.getFreePointer ().asUint256 ();
29
+ Memory.Pointer destPtr = bytes32 (bound (destSeed, minDestPtr, minDestPtr + END_PTR)).asPointer ();
30
+ destPtr.addOffset (data.length + 32 ).setFreePointer ();
31
+ destPtr.copy (data.asPointer (), data.length + 32 );
32
+ bytes memory copiedData = destPtr.asBytes ();
33
+ assertEq (data.length , copiedData.length );
34
+ for (uint256 i = 0 ; i < data.length ; i++ ) {
35
+ assertEq (data[i], copiedData[i]);
36
+ }
37
+ }
36
38
37
- function testExtractByte (uint256 seed , uint256 index ) public pure {
39
+ function testExtractByte (uint256 seed , uint256 index , bytes32 value ) public pure {
38
40
index = bound (index, 0 , 31 );
39
41
Memory.Pointer ptr = bytes32 (bound (seed, START_PTR, END_PTR)).asPointer ();
40
- assertEq (ptr.extractByte (index), bytes1 (ptr.asBytes32 () >> (256 - index * 8 )));
42
+
43
+ assembly ("memory-safe" ) {
44
+ mstore (ptr, value)
45
+ }
46
+
47
+ bytes1 expected;
48
+ assembly ("memory-safe" ) {
49
+ expected := byte (index, value)
50
+ }
51
+ assertEq (ptr.extractByte (index), expected);
41
52
}
42
53
43
- // function testExtractWord(uint256 seed) public pure {
44
- // Memory.Pointer ptr = bytes32(bound(seed, START_PTR, END_PTR)).asPointer();
45
- // assertEq(ptr.extractWord(), ptr.asBytes32());
46
- // }
54
+ function testExtractWord (uint256 seed , bytes32 value ) public pure {
55
+ Memory.Pointer ptr = bytes32 (bound (seed, START_PTR, END_PTR)).asPointer ();
56
+ assembly ("memory-safe" ) {
57
+ mstore (ptr, value)
58
+ }
59
+ assertEq (ptr.extractWord (), value);
60
+ }
47
61
48
- // function testAddOffset(uint256 seed, uint256 offset) public pure {
49
- // Memory.Pointer ptr = bytes32(bound(seed, START_PTR, END_PTR)).asPointer();
50
- // assertEq(ptr.addOffset(offset).asUint256(), ptr.asUint256() + offset);
51
- // }
62
+ function testSymbolicAddOffset (uint256 seed , uint256 offset ) public pure {
63
+ offset = bound (offset, 0 , type (uint256 ).max - END_PTR);
64
+ Memory.Pointer ptr = bytes32 (bound (seed, START_PTR, END_PTR)).asPointer ();
65
+ assertEq (ptr.addOffset (offset).asUint256 (), ptr.asUint256 () + offset);
66
+ }
52
67
}
0 commit comments