-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLibBytecode.sourcePointer.t.sol
More file actions
59 lines (53 loc) · 2.62 KB
/
LibBytecode.sourcePointer.t.sol
File metadata and controls
59 lines (53 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// SPDX-License-Identifier: LicenseRef-DCL-1.0
// SPDX-FileCopyrightText: Copyright (c) 2020 Rain Open Source Software Ltd
pragma solidity =0.8.25;
import {BytecodeTest} from "test/abstract/BytecodeTest.sol";
import {LibBytecode, SourceIndexOutOfBounds} from "src/lib/bytecode/LibBytecode.sol";
import {LibBytecodeSlow} from "test/src/lib/bytecode/LibBytecodeSlow.sol";
import {Pointer} from "rain.solmem/lib/LibPointer.sol";
contract LibBytecodeSourcePointerTest is BytecodeTest {
function sourcePointerExternal(bytes memory bytecode, uint256 sourceIndex) external pure returns (Pointer pointer) {
return LibBytecode.sourcePointer(bytecode, sourceIndex);
}
/// Getting the source pointer for an empty bytecode should fail. Tests empty
/// bytes.
function testSourcePointerEmpty0(uint256 sourceIndex) external {
bytes memory bytecode = hex"";
vm.expectRevert(abi.encodeWithSelector(SourceIndexOutOfBounds.selector, sourceIndex, bytecode));
this.sourcePointerExternal(bytecode, sourceIndex);
}
/// Getting the source pointer for an empty bytecode should fail. Tests
/// non-empty bytes but with no source.
function testSourcePointerEmpty1(uint256 sourceIndex) external {
bytes memory bytecode = hex"00";
vm.expectRevert(abi.encodeWithSelector(SourceIndexOutOfBounds.selector, sourceIndex, bytecode));
this.sourcePointerExternal(bytecode, sourceIndex);
}
/// Getting a source pointer for an index beyond the sources should fail.
function testSourcePointerIndexOutOfBounds(
bytes memory bytecode,
uint256 sourceCount,
uint256 sourceIndex,
bytes32 seed
) external {
conformBytecode(bytecode, sourceCount, seed);
sourceCount = LibBytecode.sourceCount(bytecode);
sourceIndex = bound(sourceIndex, sourceCount, type(uint256).max);
vm.expectRevert(abi.encodeWithSelector(SourceIndexOutOfBounds.selector, sourceIndex, bytecode));
this.sourcePointerExternal(bytecode, sourceIndex);
}
/// Test against a reference implementation.
function testSourcePointerAgainstSlow(bytes memory bytecode, uint256 sourceCount, uint256 sourceIndex, bytes32 seed)
external
pure
{
conformBytecode(bytecode, sourceCount, seed);
sourceCount = LibBytecode.sourceCount(bytecode);
vm.assume(sourceCount > 0);
sourceIndex = bound(sourceIndex, 0, sourceCount - 1);
assertEq(
Pointer.unwrap(LibBytecode.sourcePointer(bytecode, sourceIndex)),
Pointer.unwrap(LibBytecodeSlow.sourcePointerSlow(bytecode, sourceIndex))
);
}
}