Skip to content

Commit c3237df

Browse files
committed
Finish fuzz tests and FV
1 parent 124ccee commit c3237df

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

test/utils/Memory.t.sol

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ pragma solidity ^0.8.20;
44

55
import {Test} from "forge-std/Test.sol";
66
import {Memory} from "@openzeppelin/contracts/utils/Memory.sol";
7-
import {Bytes} from "@openzeppelin/contracts/utils/Bytes.sol";
87

98
contract MemoryTest is Test {
109
using Memory for *;
@@ -25,28 +24,44 @@ contract MemoryTest is Test {
2524
assertEq(ptr.asBytes().contentPointer().asBytes32(), ptr.addOffset(32).asBytes32());
2625
}
2726

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+
}
3638

37-
function testExtractByte(uint256 seed, uint256 index) public pure {
39+
function testExtractByte(uint256 seed, uint256 index, bytes32 value) public pure {
3840
index = bound(index, 0, 31);
3941
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);
4152
}
4253

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+
}
4761

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+
}
5267
}

0 commit comments

Comments
 (0)