forked from foundry-rs/foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapping.t.sol
More file actions
68 lines (57 loc) · 2.87 KB
/
Mapping.t.sol
File metadata and controls
68 lines (57 loc) · 2.87 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
60
61
62
63
64
65
66
67
68
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.8.0;
import "ds-test/test.sol";
import "cheats/Vm.sol";
contract RecordMapping {
int256 length;
mapping(address => int256) data;
mapping(int256 => mapping(int256 => int256)) nestedData;
function setData(address addr, int256 value) public {
data[addr] = value;
}
function setNestedData(int256 i, int256 j) public {
nestedData[i][j] = i * j;
}
}
contract RecordMappingTest is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);
function testRecordMapping() public {
RecordMapping target = new RecordMapping();
// Start recording
vm.startMappingRecording();
// Verify Records
target.setData(address(this), 100);
target.setNestedData(99, 10);
target.setNestedData(98, 10);
bool found;
bytes32 key;
bytes32 parent;
bytes32 dataSlot = bytes32(uint256(1));
bytes32 nestDataSlot = bytes32(uint256(2));
assertEq(uint256(vm.getMappingLength(address(target), dataSlot)), 1, "number of data is incorrect");
assertEq(uint256(vm.getMappingLength(address(this), dataSlot)), 0, "number of data is incorrect");
assertEq(uint256(vm.getMappingLength(address(target), nestDataSlot)), 2, "number of nestedData is incorrect");
bytes32 dataValueSlot = vm.getMappingSlotAt(address(target), dataSlot, 0);
(found, key, parent) = vm.getMappingKeyAndParentOf(address(target), dataValueSlot);
assert(found);
assertEq(address(uint160(uint256(key))), address(this), "key of data[i] is incorrect");
assertEq(parent, dataSlot, "parent of data[i] is incorrect");
assertGt(uint256(dataValueSlot), 0);
assertEq(uint256(vm.load(address(target), dataValueSlot)), 100);
for (uint256 k; k < vm.getMappingLength(address(target), nestDataSlot); k++) {
bytes32 subSlot = vm.getMappingSlotAt(address(target), nestDataSlot, k);
(found, key, parent) = vm.getMappingKeyAndParentOf(address(target), subSlot);
uint256 i = uint256(key);
assertEq(parent, nestDataSlot, "parent of nestedData[i][j] is incorrect");
assertEq(uint256(vm.getMappingLength(address(target), subSlot)), 1, "number of nestedData[i] is incorrect");
bytes32 leafSlot = vm.getMappingSlotAt(address(target), subSlot, 0);
(found, key, parent) = vm.getMappingKeyAndParentOf(address(target), leafSlot);
uint256 j = uint256(key);
assertEq(parent, subSlot, "parent of nestedData[i][j] is incorrect");
assertEq(j, 10);
assertEq(uint256(vm.load(address(target), leafSlot)), i * j, "value of nestedData[i][j] is incorrect");
}
vm.stopMappingRecording();
assertEq(uint256(vm.getMappingLength(address(target), dataSlot)), 0, "number of data is incorrect");
}
}