Skip to content

Commit ea47ab9

Browse files
Strings
1 parent f9fb930 commit ea47ab9

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

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

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,21 @@ const NUMBER_TEST_CASES: Array<ValueTestCase<number>> = [
148148
)
149149
];
150150

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', () => {
152166
it('computes number of leading zeros', () => {
153167
for (let i = 0; i < 0xff; ++i) {
154168
let zeros = 0;
@@ -164,25 +178,41 @@ describe('Ordered Code Writer', () => {
164178
});
165179

166180
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);
169199
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
172202
);
173203
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
176206
);
177207
}
178-
});
208+
}
179209

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;
184214
const leftBytes = getBytes(left);
185-
const right = NUMBER_TEST_CASES[j].val;
215+
const right = testCases[j].val;
186216
const rightBytes = getBytes(right);
187217
expect(compare(leftBytes.asc, rightBytes.asc)).to.equal(
188218
i === j ? 0 : -1,
@@ -194,7 +224,7 @@ describe('Ordered Code Writer', () => {
194224
);
195225
}
196226
}
197-
});
227+
}
198228
});
199229

200230
function compare(left: Uint8Array, right: Uint8Array): number {
@@ -215,6 +245,9 @@ function getBytes(val: unknown): { asc: Uint8Array; desc: Uint8Array } {
215245
if (typeof val === 'number') {
216246
ascWriter.writeNumberAscending(val);
217247
descWriter.writeNumberDescending(val);
248+
} else if (typeof val === 'string') {
249+
ascWriter.writeUtf8Ascending(val);
250+
descWriter.writeUtf8Descending(val);
218251
} else {
219252
throw new Error('Encoding not yet supported for ' + val);
220253
}

0 commit comments

Comments
 (0)