Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/linejs/base/thrift/readwrite/tmc.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { assertEquals } from "@std/assert";
import { TMoreCompactProtocol } from "./tmc.ts";

Deno.test("decodeZigZag matches standard thrift zigzag for positive values", () => {
const tmc = new TMoreCompactProtocol();
assertEquals(tmc.decodeZigZag(0), 0);
assertEquals(tmc.decodeZigZag(2), 1);
assertEquals(tmc.decodeZigZag(4), 2);
assertEquals(tmc.decodeZigZag(100), 50);
assertEquals(tmc.decodeZigZag(200), 100);
});

Deno.test("decodeZigZag matches standard thrift zigzag for negative values", () => {
const tmc = new TMoreCompactProtocol();
assertEquals(tmc.decodeZigZag(1), -1);
assertEquals(tmc.decodeZigZag(3), -2);
assertEquals(tmc.decodeZigZag(5), -3);
assertEquals(tmc.decodeZigZag(7), -4);
assertEquals(tmc.decodeZigZag(199), -100);
});

Deno.test("decodeZigZag round-trips with standard zigzag encode", () => {
const tmc = new TMoreCompactProtocol();
function zigzagEncode(n: number): number {
return (n << 1) ^ (n >> 31);
}
for (const v of [-1000, -100, -3, -2, -1, 0, 1, 2, 3, 100, 1000]) {
assertEquals(tmc.decodeZigZag(zigzagEncode(v)), v);
}
});
7 changes: 4 additions & 3 deletions packages/linejs/base/thrift/readwrite/tmc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ export class TMoreCompactProtocol {

// ZigZag decode
decodeZigZag(encoded: number): number {
return Number((BigInt(encoded) >> 1n) * ((encoded & 1) ? -1n : 1n));
const n = BigInt(encoded);
return Number((n >> 1n) ^ -(n & 1n));
}

// Read data by type
Expand Down Expand Up @@ -216,8 +217,8 @@ export class TMoreCompactProtocol {
}
} else if (typeId === 16) {
// STRING (string ID delta)
const temp = BigInt(this.readVarint());
const delta = (temp % 2n ? -1n : 1n) * (temp / 2n);
const raw = BigInt(this.readVarint());
const delta = (raw >> 1n) ^ -(raw & 1n);
const stringId = delta + this.lastStringId;
this.lastStringId = stringId;
value = String(stringId);
Expand Down
Loading