diff --git a/packages/firestore/test/unit/index/ordered_code_writer.test.ts b/packages/firestore/test/unit/index/ordered_code_writer.test.ts index 0da4c4dbf08..c404b9d45fb 100644 --- a/packages/firestore/test/unit/index/ordered_code_writer.test.ts +++ b/packages/firestore/test/unit/index/ordered_code_writer.test.ts @@ -148,7 +148,21 @@ const NUMBER_TEST_CASES: Array> = [ ) ]; -describe('Ordered Code Writer', () => { +const STRING_TEST_CASES: Array> = [ + new ValueTestCase( + "", + // Note: This values are taken from the Android reference implementation + new Uint8Array([0x00, 0x01]), + new Uint8Array([0xff, 0xfe]) + ), + new ValueTestCase( + "a", + new Uint8Array([0x61, 0x00, 0x01]), + new Uint8Array([0x9e, 0xff, 0xfe]) + ), +]; + +describe.only('Ordered Code Writer', () => { it('computes number of leading zeros', () => { for (let i = 0; i < 0xff; ++i) { let zeros = 0; @@ -164,25 +178,41 @@ describe('Ordered Code Writer', () => { }); it('converts numbers to bits', () => { - for (let i = 0; i < NUMBER_TEST_CASES.length; ++i) { - const bytes = getBytes(NUMBER_TEST_CASES[i].val); + verifyEncoding(NUMBER_TEST_CASES); + }); + + it('orders numbers correctly', () => { + verifyOrdering(NUMBER_TEST_CASES); + }); + + it('converts strings to bits', () => { + verifyEncoding(STRING_TEST_CASES); + }); + + it('orders strings correctly', () => { + verifyOrdering(STRING_TEST_CASES); + }); + + function verifyEncoding(testCases: Array>) { + for (let i = 0; i < testCases.length; ++i) { + const bytes = getBytes(testCases[i].val); expect(bytes.asc).to.deep.equal( - NUMBER_TEST_CASES[i].ascEncoding, - 'Ascending for ' + NUMBER_TEST_CASES[i].val + testCases[i].ascEncoding, + 'Ascending for ' + testCases[i].val ); expect(bytes.desc).to.deep.equal( - NUMBER_TEST_CASES[i].descEncoding, - 'Descending for ' + NUMBER_TEST_CASES[i].val + testCases[i].descEncoding, + 'Descending for ' + testCases[i].val ); } - }); + } - it('orders numbers correctly', () => { - for (let i = 0; i < NUMBER_TEST_CASES.length; ++i) { - for (let j = i; j < NUMBER_TEST_CASES.length; ++j) { - const left = NUMBER_TEST_CASES[i].val; + function verifyOrdering(testCases: Array>) { + for (let i = 0; i < testCases.length; ++i) { + for (let j = i; j < testCases.length; ++j) { + const left = testCases[i].val; const leftBytes = getBytes(left); - const right = NUMBER_TEST_CASES[j].val; + const right = testCases[j].val; const rightBytes = getBytes(right); expect(compare(leftBytes.asc, rightBytes.asc)).to.equal( i === j ? 0 : -1, @@ -194,7 +224,7 @@ describe('Ordered Code Writer', () => { ); } } - }); + } }); function compare(left: Uint8Array, right: Uint8Array): number { @@ -215,6 +245,9 @@ function getBytes(val: unknown): { asc: Uint8Array; desc: Uint8Array } { if (typeof val === 'number') { ascWriter.writeNumberAscending(val); descWriter.writeNumberDescending(val); + } else if (typeof val === 'string') { + ascWriter.writeUtf8Ascending(val); + descWriter.writeUtf8Descending(val); } else { throw new Error('Encoding not yet supported for ' + val); }