-
Notifications
You must be signed in to change notification settings - Fork 1
/
power_set_test.ts
107 lines (95 loc) · 2.68 KB
/
power_set_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
import { assertEquals, assertStrictEquals, range } from "./test_deps.ts";
import { combinations } from "./combinations.ts";
import { combinations1, combinations2 } from "./combinations_test.ts";
import { powerSet } from "./power_set.ts";
Deno.test("n = 0", () => {
const actual = [...powerSet([])];
assertEquals(actual, [[]]);
});
Deno.test("n = 1", () => {
const actual = [...powerSet([1])];
assertEquals(actual, [[], [1]]);
});
Deno.test("n = 2", () => {
const actual = [...powerSet("ab")];
const expected = [[], ["a"], ["b"], ["a", "b"]];
assertEquals(actual, expected);
});
Deno.test("n = 3", () => {
const actual = [...powerSet([1, 2, 3])];
const expected = [
[],
[1],
[2],
[3],
[1, 2],
[1, 3],
[2, 3],
[1, 2, 3],
];
assertEquals(actual, expected);
});
for (let n = 0; n < 8; n++) {
const iterable = range(n);
Deno.test(`exp2(${n})`, () => {
const actual = [...powerSet(iterable)];
const expectedLength = exp2(n);
assertStrictEquals(actual.length, expectedLength);
});
}
for (let n = 0; n < 8; n++) {
const iterable = range(n);
Deno.test(`powerSet1([${iterable}])`, () => {
const actual = [...powerSet(iterable)];
const expected1 = [...powerSet1(iterable)];
assertEquals(actual, expected1);
});
}
for (let n = 0; n < 8; n++) {
const iterable = range(n);
Deno.test(`powerSet2([${iterable}])`, () => {
const actual = [...powerSet(iterable)];
const expected2 = [...powerSet2(iterable)];
assertEquals(actual, expected2);
});
}
for (let n = 0; n < 8; n++) {
const iterable = range(n);
Deno.test(`powerSet3([${iterable}])`, () => {
const actual = [...powerSet(iterable)];
const expected3 = [...powerSet3(iterable)];
assertEquals(actual, expected3);
});
}
/** Return the number of subsets of a set with `n` elements. */
function exp2(n: number): number {
if (n < 0 || !Number.isInteger(n)) {
throw RangeError("n must be a non-negative integer");
} else {
return Math.pow(2, n);
}
}
/** Equivalent to `powerSet` for testing. */
function* powerSet1<T>(iterable: Iterable<T>): Generator<T[]> {
const pool = [...iterable];
const n = pool.length;
for (let r = 0; r <= n; r++) {
yield* combinations(pool, r);
}
}
/** Equivalent to `powerSet` for testing. */
function* powerSet2<T>(iterable: Iterable<T>): Generator<T[]> {
const pool = [...iterable];
const n = pool.length;
for (let r = 0; r <= n; r++) {
yield* combinations1(pool, r);
}
}
/** Equivalent to `powerSet` for testing. */
function* powerSet3<T>(iterable: Iterable<T>): Generator<T[]> {
const pool = [...iterable];
const n = pool.length;
for (let r = 0; r <= n; r++) {
yield* combinations2(pool, r);
}
}