Skip to content

Commit 644fbbf

Browse files
committed
Refactor vertex_test.dart fixes #31
1 parent 73154f2 commit 644fbbf

File tree

3 files changed

+51
-44
lines changed

3 files changed

+51
-44
lines changed

lib/graph/graph.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,15 @@ class Graph<T> {
2828

2929
/// Adds an edge
3030
void addEdge(Vertex src, Vertex dst, [num weight = 1]) {
31-
src.unlock();
32-
dst.unlock();
31+
unlockVertices(<Vertex>{src, dst});
3332
if (src.key == dst.key && !allowLoops) throw Error();
3433

3534
src = _getOrAddVertex(src);
3635
dst = _getOrAddVertex(dst);
3736
src.addConnection(dst, weight);
3837

3938
if (!isDigraph) dst.addConnection(src, weight);
40-
src.lock();
41-
dst.lock();
39+
lockVertices(<Vertex>{src, dst});
4240
}
4341

4442
/// Checks if vertex is included

lib/graph/vertex.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,13 @@ class Vertex<T> {
8888
@override
8989
String toString() => key;
9090
}
91+
92+
/// Unlocks a set of vertices
93+
void unlockVertices(Set<Vertex> vertices) {
94+
vertices.forEach((v) => v.unlock());
95+
}
96+
97+
/// Locks a set of vertices
98+
void lockVertices(Set<Vertex> vertices) {
99+
vertices.forEach((v) => v.lock());
100+
}

test/graph/vertex_test.dart

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,42 @@ import 'package:test/test.dart';
33
import 'package:algorithms/graph/vertex.dart';
44

55
void main() {
6-
Vertex root;
7-
Vertex rootWithValue;
8-
Vertex connectedVertex;
9-
Vertex toBeAdded;
10-
Vertex anotherVertex;
11-
12-
setUp(() {
6+
Vertex root,
7+
rootWithValue,
8+
connectedVertex,
9+
toBeAdded,
10+
anotherVertex,
11+
a,
12+
b,
13+
c;
14+
15+
void _initializeVertices() {
1316
root = Vertex('A');
1417
rootWithValue = Vertex('a', 'Wake up');
15-
1618
connectedVertex = Vertex('0');
1719
toBeAdded = Vertex('1');
1820
anotherVertex = Vertex('2');
21+
a = Vertex('a');
22+
b = Vertex('b');
23+
c = Vertex('c');
24+
}
25+
26+
void _unlockVertices() {
27+
unlockVertices(<Vertex>{
28+
root,
29+
rootWithValue,
30+
connectedVertex,
31+
toBeAdded,
32+
anotherVertex,
33+
a,
34+
b,
35+
c
36+
});
37+
}
1938

20-
// For test purposes, unlock all vertices
21-
root.unlock();
22-
rootWithValue.unlock();
23-
connectedVertex.unlock();
24-
toBeAdded.unlock();
25-
anotherVertex.unlock();
26-
39+
setUp(() {
40+
_initializeVertices();
41+
_unlockVertices();
2742
connectedVertex.addConnection(toBeAdded);
2843
connectedVertex.addConnection(anotherVertex);
2944
});
@@ -36,10 +51,6 @@ void main() {
3651
});
3752

3853
test('Successfully add a vertex', () {
39-
var b = Vertex('B');
40-
var c = Vertex('C');
41-
b.unlock();
42-
c.unlock();
4354
expect(root.addConnection(b), isTrue);
4455
expect(root.addConnection(c), isTrue);
4556
expect(root.outgoingConnections.containsKey(b), isTrue);
@@ -49,8 +60,6 @@ void main() {
4960
});
5061

5162
test('Unsuccessfully add a vertex', () {
52-
var b = Vertex('B');
53-
b.unlock();
5463
root.addConnection(b);
5564
expect(root.addConnection(b), isFalse);
5665
});
@@ -62,44 +71,42 @@ void main() {
6271
});
6372

6473
test('Unsuccessfully remove a vertex', () {
65-
var aVertex = Vertex('-1');
66-
aVertex.unlock();
67-
expect(connectedVertex.removeConnection(aVertex), isFalse);
74+
expect(connectedVertex.removeConnection(a), isFalse);
6875
});
6976

7077
test('Trying to add to a locked vertex throws error', () {
7178
var locked = Vertex('PROTECTED');
72-
expect(() => locked.addConnection(root), throwsA(isA<Error>()));
79+
expect(() => locked.addConnection(root), throwsA(isA<UnsupportedError>()));
7380
root.lock();
74-
expect(() => root.addConnection(root), throwsA(isA<Error>()));
81+
expect(() => root.addConnection(root), throwsA(isA<UnsupportedError>()));
7582
});
7683

7784
test('Trying to add a locked vertex throws error', () {
7885
var locked = Vertex('PROTECTED');
79-
expect(() => root.addConnection(locked), throwsA(isA<Error>()));
86+
expect(() => root.addConnection(locked), throwsA(isA<UnsupportedError>()));
8087
locked.unlock();
8188
root.lock();
82-
expect(() => locked.addConnection(root), throwsA(isA<Error>()));
89+
expect(() => locked.addConnection(root), throwsA(isA<UnsupportedError>()));
8390
});
8491

8592
test('Trying to remove from a locked vertex throws error', () {
8693
toBeAdded.lock();
8794
expect(() => connectedVertex.removeConnection(toBeAdded),
88-
throwsA(isA<Error>()));
95+
throwsA(isA<UnsupportedError>()));
8996
});
9097

9198
test('Trying to remove a locked vertex throws error', () {
9299
connectedVertex.lock();
93100
expect(() => connectedVertex.removeConnection(toBeAdded),
94-
throwsA(isA<Error>()));
101+
throwsA(isA<UnsupportedError>()));
95102
});
96103

97104
test('Trying to remove a locked vertex throws error', () {
98105
var locked = Vertex('PROTECTED');
99-
expect(() => root.addConnection(locked), throwsA(isA<Error>()));
106+
expect(() => root.addConnection(locked), throwsA(isA<UnsupportedError>()));
100107
locked.unlock();
101108
root.lock();
102-
expect(() => locked.addConnection(root), throwsA(isA<Error>()));
109+
expect(() => locked.addConnection(root), throwsA(isA<UnsupportedError>()));
103110
});
104111

105112
test('Check for vertex containment', () {
@@ -110,12 +117,6 @@ void main() {
110117
});
111118

112119
test('Get a list of vertices', () {
113-
var a = Vertex('A');
114-
var b = Vertex('B');
115-
var c = Vertex('C');
116-
a.unlock();
117-
b.unlock();
118-
c.unlock();
119120
root.addConnection(a);
120121
root.addConnection(b);
121122
root.addConnection(c);
@@ -131,8 +132,6 @@ void main() {
131132
});
132133

133134
test('Get out degree for vertex', () {
134-
var c = Vertex('c');
135-
c.unlock();
136135
expect(root.outDegree, equals(0));
137136
connectedVertex.addConnection(c);
138137
expect(connectedVertex.outDegree, equals(3));

0 commit comments

Comments
 (0)