Skip to content

Instantly share code, notes, and snippets.

@hoorayimhelping
Created September 7, 2015 04:51
Show Gist options
  • Save hoorayimhelping/892a269af520c68cd07f to your computer and use it in GitHub Desktop.
Save hoorayimhelping/892a269af520c68cd07f to your computer and use it in GitHub Desktop.
diff --git a/gulpfile.js b/gulpfile.js
index fe7ae80..c0b13dc 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -28,6 +28,11 @@ gulp.task('test', shell.task([
'tape ' + paths.js_test + '* | faucet',
]));
+gulp.task('test-debug', function() {
+ return gulp.src(paths.js_test + '/*.js')
+ .pipe(tape());
+});
+
gulp.task('build', function() {
return browserify(paths.js_source + 'main.js')
.bundle()
diff --git a/js/src/graph.js b/js/src/graph.js
new file mode 100644
index 0000000..3c73c0f
--- /dev/null
+++ b/js/src/graph.js
@@ -0,0 +1,25 @@
+var Node = require('./node');
+
+var Graph = function() {
+ this.graph = [];
+};
+
+// https://en.wikipedia.org/wiki/Graph_(abstract_data_type)
+Graph.prototype = {
+ add: function(node1, node2) {
+ node1.add(node2);
+ node2.add(node1);
+ },
+
+ isAdjacent: function(node1, node2) {
+ return node1.isAdjacentWith(node2) || node2.isAdjacentWith(node1);
+ },
+
+ neighbors: function(node) {}
+ // : adds to G the edge from x to y, if it is not there.
+ // delete(G, x, y): removes the edge from x to y, if it is there.
+ // get_node_value(G, x): returns the value associated with the node x.
+ // set_node_value(G, x, a): sets the value associated with the node x to a.
+};
+
+module.exports = Graph;
\ No newline at end of file
diff --git a/js/src/node.js b/js/src/node.js
index e1bb5e2..b5595a7 100644
--- a/js/src/node.js
+++ b/js/src/node.js
@@ -8,7 +8,7 @@ Node.prototype = {
this.neighbors.push(node);
},
- contains: function(node) {
+ isAdjacentWith: function(node) {
return this.neighbors.some(function(current_node) {
return current_node.id === node.id;
});
diff --git a/js/test/graph.js b/js/test/graph.js
index 2c1786c..8c60e07 100644
--- a/js/test/graph.js
+++ b/js/test/graph.js
@@ -1,12 +1,33 @@
var test = require('tape');
var Graph = require('../src/graph');
+var Node = require('../src/node')
-test("adjacency of nodes", function(t) {
+var newNode = function(id) {
+ var node = new Node();
+ node.id = id;
+
+ return node;
+}
+
+test("adding a node to another node's adjacency graph", function(t) {
var graph = new Graph();
- t.plan(1);
- console.log('leo dicaprio');
- t.true(true);
+ var node1 = newNode(1);
+ var node2 = newNode(2);
+ var node3 = newNode(3);
+ var node4 = newNode(4);
+
+ t.plan(4);
+
+ graph.add(node1, node2);
+ graph.add(node1, node3);
+ graph.add(node3, node4);
+
+ t.true(graph.isAdjacent(node1, node2), "nodes that have been connected are adjacent");
+ t.true(graph.isAdjacent(node3, node4), "nodes that have been connected are adjacent");
+
+ t.false(graph.isAdjacent(node2, node3), "nodes that haven't been connected aren't adjacent");
+ t.false(graph.isAdjacent(node1, node4), "adjacency isn't transitive");
t.end();
});
\ No newline at end of file
diff --git a/js/test/node.js b/js/test/node.js
index 64545f2..1333e90 100644
--- a/js/test/node.js
+++ b/js/test/node.js
@@ -20,11 +20,11 @@ test("node adjacency", function(t) {
other_node.id = 1;
node.add(other_node);
- t.true(node.contains(other_node), "returns true when the node is adjacent with this node");
+ t.true(node.isAdjacentWith(other_node), "returns true when the node is adjacent with this node");
var yet_another_node = new Node();
- t.false(node.contains(yet_another_node), "returns false when the node is not adjacent with this node");
+ t.false(node.isAdjacentWith(yet_another_node), "returns false when the node is not adjacent with this node");
t.end();
});
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment