Skip to content

Commit 1735682

Browse files
🔍 test: Clean up.
1 parent 143a527 commit 1735682

File tree

3 files changed

+97
-86
lines changed

3 files changed

+97
-86
lines changed

package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,9 @@
7777
},
7878
"dependencies": {},
7979
"devDependencies": {
80-
"@aureooms/js-binomial-heap": "^11.0.0",
81-
"@aureooms/js-functools": "^2.0.3",
80+
"@aureooms/js-itertools": "^4.1.0",
8281
"@babel/cli": "7.11.6",
8382
"@babel/core": "7.11.6",
84-
"@babel/polyfill": "7.11.5",
8583
"@babel/preset-env": "7.11.5",
8684
"@babel/register": "7.11.5",
8785
"ava": "3.12.1",

test/src/exact/blossom.js

+77-66
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,127 @@
11
import test from 'ava' ;
2+
import {enumerate} from '@aureooms/js-itertools';
23

34
import blossom from '../../../src/exact/blossom' ;
45

5-
function macro ( t , alg , callback ) {
6-
callback(alg, t);
6+
const macro = ( t , algorithm , edges , maxCardinality, expected ) => {
7+
const input = edges.map(edge => edge.slice()); // deepcopy
8+
const result = algorithm(edges, maxCardinality);
9+
t.deepEqual(expected, result);
10+
t.deepEqual(input, edges);
711
}
812

9-
const tests = {
13+
macro.title = (title, algorithm, edges, maxCardinality, expected) => title || `${algorithm.name}(${JSON.stringify(edges)}, ${maxCardinality}) = ${JSON.stringify(expected)}`;
1014

11-
test10_empty : (alg,t) => {
12-
// empty input graph
13-
t.deepEqual(alg([]), []);
14-
},
15-
test11_singleedge : (alg,t) => {
15+
const tests = {
16+
// empty input graph
17+
test10_empty : { edges: [], expected: []},
18+
test11_singleedge :
1619
// single edge
17-
t.deepEqual(alg([ [0,1,1] ]), [1, 0]);
20+
{ edges: [ [0,1,1] ], expected: [1, 0]
1821
},
1922

20-
test12 : (alg,t) => {
21-
t.deepEqual(alg([ [1,2,10], [2,3,11] ]), [ -1, -1, 3, 2 ]);
23+
test12 :
24+
{ edges: [ [1,2,10], [2,3,11] ], expected: [ -1, -1, 3, 2 ]
2225
},
2326

24-
test13 : (alg,t) => {
25-
t.deepEqual(alg([ [1,2,5], [2,3,11], [3,4,5] ]), [ -1, -1, 3, 2, -1 ]);
27+
test13 :
28+
{ edges: [ [1,2,5], [2,3,11], [3,4,5] ], expected: [ -1, -1, 3, 2, -1 ]
2629
},
2730

28-
test14_maxcard : (alg,t) => {
29-
// maximum cardinality
30-
t.deepEqual(alg([ [1,2,5], [2,3,11], [3,4,5] ], true), [ -1, 2, 1, 4, 3 ]);
31+
// maximum cardinality
32+
test14_maxcard :
33+
{ edges: [ [1,2,5], [2,3,11], [3,4,5] ], maxCardinality: true, expected: [ -1, 2, 1, 4, 3 ]
3134
},
3235

33-
test15_float : (alg,t) => {
34-
// floating point weigths
35-
t.deepEqual(alg([ [1,2,Math.PI], [2,3,Math.E], [1,3,3.0], [1,4,Math.sqrt(2.0)] ]), [ -1, 4, 3, 2, 1 ]);
36+
// floating point weigths
37+
test15_float :
38+
{ edges: [ [1,2,Math.PI], [2,3,Math.E], [1,3,3.0], [1,4,Math.sqrt(2.0)] ], expected: [ -1, 4, 3, 2, 1 ]
3639
},
3740

38-
test16_negative : (alg,t) => {
39-
// negative weights
40-
t.deepEqual(alg([ [1,2,2], [1,3,-2], [2,3,1], [2,4,-1], [3,4,-6] ], false), [ -1, 2, 1, -1, -1 ]);
41-
t.deepEqual(alg([ [1,2,2], [1,3,-2], [2,3,1], [2,4,-1], [3,4,-6] ], true), [ -1, 3, 4, 1, 2 ]);
41+
// negative weights
42+
test16_negative :
43+
{ edges: [ [1,2,2], [1,3,-2], [2,3,1], [2,4,-1], [3,4,-6] ], maxCardinality: false, expected: [ -1, 2, 1, -1, -1 ]
44+
},
45+
test16_negative_maxCardinality :
46+
{ edges: [ [1,2,2], [1,3,-2], [2,3,1], [2,4,-1], [3,4,-6] ], maxCardinality: true, expected: [ -1, 3, 4, 1, 2 ]
4247
},
4348

44-
test20_sblossom : (alg,t) => {
45-
// create S-blossom and use it for augmentation
46-
t.deepEqual(alg([ [1,2,8], [1,3,9], [2,3,10], [3,4,7] ]), [ -1, 2, 1, 4, 3 ]);
47-
t.deepEqual(alg([ [1,2,8], [1,3,9], [2,3,10], [3,4,7], [1,6,5], [4,5,6] ]), [ -1, 6, 3, 2, 5, 4, 1 ]);
49+
// create S-blossom and use it for augmentation
50+
test20_sblossom_1 :
51+
{ edges: [ [1,2,8], [1,3,9], [2,3,10], [3,4,7] ], expected: [ -1, 2, 1, 4, 3 ]
52+
},
53+
test20_sblossom_2 :
54+
{ edges: [ [1,2,8], [1,3,9], [2,3,10], [3,4,7], [1,6,5], [4,5,6] ], expected: [ -1, 6, 3, 2, 5, 4, 1 ]
4855
},
4956

50-
test21_tblossom : (alg,t) => {
51-
// create S-blossom, relabel as T-blossom, use for augmentation
52-
t.deepEqual(alg([ [1,2,9], [1,3,8], [2,3,10], [1,4,5], [4,5,4], [1,6,3] ]), [ -1, 6, 3, 2, 5, 4, 1 ]);
53-
t.deepEqual(alg([ [1,2,9], [1,3,8], [2,3,10], [1,4,5], [4,5,3], [1,6,4] ]), [ -1, 6, 3, 2, 5, 4, 1 ]);
54-
t.deepEqual(alg([ [1,2,9], [1,3,8], [2,3,10], [1,4,5], [4,5,3], [3,6,4] ]), [ -1, 2, 1, 6, 5, 4, 3 ]);
57+
// create S-blossom, relabel as T-blossom, use for augmentation
58+
test21_tblossom_1 :
59+
{ edges: [ [1,2,9], [1,3,8], [2,3,10], [1,4,5], [4,5,4], [1,6,3] ], expected: [ -1, 6, 3, 2, 5, 4, 1 ]
60+
},
61+
test21_tblossom_2 :
62+
{ edges: [ [1,2,9], [1,3,8], [2,3,10], [1,4,5], [4,5,3], [1,6,4] ], expected: [ -1, 6, 3, 2, 5, 4, 1 ]
63+
},
64+
test21_tblossom_3 :
65+
{ edges: [ [1,2,9], [1,3,8], [2,3,10], [1,4,5], [4,5,3], [3,6,4] ], expected: [ -1, 2, 1, 6, 5, 4, 3 ]
5566
},
5667

57-
test22_s_nest : (alg,t) => {
58-
// create nested S-blossom, use for augmentation
59-
t.deepEqual(alg([ [1,2,9], [1,3,9], [2,3,10], [2,4,8], [3,5,8], [4,5,10], [5,6,6] ]), [ -1, 3, 4, 1, 2, 6, 5 ]);
68+
// create nested S-blossom, use for augmentation
69+
test22_s_nest :
70+
{ edges: [ [1,2,9], [1,3,9], [2,3,10], [2,4,8], [3,5,8], [4,5,10], [5,6,6] ], expected: [ -1, 3, 4, 1, 2, 6, 5 ]
6071
},
6172

62-
test23_s_relabel_nest : (alg,t) => {
63-
// create S-blossom, relabel as S, include in nested S-blossom
64-
t.deepEqual(alg([ [1,2,10], [1,7,10], [2,3,12], [3,4,20], [3,5,20], [4,5,25], [5,6,10], [6,7,10], [7,8,8] ]), [ -1, 2, 1, 4, 3, 6, 5, 8, 7 ]);
73+
// create S-blossom, relabel as S, include in nested S-blossom
74+
test23_s_relabel_nest :
75+
{ edges: [ [1,2,10], [1,7,10], [2,3,12], [3,4,20], [3,5,20], [4,5,25], [5,6,10], [6,7,10], [7,8,8] ], expected: [ -1, 2, 1, 4, 3, 6, 5, 8, 7 ]
6576
},
6677

67-
test24_s_nest_expand : (alg,t) => {
68-
// create nested S-blossom, augment, expand recursively
69-
t.deepEqual(alg([ [1,2,8], [1,3,8], [2,3,10], [2,4,12], [3,5,12], [4,5,14], [4,6,12], [5,7,12], [6,7,14], [7,8,12] ]), [ -1, 2, 1, 5, 6, 3, 4, 8, 7 ]);
78+
// create nested S-blossom, augment, expand recursively
79+
test24_s_nest_expand :
80+
{ edges: [ [1,2,8], [1,3,8], [2,3,10], [2,4,12], [3,5,12], [4,5,14], [4,6,12], [5,7,12], [6,7,14], [7,8,12] ], expected: [ -1, 2, 1, 5, 6, 3, 4, 8, 7 ]
7081
},
7182

72-
test25_s_t_expand : (alg,t) => {
73-
// create S-blossom, relabel as T, expand
74-
t.deepEqual(alg([ [1,2,23], [1,5,22], [1,6,15], [2,3,25], [3,4,22], [4,5,25], [4,8,14], [5,7,13] ]), [ -1, 6, 3, 2, 8, 7, 1, 5, 4 ]);
83+
// create S-blossom, relabel as T, expand
84+
test25_s_t_expand :
85+
{ edges: [ [1,2,23], [1,5,22], [1,6,15], [2,3,25], [3,4,22], [4,5,25], [4,8,14], [5,7,13] ], expected: [ -1, 6, 3, 2, 8, 7, 1, 5, 4 ]
7586
},
7687

77-
test26_s_nest_t_expand : (alg,t) => {
78-
// create nested S-blossom, relabel as T, expand
79-
t.deepEqual(alg([ [1,2,19], [1,3,20], [1,8,8], [2,3,25], [2,4,18], [3,5,18], [4,5,13], [4,7,7], [5,6,7] ]), [ -1, 8, 3, 2, 7, 6, 5, 4, 1 ]);
88+
// create nested S-blossom, relabel as T, expand
89+
test26_s_nest_t_expand :
90+
{ edges: [ [1,2,19], [1,3,20], [1,8,8], [2,3,25], [2,4,18], [3,5,18], [4,5,13], [4,7,7], [5,6,7] ], expected: [ -1, 8, 3, 2, 7, 6, 5, 4, 1 ]
8091
},
8192

82-
test30_tnasty_expand : (alg,t) => {
83-
// create blossom, relabel as T in more than one way, expand, augment
84-
t.deepEqual(alg([ [1,2,45], [1,5,45], [2,3,50], [3,4,45], [4,5,50], [1,6,30], [3,9,35], [4,8,35], [5,7,26], [9,10,5] ]), [ -1, 6, 3, 2, 8, 7, 1, 5, 4, 10, 9 ]);
93+
// create blossom, relabel as T in more than one way, expand, augment
94+
test30_tnasty_expand :
95+
{ edges: [ [1,2,45], [1,5,45], [2,3,50], [3,4,45], [4,5,50], [1,6,30], [3,9,35], [4,8,35], [5,7,26], [9,10,5] ], expected: [ -1, 6, 3, 2, 8, 7, 1, 5, 4, 10, 9 ]
8596
},
8697

87-
test31_tnasty2_expand : (alg,t) => {
88-
// again but slightly different
89-
t.deepEqual(alg([ [1,2,45], [1,5,45], [2,3,50], [3,4,45], [4,5,50], [1,6,30], [3,9,35], [4,8,26], [5,7,40], [9,10,5] ]), [ -1, 6, 3, 2, 8, 7, 1, 5, 4, 10, 9 ]);
98+
// again but slightly different
99+
test31_tnasty2_expand :
100+
{ edges: [ [1,2,45], [1,5,45], [2,3,50], [3,4,45], [4,5,50], [1,6,30], [3,9,35], [4,8,26], [5,7,40], [9,10,5] ], expected: [ -1, 6, 3, 2, 8, 7, 1, 5, 4, 10, 9 ]
90101
},
91102

92-
test32_t_expand_leastslack : (alg,t) => {
93-
// create blossom, relabel as T, expand such that a new least-slack S-to-free edge is produced, augment
94-
t.deepEqual(alg([ [1,2,45], [1,5,45], [2,3,50], [3,4,45], [4,5,50], [1,6,30], [3,9,35], [4,8,28], [5,7,26], [9,10,5] ]), [ -1, 6, 3, 2, 8, 7, 1, 5, 4, 10, 9 ]);
103+
// create blossom, relabel as T, expand such that a new least-slack S-to-free edge is produced, augment
104+
test32_t_expand_leastslack :
105+
{ edges: [ [1,2,45], [1,5,45], [2,3,50], [3,4,45], [4,5,50], [1,6,30], [3,9,35], [4,8,28], [5,7,26], [9,10,5] ], expected: [ -1, 6, 3, 2, 8, 7, 1, 5, 4, 10, 9 ]
95106
},
96107

97-
test33_nest_tnasty_expand : (alg,t) => {
98-
// create nested blossom, relabel as T in more than one way, expand outer blossom such that inner blossom ends up on an augmenting path
99-
t.deepEqual(alg([ [1,2,45], [1,7,45], [2,3,50], [3,4,45], [4,5,95], [4,6,94], [5,6,94], [6,7,50], [1,8,30], [3,11,35], [5,9,36], [7,10,26], [11,12,5] ]), [ -1, 8, 3, 2, 6, 9, 4, 10, 1, 5, 7, 12, 11 ]);
108+
// create nested blossom, relabel as T in more than one way, expand outer blossom such that inner blossom ends up on an augmenting path
109+
test33_nest_tnasty_expand :
110+
{ edges: [ [1,2,45], [1,7,45], [2,3,50], [3,4,45], [4,5,95], [4,6,94], [5,6,94], [6,7,50], [1,8,30], [3,11,35], [5,9,36], [7,10,26], [11,12,5] ], expected: [ -1, 8, 3, 2, 6, 9, 4, 10, 1, 5, 7, 12, 11 ]
100111
},
101112

102-
test34_nest_relabel_expand : (alg,t) => {
103-
// create nested S-blossom, relabel as S, expand recursively
104-
t.deepEqual(alg([ [1,2,40], [1,3,40], [2,3,60], [2,4,55], [3,5,55], [4,5,50], [1,8,15], [5,7,30], [7,6,10], [8,10,10], [4,9,30] ]), [ -1, 2, 1, 5, 9, 3, 7, 6, 10, 4, 8 ]);
113+
// create nested S-blossom, relabel as S, expand recursively
114+
test34_nest_relabel_expand :
115+
{ edges: [ [1,2,40], [1,3,40], [2,3,60], [2,4,55], [3,5,55], [4,5,50], [1,8,15], [5,7,30], [7,6,10], [8,10,10], [4,9,30] ], expected: [ -1, 2, 1, 5, 9, 3, 7, 6, 10, 4, 8 ]
105116
}
106117
};
107118

108-
const alg = [
119+
const algorithms = [
109120
blossom(true, true),
110121
blossom(false, false),
111122
blossom()
112123
];
113124

114-
for(let i = 0; i < alg.length; ++i)
115-
for(const key in tests)
116-
test(`${key} ${i}`, macro, alg[i], tests[key]);
125+
for(const [i, algorithm] of enumerate(algorithms))
126+
for(const [key, {edges, maxCardinality, expected}] of Object.entries(tests))
127+
test(`${key} ${i}`, macro, algorithm, edges, maxCardinality, expected);

yarn.lock

+19-17
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@
22
# yarn lockfile v1
33

44

5-
"@aureooms/js-binomial-heap@^11.0.0":
6-
version "11.0.0"
7-
resolved "https://registry.yarnpkg.com/@aureooms/js-binomial-heap/-/js-binomial-heap-11.0.0.tgz#8ca6dbaf28834213c933eec1d4cfa6f73b352474"
8-
integrity sha1-jKbbryiDQhPJM+7B1M+m9zs1JHQ=
5+
"@aureooms/js-collections-deque@^4.0.1":
6+
version "4.0.1"
7+
resolved "https://registry.yarnpkg.com/@aureooms/js-collections-deque/-/js-collections-deque-4.0.1.tgz#bd8b827cdb597d061525c380dd704e338c77bd5f"
8+
integrity sha512-dktRVLyep0sUKpqgtG/JseA232KMdlu9t9ePq2wHcng1dZ3+63qW2C+FlKXBktrT1dU92DDxIL6PqnV97kHA1Q==
9+
dependencies:
10+
"@aureooms/js-error" "^4.0.1"
911

10-
"@aureooms/js-functools@^2.0.3":
11-
version "2.0.3"
12-
resolved "https://registry.yarnpkg.com/@aureooms/js-functools/-/js-functools-2.0.3.tgz#5139d6245278193da4cf21c44666797bf40a1d6e"
13-
integrity sha1-UTnWJFJ4GT2kzyHERmZ5e/QKHW4=
12+
"@aureooms/js-error@^4.0.1":
13+
version "4.0.1"
14+
resolved "https://registry.yarnpkg.com/@aureooms/js-error/-/js-error-4.0.1.tgz#be2740b3f31a337e0828930c9b70d38113b4dd77"
15+
integrity sha512-zsgs6wmnRsKljlusewYiBaFBM/hIJp43b7OwE9ybpLEL4wfXjGP17cJTTAY8gpzALVFwEA5/flxIK0I6gJJLHQ==
16+
17+
"@aureooms/js-itertools@^4.1.0":
18+
version "4.1.0"
19+
resolved "https://registry.yarnpkg.com/@aureooms/js-itertools/-/js-itertools-4.1.0.tgz#c731eaee7de0005c6ea41f2f87cca4bdb2d86ca1"
20+
integrity sha512-P9TfwQPuXk5T5z6QMMfwLLyzh9QsEu4sGWcBk/P/waWgim/xqr+LDTOHVo7WU4QAty+/5CBX8DuW7MrPVfXt5A==
21+
dependencies:
22+
"@aureooms/js-collections-deque" "^4.0.1"
23+
"@aureooms/js-error" "^4.0.1"
1424

1525
1626
version "7.11.6"
@@ -729,14 +739,6 @@
729739
"@babel/helper-create-regexp-features-plugin" "^7.10.4"
730740
"@babel/helper-plugin-utils" "^7.10.4"
731741

732-
733-
version "7.11.5"
734-
resolved "https://registry.yarnpkg.com/@babel/polyfill/-/polyfill-7.11.5.tgz#df550b2ec53abbc2ed599367ec59e64c7a707bb5"
735-
integrity sha512-FunXnE0Sgpd61pKSj2OSOs1D44rKTD3pGOfGilZ6LGrrIH0QEtJlTjqOqdF8Bs98JmjfGhni2BBkTfv9KcKJ9g==
736-
dependencies:
737-
core-js "^2.6.5"
738-
regenerator-runtime "^0.13.4"
739-
740742
741743
version "7.11.5"
742744
resolved "https://registry.yarnpkg.com/@babel/preset-env/-/preset-env-7.11.5.tgz#18cb4b9379e3e92ffea92c07471a99a2914e4272"
@@ -2058,7 +2060,7 @@ core-js-compat@^3.6.2:
20582060
browserslist "^4.8.5"
20592061
semver "7.0.0"
20602062

2061-
core-js@^2.0.0, core-js@^2.4.0, core-js@^2.6.5:
2063+
core-js@^2.0.0, core-js@^2.4.0:
20622064
version "2.6.11"
20632065
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.6.11.tgz#38831469f9922bded8ee21c9dc46985e0399308c"
20642066
integrity sha512-5wjnpaT/3dV+XB4borEsnAYQchn00XSgTAWKDkEqv+K8KevjbzmofK6hfJ9TZIlpj2N0xQpazy7PiRQiWHqzWg==

0 commit comments

Comments
 (0)