Skip to content

Commit 22e5154

Browse files
committed
Ensure vertices with duplicate keys are not added to graph
1 parent 644fbbf commit 22e5154

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

lib/graph/vertex.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,24 @@ class Vertex<T> {
8787

8888
@override
8989
String toString() => key;
90+
91+
// ignore: avoid_equals_and_hash_code_on_mutable_classes
92+
bool operator ==(Object other) => other is Vertex && key == other.key;
93+
94+
// ignore: avoid_equals_and_hash_code_on_mutable_classes
95+
int get hashCode => key.hashCode;
9096
}
9197

9298
/// Unlocks a set of vertices
9399
void unlockVertices(Set<Vertex> vertices) {
94-
vertices.forEach((v) => v.unlock());
100+
for (var vertex in vertices) {
101+
vertex.unlock();
102+
}
95103
}
96104

97105
/// Locks a set of vertices
98106
void lockVertices(Set<Vertex> vertices) {
99-
vertices.forEach((v) => v.lock());
107+
for (var vertex in vertices) {
108+
vertex.lock();
109+
}
100110
}

test/graph/graph_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,10 @@ void main() {
9191
});
9292

9393
test('Does not add vertex with existing key', () {
94+
var aDuplicate = Vertex(a.key);
9495
expect(simpleGraph.numberOfVertices, equals(6));
9596
expect(simpleGraph.addVertex(a), isFalse);
97+
expect(simpleGraph.addVertex(aDuplicate), isFalse);
9698
expect(simpleGraph.numberOfVertices, equals(6));
9799
});
98100

test/graph/vertex_test.dart

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void main() {
1414

1515
void _initializeVertices() {
1616
root = Vertex('A');
17-
rootWithValue = Vertex('a', 'Wake up');
17+
rootWithValue = Vertex('i', 'Wake up');
1818
connectedVertex = Vertex('0');
1919
toBeAdded = Vertex('1');
2020
anotherVertex = Vertex('2');
@@ -46,7 +46,7 @@ void main() {
4646
test('Initialization of a node', () {
4747
expect(root.key, equals('A'));
4848
expect(root.value, equals('A'));
49-
expect(rootWithValue.key, equals('a'));
49+
expect(rootWithValue.key, equals('i'));
5050
expect(rootWithValue.value, equals('Wake up'));
5151
});
5252

@@ -59,11 +59,17 @@ void main() {
5959
expect(c.incomingVertices.contains(root), isTrue);
6060
});
6161

62-
test('Unsuccessfully add a vertex', () {
63-
root.addConnection(b);
62+
test('Cannot add vertex that already exists', () {
63+
expect(root.addConnection(b), isTrue);
6464
expect(root.addConnection(b), isFalse);
6565
});
6666

67+
test('Cannot add vertex with the same key', () {
68+
expect(root.addConnection(b), isTrue);
69+
var bDuplicate = Vertex(b.key)..unlock();
70+
expect(root.addConnection(bDuplicate), isFalse);
71+
});
72+
6773
test('Successfully remove a vertex', () {
6874
expect(toBeAdded.incomingVertices.isEmpty, isFalse);
6975
expect(connectedVertex.removeConnection(toBeAdded), isTrue);

0 commit comments

Comments
 (0)