From 2cc0ce77844d1594ea061d523547d2e959253ddc Mon Sep 17 00:00:00 2001 From: Nguyen Viet Dung <29406816+magnified103@users.noreply.github.com> Date: Wed, 13 Nov 2024 23:30:17 +0700 Subject: [PATCH] Add tests to check for decoded symbols validity --- packages/node/src/riblt/decoder.ts | 8 ++++-- packages/node/tests/riblt.test.ts | 46 +++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/packages/node/src/riblt/decoder.ts b/packages/node/src/riblt/decoder.ts index 02000bc35..76e6cb502 100644 --- a/packages/node/src/riblt/decoder.ts +++ b/packages/node/src/riblt/decoder.ts @@ -44,13 +44,14 @@ export class Decoder extends CodingPrefix { 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, 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; @@ -60,6 +61,7 @@ export class Decoder extends CodingPrefix { } tryDecode(): boolean { + this.computePrefix(this.codedSymbols.length); while (this.modifiedCodedSymbols.length > 0) { const candidates: number[] = []; for (const index of this.modifiedCodedSymbols) { diff --git a/packages/node/tests/riblt.test.ts b/packages/node/tests/riblt.test.ts index b7691f64b..b8aa9dca4 100644 --- a/packages/node/tests/riblt.test.ts +++ b/packages/node/tests/riblt.test.ts @@ -54,7 +54,7 @@ class VertexSymbolFactory extends SymbolFactory { 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; @@ -67,24 +67,32 @@ 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; @@ -92,19 +100,29 @@ describe("RIBLT test", async () => { 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(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`); },