github.com/ethereum-optimism/optimism@v1.7.2/packages/contracts-bedrock/test/libraries/Storage.t.sol (about) 1 // SPDX-License-Identifier: MIT 2 pragma solidity 0.8.15; 3 4 // Target contract 5 import { Storage } from "src/libraries/Storage.sol"; 6 import { StorageSetter } from "src/universal/StorageSetter.sol"; 7 import { Test } from "forge-std/Test.sol"; 8 9 /// @title Storage_Roundtrip_Test 10 /// @notice Tests the storage setting and getting through the StorageSetter contract. 11 /// This contract simply wraps the Storage library, this is required as to 12 /// not poison the storage of the `Test` contract. 13 contract Storage_Roundtrip_Test is Test { 14 StorageSetter setter; 15 16 /// @notice A set of storage slots to pass to `setBytes32`. 17 StorageSetter.Slot[] slots; 18 /// @notice Used to deduplicate slots passed to `setBytes32`. 19 mapping(bytes32 => bool) keys; 20 21 function setUp() external { 22 setter = new StorageSetter(); 23 } 24 25 function test_setGetUint_succeeds(bytes32 slot, uint256 num) external { 26 setter.setUint(slot, num); 27 assertEq(setter.getUint(slot), num); 28 assertEq(num, uint256(vm.load(address(setter), slot))); 29 } 30 31 function test_setGetAddress_succeeds(bytes32 slot, address addr) external { 32 setter.setAddress(slot, addr); 33 assertEq(setter.getAddress(slot), addr); 34 assertEq(addr, address(uint160(uint256(vm.load(address(setter), slot))))); 35 } 36 37 function test_setGetBytes32_succeeds(bytes32 slot, bytes32 hash) external { 38 setter.setBytes32(slot, hash); 39 assertEq(setter.getBytes32(slot), hash); 40 assertEq(hash, vm.load(address(setter), slot)); 41 } 42 43 function test_setGetBool_succeeds(bytes32 slot, bool value) external { 44 setter.setBool(slot, value); 45 assertEq(setter.getBool(slot), value); 46 assertEq(value, vm.load(address(setter), slot) == bytes32(uint256(1))); 47 } 48 49 /// @dev All keys must be unique in the input so deduplication is required. 50 function testFuzz_setGetBytes32Multi_succeeds(StorageSetter.Slot[] calldata _slots) external { 51 for (uint256 i; i < _slots.length; i++) { 52 if (keys[_slots[i].key]) { 53 continue; 54 } 55 slots.push(_slots[i]); 56 keys[_slots[i].key] = true; 57 } 58 59 setter.setBytes32(slots); 60 for (uint256 i; i < slots.length; i++) { 61 assertEq(setter.getBytes32(slots[i].key), slots[i].value); 62 assertEq(slots[i].value, vm.load(address(setter), slots[i].key)); 63 } 64 } 65 }