Skip to content

Add assert approx and assert false #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ contract DSTest {
}
}

function assertFalse(bool condition) internal {
if (condition) {
emit log("Error: Assertion Failed");
fail();
}
}
Comment on lines +72 to +77

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this simply call assertTrue like this?

function assertFalse(bool condition) internal {
    assertTrue(!condition);
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we can also do this !


function assertFalse(bool condition, string memory err) internal {
if (condition) {
emit log_named_string("Error", err);
assertFalse(condition);
}
}

function assertEq(address a, address b) internal {
if (a != b) {
emit log("Error: a == b not satisfied [address]");
Expand Down Expand Up @@ -161,6 +175,40 @@ contract DSTest {
assertEqDecimal(a, b, decimals);
}
}
function assertApproxEq(uint a, uint b, uint margin_of_error) internal {
if (a > b) {
if (a - b > margin_of_error) {
emit log("Error a not equal to b");
emit log_named_uint(" Expected", b);
emit log_named_uint(" Actual", a);
fail();
}
} else {
if (b - a > margin_of_error) {
emit log("Error a not equal to b");
emit log_named_uint(" Expected", b);
emit log_named_uint(" Actual", a);
fail();
}
}
}
function assertApproxEq(uint a, uint b, uint margin_of_error, string memory err) internal {
if (a > b) {
if (a - b > margin_of_error) {
emit log_named_string("Error", err);
emit log_named_uint(" Expected", b);
emit log_named_uint(" Actual", a);
fail();
}
} else {
if (b - a > margin_of_error) {
emit log_named_string("Error", err);
emit log_named_uint(" Expected", b);
emit log_named_uint(" Actual", a);
fail();
}
}
}

function assertGt(uint a, uint b) internal {
if (a <= b) {
Expand Down