Skip to content

Commit

Permalink
Merge pull request #5 from rainlanguage/2024-11-01-signed-parse
Browse files Browse the repository at this point in the history
tests for signed int
  • Loading branch information
thedavidmeister authored Nov 1, 2024
2 parents 031cdfa + 14f2341 commit d0cd134
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions test/src/lib/LibParseDecimal.unsafeDecimalStringToSignedInt.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// SPDX-License-Identifier: LicenseRef-DCL-1.0
// SPDX-FileCopyrightText: Copyright (c) 2020 thedavidmeister
pragma solidity =0.8.25;

import {Test} from "forge-std/Test.sol";
import {Strings} from "openzeppelin-contracts/contracts/utils/Strings.sol";
import {LibBytes, Pointer} from "rain.solmem/lib/LibBytes.sol";
import {LibParseDecimal} from "src/lib/parse/LibParseDecimal.sol";

/// @title TestLibParseDecimalUnsafeDecimalStringToSignedInt
contract TestLibParseDecimalUnsafeDecimalStringToSignedInt is Test {
using Strings for uint256;
using LibBytes for bytes;

function testUnsafeStrToSignedIntRoundTrip(uint256 value, uint8 leadingZerosCount, bool isNeg) external pure {
value = bound(value, 0, uint256(type(int256).max) + (isNeg ? 1 : 0));
string memory str = value.toString();

string memory leadingZeros = new string(leadingZerosCount);
for (uint8 i = 0; i < leadingZerosCount; i++) {
bytes(leadingZeros)[i] = "0";
}

string memory input = string(abi.encodePacked((isNeg ? "-" : ""), leadingZeros, str));

(uint256 success, int256 result) = LibParseDecimal.unsafeDecimalStringToSignedInt(
Pointer.unwrap(bytes(input).dataPointer()), Pointer.unwrap(bytes(input).endDataPointer())
);
assertEq(success, 1);

if (isNeg) {
if (result == type(int256).min) {
assertEq(value, uint256(type(int256).max) + 1);
} else {
assertEq(result, -int256(value));
}
} else {
assertEq(result, int256(value));
}
}

/// Test positive overflow.
function testUnsafeStrToSignedIntOverflowPositive(uint256 value, uint8 leadingZerosCount) external pure {
value = bound(value, uint256(type(int256).max) + 1, type(uint256).max);
string memory str = value.toString();

string memory leadingZeros = new string(leadingZerosCount);
for (uint8 i = 0; i < leadingZerosCount; i++) {
bytes(leadingZeros)[i] = "0";
}

string memory input = string(abi.encodePacked(leadingZeros, str));

(uint256 success,) = LibParseDecimal.unsafeDecimalStringToSignedInt(
Pointer.unwrap(bytes(input).dataPointer()), Pointer.unwrap(bytes(input).endDataPointer())
);
assertEq(success, 0);
}

/// Test negative overflow.
function testUnsafeStrToSignedIntOverflowNegative(uint256 value, uint8 leadingZerosCount) external pure {
value = bound(value, uint256(type(int256).max) + 2, type(uint256).max);
string memory str = value.toString();

string memory leadingZeros = new string(leadingZerosCount);
for (uint8 i = 0; i < leadingZerosCount; i++) {
bytes(leadingZeros)[i] = "0";
}

string memory input = string(abi.encodePacked("-", leadingZeros, str));

(uint256 success,) = LibParseDecimal.unsafeDecimalStringToSignedInt(
Pointer.unwrap(bytes(input).dataPointer()), Pointer.unwrap(bytes(input).endDataPointer())
);
assertEq(success, 0);
}
}

0 comments on commit d0cd134

Please sign in to comment.