22 * (c) 2019 Micro:bit Educational Foundation and the microbit-fs contributors.
33 * SPDX-License-Identifier: MIT
44 */
5- import { bytesToStr , strToBytes } from '../common' ;
5+ import {
6+ bytesToStr ,
7+ strToBytes ,
8+ concatUint8Array ,
9+ areUint8ArraysEqual ,
10+ } from '../common' ;
611
7- describe ( ` strToBytes` , ( ) => {
8- it ( ` works with 1 byte characters` , ( ) => {
12+ describe ( ' strToBytes' , ( ) => {
13+ it ( ' works with 1 byte characters' , ( ) => {
914 const testString = 'test' ;
1015 const testCodes = [ 116 , 101 , 115 , 116 ] ;
1116
@@ -15,7 +20,8 @@ describe(`strToBytes`, () => {
1520 expect ( tester . next ( ) . value ) . toEqual ( code ) ;
1621 }
1722 } ) ;
18- it ( `works with 2 byte characters` , ( ) => {
23+
24+ it ( 'works with 2 byte characters' , ( ) => {
1925 const testString = 'Ση' ;
2026 const testCodes = [ 206 , 163 , 206 , 183 ] ;
2127
@@ -25,7 +31,8 @@ describe(`strToBytes`, () => {
2531 expect ( tester . next ( ) . value ) . toEqual ( code ) ;
2632 }
2733 } ) ;
28- it ( `works with 3 byte characters` , ( ) => {
34+
35+ it ( 'works with 3 byte characters' , ( ) => {
2936 const testString = '世' ;
3037 const testCodes = [ 228 , 184 , 150 ] ;
3138
@@ -37,20 +44,107 @@ describe(`strToBytes`, () => {
3744 } ) ;
3845} ) ;
3946
40- describe ( ` bytesToStr` , ( ) => {
41- it ( ` works with 1 byte characters` , ( ) => {
47+ describe ( ' bytesToStr' , ( ) => {
48+ it ( ' works with 1 byte characters' , ( ) => {
4249 const testCodes : Uint8Array = new Uint8Array ( [ 116 , 101 , 115 , 116 ] ) ;
4350
4451 expect ( bytesToStr ( testCodes ) ) . toEqual ( 'test' ) ;
4552 } ) ;
46- it ( `works with 2 byte characters` , ( ) => {
53+
54+ it ( 'works with 2 byte characters' , ( ) => {
4755 const testCodes : Uint8Array = new Uint8Array ( [ 206 , 163 , 206 , 183 ] ) ;
4856
4957 expect ( bytesToStr ( testCodes ) ) . toEqual ( 'Ση' ) ;
5058 } ) ;
51- it ( `works with 3 byte characters` , ( ) => {
59+
60+ it ( 'works with 3 byte characters' , ( ) => {
5261 const testCodes : Uint8Array = new Uint8Array ( [ 228 , 184 , 150 ] ) ;
5362
5463 expect ( bytesToStr ( testCodes ) ) . toEqual ( '世' ) ;
5564 } ) ;
5665} ) ;
66+
67+ describe ( 'concatUint8Array' , ( ) => {
68+ it ( 'concatenates correctly' , ( ) => {
69+ const firstArray = [ 116 , 101 , 115 , 116 ] ;
70+ const secondArray = [ 234 , 56 , 45 , 98 ] ;
71+ const first : Uint8Array = new Uint8Array ( firstArray ) ;
72+ const second : Uint8Array = new Uint8Array ( secondArray ) ;
73+
74+ const result1 = concatUint8Array ( first , first ) ;
75+ const result2 = concatUint8Array ( second , second ) ;
76+ const result3 = concatUint8Array ( first , second ) ;
77+
78+ expect ( result1 ) . toEqual ( new Uint8Array ( firstArray . concat ( firstArray ) ) ) ;
79+ expect ( result2 ) . toEqual ( new Uint8Array ( secondArray . concat ( secondArray ) ) ) ;
80+ expect ( result3 ) . toEqual ( new Uint8Array ( firstArray . concat ( secondArray ) ) ) ;
81+ } ) ;
82+
83+ it ( 'concatenates correctly empty arrays' , ( ) => {
84+ const first : Uint8Array = new Uint8Array ( [ ] ) ;
85+ const second : Uint8Array = new Uint8Array ( [ ] ) ;
86+
87+ const result = concatUint8Array ( first , second ) ;
88+
89+ expect ( result ) . toEqual ( new Uint8Array ( [ ] ) ) ;
90+ } ) ;
91+
92+ it ( 'concatenates arrays of different length' , ( ) => {
93+ const firstArray = [ 116 , 101 , 115 , 116 ] ;
94+ const secondArray = [ 234 , 56 , 45 , 98 , 0 ] ;
95+ const first : Uint8Array = new Uint8Array ( firstArray ) ;
96+ const second : Uint8Array = new Uint8Array ( secondArray ) ;
97+
98+ const result1 = concatUint8Array ( first , second ) ;
99+ const result2 = concatUint8Array ( second , first ) ;
100+
101+ expect ( result1 ) . toEqual ( new Uint8Array ( firstArray . concat ( secondArray ) ) ) ;
102+ expect ( result2 ) . toEqual ( new Uint8Array ( secondArray . concat ( firstArray ) ) ) ;
103+ } ) ;
104+ } ) ;
105+
106+ describe ( 'areUint8ArraysEqual' , ( ) => {
107+ it ( 'compares correctly equal arrays' , ( ) => {
108+ const first : Uint8Array = new Uint8Array ( [ 116 , 101 , 115 , 116 ] ) ;
109+ const second : Uint8Array = new Uint8Array ( [ 116 , 101 , 115 , 116 ] ) ;
110+
111+ const result1 = areUint8ArraysEqual ( first , first ) ;
112+ const result2 = areUint8ArraysEqual ( second , second ) ;
113+ const result3 = areUint8ArraysEqual ( first , second ) ;
114+
115+ expect ( result1 ) . toBeTruthy ( ) ;
116+ expect ( result2 ) . toBeTruthy ( ) ;
117+ expect ( result3 ) . toBeTruthy ( ) ;
118+ } ) ;
119+
120+ it ( 'compares correctly empty arrays' , ( ) => {
121+ const first : Uint8Array = new Uint8Array ( [ ] ) ;
122+ const second : Uint8Array = new Uint8Array ( [ ] ) ;
123+
124+ const result = areUint8ArraysEqual ( first , second ) ;
125+
126+ expect ( result ) . toBeTruthy ( ) ;
127+ } ) ;
128+
129+ it ( 'compares arrays of different length' , ( ) => {
130+ const first : Uint8Array = new Uint8Array ( [ 5 , 12 , 46 ] ) ;
131+ const second : Uint8Array = new Uint8Array ( [ 5 , 12 , 46 , 0 ] ) ;
132+
133+ const result1 = areUint8ArraysEqual ( first , second ) ;
134+ const result2 = areUint8ArraysEqual ( second , first ) ;
135+
136+ expect ( result1 ) . toBeFalsy ( ) ;
137+ expect ( result2 ) . toBeFalsy ( ) ;
138+ } ) ;
139+
140+ it ( 'compares different arrays' , ( ) => {
141+ const first : Uint8Array = new Uint8Array ( [ 1 , 2 , 3 ] ) ;
142+ const second : Uint8Array = new Uint8Array ( [ 4 , 5 , 6 ] ) ;
143+
144+ const result1 = areUint8ArraysEqual ( first , second ) ;
145+ const result2 = areUint8ArraysEqual ( second , first ) ;
146+
147+ expect ( result1 ) . toBeFalsy ( ) ;
148+ expect ( result2 ) . toBeFalsy ( ) ;
149+ } ) ;
150+ } ) ;
0 commit comments