-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-helpers.ts
More file actions
44 lines (41 loc) · 1.38 KB
/
test-helpers.ts
File metadata and controls
44 lines (41 loc) · 1.38 KB
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
import type { Logger } from "./simulation.ts";
import type { EntropySource } from "./entropy.ts";
export class ArrayLogger implements Logger {
readonly logs: string[] = [];
readonly errors: string[] = [];
log(...args: readonly unknown[]): void {
this.logs.push(args.map(String).join(" "));
}
error(...args: readonly unknown[]): void {
this.errors.push(args.map(String).join(" "));
}
}
export class FixedEntropySource implements EntropySource {
private readonly values: number[];
private index = 0;
constructor(values: number[]) {
this.values = values;
}
random(): number {
const v = this.values[this.index];
if (v === undefined) throw new Error(`FixedEntropySource exhausted at index ${this.index}`);
this.index++;
return v;
}
}
/** Like FixedEntropySource but also records the names passed to random(). */
export class SpyEntropySource implements EntropySource {
readonly calledNames: string[] = [];
private readonly values: number[];
private index = 0;
constructor(values: number[]) {
this.values = values;
}
random(name: string): number {
this.calledNames.push(name);
const v = this.values[this.index];
if (v === undefined) throw new Error(`SpyEntropySource exhausted at index ${this.index}`);
this.index++;
return v;
}
}