AssertAddress.sol
1 //SPDX-License-Identifier: MIT 2 pragma solidity >= 0.4.15 < 0.9.0; 3 4 library AssertAddress { 5 6 // Constant: ADDRESS_NULL 7 // The null address: 0 8 address constant ADDRESS_NULL = 0x0000000000000000000000000000000000000000; 9 10 /* 11 Event: TestEvent 12 13 Fired when an assertion is made. 14 15 Params: 16 result (bool) - Whether or not the assertion holds. 17 message (string) - A message to display if the assertion does not hold. 18 */ 19 event TestEvent(bool indexed result, string message); 20 21 // ************************************** address ************************************** 22 23 /* 24 Function: equal(address) 25 26 Assert that two addresses are equal. 27 28 : A == B 29 30 Params: 31 A (address) - The first address. 32 B (address) - The second address. 33 message (string) - A message that is sent if the assertion fails. 34 35 Returns: 36 result (bool) - The result. 37 */ 38 function equal(address a, address b, string memory message) public returns (bool result) { 39 result = (a == b); 40 _report(result, message); 41 } 42 /* 43 Function: notEqual(address) 44 45 Assert that two addresses are not equal. 46 47 : A != B 48 49 Params: 50 A (address) - The first address. 51 B (address) - The second address. 52 message (string) - A message that is sent if the assertion fails. 53 54 Returns: 55 result (bool) - The result. 56 */ 57 function notEqual(address a, address b, string memory message) public returns (bool result) { 58 result = (a != b); 59 _report(result, message); 60 } 61 62 /* 63 Function: isZero(address) 64 65 Assert that an address is zero. 66 67 : addr == ADDRESS_NULL 68 69 Params: 70 addr (address) - The address. 71 message (string) - A message that is sent if the assertion fails. 72 73 Returns: 74 result (bool) - The result. 75 */ 76 function isZero(address addr, string memory message) public returns (bool result) { 77 result = (addr == ADDRESS_NULL); 78 _report(result, message); 79 } 80 81 /* 82 Function: isNotZero(address) 83 84 Assert that an address is not zero. 85 86 : addr != ADDRESS_NULL 87 88 Params: 89 addr (address) - The address. 90 message (string) - A message that is sent if the assertion fails. 91 92 Returns: 93 result (bool) - The result. 94 */ 95 function isNotZero(address addr, string memory message) public returns (bool result) { 96 result = (addr != ADDRESS_NULL); 97 _report(result, message); 98 } 99 100 /******************************** internal ********************************/ 101 102 /* 103 Function: _report 104 105 Internal function for triggering <TestEvent>. 106 107 Params: 108 result (bool) - The test result (true or false). 109 message (string) - The message that is sent if the assertion fails. 110 */ 111 function _report(bool result, string memory message) internal { 112 if(result) 113 emit TestEvent(true, ""); 114 else 115 emit TestEvent(false, message); 116 } 117 }