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