File tree Expand file tree Collapse file tree 3 files changed +21
-0
lines changed
Expand file tree Collapse file tree 3 files changed +21
-0
lines changed Original file line number Diff line number Diff line change 33pragma solidity ^ 0.8.8 ;
44
55library Math {
6+ /**
7+ * @notice calculate the absolute value of a number
8+ * @param a number whose absoluve value to calculate
9+ * @return absolute value
10+ */
11+ function abs (int256 a ) internal pure returns (uint256 ) {
12+ return uint256 (a < 0 ? - a : a);
13+ }
14+
615 /**
716 * @notice select the greater of two numbers
817 * @param a first number
Original file line number Diff line number Diff line change @@ -7,6 +7,10 @@ import { Math } from './Math.sol';
77contract MathMock {
88 using Math for uint256 ;
99
10+ function abs (int256 a ) external pure returns (uint256 ) {
11+ return Math.abs (a);
12+ }
13+
1014 function max (uint256 a , uint256 b ) external pure returns (uint256 ) {
1115 return Math.max (a, b);
1216 }
Original file line number Diff line number Diff line change @@ -11,6 +11,14 @@ describe('Math', function () {
1111 } ) ;
1212
1313 describe ( '__internal' , function ( ) {
14+ describe ( '#abs(int256)' , function ( ) {
15+ it ( 'returns the absolute value of a number' , async ( ) => {
16+ expect ( await instance . callStatic . abs ( - 1 ) ) . to . equal ( 1 ) ;
17+ expect ( await instance . callStatic . abs ( 0 ) ) . to . equal ( 0 ) ;
18+ expect ( await instance . callStatic . abs ( 1 ) ) . to . equal ( 1 ) ;
19+ } ) ;
20+ } ) ;
21+
1422 describe ( '#max(uint256,uint256)' , function ( ) {
1523 it ( 'returns the greater of two numbers' , async ( ) => {
1624 expect (
You can’t perform that action at this time.
0 commit comments