Skip to content

Commit a1e0f5c

Browse files
author
Ryan
committed
Added unit test for custom type Stream
1 parent 16079be commit a1e0f5c

3 files changed

Lines changed: 86 additions & 11 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,4 @@ $ forge --help
6464
$ anvil --help
6565
$ cast --help
6666
```
67+
# route-processor

src/types/Stream.sol

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,26 @@ library StreamLibrary {
3131
}
3232
}
3333

34-
/// @dev Reads an address (20 bytes) and advances the cursor.
35-
function parseAddress(Stream stream) internal pure returns (address result) {
34+
/// @dev Reads and casts one byte as a {Protocol} enum.
35+
function parseProtocol(Stream stream) internal pure returns (Protocol result) {
3636
assembly ("memory-safe") {
37-
let pos := add(mload(stream), 0x14)
38-
result := and(mload(pos), 0xffffffffffffffffffffffffffffffffffffffff)
37+
let pos := add(mload(stream), 0x01)
38+
result := and(mload(pos), 0xff)
3939
mstore(stream, pos)
4040
}
4141
}
4242

43-
/// @dev Reads one byte and interprets it as a boolean.
44-
function parseBool(Stream stream) internal pure returns (bool result) {
43+
/// @dev Reads 20 bytes and returns an address.
44+
function parseAddress(Stream stream) internal pure returns (address result) {
4545
assembly ("memory-safe") {
46-
let pos := add(mload(stream), 0x01)
47-
result := and(mload(pos), 0xff)
46+
let pos := add(mload(stream), 0x14)
47+
result := and(mload(pos), 0xffffffffffffffffffffffffffffffffffffffff)
4848
mstore(stream, pos)
4949
}
5050
}
5151

52-
/// @dev Reads and casts one byte as a {Protocol} enum.
53-
function parseProtocol(Stream stream) internal pure returns (Protocol result) {
52+
/// @dev Reads one byte and interprets it as a boolean.
53+
function parseBool(Stream stream) internal pure returns (bool result) {
5454
assembly ("memory-safe") {
5555
let pos := add(mload(stream), 0x01)
5656
result := and(mload(pos), 0xff)
@@ -98,7 +98,7 @@ library StreamLibrary {
9898
function parseUint128(Stream stream) internal pure returns (uint128 result) {
9999
assembly ("memory-safe") {
100100
let pos := add(mload(stream), 0x10)
101-
result := and(mload(pos), 0xffffffffffffffffffffffffffff)
101+
result := and(mload(pos), 0xffffffffffffffffffffffffffffffff)
102102
mstore(stream, pos)
103103
}
104104
}

test/types/Stream.t.sol

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.30;
3+
4+
import {Test} from "forge-std/Test.sol";
5+
import {Protocol} from "src/types/Enums.sol";
6+
import {Stream, createStream} from "src/types/Stream.sol";
7+
8+
contract StreamTest is Test {
9+
function test_fuzz_parseProtocol(uint8 x) public pure {
10+
vm.assume(x <= uint8(type(Protocol).max));
11+
Stream s = createStream(abi.encodePacked(Protocol(x)));
12+
assertEq(uint8(s.parseProtocol()), x);
13+
}
14+
15+
function test_fuzz_parseAddress(address x) public pure {
16+
Stream s = createStream(abi.encodePacked(x));
17+
assertEq(s.parseAddress(), x);
18+
}
19+
20+
function test_fuzz_parseUint8(uint8 x) public pure {
21+
Stream s = createStream(abi.encodePacked(x));
22+
assertEq(s.parseUint8(), x);
23+
}
24+
25+
function test_fuzz_parseUint16(uint16 x) public pure {
26+
Stream s = createStream(abi.encodePacked(x));
27+
assertEq(s.parseUint16(), x);
28+
}
29+
30+
function test_fuzz_parseUint24(uint24 x) public pure {
31+
Stream s = createStream(abi.encodePacked(x));
32+
assertEq(s.parseUint24(), x);
33+
}
34+
35+
function test_fuzz_parseUint48(uint48 x) public pure {
36+
Stream s = createStream(abi.encodePacked(x));
37+
assertEq(s.parseUint48(), x);
38+
}
39+
40+
function test_fuzz_parseUint128(uint128 x) public pure {
41+
Stream s = createStream(abi.encodePacked(x));
42+
assertEq(s.parseUint128(), x);
43+
}
44+
45+
function test_fuzz_parseUint160(uint160 x) public pure {
46+
Stream s = createStream(abi.encodePacked(x));
47+
assertEq(s.parseUint160(), x);
48+
}
49+
50+
function test_fuzz_parseUint256(uint256 x) public pure {
51+
Stream s = createStream(abi.encodePacked(x));
52+
assertEq(s.parseUint256(), x);
53+
}
54+
55+
function test_fuzz_parseInt256(int256 x) public pure {
56+
Stream s = createStream(abi.encodePacked(x));
57+
assertEq(s.parseInt256(), x);
58+
}
59+
60+
function test_fuzz_parseBytes4(bytes4 x) public pure {
61+
Stream s = createStream(abi.encodePacked(x));
62+
assertEq(s.parseBytes4(), x);
63+
}
64+
65+
function test_fuzz_parseBytes32(bytes32 x) public pure {
66+
Stream s = createStream(abi.encodePacked(x));
67+
assertEq(s.parseBytes32(), x);
68+
}
69+
70+
function test_fuzz_parseBytes(bytes memory x) public pure {
71+
Stream s = createStream(abi.encodePacked(x.length, x));
72+
assertEq(s.parseBytes(), x);
73+
}
74+
}

0 commit comments

Comments
 (0)