@@ -517,6 +517,123 @@ 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 . deep . equal ( Buffer . from ( [ 1 , 2 , 3 , 4 ] ) ) ;
581+ }
582+ } ,
583+ {
584+ name : 'concatenates multiple arrays in order' ,
585+ inputs : [ [ new Uint8Array ( [ 1 ] ) , new Uint8Array ( [ 2 , 3 ] ) , new Uint8Array ( [ 4 , 5 , 6 ] ) ] ] ,
586+ expectation ( { output, error } ) {
587+ expect ( error ) . to . be . null ;
588+ expect ( output ) . to . deep . equal ( Buffer . from ( [ 1 , 2 , 3 , 4 , 5 , 6 ] ) ) ;
589+ }
590+ } ,
591+ {
592+ name : 'returns an empty Uint8Array when given an empty list' ,
593+ inputs : [ [ ] ] ,
594+ expectation ( { output, error } ) {
595+ expect ( error ) . to . be . null ;
596+ expect ( output ) . to . have . property ( 'byteLength' , 0 ) ;
597+ expect ( output ) . to . deep . equal ( Buffer . from ( [ ] ) ) ;
598+ }
599+ } ,
600+ {
601+ name : 'returns the same contents when given a single array' ,
602+ inputs : [ [ new Uint8Array ( [ 7 , 8 , 9 ] ) ] ] ,
603+ expectation ( { output, error } ) {
604+ expect ( error ) . to . be . null ;
605+ expect ( output ) . to . deep . equal ( Buffer . from ( [ 7 , 8 , 9 ] ) ) ;
606+ }
607+ } ,
608+ {
609+ name : 'handles concatenation with empty arrays inside the list' ,
610+ inputs : [
611+ [ new Uint8Array ( [ ] ) , new Uint8Array ( [ 1 , 2 , 3 ] ) , new Uint8Array ( [ ] ) , new Uint8Array ( [ 4 ] ) ]
612+ ] ,
613+ expectation ( { output, error } ) {
614+ expect ( error ) . to . be . null ;
615+ expect ( output ) . to . deep . equal ( Buffer . from ( [ 1 , 2 , 3 , 4 ] ) ) ;
616+ }
617+ } ,
618+ {
619+ name : 'result has correct total byteLength' ,
620+ inputs : [ [ new Uint8Array ( [ 1 , 2 ] ) , new Uint8Array ( [ 3 ] ) , new Uint8Array ( [ 4 , 5 , 6 ] ) ] ] ,
621+ expectation ( { output, error } ) {
622+ expect ( error ) . to . be . null ;
623+ // 2 + 1 + 3 = 6
624+ expect ( output ) . to . have . property ( 'byteLength' , 6 ) ;
625+ expect ( output ) . to . deep . equal ( Buffer . from ( [ 1 , 2 , 3 , 4 , 5 , 6 ] ) ) ;
626+ }
627+ } ,
628+ {
629+ name : 'concatenates arrays with overlapping contents correctly' ,
630+ inputs : [ [ new Uint8Array ( [ 0 , 0 , 1 ] ) , new Uint8Array ( [ 1 , 0 , 0 ] ) ] ] ,
631+ expectation ( { output, error } ) {
632+ expect ( error ) . to . be . null ;
633+ expect ( output ) . to . deep . equal ( Buffer . from ( [ 0 , 0 , 1 , 1 , 0 , 0 ] ) ) ;
634+ }
635+ }
636+ ] ;
520637
521638const utils = new Map ( [
522639 [ 'nodeJsByteUtils' , nodeJsByteUtils ] ,
@@ -538,7 +655,9 @@ const table = new Map<keyof ByteUtils, ByteUtilTest<keyof ByteUtils>[]>([
538655 [ 'toUTF8' , toUTF8Tests ] ,
539656 [ 'utf8ByteLength' , utf8ByteLengthTests ] ,
540657 [ 'randomBytes' , randomBytesTests ] ,
541- [ 'swap32' , swap32Tests ]
658+ [ 'swap32' , swap32Tests ] ,
659+ [ 'compare' , compareTests ] ,
660+ [ 'concat' , concatTests ]
542661] ) ;
543662
544663describe ( 'ByteUtils' , ( ) => {
0 commit comments