Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
magnified103 committed Nov 12, 2024
1 parent 1e1b9fc commit 2e30bd9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 31 deletions.
18 changes: 14 additions & 4 deletions packages/node/src/riblt/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import { CodingPrefix } from "./encoder.js";


export class Decoder<T extends SourceSymbol> extends CodingPrefix<T> {
decodedSymbols: T[];
decodedLocalSymbols: T[];
decodedRemoteSymbols: T[];
isDecoded: boolean[];
remaining: number;
pureSymbols: CodedSymbol<T>[];

constructor(sourceSymbolFactory: SourceSymbolFactory<T>) {
super(sourceSymbolFactory);
this.decodedSymbols = [];
this.decodedLocalSymbols = [];
this.decodedRemoteSymbols = [];
this.isDecoded = [];
this.remaining = 0;
this.pureSymbols = [];
Expand Down Expand Up @@ -42,12 +44,18 @@ export class Decoder<T extends SourceSymbol> extends CodingPrefix<T> {
tryDecode(): boolean {
while (this.pureSymbols.length > 0) {
const symbol = this.pureSymbols.pop() as CodedSymbol<T>;
// console.log(`pure symbol: ${symbol.sum.data} ${symbol.count}`);
console.log(`pure symbol: ${symbol.sum.data} ${symbol.count}`);
if (symbol.isZero()) {
continue;
}
const decodedSymbol = this.sourceSymbolFactory.clone(symbol.sum)
this.decodedSymbols.push(decodedSymbol);
if (symbol.count === 1) {
this.decodedLocalSymbols.push(decodedSymbol);
} else if (symbol.count === -1) {
this.decodedRemoteSymbols.push(decodedSymbol);
} else {
throw Error(`Invalid pure symbol ${symbol.sum.data} ${symbol.count}`);
}

const mapping = new RandomMapping(symbol.checksum, 0);
while (mapping.lastIdx < this.codedSymbols.length) {
Expand All @@ -64,8 +72,10 @@ export class Decoder<T extends SourceSymbol> extends CodingPrefix<T> {
mapping.nextIndex();
}
this.addHashedSymbolWithMapping(new HashedSymbol<T>(decodedSymbol), mapping, -symbol.count);
console.log('abc')
}

console.log(this.remaining);
return this.remaining === 0;
}
}
2 changes: 1 addition & 1 deletion packages/node/src/riblt/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class CodingPrefix<T extends SourceSymbol> {
this.addHashedSymbol(hashedSymbol);
}

addHashedSymbol(hashedSymbol: HashedSymbol<T>, direction = 1): void {
protected addHashedSymbol(hashedSymbol: HashedSymbol<T>, direction = 1): void {
const mapping = new RandomMapping(hashedSymbol.checksum, 0);
this.addHashedSymbolWithMapping(hashedSymbol, mapping, direction);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/riblt/symbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class HashedSymbol<T extends SourceSymbol> {
isPure(): boolean {
const checksum = this.sum.hash();
if (checksum.length !== this.checksum.length) {
return false;
throw Error("Checksum length mismatch");
}
for (let i = 0; i < checksum.length; i++) {
if (checksum[i] !== this.checksum[i]) {
Expand Down
66 changes: 41 additions & 25 deletions packages/node/tests/riblt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,42 +45,58 @@ class VertexSymbolFactory implements SourceSymbolFactory<VertexSymbol> {
clone(s: VertexSymbol): VertexSymbol {
return new VertexSymbol(new Uint8Array(s.data));
}

newTestSymbol(i): VertexSymbol {
const data = new Uint32Array(8);
data[0] = i;
return new VertexSymbol(new Uint8Array(data.buffer));
}
}


describe("RIBLT test", async () => {
const factory = new VertexSymbolFactory();
const v0 = factory.empty();
const v1 = factory.empty();
const v2 = factory.empty();

v0.data[0] = 1;
v1.data[0] = 2;
v2.data[0] = 4;

const aliceEncoder = new Encoder(factory);
const bobEncoder = new Encoder(factory);
test.each([4])("d=%i", async (d) => {
const nlocal = d >> 1;
const nremote = d >> 1;
const ncommon = d;

aliceEncoder.addSymbol(v0);
aliceEncoder.addSymbol(v2);
let symbolIndex = 0;

bobEncoder.addSymbol(v1);
bobEncoder.addSymbol(v2);
const localEncoder = new Encoder(factory);
const remoteEncoder = new Encoder(factory);
const localDecoder = new Decoder(factory);

aliceEncoder.extendPrefix(10);
bobEncoder.extendPrefix(10);
const localSymbols: VertexSymbol[] = [];
const remoteSymbols: VertexSymbol[] = [];

const bobDecoder = new Decoder(factory);

for (let i = 0; i < 10; i++) {
// console.log(`${i}: ${aliceEncoder.codedSymbols[i].sum.data} ${aliceEncoder.codedSymbols[i].count} ${bobEncoder.codedSymbols[i].sum.data} ${bobEncoder.codedSymbols[i].count}`);
bobDecoder.applyCodedSymbol(i, aliceEncoder.codedSymbols[i], bobEncoder.codedSymbols[i]);
}
for (let i = 0; i < nlocal; i++) {
const localSymbol = factory.newTestSymbol(symbolIndex++);
localSymbols.push(localSymbol);
localEncoder.addSymbol(localSymbol);
}
for (let i = 0; i < nremote; i++) {
const remoteSymbol = factory.newTestSymbol(symbolIndex++);
remoteSymbols.push(remoteSymbol);
remoteEncoder.addSymbol(remoteSymbol);
}
for (let i = 0; i < ncommon; i++) {
const localSymbol = factory.newTestSymbol(symbolIndex++);
const remoteSymbol = factory.clone(localSymbol);
localEncoder.addSymbol(localSymbol);
remoteEncoder.addSymbol(remoteSymbol);
}

// for (let i = 0; i < 10; i++) {
// console.log(`Decoded: ${bobDecoder.codedSymbols[i].sum.data} ${bobDecoder.codedSymbols[i].count}`);
// }
let sequenceSize = 0;
do {
sequenceSize++;
localEncoder.extendPrefix(sequenceSize);
remoteEncoder.extendPrefix(sequenceSize);
localDecoder.applyCodedSymbol(sequenceSize - 1, localEncoder.codedSymbols[sequenceSize - 1], remoteEncoder.codedSymbols[sequenceSize - 1]);
} while (!localDecoder.tryDecode());

expect(bobDecoder.tryDecode()).toBe(true);
console.log(bobDecoder.decodedSymbols);
console.log(sequenceSize);
});
});

0 comments on commit 2e30bd9

Please sign in to comment.