@@ -148,7 +148,21 @@ const NUMBER_TEST_CASES: Array<ValueTestCase<number>> = [
148
148
)
149
149
] ;
150
150
151
- describe ( 'Ordered Code Writer' , ( ) => {
151
+ const STRING_TEST_CASES : Array < ValueTestCase < string > > = [
152
+ new ValueTestCase (
153
+ "" ,
154
+ // Note: This values are taken from the Android reference implementation
155
+ new Uint8Array ( [ 0x00 , 0x01 ] ) ,
156
+ new Uint8Array ( [ 0xff , 0xfe ] )
157
+ ) ,
158
+ new ValueTestCase (
159
+ "a" ,
160
+ new Uint8Array ( [ 0x61 , 0x00 , 0x01 ] ) ,
161
+ new Uint8Array ( [ 0x9e , 0xff , 0xfe ] )
162
+ ) ,
163
+ ] ;
164
+
165
+ describe . only ( 'Ordered Code Writer' , ( ) => {
152
166
it ( 'computes number of leading zeros' , ( ) => {
153
167
for ( let i = 0 ; i < 0xff ; ++ i ) {
154
168
let zeros = 0 ;
@@ -164,25 +178,41 @@ describe('Ordered Code Writer', () => {
164
178
} ) ;
165
179
166
180
it ( 'converts numbers to bits' , ( ) => {
167
- for ( let i = 0 ; i < NUMBER_TEST_CASES . length ; ++ i ) {
168
- const bytes = getBytes ( NUMBER_TEST_CASES [ i ] . val ) ;
181
+ verifyEncoding ( NUMBER_TEST_CASES ) ;
182
+ } ) ;
183
+
184
+ it ( 'orders numbers correctly' , ( ) => {
185
+ verifyOrdering ( NUMBER_TEST_CASES ) ;
186
+ } ) ;
187
+
188
+ it ( 'converts strings to bits' , ( ) => {
189
+ verifyEncoding ( STRING_TEST_CASES ) ;
190
+ } ) ;
191
+
192
+ it ( 'orders strings correctly' , ( ) => {
193
+ verifyOrdering ( STRING_TEST_CASES ) ;
194
+ } ) ;
195
+
196
+ function verifyEncoding ( testCases : Array < ValueTestCase < unknown > > ) {
197
+ for ( let i = 0 ; i < testCases . length ; ++ i ) {
198
+ const bytes = getBytes ( testCases [ i ] . val ) ;
169
199
expect ( bytes . asc ) . to . deep . equal (
170
- NUMBER_TEST_CASES [ i ] . ascEncoding ,
171
- 'Ascending for ' + NUMBER_TEST_CASES [ i ] . val
200
+ testCases [ i ] . ascEncoding ,
201
+ 'Ascending for ' + testCases [ i ] . val
172
202
) ;
173
203
expect ( bytes . desc ) . to . deep . equal (
174
- NUMBER_TEST_CASES [ i ] . descEncoding ,
175
- 'Descending for ' + NUMBER_TEST_CASES [ i ] . val
204
+ testCases [ i ] . descEncoding ,
205
+ 'Descending for ' + testCases [ i ] . val
176
206
) ;
177
207
}
178
- } ) ;
208
+ }
179
209
180
- it ( 'orders numbers correctly' , ( ) => {
181
- for ( let i = 0 ; i < NUMBER_TEST_CASES . length ; ++ i ) {
182
- for ( let j = i ; j < NUMBER_TEST_CASES . length ; ++ j ) {
183
- const left = NUMBER_TEST_CASES [ i ] . val ;
210
+ function verifyOrdering ( testCases : Array < ValueTestCase < unknown > > ) {
211
+ for ( let i = 0 ; i < testCases . length ; ++ i ) {
212
+ for ( let j = i ; j < testCases . length ; ++ j ) {
213
+ const left = testCases [ i ] . val ;
184
214
const leftBytes = getBytes ( left ) ;
185
- const right = NUMBER_TEST_CASES [ j ] . val ;
215
+ const right = testCases [ j ] . val ;
186
216
const rightBytes = getBytes ( right ) ;
187
217
expect ( compare ( leftBytes . asc , rightBytes . asc ) ) . to . equal (
188
218
i === j ? 0 : - 1 ,
@@ -194,7 +224,7 @@ describe('Ordered Code Writer', () => {
194
224
) ;
195
225
}
196
226
}
197
- } ) ;
227
+ }
198
228
} ) ;
199
229
200
230
function compare ( left : Uint8Array , right : Uint8Array ) : number {
@@ -215,6 +245,9 @@ function getBytes(val: unknown): { asc: Uint8Array; desc: Uint8Array } {
215
245
if ( typeof val === 'number' ) {
216
246
ascWriter . writeNumberAscending ( val ) ;
217
247
descWriter . writeNumberDescending ( val ) ;
248
+ } else if ( typeof val === 'string' ) {
249
+ ascWriter . writeUtf8Ascending ( val ) ;
250
+ descWriter . writeUtf8Descending ( val ) ;
218
251
} else {
219
252
throw new Error ( 'Encoding not yet supported for ' + val ) ;
220
253
}
0 commit comments