@@ -4,22 +4,22 @@ const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
4
4
5
5
const { randomArray, generators } = require ( '../helpers/random' ) ;
6
6
7
- // See https://en.cppreference.com/w/cpp/algorithm/ranges/ lower_bound
7
+ // See https://en.cppreference.com/w/cpp/algorithm/lower_bound
8
8
const lowerBound = ( array , value ) => {
9
9
const i = array . findIndex ( element => value <= element ) ;
10
10
return i == - 1 ? array . length : i ;
11
11
} ;
12
12
13
13
// See https://en.cppreference.com/w/cpp/algorithm/upper_bound
14
- // const upperBound = (array, value) => {
15
- // const i = array.findIndex(element => value < element);
16
- // return i == -1 ? array.length : i;
17
- // };
14
+ const upperBound = ( array , value ) => {
15
+ const i = array . findIndex ( element => value < element ) ;
16
+ return i == - 1 ? array . length : i ;
17
+ } ;
18
18
19
19
const hasDuplicates = array => array . some ( ( v , i ) => array . indexOf ( v ) != i ) ;
20
20
21
21
describe ( 'Arrays' , function ( ) {
22
- describe ( 'findUpperBound ' , function ( ) {
22
+ describe ( 'search ' , function ( ) {
23
23
for ( const [ title , { array, tests } ] of Object . entries ( {
24
24
'Even number of elements' : {
25
25
array : [ 11n , 12n , 13n , 14n , 15n , 16n , 17n , 18n , 19n , 20n ] ,
@@ -82,40 +82,55 @@ describe('Arrays', function () {
82
82
} ) ;
83
83
84
84
for ( const [ name , input ] of Object . entries ( tests ) ) {
85
- it ( name , async function ( ) {
86
- // findUpperBound does not support duplicated
87
- if ( hasDuplicates ( array ) ) this . skip ( ) ;
88
- expect ( await this . mock . findUpperBound ( input ) ) . to . equal ( lowerBound ( array , input ) ) ;
85
+ describe ( name , function ( ) {
86
+ it ( '[deprecated] findUpperBound' , async function ( ) {
87
+ // findUpperBound does not support duplicated
88
+ if ( hasDuplicates ( array ) ) {
89
+ expect ( await this . mock . findUpperBound ( input ) ) . to . be . equal ( upperBound ( array , input ) - 1 ) ;
90
+ } else {
91
+ expect ( await this . mock . findUpperBound ( input ) ) . to . be . equal ( lowerBound ( array , input ) ) ;
92
+ }
93
+ } ) ;
94
+
95
+ it ( 'lowerBound' , async function ( ) {
96
+ expect ( await this . mock . lowerBound ( input ) ) . to . be . equal ( lowerBound ( array , input ) ) ;
97
+ expect ( await this . mock . lowerBoundMemory ( array , input ) ) . to . be . equal ( lowerBound ( array , input ) ) ;
98
+ } ) ;
99
+
100
+ it ( 'upperBound' , async function ( ) {
101
+ expect ( await this . mock . upperBound ( input ) ) . to . be . equal ( upperBound ( array , input ) ) ;
102
+ expect ( await this . mock . upperBoundMemory ( array , input ) ) . to . be . equal ( upperBound ( array , input ) ) ;
103
+ } ) ;
89
104
} ) ;
90
105
}
91
106
} ) ;
92
107
}
93
108
} ) ;
94
109
95
110
describe ( 'unsafeAccess' , function ( ) {
96
- const contractCases = {
111
+ for ( const [ title , { artifact , elements } ] of Object . entries ( {
97
112
address : { artifact : 'AddressArraysMock' , elements : randomArray ( generators . address , 10 ) } ,
98
113
bytes32 : { artifact : 'Bytes32ArraysMock' , elements : randomArray ( generators . bytes32 , 10 ) } ,
99
114
uint256 : { artifact : 'Uint256ArraysMock' , elements : randomArray ( generators . uint256 , 10 ) } ,
100
- } ;
101
-
102
- const fixture = async ( ) => {
103
- const contracts = { } ;
104
- for ( const [ name , { artifact, elements } ] of Object . entries ( contractCases ) ) {
105
- contracts [ name ] = await ethers . deployContract ( artifact , [ elements ] ) ;
106
- }
107
- return { contracts } ;
108
- } ;
115
+ } ) ) {
116
+ describe ( title , function ( ) {
117
+ const fixture = async ( ) => {
118
+ return { mock : await ethers . deployContract ( artifact , [ elements ] ) } ;
119
+ } ;
109
120
110
- beforeEach ( async function ( ) {
111
- Object . assign ( this , await loadFixture ( fixture ) ) ;
112
- } ) ;
121
+ beforeEach ( async function ( ) {
122
+ Object . assign ( this , await loadFixture ( fixture ) ) ;
123
+ } ) ;
113
124
114
- for ( const [ name , { elements } ] of Object . entries ( contractCases ) ) {
115
- it ( name , async function ( ) {
116
125
for ( const i in elements ) {
117
- expect ( await this . contracts [ name ] . unsafeAccess ( i ) ) . to . equal ( elements [ i ] ) ;
126
+ it ( `unsafeAccess within bounds #${ i } ` , async function ( ) {
127
+ expect ( await this . mock . unsafeAccess ( i ) ) . to . equal ( elements [ i ] ) ;
128
+ } ) ;
118
129
}
130
+
131
+ it ( 'unsafeAccess outside bounds' , async function ( ) {
132
+ await expect ( this . mock . unsafeAccess ( elements . length ) ) . to . not . be . rejected ;
133
+ } ) ;
119
134
} ) ;
120
135
}
121
136
} ) ;
0 commit comments