Skip to content

Commit

Permalink
Add tests to check for decoded symbols validity
Browse files Browse the repository at this point in the history
  • Loading branch information
magnified103 committed Nov 13, 2024
1 parent c49f7b2 commit 2cc0ce7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
8 changes: 5 additions & 3 deletions packages/node/src/riblt/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ export class Decoder<T extends SourceSymbol> extends CodingPrefix<T> {
this.extendPrefix(index + 1);
this.codedSymbols[index].apply(localSymbol, localSymbol.count);
this.codedSymbols[index].apply(remoteSymbol, -remoteSymbol.count);
this.computePrefix(index + 1);
this.modifiedCodedSymbols.push(index);
if (!this.visited[index]) {
this.visited[index] = true;
this.modifiedCodedSymbols.push(index);
}
}

maps(index: number, hashedSymbol: HashedSymbol<T>, direction: number): void {
if (!this.isDecoded[index]) {
// console.log(`+ map ${hashedSymbol}, dir=${direction} to index ${index}`)
this.codedSymbols[index].apply(hashedSymbol, direction);
if (!this.visited[index]) {
this.visited[index] = true;
Expand All @@ -60,6 +61,7 @@ export class Decoder<T extends SourceSymbol> extends CodingPrefix<T> {
}

tryDecode(): boolean {
this.computePrefix(this.codedSymbols.length);
while (this.modifiedCodedSymbols.length > 0) {
const candidates: number[] = [];
for (const index of this.modifiedCodedSymbols) {
Expand Down
46 changes: 32 additions & 14 deletions packages/node/tests/riblt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class VertexSymbolFactory extends SymbolFactory<VertexSymbol> {
describe("RIBLT test", async () => {
const factory = new VertexSymbolFactory();

test.each([10, 20, 40, 100, 1000, 10000, 50000, 100000, 300000])(
test.each([10, 20, 40, 100, 1000, 10000, 50000, 100000])(
"d=%i",
async (d) => {
const nlocal = d >> 1;
Expand All @@ -67,44 +67,62 @@ describe("RIBLT test", async () => {
const remoteEncoder = new Encoder(factory);
const localDecoder = new Decoder(factory);

const localSymbols: VertexSymbol[] = [];
const remoteSymbols: VertexSymbol[] = [];
enum SymbolState {
Local = 0,
Remote = 1,
Common = 2,
};
const symbolState: SymbolState[] = [];

for (let i = 0; i < nlocal; i++) {
const localSymbol = factory.newTestSymbol(symbolIndex++);
localSymbols.push(localSymbol);
const localSymbol = factory.newTestSymbol(symbolIndex);
symbolState.push(SymbolState.Local);
localEncoder.addSymbol(localSymbol);
symbolIndex++;
}
for (let i = 0; i < nremote; i++) {
const remoteSymbol = factory.newTestSymbol(symbolIndex++);
remoteSymbols.push(remoteSymbol);
const remoteSymbol = factory.newTestSymbol(symbolIndex);
symbolState.push(SymbolState.Remote);
remoteEncoder.addSymbol(remoteSymbol);
symbolIndex++;
}
for (let i = 0; i < ncommon; i++) {
const localSymbol = factory.newTestSymbol(symbolIndex++);
const localSymbol = factory.newTestSymbol(symbolIndex);
const remoteSymbol = factory.cloneSource(localSymbol);
symbolState.push(SymbolState.Common);
localEncoder.addSymbol(localSymbol);
remoteEncoder.addSymbol(remoteSymbol);
symbolIndex++;
}

let sequenceSize = 0;
do {
sequenceSize++;
localEncoder.producePrefix(sequenceSize);
remoteEncoder.producePrefix(sequenceSize);
// console.log(`localEncoder[${sequenceSize - 1}]: ${localEncoder.codedSymbols[sequenceSize - 1]}`);
// console.log(`remoteEncoder[${sequenceSize - 1}]: ${remoteEncoder.codedSymbols[sequenceSize - 1]}`);
localDecoder.addCodedSymbol(
sequenceSize - 1,
localEncoder.codedSymbols[sequenceSize - 1],
remoteEncoder.codedSymbols[sequenceSize - 1],
);
// console.log(`localDecoder[${sequenceSize - 1}]: ${localDecoder.codedSymbols[sequenceSize - 1]}`);
} while (!localDecoder.tryDecode());

// console.log(localDecoder.decodedLocalSymbols);
// console.log(localDecoder.decodedRemoteSymbols);
// console.log(localDecoder.remaining);
const visited = new Array<boolean>(symbolIndex).fill(false);

for (const symbol of localDecoder.decodedLocalSymbols) {
expect(Number.isInteger(symbol.data)).toBe(true);
expect(symbol.data >= 0 && symbol.data < symbolIndex).toBe(true);
expect(visited[symbol.data]).toBe(false);
visited[symbol.data] = true;
expect(symbolState[symbol.data]).toBe(SymbolState.Local);
}
for (const symbol of localDecoder.decodedRemoteSymbols) {
expect(Number.isInteger(symbol.data)).toBe(true);
expect(symbol.data >= 0 && symbol.data < symbolIndex).toBe(true);
expect(visited[symbol.data]).toBe(false);
visited[symbol.data] = true;
expect(symbolState[symbol.data]).toBe(SymbolState.Remote);
}

console.log(`${sequenceSize / d} symbols/diff`);
},
Expand Down

0 comments on commit 2cc0ce7

Please sign in to comment.