@@ -70,17 +70,42 @@ describe('Math', () => {
70
70
} ) ;
71
71
72
72
describe ( '#sqrt(uint256)' , ( ) => {
73
- it ( 'returns the sqrt of a positive integer from 0 to maxUint256' , async ( ) => {
74
- expect ( await instance . sqrt . staticCall ( 16 ) ) . to . eq ( 4 ) ;
73
+ it ( 'returns the square root of 0' , async ( ) => {
74
+ expect ( await instance . sqrt . staticCall ( 0n ) ) . to . eq ( 0n ) ;
75
+ } ) ;
76
+
77
+ it ( 'returns the square root of 1' , async ( ) => {
78
+ expect ( await instance . sqrt . staticCall ( 1n ) ) . to . eq ( 1n ) ;
79
+ } ) ;
80
+
81
+ it ( 'returns the square root of 2' , async ( ) => {
82
+ expect ( await instance . sqrt . staticCall ( 2n ) ) . to . eq ( 1n ) ;
83
+ } ) ;
75
84
76
- for ( let i = 10 ; i < 16 ; i ++ ) {
77
- expect ( await instance . sqrt . staticCall ( i . toString ( ) ) ) . to . eq ( 3 ) ;
85
+ it ( 'returns the square root of positive integers' , async ( ) => {
86
+ for ( let i = 2 ; i < 16 ; i ++ ) {
87
+ expect ( await instance . sqrt . staticCall ( BigInt ( i ) ) ) . to . eq (
88
+ Math . floor ( Math . sqrt ( i ) ) ,
89
+ ) ;
78
90
}
91
+ } ) ;
79
92
80
- expect ( await instance . sqrt . staticCall ( 0 ) ) . to . eq ( 0 ) ;
93
+ it ( 'returns the square root of powers of 2' , async ( ) => {
94
+ for ( let i = 0 ; i < 256 ; i ++ ) {
95
+ const input = 2n ** BigInt ( i ) ;
96
+ const output = await instance . sqrt . staticCall ( input ) ;
97
+ expect ( output ** 2n ) . to . be . lte ( input ) ;
98
+ expect ( ( output + 1n ) ** 2n ) . to . be . gt ( input ) ;
99
+ }
100
+ } ) ;
81
101
102
+ it ( 'returns the square root of max values' , async ( ) => {
82
103
expect ( await instance . sqrt . staticCall ( ethers . MaxUint256 - 1n ) ) . to . eq (
83
- BigInt ( '340282366920938463463374607431768211455' ) ,
104
+ 340282366920938463463374607431768211455n ,
105
+ ) ;
106
+
107
+ expect ( await instance . sqrt . staticCall ( ethers . MaxUint256 ) ) . to . eq (
108
+ 340282366920938463463374607431768211455n ,
84
109
) ;
85
110
} ) ;
86
111
} ) ;
0 commit comments