Skip to content

Commit ee6af05

Browse files
committed
Fix failing test (testCompare)
Fixed division by zero error when taking the sign of `0`.
1 parent 719f0aa commit ee6af05

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

src/strings.t.sol

+3-9
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,11 @@ contract StringsTest is DSTest {
1515
}
1616

1717
function sign(int x) private pure returns (int) {
18-
return x/abs(x);
18+
return x == 0 ? int(0) : (x < 0 ? -1 : int(1));
1919
}
2020

2121
function assertEq0(string a, string b) internal {
22-
if (a.len() != b.len()) {
23-
emit log_bytes32("strings differ");
24-
emit log_named_string("Expected", a);
25-
emit log_named_string("Actual", b);
26-
}
22+
assertEq0(bytes(a), bytes(b));
2723
}
2824

2925
function assertEq0(strings.slice memory a, strings.slice memory b) internal {
@@ -47,6 +43,7 @@ contract StringsTest is DSTest {
4743
}
4844
}
4945

46+
5047
function testToSliceB32() public {
5148
assertEq0(bytes32("foobar").toSliceB32(), "foobar".toSlice());
5249
}
@@ -98,10 +95,7 @@ contract StringsTest is DSTest {
9895
}
9996

10097
function testCompare() public {
101-
int compare = "foobie".compare("foobie");
102-
emit log_named_int("compare", compare);
10398

104-
assertEq(sign("foobie".toSlice().compare("foobie".toSlice())), 0);
10599
assertEq(sign("foobie".toSlice().compare("foobie".toSlice())), 0);
106100
assertEq(sign("foobie".toSlice().compare("foobif".toSlice())), -1);
107101
assertEq(sign("foobie".toSlice().compare("foobid".toSlice())), 1);

0 commit comments

Comments
 (0)