summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2014-01-29 19:22:56 +0100
committerPatrick Simianer <p@simianer.de>2014-01-29 19:22:56 +0100
commitd9d72e06db07087aa54401fae8b259f0c4ccd649 (patch)
tree97f0444314c40d2894ac0892d5559101eda01acf /test
parent22644ed1365e566c8bf806bfff4ecd43c46ce089 (diff)
first usable version, name change => nlp_ruby
Diffstat (limited to 'test')
-rwxr-xr-xtest/dags.rb47
-rw-r--r--test/dags/example.json45
-rw-r--r--test/dags/simple.json25
3 files changed, 117 insertions, 0 deletions
diff --git a/test/dags.rb b/test/dags.rb
new file mode 100755
index 0000000..0e90d1b
--- /dev/null
+++ b/test/dags.rb
@@ -0,0 +1,47 @@
+#!/usr/bin/env ruby
+
+require 'nlp_ruby'
+require 'test/unit'
+
+
+class TestDAG < Test::Unit::TestCase
+
+ def test_viterbi
+ semiring = ViterbiSemiring.new
+ graph, nodes_by_label = DAG::read_graph_from_json('dags/example.json', semiring)
+ DAG::viterbi(graph, semiring, nodes_by_label['0'])
+ assert_equal(nodes_by_label['100'].score, 0.003)
+ end
+
+ # no negative weights here!
+ def test_dijkstra
+ semiring = RealSemiring.new
+ graph, nodes_by_label = DAG::read_graph_from_json('dags/example.json', semiring)
+ DAG::dijkstra(graph, semiring, nodes_by_label['0'])
+ assert_equal(nodes_by_label['100'].score, 0.5)
+ end
+
+ def test_bellman_ford
+ semiring = RealSemiring.new
+ graph, nodes_by_label = DAG::read_graph_from_json('dags/example.json', semiring)
+ DAG::bellman_ford(graph, semiring, nodes_by_label['0'])
+ assert_equal(nodes_by_label['100'].score, 0.5)
+ end
+
+ def test_floyd
+ graph, nodes_by_label = DAG::read_graph_from_json('dags/example.json')
+ d = DAG::floyd(graph)
+ assert_equal(d[0][graph.size-1], 0.5)
+ end
+
+ def test_dfs
+ graph, nodes_by_label = DAG::read_graph_from_json('dags/example.json')
+ assert_equal(nodes_by_label['100'], DAG::dfs(nodes_by_label['0'], '100'))
+ end
+
+ def test_bfs
+ graph, nodes_by_label = DAG::read_graph_from_json('dags/example.json')
+ assert_equal(nodes_by_label['100'], DAG::bfs(nodes_by_label['0'], '100'))
+ end
+end
+
diff --git a/test/dags/example.json b/test/dags/example.json
new file mode 100644
index 0000000..3763359
--- /dev/null
+++ b/test/dags/example.json
@@ -0,0 +1,45 @@
+{
+
+"nodes":
+[
+ {"label":"0"},
+ {"label":"7"},
+ {"label":"5"},
+ {"label":"3"},
+ {"label":"11"},
+ {"label":"8"},
+ {"label":"2"},
+ {"label":"9"},
+ {"label":"10"},
+ {"label":"100"}
+],
+
+"edges":
+[
+ { "tail":"0", "head":"7", "weight":"0.1" },
+ { "tail":"0", "head":"5", "weight":"0.1" },
+ { "tail":"0", "head":"3", "weight":"0.3" },
+
+ { "tail":"7", "head":"11", "weight":"0.5" },
+ { "tail":"7", "head":"8", "weight":"0.3" },
+
+ { "tail":"5", "head":"11", "weight":"0.5" },
+
+ { "tail":"3", "head":"8", "weight":"0.1" },
+ { "tail":"3", "head":"10", "weight":"0.1" },
+
+ { "tail":"11", "head":"2", "weight":"0.2" },
+ { "tail":"11", "head":"9", "weight":"0.2" },
+ { "tail":"11", "head":"10", "weight":"0.3" },
+
+ { "tail":"8", "head":"9", "weight":"0.3" },
+
+ { "tail":"2", "head":"100", "weight":"0.1" },
+
+ { "tail":"9", "head":"100", "weight":"0.1" },
+
+ { "tail":"10", "head":"100", "weight":"0.1" }
+]
+
+}
+
diff --git a/test/dags/simple.json b/test/dags/simple.json
new file mode 100644
index 0000000..16a2ef0
--- /dev/null
+++ b/test/dags/simple.json
@@ -0,0 +1,25 @@
+{
+
+"nodes":
+[
+ {"label":"0"},
+ {"label":"7"},
+ {"label":"5"},
+ {"label":"6"},
+ {"label":"100"}
+],
+
+"arcs":
+[
+ { "tail":"0", "head":"7", "weight":"0.1" },
+ { "tail":"0", "head":"5", "weight":"0.1" },
+
+ { "tail":"7", "head":"100", "weight":"0.9" },
+
+ { "tail":"5", "head":"6", "weight":"0.3" },
+
+ { "tail":"6", "head":"100", "weight":"0.3" }
+]
+
+}
+