@@ -517,6 +517,130 @@ const swap32Tests: ByteUtilTest<'swap32'>[] = [
517517 }
518518 }
519519] ;
520+ const compareTests : ByteUtilTest < 'compare' > [ ] = [
521+ {
522+ name : 'returns 0 for two equal arrays (same content)' ,
523+ inputs : [ new Uint8Array ( [ 1 , 2 , 3 ] ) , new Uint8Array ( [ 1 , 2 , 3 ] ) ] ,
524+ expectation ( { output } ) {
525+ expect ( output ) . to . equal ( 0 ) ;
526+ }
527+ } ,
528+ {
529+ name : 'returns 0 when comparing the same buffer by reference' ,
530+ inputs : ( ( ) => {
531+ const buf = new Uint8Array ( [ 5 , 6 , 7 ] ) ;
532+ return [ buf , buf ] ;
533+ } ) ( ) ,
534+ expectation ( { output } ) {
535+ expect ( output ) . to . equal ( 0 ) ;
536+ }
537+ } ,
538+ {
539+ name : 'array a is lexicographically less than array b (first differing byte)' ,
540+ inputs : [ new Uint8Array ( [ 1 , 2 , 3 ] ) , new Uint8Array ( [ 1 , 2 , 4 ] ) ] ,
541+ expectation ( { output } ) {
542+ expect ( output ) . to . equal ( - 1 ) ;
543+ }
544+ } ,
545+ {
546+ name : 'array a is lexicographically greater than array b (first differing byte)' ,
547+ inputs : [ new Uint8Array ( [ 1 , 2 , 4 ] ) , new Uint8Array ( [ 1 , 2 , 3 ] ) ] ,
548+ expectation ( { output } ) {
549+ expect ( output ) . to . equal ( 1 ) ;
550+ }
551+ } ,
552+ {
553+ name : 'a is a strict prefix of b (a shorter, same starting bytes) -> a < b' ,
554+ inputs : [ new Uint8Array ( [ 1 , 2 ] ) , new Uint8Array ( [ 1 , 2 , 3 ] ) ] ,
555+ expectation ( { output } ) {
556+ expect ( output ) . to . equal ( - 1 ) ;
557+ }
558+ } ,
559+ {
560+ name : 'b is a strict prefix of a (b shorter, same starting bytes) -> a > b' ,
561+ inputs : [ new Uint8Array ( [ 1 , 2 , 3 ] ) , new Uint8Array ( [ 1 , 2 ] ) ] ,
562+ expectation ( { output } ) {
563+ expect ( output ) . to . equal ( 1 ) ;
564+ }
565+ } ,
566+ {
567+ name : 'handles empty arrays' ,
568+ inputs : [ new Uint8Array ( [ ] ) , new Uint8Array ( [ ] ) ] ,
569+ expectation ( { output } ) {
570+ expect ( output ) . to . equal ( 0 ) ;
571+ }
572+ }
573+ ] ;
574+ const concatTests : ByteUtilTest < 'concat' > [ ] = [
575+ {
576+ name : 'concatenates two non-empty arrays' ,
577+ inputs : [ [ new Uint8Array ( [ 1 , 2 ] ) , new Uint8Array ( [ 3 , 4 ] ) ] ] ,
578+ expectation ( { output, error } ) {
579+ expect ( error ) . to . be . null ;
580+ expect ( output ) . to . not . be . undefined ;
581+ expect ( [ ...( output ?? [ ] ) ] ) . to . deep . equal ( [ 1 , 2 , 3 , 4 ] ) ;
582+ }
583+ } ,
584+ {
585+ name : 'concatenates multiple arrays in order' ,
586+ inputs : [ [ new Uint8Array ( [ 1 ] ) , new Uint8Array ( [ 2 , 3 ] ) , new Uint8Array ( [ 4 , 5 , 6 ] ) ] ] ,
587+ expectation ( { output, error } ) {
588+ expect ( error ) . to . be . null ;
589+ expect ( output ) . to . not . be . undefined ;
590+ expect ( [ ...( output ?? [ ] ) ] ) . to . deep . equal ( [ 1 , 2 , 3 , 4 , 5 , 6 ] ) ;
591+ }
592+ } ,
593+ {
594+ name : 'returns an empty Uint8Array when given an empty list' ,
595+ inputs : [ [ ] ] ,
596+ expectation ( { output, error } ) {
597+ expect ( error ) . to . be . null ;
598+ expect ( output ) . to . not . be . undefined ;
599+ expect ( output ) . to . have . property ( 'byteLength' , 0 ) ;
600+ expect ( [ ...( output ?? [ ] ) ] ) . to . deep . equal ( [ ] ) ;
601+ }
602+ } ,
603+ {
604+ name : 'returns the same contents when given a single array' ,
605+ inputs : [ [ new Uint8Array ( [ 7 , 8 , 9 ] ) ] ] ,
606+ expectation ( { output, error } ) {
607+ expect ( error ) . to . be . null ;
608+ expect ( output ) . to . not . be . undefined ;
609+ expect ( [ ...( output ?? [ ] ) ] ) . to . deep . equal ( [ 7 , 8 , 9 ] ) ;
610+ }
611+ } ,
612+ {
613+ name : 'handles concatenation with empty arrays inside the list' ,
614+ inputs : [
615+ [ new Uint8Array ( [ ] ) , new Uint8Array ( [ 1 , 2 , 3 ] ) , new Uint8Array ( [ ] ) , new Uint8Array ( [ 4 ] ) ]
616+ ] ,
617+ expectation ( { output, error } ) {
618+ expect ( error ) . to . be . null ;
619+ expect ( output ) . to . not . be . undefined ;
620+ expect ( [ ...( output ?? [ ] ) ] ) . to . deep . equal ( [ 1 , 2 , 3 , 4 ] ) ;
621+ }
622+ } ,
623+ {
624+ name : 'result has correct total byteLength' ,
625+ inputs : [ [ new Uint8Array ( [ 1 , 2 ] ) , new Uint8Array ( [ 3 ] ) , new Uint8Array ( [ 4 , 5 , 6 ] ) ] ] ,
626+ expectation ( { output, error } ) {
627+ expect ( error ) . to . be . null ;
628+ expect ( output ) . to . not . be . undefined ;
629+ // 2 + 1 + 3 = 6
630+ expect ( output ) . to . have . property ( 'byteLength' , 6 ) ;
631+ expect ( [ ...( output ?? [ ] ) ] ) . to . deep . equal ( [ 1 , 2 , 3 , 4 , 5 , 6 ] ) ;
632+ }
633+ } ,
634+ {
635+ name : 'concatenates arrays with overlapping contents correctly' ,
636+ inputs : [ [ new Uint8Array ( [ 0 , 0 , 1 ] ) , new Uint8Array ( [ 1 , 0 , 0 ] ) ] ] ,
637+ expectation ( { output, error } ) {
638+ expect ( error ) . to . be . null ;
639+ expect ( output ) . to . not . be . undefined ;
640+ expect ( [ ...( output ?? [ ] ) ] ) . to . deep . equal ( [ 0 , 0 , 1 , 1 , 0 , 0 ] ) ;
641+ }
642+ }
643+ ] ;
520644
521645const utils = new Map ( [
522646 [ 'nodeJsByteUtils' , nodeJsByteUtils ] ,
@@ -538,7 +662,9 @@ const table = new Map<keyof ByteUtils, ByteUtilTest<keyof ByteUtils>[]>([
538662 [ 'toUTF8' , toUTF8Tests ] ,
539663 [ 'utf8ByteLength' , utf8ByteLengthTests ] ,
540664 [ 'randomBytes' , randomBytesTests ] ,
541- [ 'swap32' , swap32Tests ]
665+ [ 'swap32' , swap32Tests ] ,
666+ [ 'compare' , compareTests ] ,
667+ [ 'concat' , concatTests ]
542668] ) ;
543669
544670describe ( 'ByteUtils' , ( ) => {
0 commit comments