1 | describe('default Graph', () => {
|
2 | const no = require('not-defined')
|
3 | const should = require('should')
|
4 |
|
5 | const Graph = require('iper').Graph
|
6 |
|
7 | const nodeData1 = 'foo'
|
8 | const nodeData2 = ['bar']
|
9 |
|
10 | const graph = new Graph()
|
11 | var nodeId1
|
12 | var nodeId2
|
13 | var edgeId1
|
14 |
|
15 | describe('constructor', () => {
|
16 | it('defaults to an empty graph', () => {
|
17 | graph.edges.should.deepEqual({})
|
18 | graph.nodes.should.deepEqual({})
|
19 |
|
20 | should.not.exist(graph.multigraph)
|
21 | should.not.exist(graph.pseudograph)
|
22 | })
|
23 | })
|
24 |
|
25 | describe('addNode()', () => {
|
26 | it('creates a node', () => {
|
27 | nodeId1 = graph.addNode(nodeData1)
|
28 | graph.nodes[nodeId1].should.be.eql(nodeData1)
|
29 | })
|
30 |
|
31 | it('returns an id', () => {
|
32 | nodeId2 = graph.addNode(nodeData2)
|
33 | nodeId2.should.be.a.String
|
34 | })
|
35 | })
|
36 |
|
37 | describe('addEdge()', () => {
|
38 | it('creates an edge', () => {
|
39 | const nodeIds = [nodeId1, nodeId2]
|
40 |
|
41 | edgeId1 = graph.addEdge(nodeIds)
|
42 |
|
43 | graph.edges[edgeId1].should.be.eql(nodeIds)
|
44 | })
|
45 |
|
46 | it('requires at least two nodeIds', () => {
|
47 | const nodeId = graph.addNode()
|
48 |
|
49 | const nodeIds = [nodeId]
|
50 |
|
51 | ;(() => {
|
52 | graph.addEdge(nodeIds)
|
53 | }).should.throw()
|
54 | })
|
55 |
|
56 | it('cannot create an edge pointing to some nodeId not found', () => {
|
57 | const nodeId1 = graph.addNode()
|
58 | const nodeId2 = graph.addNode()
|
59 |
|
60 | const nodeIds = [nodeId1, 'nodeIdNotFound', nodeId2]
|
61 |
|
62 | ;(() => {
|
63 | graph.addEdge(nodeIds)
|
64 | }).should.throw()
|
65 | })
|
66 |
|
67 | it('can create edges with cardinality greater than 2', () => {
|
68 | const graph = new Graph()
|
69 |
|
70 | const nodeId1 = graph.addNode()
|
71 | const nodeId2 = graph.addNode()
|
72 | const nodeId3 = graph.addNode()
|
73 |
|
74 | const nodeIds = [nodeId1, nodeId2, nodeId3]
|
75 |
|
76 | const edgeId1 = graph.addEdge(nodeIds)
|
77 |
|
78 | graph.edges[edgeId1].should.be.eql(nodeIds)
|
79 | })
|
80 |
|
81 | it('returns an id', () => {
|
82 | edgeId1.should.be.a.String
|
83 | })
|
84 |
|
85 | it('cannot create loops', () => {
|
86 | const nodeId = graph.addNode()
|
87 |
|
88 | const nodeIds = [nodeId, nodeId]
|
89 |
|
90 | ;(() => {
|
91 | graph.addEdge(nodeIds)
|
92 | }).should.throw()
|
93 | })
|
94 |
|
95 | it('cannot create duplicated edges', () => {
|
96 | const nodeId1 = graph.addNode()
|
97 | const nodeId2 = graph.addNode()
|
98 |
|
99 | graph.addEdge([nodeId1, nodeId2])
|
100 |
|
101 | ;(() => {
|
102 | graph.addEdge([nodeId1, nodeId2])
|
103 | }).should.throw()
|
104 | })
|
105 | })
|
106 |
|
107 | describe('degreeOf(nodeId)', () => {
|
108 | it('returns the degree of a node', () => {
|
109 | graph.degreeOf(nodeId1).should.be.eql(1)
|
110 | })
|
111 | })
|
112 |
|
113 | describe('delNode()', () => {
|
114 | it('removes a node', () => {
|
115 | graph.delNode(nodeId1)
|
116 |
|
117 | const nodeNotDefined = no(graph.nodes[nodeId1])
|
118 | nodeNotDefined.should.be.true
|
119 | })
|
120 |
|
121 | it('removes incident edges', () => {
|
122 | const incidentEdgeRemoved = no(graph.edges[edgeId1])
|
123 | incidentEdgeRemoved.should.be.true
|
124 | })
|
125 | })
|
126 |
|
127 | describe('delEdge()', () => {
|
128 | it('removes an edge', () => {
|
129 | nodeId1 = graph.addNode(nodeData1)
|
130 | nodeId2 = graph.addNode(nodeData2)
|
131 | const nodeIds = [nodeId1, nodeId2]
|
132 | edgeId1 = graph.addEdge(nodeIds)
|
133 |
|
134 | graph.delEdge(edgeId1)
|
135 |
|
136 | var edgeNotDefined = (typeof graph.edges[edgeId1] === 'undefined')
|
137 | edgeNotDefined.should.be.true
|
138 | })
|
139 | })
|
140 |
|
141 | describe('getRank()', () => {
|
142 | it('returns the max cardinality of any edge', () => {
|
143 | const graph = new Graph()
|
144 |
|
145 | const nodeId1 = graph.addNode()
|
146 | const nodeId2 = graph.addNode()
|
147 | const nodeId3 = graph.addNode()
|
148 | const nodeId4 = graph.addNode()
|
149 |
|
150 | graph.addEdge([nodeId1, nodeId2])
|
151 | graph.addEdge([nodeId3, nodeId2])
|
152 | graph.addEdge([nodeId1, nodeId4])
|
153 |
|
154 | graph.getRank().should.be.eql(2)
|
155 |
|
156 | graph.addEdge([nodeId1, nodeId2, nodeId3])
|
157 |
|
158 | graph.getRank().should.be.eql(3)
|
159 |
|
160 | graph.addEdge([nodeId4, nodeId1, nodeId2, nodeId3])
|
161 |
|
162 | graph.getRank().should.be.eql(4)
|
163 | })
|
164 | })
|
165 | })
|