@@ -4,22 +4,22 @@ const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
44
55const { randomArray, generators } = require ( '../helpers/random' ) ;
66
7- // See https://en.cppreference.com/w/cpp/algorithm/ranges/ lower_bound
7+ // See https://en.cppreference.com/w/cpp/algorithm/lower_bound
88const lowerBound = ( array , value ) => {
99 const i = array . findIndex ( element => value <= element ) ;
1010 return i == - 1 ? array . length : i ;
1111} ;
1212
1313// 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+ } ;
1818
1919const hasDuplicates = array => array . some ( ( v , i ) => array . indexOf ( v ) != i ) ;
2020
2121describe ( 'Arrays' , function ( ) {
22- describe ( 'findUpperBound ' , function ( ) {
22+ describe ( 'search ' , function ( ) {
2323 for ( const [ title , { array, tests } ] of Object . entries ( {
2424 'Even number of elements' : {
2525 array : [ 11n , 12n , 13n , 14n , 15n , 16n , 17n , 18n , 19n , 20n ] ,
@@ -82,40 +82,55 @@ describe('Arrays', function () {
8282 } ) ;
8383
8484 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+ } ) ;
89104 } ) ;
90105 }
91106 } ) ;
92107 }
93108 } ) ;
94109
95110 describe ( 'unsafeAccess' , function ( ) {
96- const contractCases = {
111+ for ( const [ title , { artifact , elements } ] of Object . entries ( {
97112 address : { artifact : 'AddressArraysMock' , elements : randomArray ( generators . address , 10 ) } ,
98113 bytes32 : { artifact : 'Bytes32ArraysMock' , elements : randomArray ( generators . bytes32 , 10 ) } ,
99114 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+ } ;
109120
110- beforeEach ( async function ( ) {
111- Object . assign ( this , await loadFixture ( fixture ) ) ;
112- } ) ;
121+ beforeEach ( async function ( ) {
122+ Object . assign ( this , await loadFixture ( fixture ) ) ;
123+ } ) ;
113124
114- for ( const [ name , { elements } ] of Object . entries ( contractCases ) ) {
115- it ( name , async function ( ) {
116125 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+ } ) ;
118129 }
130+
131+ it ( 'unsafeAccess outside bounds' , async function ( ) {
132+ await expect ( this . mock . unsafeAccess ( elements . length ) ) . to . not . be . rejected ;
133+ } ) ;
119134 } ) ;
120135 }
121136 } ) ;
0 commit comments