Packing.t.sol 1012 B

123456789101112131415161718192021222324252627
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.20;
  3. import {Test} from "forge-std/Test.sol";
  4. import {Packing} from "@openzeppelin/contracts/utils/Packing.sol";
  5. contract PackingTest is Test {
  6. using Packing for *;
  7. // Pack a pair of arbitrary uint128, and check that split recovers the correct values
  8. function testSymbolicUint128x2(uint128 first, uint128 second) external {
  9. Packing.Uint128x2 packed = Packing.pack(first, second);
  10. assertEq(packed.first(), first);
  11. assertEq(packed.second(), second);
  12. (uint128 recoveredFirst, uint128 recoveredSecond) = packed.split();
  13. assertEq(recoveredFirst, first);
  14. assertEq(recoveredSecond, second);
  15. }
  16. // split an arbitrary bytes32 into a pair of uint128, and check that repack matches the input
  17. function testSymbolicUint128x2(bytes32 input) external {
  18. (uint128 first, uint128 second) = input.asUint128x2().split();
  19. assertEq(Packing.pack(first, second).asBytes32(), input);
  20. }
  21. }