// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see . pragma solidity >=0.5.0; contract DSTest { event log(string); event logs(bytes); event log_address(address); event log_bytes32(bytes32); event log_int(int256); event log_uint(uint256); event log_bytes(bytes); event log_string(string); event log_named_address(string key, address val); event log_named_bytes32(string key, bytes32 val); event log_named_decimal_int(string key, int256 val, uint256 decimals); event log_named_decimal_uint(string key, uint256 val, uint256 decimals); event log_named_int(string key, int256 val); event log_named_uint(string key, uint256 val); event log_named_bytes(string key, bytes val); event log_named_string(string key, string val); bool public IS_TEST = true; bool private _failed; address constant HEVM_ADDRESS = address(bytes20(uint160(uint256(keccak256("hevm cheat code"))))); modifier mayRevert() { _; } modifier testopts(string memory) { _; } function failed() public returns (bool) { if (_failed) { return _failed; } else { bool globalFailed = false; if (hasHEVMContext()) { (, bytes memory retdata) = HEVM_ADDRESS.call( abi.encodePacked( bytes4(keccak256("load(address,bytes32)")), abi.encode(HEVM_ADDRESS, bytes32("failed")) ) ); globalFailed = abi.decode(retdata, (bool)); } return globalFailed; } } function fail() internal { if (hasHEVMContext()) { (bool status, ) = HEVM_ADDRESS.call( abi.encodePacked( bytes4(keccak256("store(address,bytes32,bytes32)")), abi.encode( HEVM_ADDRESS, bytes32("failed"), bytes32(uint256(0x01)) ) ) ); status; // Silence compiler warnings } _failed = true; } function hasHEVMContext() internal view returns (bool) { uint256 hevmCodeSize = 0; assembly { hevmCodeSize := extcodesize( 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D ) } return hevmCodeSize > 0; } modifier logs_gas() { uint256 startGas = gasleft(); _; uint256 endGas = gasleft(); emit log_named_uint("gas", startGas - endGas); } function assertTrue(bool condition) internal { if (!condition) { emit log("Error: Assertion Failed"); fail(); } } function assertTrue(bool condition, string memory err) internal { if (!condition) { emit log_named_string("Error", err); assertTrue(condition); } } function assertEq(address a, address b) internal { if (a != b) { emit log("Error: a == b not satisfied [address]"); emit log_named_address(" Left", a); emit log_named_address(" Right", b); fail(); } } function assertEq( address a, address b, string memory err ) internal { if (a != b) { emit log_named_string("Error", err); assertEq(a, b); } } function assertEq(bytes32 a, bytes32 b) internal { if (a != b) { emit log("Error: a == b not satisfied [bytes32]"); emit log_named_bytes32(" Left", a); emit log_named_bytes32(" Right", b); fail(); } } function assertEq( bytes32 a, bytes32 b, string memory err ) internal { if (a != b) { emit log_named_string("Error", err); assertEq(a, b); } } function assertEq32(bytes32 a, bytes32 b) internal { assertEq(a, b); } function assertEq32( bytes32 a, bytes32 b, string memory err ) internal { assertEq(a, b, err); } function assertEq(int256 a, int256 b) internal { if (a != b) { emit log("Error: a == b not satisfied [int]"); emit log_named_int(" Left", a); emit log_named_int(" Right", b); fail(); } } function assertEq( int256 a, int256 b, string memory err ) internal { if (a != b) { emit log_named_string("Error", err); assertEq(a, b); } } function assertEq(uint256 a, uint256 b) internal { if (a != b) { emit log("Error: a == b not satisfied [uint]"); emit log_named_uint(" Left", a); emit log_named_uint(" Right", b); fail(); } } function assertEq( uint256 a, uint256 b, string memory err ) internal { if (a != b) { emit log_named_string("Error", err); assertEq(a, b); } } function assertEqDecimal( int256 a, int256 b, uint256 decimals ) internal { if (a != b) { emit log("Error: a == b not satisfied [decimal int]"); emit log_named_decimal_int(" Left", a, decimals); emit log_named_decimal_int(" Right", b, decimals); fail(); } } function assertEqDecimal( int256 a, int256 b, uint256 decimals, string memory err ) internal { if (a != b) { emit log_named_string("Error", err); assertEqDecimal(a, b, decimals); } } function assertEqDecimal( uint256 a, uint256 b, uint256 decimals ) internal { if (a != b) { emit log("Error: a == b not satisfied [decimal uint]"); emit log_named_decimal_uint(" Left", a, decimals); emit log_named_decimal_uint(" Right", b, decimals); fail(); } } function assertEqDecimal( uint256 a, uint256 b, uint256 decimals, string memory err ) internal { if (a != b) { emit log_named_string("Error", err); assertEqDecimal(a, b, decimals); } } function assertGt(uint256 a, uint256 b) internal { if (a <= b) { emit log("Error: a > b not satisfied [uint]"); emit log_named_uint(" Value a", a); emit log_named_uint(" Value b", b); fail(); } } function assertGt( uint256 a, uint256 b, string memory err ) internal { if (a <= b) { emit log_named_string("Error", err); assertGt(a, b); } } function assertGt(int256 a, int256 b) internal { if (a <= b) { emit log("Error: a > b not satisfied [int]"); emit log_named_int(" Value a", a); emit log_named_int(" Value b", b); fail(); } } function assertGt( int256 a, int256 b, string memory err ) internal { if (a <= b) { emit log_named_string("Error", err); assertGt(a, b); } } function assertGtDecimal( int256 a, int256 b, uint256 decimals ) internal { if (a <= b) { emit log("Error: a > b not satisfied [decimal int]"); emit log_named_decimal_int(" Value a", a, decimals); emit log_named_decimal_int(" Value b", b, decimals); fail(); } } function assertGtDecimal( int256 a, int256 b, uint256 decimals, string memory err ) internal { if (a <= b) { emit log_named_string("Error", err); assertGtDecimal(a, b, decimals); } } function assertGtDecimal( uint256 a, uint256 b, uint256 decimals ) internal { if (a <= b) { emit log("Error: a > b not satisfied [decimal uint]"); emit log_named_decimal_uint(" Value a", a, decimals); emit log_named_decimal_uint(" Value b", b, decimals); fail(); } } function assertGtDecimal( uint256 a, uint256 b, uint256 decimals, string memory err ) internal { if (a <= b) { emit log_named_string("Error", err); assertGtDecimal(a, b, decimals); } } function assertGe(uint256 a, uint256 b) internal { if (a < b) { emit log("Error: a >= b not satisfied [uint]"); emit log_named_uint(" Value a", a); emit log_named_uint(" Value b", b); fail(); } } function assertGe( uint256 a, uint256 b, string memory err ) internal { if (a < b) { emit log_named_string("Error", err); assertGe(a, b); } } function assertGe(int256 a, int256 b) internal { if (a < b) { emit log("Error: a >= b not satisfied [int]"); emit log_named_int(" Value a", a); emit log_named_int(" Value b", b); fail(); } } function assertGe( int256 a, int256 b, string memory err ) internal { if (a < b) { emit log_named_string("Error", err); assertGe(a, b); } } function assertGeDecimal( int256 a, int256 b, uint256 decimals ) internal { if (a < b) { emit log("Error: a >= b not satisfied [decimal int]"); emit log_named_decimal_int(" Value a", a, decimals); emit log_named_decimal_int(" Value b", b, decimals); fail(); } } function assertGeDecimal( int256 a, int256 b, uint256 decimals, string memory err ) internal { if (a < b) { emit log_named_string("Error", err); assertGeDecimal(a, b, decimals); } } function assertGeDecimal( uint256 a, uint256 b, uint256 decimals ) internal { if (a < b) { emit log("Error: a >= b not satisfied [decimal uint]"); emit log_named_decimal_uint(" Value a", a, decimals); emit log_named_decimal_uint(" Value b", b, decimals); fail(); } } function assertGeDecimal( uint256 a, uint256 b, uint256 decimals, string memory err ) internal { if (a < b) { emit log_named_string("Error", err); assertGeDecimal(a, b, decimals); } } function assertLt(uint256 a, uint256 b) internal { if (a >= b) { emit log("Error: a < b not satisfied [uint]"); emit log_named_uint(" Value a", a); emit log_named_uint(" Value b", b); fail(); } } function assertLt( uint256 a, uint256 b, string memory err ) internal { if (a >= b) { emit log_named_string("Error", err); assertLt(a, b); } } function assertLt(int256 a, int256 b) internal { if (a >= b) { emit log("Error: a < b not satisfied [int]"); emit log_named_int(" Value a", a); emit log_named_int(" Value b", b); fail(); } } function assertLt( int256 a, int256 b, string memory err ) internal { if (a >= b) { emit log_named_string("Error", err); assertLt(a, b); } } function assertLtDecimal( int256 a, int256 b, uint256 decimals ) internal { if (a >= b) { emit log("Error: a < b not satisfied [decimal int]"); emit log_named_decimal_int(" Value a", a, decimals); emit log_named_decimal_int(" Value b", b, decimals); fail(); } } function assertLtDecimal( int256 a, int256 b, uint256 decimals, string memory err ) internal { if (a >= b) { emit log_named_string("Error", err); assertLtDecimal(a, b, decimals); } } function assertLtDecimal( uint256 a, uint256 b, uint256 decimals ) internal { if (a >= b) { emit log("Error: a < b not satisfied [decimal uint]"); emit log_named_decimal_uint(" Value a", a, decimals); emit log_named_decimal_uint(" Value b", b, decimals); fail(); } } function assertLtDecimal( uint256 a, uint256 b, uint256 decimals, string memory err ) internal { if (a >= b) { emit log_named_string("Error", err); assertLtDecimal(a, b, decimals); } } function assertLe(uint256 a, uint256 b) internal { if (a > b) { emit log("Error: a <= b not satisfied [uint]"); emit log_named_uint(" Value a", a); emit log_named_uint(" Value b", b); fail(); } } function assertLe( uint256 a, uint256 b, string memory err ) internal { if (a > b) { emit log_named_string("Error", err); assertLe(a, b); } } function assertLe(int256 a, int256 b) internal { if (a > b) { emit log("Error: a <= b not satisfied [int]"); emit log_named_int(" Value a", a); emit log_named_int(" Value b", b); fail(); } } function assertLe( int256 a, int256 b, string memory err ) internal { if (a > b) { emit log_named_string("Error", err); assertLe(a, b); } } function assertLeDecimal( int256 a, int256 b, uint256 decimals ) internal { if (a > b) { emit log("Error: a <= b not satisfied [decimal int]"); emit log_named_decimal_int(" Value a", a, decimals); emit log_named_decimal_int(" Value b", b, decimals); fail(); } } function assertLeDecimal( int256 a, int256 b, uint256 decimals, string memory err ) internal { if (a > b) { emit log_named_string("Error", err); assertLeDecimal(a, b, decimals); } } function assertLeDecimal( uint256 a, uint256 b, uint256 decimals ) internal { if (a > b) { emit log("Error: a <= b not satisfied [decimal uint]"); emit log_named_decimal_uint(" Value a", a, decimals); emit log_named_decimal_uint(" Value b", b, decimals); fail(); } } function assertLeDecimal( uint256 a, uint256 b, uint256 decimals, string memory err ) internal { if (a > b) { emit log_named_string("Error", err); assertGeDecimal(a, b, decimals); } } function assertEq(string memory a, string memory b) internal { if (keccak256(abi.encodePacked(a)) != keccak256(abi.encodePacked(b))) { emit log("Error: a == b not satisfied [string]"); emit log_named_string(" Left", a); emit log_named_string(" Right", b); fail(); } } function assertEq( string memory a, string memory b, string memory err ) internal { if (keccak256(abi.encodePacked(a)) != keccak256(abi.encodePacked(b))) { emit log_named_string("Error", err); assertEq(a, b); } } function checkEq0(bytes memory a, bytes memory b) internal pure returns (bool ok) { ok = true; if (a.length == b.length) { for (uint256 i = 0; i < a.length; i++) { if (a[i] != b[i]) { ok = false; } } } else { ok = false; } } function assertEq0(bytes memory a, bytes memory b) internal { if (!checkEq0(a, b)) { emit log("Error: a == b not satisfied [bytes]"); emit log_named_bytes(" Left", a); emit log_named_bytes(" Right", b); fail(); } } function assertEq0( bytes memory a, bytes memory b, string memory err ) internal { if (!checkEq0(a, b)) { emit log_named_string("Error", err); assertEq0(a, b); } } }