Skip to content

Commit

Permalink
Strings
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Dec 15, 2021
1 parent f9fb930 commit ea47ab9
Showing 1 changed file with 47 additions and 14 deletions.
61 changes: 47 additions & 14 deletions packages/firestore/test/unit/index/ordered_code_writer.test.ts
Expand Up @@ -148,7 +148,21 @@ const NUMBER_TEST_CASES: Array<ValueTestCase<number>> = [
)
];

describe('Ordered Code Writer', () => {
const STRING_TEST_CASES: Array<ValueTestCase<string>> = [
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;
Expand All @@ -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<ValueTestCase<unknown>>) {
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<ValueTestCase<unknown>>) {
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,
Expand All @@ -194,7 +224,7 @@ describe('Ordered Code Writer', () => {
);
}
}
});
}
});

function compare(left: Uint8Array, right: Uint8Array): number {
Expand All @@ -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);
}
Expand Down

0 comments on commit ea47ab9

Please sign in to comment.