2
2
* (c) 2019 Micro:bit Educational Foundation and the microbit-fs contributors.
3
3
* SPDX-License-Identifier: MIT
4
4
*/
5
- import { bytesToStr , strToBytes } from '../common' ;
5
+ import {
6
+ bytesToStr ,
7
+ strToBytes ,
8
+ concatUint8Array ,
9
+ areUint8ArraysEqual ,
10
+ } from '../common' ;
6
11
7
- describe ( ` strToBytes` , ( ) => {
8
- it ( ` works with 1 byte characters` , ( ) => {
12
+ describe ( ' strToBytes' , ( ) => {
13
+ it ( ' works with 1 byte characters' , ( ) => {
9
14
const testString = 'test' ;
10
15
const testCodes = [ 116 , 101 , 115 , 116 ] ;
11
16
@@ -15,7 +20,8 @@ describe(`strToBytes`, () => {
15
20
expect ( tester . next ( ) . value ) . toEqual ( code ) ;
16
21
}
17
22
} ) ;
18
- it ( `works with 2 byte characters` , ( ) => {
23
+
24
+ it ( 'works with 2 byte characters' , ( ) => {
19
25
const testString = 'Ση' ;
20
26
const testCodes = [ 206 , 163 , 206 , 183 ] ;
21
27
@@ -25,7 +31,8 @@ describe(`strToBytes`, () => {
25
31
expect ( tester . next ( ) . value ) . toEqual ( code ) ;
26
32
}
27
33
} ) ;
28
- it ( `works with 3 byte characters` , ( ) => {
34
+
35
+ it ( 'works with 3 byte characters' , ( ) => {
29
36
const testString = '世' ;
30
37
const testCodes = [ 228 , 184 , 150 ] ;
31
38
@@ -37,20 +44,107 @@ describe(`strToBytes`, () => {
37
44
} ) ;
38
45
} ) ;
39
46
40
- describe ( ` bytesToStr` , ( ) => {
41
- it ( ` works with 1 byte characters` , ( ) => {
47
+ describe ( ' bytesToStr' , ( ) => {
48
+ it ( ' works with 1 byte characters' , ( ) => {
42
49
const testCodes : Uint8Array = new Uint8Array ( [ 116 , 101 , 115 , 116 ] ) ;
43
50
44
51
expect ( bytesToStr ( testCodes ) ) . toEqual ( 'test' ) ;
45
52
} ) ;
46
- it ( `works with 2 byte characters` , ( ) => {
53
+
54
+ it ( 'works with 2 byte characters' , ( ) => {
47
55
const testCodes : Uint8Array = new Uint8Array ( [ 206 , 163 , 206 , 183 ] ) ;
48
56
49
57
expect ( bytesToStr ( testCodes ) ) . toEqual ( 'Ση' ) ;
50
58
} ) ;
51
- it ( `works with 3 byte characters` , ( ) => {
59
+
60
+ it ( 'works with 3 byte characters' , ( ) => {
52
61
const testCodes : Uint8Array = new Uint8Array ( [ 228 , 184 , 150 ] ) ;
53
62
54
63
expect ( bytesToStr ( testCodes ) ) . toEqual ( '世' ) ;
55
64
} ) ;
56
65
} ) ;
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