-
Notifications
You must be signed in to change notification settings - Fork 161
/
CachedKeyDecoder.test.ts
76 lines (60 loc) · 2.61 KB
/
CachedKeyDecoder.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import assert from "assert";
import { CachedKeyDecoder, KeyDecoder } from "../src/CachedKeyDecoder";
import { utf8EncodeJs, utf8Count } from "../src/utils/utf8";
function tryDecode(keyDecoder: KeyDecoder, str: string): string {
const byteLength = utf8Count(str);
const bytes = new Uint8Array(byteLength);
utf8EncodeJs(str, bytes, 0);
if (!keyDecoder.canBeCached(byteLength)) {
throw new Error("Unexpected precondition");
}
return keyDecoder.decode(bytes, 0, byteLength);
}
describe("CachedKeyDecoder", () => {
context("basic behavior", () => {
it("decodes a string", () => {
const decoder = new CachedKeyDecoder();
assert.deepStrictEqual(tryDecode(decoder, "foo"), "foo");
assert.deepStrictEqual(tryDecode(decoder, "foo"), "foo");
assert.deepStrictEqual(tryDecode(decoder, "foo"), "foo");
// console.dir(decoder, { depth: 100 });
});
it("decodes strings", () => {
const decoder = new CachedKeyDecoder();
assert.deepStrictEqual(tryDecode(decoder, "foo"), "foo");
assert.deepStrictEqual(tryDecode(decoder, "bar"), "bar");
assert.deepStrictEqual(tryDecode(decoder, "foo"), "foo");
// console.dir(decoder, { depth: 100 });
});
it("decodes strings with purging records", () => {
const decoder = new CachedKeyDecoder(16, 4);
for (let i = 0; i < 100; i++) {
assert.deepStrictEqual(tryDecode(decoder, "foo1"), "foo1");
assert.deepStrictEqual(tryDecode(decoder, "foo2"), "foo2");
assert.deepStrictEqual(tryDecode(decoder, "foo3"), "foo3");
assert.deepStrictEqual(tryDecode(decoder, "foo4"), "foo4");
assert.deepStrictEqual(tryDecode(decoder, "foo5"), "foo5");
}
// console.dir(decoder, { depth: 100 });
});
});
context("edge cases", () => {
// len=0 is not supported because it is just an empty string
it("decodes str with len=1", () => {
const decoder = new CachedKeyDecoder();
assert.deepStrictEqual(tryDecode(decoder, "f"), "f");
assert.deepStrictEqual(tryDecode(decoder, "a"), "a");
assert.deepStrictEqual(tryDecode(decoder, "f"), "f");
assert.deepStrictEqual(tryDecode(decoder, "a"), "a");
// console.dir(decoder, { depth: 100 });
});
it("decodes str with len=maxKeyLength", () => {
const decoder = new CachedKeyDecoder(1);
assert.deepStrictEqual(tryDecode(decoder, "f"), "f");
assert.deepStrictEqual(tryDecode(decoder, "a"), "a");
assert.deepStrictEqual(tryDecode(decoder, "f"), "f");
assert.deepStrictEqual(tryDecode(decoder, "a"), "a");
//console.dir(decoder, { depth: 100 });
});
});
});