Skip to content

Commit

Permalink
Merge pull request clarle#3 from sjl/master
Browse files Browse the repository at this point in the history
Add a few tests and add clojure to the main README.
  • Loading branch information
clarle committed Oct 12, 2011
2 parents 3c8c3d3 + c1a0196 commit 74e0d8b
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -11,8 +11,9 @@ Implementations of any algorithms learned in the course can be developed in any
Current available languages
-----------------------------

* Python
* Clojure
* JavaScript
* Python

How to use
-----------
Expand Down
2 changes: 2 additions & 0 deletions unit02/clojure/unit02/README.markdown
Expand Up @@ -5,6 +5,8 @@ Usage
-----

lein deps
lein test

lein repl

(require '[unit02.trees :as trees])
Expand Down
8 changes: 4 additions & 4 deletions unit02/clojure/unit02/src/unit02/trees.clj
Expand Up @@ -40,10 +40,10 @@

; Sample Trees ----------------------------------------------------------------

(def sample-tree-1 (Node. :a [(node :b [(node :d [])
(node :e [])])
(node :c [(node :f [])
(node :g [])])]))
(def sample-tree-1 (node :a [(node :b [(node :d [])
(node :e [])])
(node :c [(node :f [])
(node :g [])])]))

(def sample-tree-2 (node :a [(node :b [(node :d [])
(node :e [])])
Expand Down
164 changes: 164 additions & 0 deletions unit02/clojure/unit02/test/unit02/test/trees.clj
@@ -0,0 +1,164 @@
(ns unit02.test.trees
(:use [unit02.trees])
(:use [clojure.test]))


(def tree-1 (node :a [(node :b [(node :d [])
(node :e [])])
(node :c [(node :f [])
(node :g [])])]))

(def tree-2 (node :a [(node :b [(node :d [])
(node :e [])])
(node :q [])
(node :c [(node :f [(node :x [])
(node :y [(node :z [])])])
(node :g [])])]))


(deftest test-bf-binary-paths
(is (= (:path (search-breadth tree-1 :a))
[:a])
"Breadth-first search didn't find the path to the root node")

(is (= (:path (search-breadth tree-1 :b))
[:a :b])
"Breadth-first search didn't find the path to a child node")

(is (= (:path (search-breadth tree-1 :c))
[:a :c])
"Breadth-first search didn't find the path to a child node")

(is (= (:path (search-breadth tree-1 :d))
[:a :b :d])
"Breadth-first search didn't find the path to a leaf node")

(is (= (:path (search-breadth tree-1 :g))
[:a :c :g])
"Breadth-first search didn't find the path to a leaf node"))

(deftest test-df-binary-paths
(is (= (:path (search-depth tree-1 :a))
[:a])
"Depth-first search didn't find the path to the root node")

(is (= (:path (search-depth tree-1 :b))
[:a :b])
"Depth-first search didn't find the path to a child node")

(is (= (:path (search-depth tree-1 :c))
[:a :c])
"Depth-first search didn't find the path to a child node")

(is (= (:path (search-depth tree-1 :d))
[:a :b :d])
"Depth-first search didn't find the path to a leaf node")

(is (= (:path (search-depth tree-1 :g))
[:a :c :g])
"Depth-first search didn't find the path to a leaf node"))

(deftest test-bf-nonbinary-paths
(is (= (:path (search-breadth tree-2 :a))
[:a])
"Breadth-first search didn't find the path to the root node")

(is (= (:path (search-breadth tree-2 :q))
[:a :q])
"Breadth-first search didn't find the path to a leaf node")

(is (= (:path (search-breadth tree-2 :c))
[:a :c])
"Breadth-first search didn't find the path to a child node")

(is (= (:path (search-breadth tree-2 :y))
[:a :c :f :y])
"Breadth-first search didn't find the path to a child node")

(is (= (:path (search-breadth tree-2 :z))
[:a :c :f :y :z])
"Breadth-first search didn't find the path to a leaf node"))

(deftest test-df-nonbinary-paths
(is (= (:path (search-depth tree-2 :a))
[:a])
"Depth-first search didn't find the path to the root node")

(is (= (:path (search-depth tree-2 :q))
[:a :q])
"Depth-first search didn't find the path to a child node")

(is (= (:path (search-depth tree-2 :c))
[:a :c])
"Depth-first search didn't find the path to a child node")

(is (= (:path (search-depth tree-2 :y))
[:a :c :f :y])
"Depth-first search didn't find the path to a leaf node")

(is (= (:path (search-depth tree-2 :z))
[:a :c :f :y :z])
"Depth-first search didn't find the path to a leaf node"))
(deftest test-bf-binary-costs
(is (= (:examined (search-breadth tree-1 :a)) 1)
"Breadth-first search didn't examine the correct number of nodes to find the root node")

(is (= (:examined (search-breadth tree-1 :b)) 2)
"Breadth-first search didn't examine the correct number of nodes to find a child node")

(is (= (:examined (search-breadth tree-1 :c)) 3)
"Breadth-first search didn't examine the correct number of nodes to find a child node")

(is (= (:examined (search-breadth tree-1 :d)) 4)
"Breadth-first search didn't examine the correct number of nodes to find a leaf node")

(is (= (:examined (search-breadth tree-1 :g)) 7)
"Breadth-first search didn't examine the correct number of nodes to find a leaf node"))

(deftest test-df-binary-costs
(is (= (:examined (search-depth tree-1 :a)) 1)
"Depth-first search didn't examine the correct number of nodes to find the root node")

(is (= (:examined (search-depth tree-1 :b)) 2)
"Depth-first search didn't examine the correct number of nodes to find a child node")

(is (= (:examined (search-depth tree-1 :c)) 5)
"Depth-first search didn't examine the correct number of nodes to find a child node")

(is (= (:examined (search-depth tree-1 :d)) 3)
"Depth-first search didn't examine the correct number of nodes to find a leaf node")

(is (= (:examined (search-depth tree-1 :g)) 7)
"Depth-first search didn't examine the correct number of nodes to find a leaf node"))

(deftest test-bf-nonbinary-costs
(is (= (:examined (search-breadth tree-2 :a)) 1)
"Breadth-first search didn't examine the correct number of nodes to find the root node")

(is (= (:examined (search-breadth tree-2 :q)) 3)
"Breadth-first search didn't examine the correct number of nodes to find a leaf node")

(is (= (:examined (search-breadth tree-2 :c)) 4)
"Breadth-first search didn't examine the correct number of nodes to find a child node")

(is (= (:examined (search-breadth tree-2 :y)) 10)
"Breadth-first search didn't examine the correct number of nodes to find a child node")

(is (= (:examined (search-breadth tree-2 :z)) 11)
"Breadth-first search didn't examine the correct number of nodes to find a leaf node"))

(deftest test-df-nonbinary-costs
(is (= (:examined (search-depth tree-2 :a)) 1)
"Depth-first search didn't examine the correct number of nodes to find the root node")

(is (= (:examined (search-depth tree-2 :q)) 5)
"Depth-first search didn't examine the correct number of nodes to find a child node")

(is (= (:examined (search-depth tree-2 :c)) 6)
"Depth-first search didn't examine the correct number of nodes to find a child node")

(is (= (:examined (search-depth tree-2 :y)) 9)
"Depth-first search didn't examine the correct number of nodes to find a leaf node")

(is (= (:examined (search-depth tree-2 :z)) 10)
"Depth-first search didn't examine the correct number of nodes to find a leaf node"))

0 comments on commit 74e0d8b

Please sign in to comment.