-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.test.ts
184 lines (164 loc) · 4.77 KB
/
tests.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { expect, test, describe } from "bun:test";
import { DistinctIterationFilter, IterationFilter } from "./permutations";
import { binary_search } from "./filters";
import STATS from "./stats";
describe("permutations", () => {
const cases = [
{
in_: [[1, 2, 3], [1, 2, 3], [1, 2, 3]],
expected_: [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
},
{
in_: [[1, 2], [3, 4], [5, 6, 7]],
expected_: [[1, 3, 5], [1, 3, 6], [1, 3, 7], [1, 4, 5], [1, 4, 6], [1, 4, 7],
[2, 3, 5], [2, 3, 6], [2, 3, 7], [2, 4, 5], [2, 4, 6], [2, 4, 7]]
},
{
in_: [[1, 2], [3, 1], [4, 2]],
expected_: [[1, 3, 4], [1, 3, 2], [2, 3, 4], [2, 1, 4]]
}
]
let total_time = 0;
let total_cases = 0;
test.each(cases)('.permutations of $in_', ({ in_, expected_ }) => {
const t0 = performance.now();
const it = new DistinctIterationFilter<number>();
const s = it.generate_permutations(in_);
const output: number[][] = [];
while (true) {
const n = s.next();
if (n.done) break;
const v: number[] = n.value;
output.push(v.slice());
}
const t1 = performance.now();
total_time += ((t1 - t0)*1000);
++total_cases;
console.log(`Total calc_time=${total_time}us. average=${(total_time / total_cases).toFixed(2)}us`);
expect(output).toBeArray()
expect(output).toBeArrayOfSize(expected_.length);
expect(Bun.deepEquals(output, expected_)).toBeTrue();
});
});
describe("permutations_indistinct", () => {
class EmptyIterationFilter extends IterationFilter<number> {
stats_name: string = "Empty";
clear() {
}
delete(obj: number) {
}
try_add(obj: number): boolean {
return true;
}
is_permutation_approved(permutation: number[]): boolean {
return true;
}
}
function generate_array(from: number, to: number): number[] {
const arr: number[] = [];
for (let i = from; i <= to; i++) {
arr.push(i);
}
return arr;
}
function generate_arrays(arr: number[]): [number[][], bigint] {
const res: number[][] = [];
let total = BigInt(1)
for (let i = 0; i < arr.length; i++) {
res.push(generate_array(1, arr[i]));
total *= BigInt(arr[i]);
}
return [res, total];
}
const cases = [
{
in_: [[1, 2, 3], [1, 2, 3], [1, 2, 3]],
expected_: 27n
},
{
in_: [[1, 2], [3, 4], [5, 6, 7]],
expected_: 12n
},
{
in_: [[1, 2], [3, 1], [4, 2]],
expected_: 8n
},
{
in_: [generate_array(1, 20), generate_array(1, 25), generate_array(1, 25)],
expected_: 12500n
},
(function() {
const [array, total] = generate_arrays([25, 31, 77, 77, 3]);
return {
in_: array,
expected_: total
}
})()
]
test.each(cases)('.permutations of $in_', ({ in_, expected_ }) => {
STATS.reset();
const it = new EmptyIterationFilter();
const s = it.generate_permutations(in_);
let count = BigInt(0);
while (true) {
const n = s.next();
if (n.done) break;
count++;
}
STATS.print();
expect(count).toBe(expected_);
});
});
describe("binary_search", () => {
const cases = [
{
arr: [1, 3, 5, 7, 9, 11, 13],
target: 6,
expected_: 3
},
{
arr: [1, 3, 5, 7, 9, 11, 13],
target: 9,
expected_: 4
},
{
arr: [1, 3, 5, 7, 9, 11, 13],
target: 0,
expected_: 0
},
{
arr: [1, 3, 5, 7, 9, 11, 13],
target: 14,
expected_: -1
},
{
arr: [1, 3, 5, 7, 9, 11, 13],
target: 12,
expected_: 6
},
{
arr: [1, 1, 1, 2, 9, 11, 13],
target: 1,
expected_: 0
},
{
arr: [1, 1, 1, 2, 9, 11, 13],
target: 2,
expected_: 3
},
{
arr: [1, 1, 1, 2, 9, 11, 13],
target: 3,
expected_: 4
},
{
arr: [1, 1, 1, 2, 9, 11, 13],
target: 14,
expected_: -1
}
]
test.each(cases)('.permutations of $target', ({ arr, target, expected_ }) => {
const res = binary_search(arr, target);
expect(res).toBe(expected_);
});
});