Skip to content

Commit fc14124

Browse files
committed
[dag.algorithms] Add unit tests for descendants/ancestors
1 parent 4b39f2c commit fc14124

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

src/algorithms/__tests__/dag-test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*globals assert, utils*/
22
'use strict';
33

4+
import { Set } from '../../_internals/';
45
import {Graph, DiGraph} from '../../classes';
56
import JSNetworkXError from '../../exceptions/JSNetworkXError';
67
import JSNetworkXUnfeasible from '../../exceptions/JSNetworkXUnfeasible';
@@ -162,5 +163,34 @@ export var TestDAG = {
162163
G.addCycle([0,1,2]);
163164
G.addEdge(3,3);
164165
assert(!dag.isAperiodic(G));
166+
},
167+
168+
// dag.ancestors
169+
// https://github.com/networkx/networkx/blob/master/networkx/algorithms/tests/test_dag.py#L281
170+
testAncestors: function () {
171+
var DG = new DiGraph();
172+
DG.addEdgesFrom([[1, 2], [1, 3], [4, 2], [4, 3], [4, 5], [2, 6], [5, 6]]);
173+
assert.deepEqual(dag.ancestors(DG, 6), new Set([1, 2, 4, 5]));
174+
assert.deepEqual(dag.ancestors(DG, 3), new Set([1, 4]));
175+
assert.deepEqual(dag.ancestors(DG, 1), new Set());
176+
},
177+
testAncestorsInvalidSource: function () {
178+
var DG = new DiGraph();
179+
DG.addEdgesFrom([[1, 2], [1, 3]]); // tested node is not in graph
180+
assert.throws(() => dag.ancestors(DG, 0), JSNetworkXError);
181+
},
182+
183+
// dag.descendants
184+
testDescendants: function () {
185+
var DG = new DiGraph();
186+
DG.addEdgesFrom([[1, 2], [1, 3], [4, 2], [4, 3], [4, 5], [2, 6], [5, 6]]);
187+
assert.deepEqual(dag.descendants(DG, 1), new Set([2, 3, 6]));
188+
assert.deepEqual(dag.descendants(DG, 4), new Set([2, 3, 5, 6]));
189+
assert.deepEqual(dag.descendants(DG, 3), new Set([]));
190+
},
191+
testDescendantsInvalidSource: function () {
192+
var DG = new DiGraph();
193+
DG.addEdgesFrom([[1, 2], [1, 3]]);
194+
assert.throws(() => dag.descendants(DG, 0), JSNetworkXError);
165195
}
166196
};

0 commit comments

Comments
 (0)