Skip to content

Commit d59b28a

Browse files
Merge
2 parents 29229d3 + 98b861a commit d59b28a

File tree

2 files changed

+56
-172
lines changed

2 files changed

+56
-172
lines changed

packages/firestore/src/index/ordered_code_writer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export function numberOfLeadingZerosInByte(x: number): number {
7777
/** Counts the number of leading zeros in the given byte array. */
7878
function numberOfLeadingZeros(bytes: Uint8Array): number {
7979
debugAssert(
80-
bytes.length == 8,
80+
bytes.length === 8,
8181
'Can only count leading zeros in 64-bit numbers'
8282
);
8383
let leadingZeros = 0;

packages/firestore/test/unit/index/ordered_code_writer.test.ts

Lines changed: 55 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -24,211 +24,87 @@ import {
2424
class ValueTestCase<T> {
2525
constructor(
2626
readonly val: T,
27-
readonly ascEncoding: Uint8Array,
28-
readonly descEncoding: Uint8Array
27+
readonly ascString: string,
28+
readonly descString: string
2929
) {}
3030
}
3131

3232
const NUMBER_TEST_CASES: Array<ValueTestCase<number>> = [
3333
new ValueTestCase(
3434
Number.NEGATIVE_INFINITY,
3535
// Note: This values are taken from the Android reference implementation
36-
new Uint8Array([0x07, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
37-
new Uint8Array([0xf8, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
36+
'070fffffffffffff',
37+
'f8f0000000000000'
3838
),
3939
new ValueTestCase(
4040
Number.MIN_SAFE_INTEGER,
41-
new Uint8Array([0x08, 0x3c, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
42-
new Uint8Array([0xf7, 0xc3, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])
43-
),
44-
new ValueTestCase(
45-
-2,
46-
new Uint8Array([0x08, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
47-
new Uint8Array([0xf7, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
48-
),
49-
new ValueTestCase(
50-
-1,
51-
new Uint8Array([0x08, 0x40, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
52-
new Uint8Array([0xf7, 0xbf, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
53-
),
54-
new ValueTestCase(
55-
-0.1,
56-
new Uint8Array([0x08, 0x40, 0x46, 0x66, 0x66, 0x66, 0x66, 0x66, 0x65]),
57-
new Uint8Array([0xf7, 0xbf, 0xb9, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a])
58-
),
59-
new ValueTestCase(
60-
-0.0,
61-
new Uint8Array([0x08, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
62-
new Uint8Array([0xf7, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
63-
),
64-
new ValueTestCase(
65-
0,
66-
new Uint8Array([0x08, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
67-
new Uint8Array([0xf7, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])
41+
'083cc0000000000000',
42+
'f7c33fffffffffffff'
6843
),
44+
new ValueTestCase(-2, '083fffffffffffffff', 'f7c000000000000000'),
45+
new ValueTestCase(-1, '08400fffffffffffff', 'f7bff0000000000000'),
46+
new ValueTestCase(-0.1, '084046666666666665', 'f7bfb999999999999a'),
47+
new ValueTestCase(-0.0, '087fffffffffffffff', 'f78000000000000000'),
48+
new ValueTestCase(0, '088000000000000000', 'f77fffffffffffffff'),
6949
new ValueTestCase(
7050
Number.MIN_VALUE,
71-
new Uint8Array([0x08, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]),
72-
new Uint8Array([0xf7, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe])
73-
),
74-
new ValueTestCase(
75-
0.1,
76-
new Uint8Array([0x08, 0xbf, 0xb9, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a]),
77-
new Uint8Array([0xf7, 0x40, 0x46, 0x66, 0x66, 0x66, 0x66, 0x66, 0x65])
78-
),
79-
new ValueTestCase(
80-
1,
81-
new Uint8Array([0x08, 0xbf, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
82-
new Uint8Array([0xf7, 0x40, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])
83-
),
84-
new ValueTestCase(
85-
2,
86-
new Uint8Array([0x08, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
87-
new Uint8Array([0xf7, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])
88-
),
89-
new ValueTestCase(
90-
4,
91-
new Uint8Array([0x08, 0xc0, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
92-
new Uint8Array([0xf7, 0x3f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])
93-
),
94-
new ValueTestCase(
95-
8,
96-
new Uint8Array([0x08, 0xc0, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
97-
new Uint8Array([0xf7, 0x3f, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])
98-
),
99-
new ValueTestCase(
100-
16,
101-
new Uint8Array([0x08, 0xc0, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
102-
new Uint8Array([0xf7, 0x3f, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])
103-
),
104-
new ValueTestCase(
105-
32,
106-
new Uint8Array([0x08, 0xc0, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
107-
new Uint8Array([0xf7, 0x3f, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])
108-
),
109-
new ValueTestCase(
110-
64,
111-
new Uint8Array([0x08, 0xc0, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
112-
new Uint8Array([0xf7, 0x3f, 0xaf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])
113-
),
114-
new ValueTestCase(
115-
128,
116-
new Uint8Array([0x08, 0xc0, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
117-
new Uint8Array([0xf7, 0x3f, 0x9f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])
118-
),
119-
new ValueTestCase(
120-
255,
121-
new Uint8Array([0x08, 0xc0, 0x6f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00]),
122-
new Uint8Array([0xf7, 0x3f, 0x90, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff])
123-
),
124-
new ValueTestCase(
125-
256,
126-
new Uint8Array([0x08, 0xc0, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
127-
new Uint8Array([0xf7, 0x3f, 0x8f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])
128-
),
129-
new ValueTestCase(
130-
257,
131-
new Uint8Array([0x08, 0xc0, 0x70, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00]),
132-
new Uint8Array([0xf7, 0x3f, 0x8f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff])
133-
),
51+
'088000000000000001',
52+
'f77ffffffffffffffe'
53+
),
54+
new ValueTestCase(0.1, '08bfb999999999999a', 'f74046666666666665'),
55+
new ValueTestCase(1, '08bff0000000000000', 'f7400fffffffffffff'),
56+
new ValueTestCase(2, '08c000000000000000', 'f73fffffffffffffff'),
57+
new ValueTestCase(4, '08c010000000000000', 'f73fefffffffffffff'),
58+
new ValueTestCase(8, '08c020000000000000', 'f73fdfffffffffffff'),
59+
new ValueTestCase(16, '08c030000000000000', 'f73fcfffffffffffff'),
60+
new ValueTestCase(32, '08c040000000000000', 'f73fbfffffffffffff'),
61+
new ValueTestCase(64, '08c050000000000000', 'f73fafffffffffffff'),
62+
new ValueTestCase(128, '08c060000000000000', 'f73f9fffffffffffff'),
63+
new ValueTestCase(255, '08c06fe00000000000', 'f73f901fffffffffff'),
64+
new ValueTestCase(256, '08c070000000000000', 'f73f8fffffffffffff'),
65+
new ValueTestCase(257, '08c070100000000000', 'f73f8fefffffffffff'),
13466
new ValueTestCase(
13567
Number.MAX_SAFE_INTEGER,
136-
new Uint8Array([0x08, 0xc3, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
137-
new Uint8Array([0xf7, 0x3c, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
68+
'08c33fffffffffffff',
69+
'f73cc0000000000000'
13870
),
13971
new ValueTestCase(
14072
Number.POSITIVE_INFINITY,
141-
new Uint8Array([0x08, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
142-
new Uint8Array([0xf7, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])
73+
'08fff0000000000000',
74+
'f7000fffffffffffff'
14375
),
144-
new ValueTestCase(
145-
Number.NaN,
146-
new Uint8Array([0x08, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
147-
new Uint8Array([0xf7, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])
148-
)
76+
new ValueTestCase(Number.NaN, '08fff8000000000000', 'f70007ffffffffffff')
14977
];
15078

15179
const STRING_TEST_CASES: Array<ValueTestCase<string>> = [
152-
new ValueTestCase(
153-
'',
154-
new Uint8Array([0x00, 0x01]),
155-
new Uint8Array([0xff, 0xfe])
156-
),
157-
new ValueTestCase(
158-
'\u0000',
159-
new Uint8Array([0x00, 0xff, 0x00, 0x01]),
160-
new Uint8Array([0xff, 0x00, 0xff, 0xfe])
161-
),
162-
new ValueTestCase(
163-
'\u0000\u0000',
164-
new Uint8Array([0x00, 0xff, 0x00, 0xff, 0x00, 0x01]),
165-
new Uint8Array([0xff, 0x00, 0xff, 0x00, 0xff, 0xfe])
166-
),
167-
new ValueTestCase(
168-
'abc',
169-
new Uint8Array([0x61, 0x62, 0x63, 0x00, 0x01]),
170-
new Uint8Array([0x9e, 0x9d, 0x9c, 0xff, 0xfe])
171-
),
80+
new ValueTestCase('', '0001', 'fffe'),
81+
new ValueTestCase('\u0000', '00ff0001', 'ff00fffe'),
82+
new ValueTestCase('\u0000\u0000', '00ff00ff0001', 'ff00ff00fffe'),
83+
new ValueTestCase('abc', '6162630001', '9e9d9cfffe'),
17284
new ValueTestCase(
17385
'xy¢z𠜎€𠜱あ𠝹',
174-
new Uint8Array([
175-
0x78, 0x79, 0xc2, 0xa2, 0x7a, 0xf0, 0xa0, 0x9c, 0x8e, 0xe2, 0x82, 0xac,
176-
0xf0, 0xa0, 0x9c, 0xb1, 0xe3, 0x81, 0x82, 0xf0, 0xa0, 0x9d, 0xb9, 0x00,
177-
0x01
178-
]),
179-
new Uint8Array([
180-
0x87, 0x86, 0x3d, 0x5d, 0x85, 0x0f, 0x5f, 0x63, 0x71, 0x1d, 0x7d, 0x53,
181-
0x0f, 0x5f, 0x63, 0x4e, 0x1c, 0x7e, 0x7d, 0x0f, 0x5f, 0x62, 0x46, 0xff,
182-
0xfe
183-
])
86+
'7879c2a27af0a09c8ee282acf0a09cb1e38182f0a09db90001',
87+
'87863d5d850f5f63711d7d530f5f634e1c7e7d0f5f6246fffe'
18488
),
18589
new ValueTestCase(
18690
'¬˚ß∂∆ç',
187-
new Uint8Array([
188-
0xc2, 0xac, 0xcb, 0x9a, 0xc3, 0x9f, 0xe2, 0x88, 0x82, 0xe2, 0x88, 0x86,
189-
0xc3, 0xa7, 0x00, 0x01
190-
]),
191-
new Uint8Array([
192-
0x3d, 0x53, 0x34, 0x65, 0x3c, 0x60, 0x1d, 0x77, 0x7d, 0x1d, 0x77, 0x79,
193-
0x3c, 0x58, 0xff, 0xfe
194-
])
91+
'c2accb9ac39fe28882e28886c3a70001',
92+
'3d5334653c601d777d1d77793c58fffe'
19593
),
19694
new ValueTestCase(
19795
'œ∑´´ß™£',
198-
new Uint8Array([
199-
0xc5, 0x93, 0xe2, 0x88, 0x91, 0xc2, 0xb4, 0xc2, 0xb4, 0xc3, 0x9f, 0xe2,
200-
0x84, 0xa2, 0xc2, 0xa3, 0x00, 0x01
201-
]),
202-
new Uint8Array([
203-
0x3a, 0x6c, 0x1d, 0x77, 0x6e, 0x3d, 0x4b, 0x3d, 0x4b, 0x3c, 0x60, 0x1d,
204-
0x7b, 0x5d, 0x3d, 0x5c, 0xff, 0xfe
205-
])
96+
'c593e28891c2b4c2b4c39fe284a2c2a30001',
97+
'3a6c1d776e3d4b3d4b3c601d7b5d3d5cfffe'
20698
),
20799
new ValueTestCase(
208100
'πåçasdl߬µœ∑âsldalskdåßµ∂π',
209-
new Uint8Array([
210-
0xcf, 0x80, 0xc3, 0xa5, 0xc3, 0xa7, 0x61, 0x73, 0x64, 0x6c, 0xc3, 0x9f,
211-
0xc2, 0xac, 0xc2, 0xb5, 0xc5, 0x93, 0xe2, 0x88, 0x91, 0xc3, 0xa2, 0x73,
212-
0x6c, 0x64, 0x61, 0x6c, 0x73, 0x6b, 0x64, 0xc3, 0xa5, 0xc3, 0x9f, 0xc2,
213-
0xb5, 0xe2, 0x88, 0x82, 0xcf, 0x80, 0x00, 0x01
214-
]),
215-
new Uint8Array([
216-
0x30, 0x7f, 0x3c, 0x5a, 0x3c, 0x58, 0x9e, 0x8c, 0x9b, 0x93, 0x3c, 0x60,
217-
0x3d, 0x53, 0x3d, 0x4a, 0x3a, 0x6c, 0x1d, 0x77, 0x6e, 0x3c, 0x5d, 0x8c,
218-
0x93, 0x9b, 0x9e, 0x93, 0x8c, 0x94, 0x9b, 0x3c, 0x5a, 0x3c, 0x60, 0x3d,
219-
0x4a, 0x1d, 0x77, 0x7d, 0x30, 0x7f, 0xff, 0xfe
220-
])
101+
'cf80c3a5c3a76173646cc39fc2acc2b5c593e28891c3a2736c64616c736b64c3a5c39fc2b5e28882cf800001',
102+
'307f3c5a3c589e8c9b933c603d533d4a3a6c1d776e3c5d8c939b9e938c949b3c5a3c603d4a1d777d307ffffe'
221103
),
222104
new ValueTestCase(
223105
'†¥¬´´`',
224-
new Uint8Array([
225-
0xe2, 0x80, 0xa0, 0xc2, 0xa5, 0xc2, 0xac, 0xc2, 0xb4, 0xc2, 0xb4, 0x60,
226-
0x00, 0x01
227-
]),
228-
new Uint8Array([
229-
0x1d, 0x7f, 0x5f, 0x3d, 0x5a, 0x3d, 0x53, 0x3d, 0x4b, 0x3d, 0x4b, 0x9f,
230-
0xff, 0xfe
231-
])
106+
'e280a0c2a5c2acc2b4c2b4600001',
107+
'1d7f5f3d5a3d533d4b3d4b9ffffe'
232108
)
233109
];
234110

@@ -267,11 +143,11 @@ describe('Ordered Code Writer', () => {
267143
for (let i = 0; i < testCases.length; ++i) {
268144
const bytes = getBytes(testCases[i].val);
269145
expect(bytes.asc).to.deep.equal(
270-
testCases[i].ascEncoding,
146+
fromHex(testCases[i].ascString),
271147
'Ascending for ' + testCases[i].val
272148
);
273149
expect(bytes.desc).to.deep.equal(
274-
testCases[i].descEncoding,
150+
fromHex(testCases[i].descString),
275151
'Descending for ' + testCases[i].val
276152
);
277153
}
@@ -297,6 +173,14 @@ describe('Ordered Code Writer', () => {
297173
}
298174
});
299175

176+
function fromHex(hexString: string): Uint8Array {
177+
const bytes = new Uint8Array(hexString.length / 2);
178+
for (let c = 0; c < hexString.length; c += 2) {
179+
bytes[c / 2] = parseInt(hexString.substr(c, 2), 16);
180+
}
181+
return bytes;
182+
}
183+
300184
function compare(left: Uint8Array, right: Uint8Array): number {
301185
for (let i = 0; i < Math.min(left.length, right.length); ++i) {
302186
if (left[i] < right[i]) {

0 commit comments

Comments
 (0)