Skip to content

Commit

Permalink
graph building works
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelkiessling committed Mar 29, 2012
1 parent 8867155 commit cbbd756
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/insertEdge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";

var insertEdge = function(graph, vertexNumber, to, EdgeNode) {
var edgeNode = new EdgeNode(to, graph.edges[vertexNumber]);
graph.edges[vertexNumber] = edgeNode;
};
2 changes: 2 additions & 0 deletions spec/SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

<!-- Source Files -->
<script type="text/javascript" src="../lib/EdgeNode.js"></script>
<script type="text/javascript" src="../lib/insertEdge.js"></script>

<!-- Spec Files -->
<script type="text/javascript" src="./EdgeNode.spec.js"></script>
<script type="text/javascript" src="./insertEdge.spec.js"></script>
</head>

<body>
Expand Down
52 changes: 52 additions & 0 deletions spec/insertEdge.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"use strict";

var graph = {
edges: []
};

var resetGraph = function() {
graph = {
edges: []
};
};

var EdgeNode = function(to, next) {
this.to = to;
this.next = next;
};

describe("insertEdge", function() {
beforeEach(function() {
resetGraph();
});

it("inserts an egdeNode into the graph", function() {
insertEdge(graph, 1, 5, EdgeNode);
expect(graph.edges[1].to).toEqual(5);
expect(graph.edges[1].next).toEqual(undefined);
/**
[1]->5->||
*/
});

it("inserts an additional edgeNode to the head of the list of its vertex", function() {
insertEdge(graph, 1, 5, EdgeNode);
insertEdge(graph, 1, 3, EdgeNode);
expect(graph.edges[1].to).toEqual(3);
expect(graph.edges[1].next.to).toEqual(5);
expect(graph.edges[1].next.next).toEqual(undefined);
});

it("correctly inserts an edgeNode for another vertex", function() {
insertEdge(graph, 1, 5, EdgeNode);
insertEdge(graph, 1, 3, EdgeNode);
insertEdge(graph, 2, 5, EdgeNode);
expect(graph.edges[1].to).toEqual(3);
expect(graph.edges[1].next.to).toEqual(5);
expect(graph.edges[1].next.next).toEqual(undefined);
expect(graph.edges[2].to).toEqual(5);
expect(graph.edges[2].next).toEqual(undefined);
});
});

0 comments on commit cbbd756

Please sign in to comment.