@@ -112,6 +112,93 @@ describe('Bytes', function () {
112112 } ) ;
113113 } ) ;
114114
115+ describe ( 'clz bytes' , function ( ) {
116+ it ( 'empty buffer' , async function ( ) {
117+ await expect ( this . mock . $clz ( '0x' ) ) . to . eventually . equal ( 0 ) ;
118+ } ) ;
119+
120+ it ( 'single zero byte' , async function ( ) {
121+ await expect ( this . mock . $clz ( '0x00' ) ) . to . eventually . equal ( 8 ) ;
122+ } ) ;
123+
124+ it ( 'single non-zero byte' , async function ( ) {
125+ await expect ( this . mock . $clz ( '0x01' ) ) . to . eventually . equal ( 7 ) ;
126+ await expect ( this . mock . $clz ( '0xff' ) ) . to . eventually . equal ( 0 ) ;
127+ } ) ;
128+
129+ it ( 'multiple leading zeros' , async function ( ) {
130+ await expect ( this . mock . $clz ( '0x0000000001' ) ) . to . eventually . equal ( 39 ) ;
131+ await expect (
132+ this . mock . $clz ( '0x0000000000000000000000000000000000000000000000000000000000000001' ) ,
133+ ) . to . eventually . equal ( 255 ) ;
134+ } ) ;
135+
136+ it ( 'all zeros of various lengths' , async function ( ) {
137+ await expect ( this . mock . $clz ( '0x00000000' ) ) . to . eventually . equal ( 32 ) ;
138+ await expect (
139+ this . mock . $clz ( '0x0000000000000000000000000000000000000000000000000000000000000000' ) ,
140+ ) . to . eventually . equal ( 256 ) ;
141+
142+ // Complete chunks
143+ await expect ( this . mock . $clz ( '0x' + '00' . repeat ( 32 ) + '01' ) ) . to . eventually . equal ( 263 ) ; // 32*8+7
144+ await expect ( this . mock . $clz ( '0x' + '00' . repeat ( 64 ) + '01' ) ) . to . eventually . equal ( 519 ) ; // 64*8+7
145+
146+ // Partial last chunk
147+ await expect ( this . mock . $clz ( '0x' + '00' . repeat ( 33 ) + '01' ) ) . to . eventually . equal ( 271 ) ; // 33*8+7
148+ await expect ( this . mock . $clz ( '0x' + '00' . repeat ( 34 ) + '01' ) ) . to . eventually . equal ( 279 ) ; // 34*8+7
149+ await expect ( this . mock . $clz ( '0x' + '00' . repeat ( 40 ) + '01' + '00' . repeat ( 9 ) ) ) . to . eventually . equal ( 327 ) ; // 40*8+7
150+ await expect ( this . mock . $clz ( '0x' + '00' . repeat ( 50 ) ) ) . to . eventually . equal ( 400 ) ; // 50*8
151+
152+ // First byte of each chunk non-zero
153+ await expect ( this . mock . $clz ( '0x80' + '00' . repeat ( 31 ) ) ) . to . eventually . equal ( 0 ) ;
154+ await expect ( this . mock . $clz ( '0x01' + '00' . repeat ( 31 ) ) ) . to . eventually . equal ( 7 ) ;
155+ await expect ( this . mock . $clz ( '0x' + '00' . repeat ( 32 ) + '80' + '00' . repeat ( 31 ) ) ) . to . eventually . equal ( 256 ) ; // 32*8
156+ await expect ( this . mock . $clz ( '0x' + '00' . repeat ( 32 ) + '01' + '00' . repeat ( 31 ) ) ) . to . eventually . equal ( 263 ) ; // 32*8+7
157+
158+ // Last byte of each chunk non-zero
159+ await expect ( this . mock . $clz ( '0x' + '00' . repeat ( 31 ) + '01' ) ) . to . eventually . equal ( 255 ) ; // 31*8+7
160+ await expect ( this . mock . $clz ( '0x' + '00' . repeat ( 63 ) + '01' ) ) . to . eventually . equal ( 511 ) ; // 63*8+7
161+
162+ // Middle byte of each chunk non-zero
163+ await expect ( this . mock . $clz ( '0x' + '00' . repeat ( 16 ) + '01' + '00' . repeat ( 15 ) ) ) . to . eventually . equal ( 135 ) ; // 16*8+7
164+ await expect ( this . mock . $clz ( '0x' + '00' . repeat ( 32 ) + '01' + '00' . repeat ( 31 ) ) ) . to . eventually . equal ( 263 ) ; // 32*8+7
165+ await expect ( this . mock . $clz ( '0x' + '00' . repeat ( 48 ) + '01' + '00' . repeat ( 47 ) ) ) . to . eventually . equal ( 391 ) ; // 48*8+7
166+ await expect ( this . mock . $clz ( '0x' + '00' . repeat ( 64 ) + '01' + '00' . repeat ( 63 ) ) ) . to . eventually . equal ( 519 ) ; // 64*8+7
167+ } ) ;
168+ } ) ;
169+
170+ describe ( 'equal' , function ( ) {
171+ it ( 'identical buffers' , async function ( ) {
172+ await expect ( this . mock . $equal ( lorem , lorem ) ) . to . eventually . be . true ;
173+ } ) ;
174+
175+ it ( 'same content' , async function ( ) {
176+ const copy = new Uint8Array ( lorem ) ;
177+ await expect ( this . mock . $equal ( lorem , copy ) ) . to . eventually . be . true ;
178+ } ) ;
179+
180+ it ( 'different content' , async function ( ) {
181+ const different = ethers . toUtf8Bytes ( 'Different content' ) ;
182+ await expect ( this . mock . $equal ( lorem , different ) ) . to . eventually . be . false ;
183+ } ) ;
184+
185+ it ( 'different lengths' , async function ( ) {
186+ const shorter = lorem . slice ( 0 , 10 ) ;
187+ await expect ( this . mock . $equal ( lorem , shorter ) ) . to . eventually . be . false ;
188+ } ) ;
189+
190+ it ( 'empty buffers' , async function ( ) {
191+ const empty1 = new Uint8Array ( 0 ) ;
192+ const empty2 = new Uint8Array ( 0 ) ;
193+ await expect ( this . mock . $equal ( empty1 , empty2 ) ) . to . eventually . be . true ;
194+ } ) ;
195+
196+ it ( 'one empty one not' , async function ( ) {
197+ const empty = new Uint8Array ( 0 ) ;
198+ await expect ( this . mock . $equal ( lorem , empty ) ) . to . eventually . be . false ;
199+ } ) ;
200+ } ) ;
201+
115202 describe ( 'reverseBits' , function ( ) {
116203 describe ( 'reverseBytes32' , function ( ) {
117204 it ( 'reverses bytes correctly' , async function ( ) {
0 commit comments