Skip to content

Commit

Permalink
Added total function and wrote some high-level tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Romney committed Aug 19, 2011
1 parent cec6a83 commit a843b20
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
8 changes: 5 additions & 3 deletions docs/uberdoc.html
Expand Up @@ -2526,7 +2526,7 @@
ul.css('margin-top', '0px')

})
</script><title>prisoners -- Marginalia</title></head><body><table><tr><td class="docs"><div class="header"><h1 class="project-name">prisoners</h1><h2 class="project-version">0.0.6-SNAPSHOT</h2><br /><p>A Prisoner's Dilemma simulation</p>
</script><title>prisoners -- Marginalia</title></head><body><table><tr><td class="docs"><div class="header"><h1 class="project-name">prisoners</h1><h2 class="project-version">0.0.7-SNAPSHOT</h2><br /><p>A Prisoner's Dilemma simulation</p>
</div><div class="dependencies"><h3>dependencies</h3><table><tr><td class="dep-name">org.clojure/clojure</td><td class="dotted"><hr /></td><td class="dep-version">1.2.1</td></tr><tr><td class="dep-name">org.clojure/clojure-contrib</td><td class="dotted"><hr /></td><td class="dep-version">1.2.0</td></tr></table></div><div class="dependencies"><h3>dev dependencies</h3><table><tr><td class="dep-name">lein-marginalia</td><td class="dotted"><hr /></td><td class="dep-version">0.6.0</td></tr></table></div></td><td class="codes" style="text-align: center; vertical-align: middle;color: #666;padding-right:20px"><br /><br /><br />(this space intentionally left almost blank)</td></tr><tr><td class="docs"><div class="toc"><a name="toc"><h3>namespaces</h3></a><ul><li><a href="#prisoners.core">prisoners.core</a></li></ul></div></td><td class="codes">&nbsp;</td></tr><tr><td class="docs"><div class="docs-header"><a class="anchor" href="#prisoners.core" name="prisoners.core"><h1 class="project-name">prisoners.core</h1><a class="toc-link" href="#toc">toc</a></a></div></td><td class="codes" /></tr><tr><td class="docs">
</td><td class="codes" /><pre class="brush: clojure">(ns prisoners.core
(:require clojure.string)
Expand Down Expand Up @@ -2617,9 +2617,11 @@
(last
(take (inc rounds)
(iterate play-round [{:name x :points [] :plays [] :opponent []}
{:name y :points [] :plays [] :opponent []}]))))</pre></tr><tr><td class="docs"><p>Summarizes a strategy's score.</p>
{:name y :points [] :plays [] :opponent []}]))))</pre></tr><tr><td class="docs"><p>Produce a total score as the sum of the points awarded during each round.</p>
</td><td class="codes" /><pre class="brush: clojure">(defn total [strategy]
(reduce + (:points strategy)))</pre></tr><tr><td class="docs"><p>Summarizes a strategy's score.</p>
</td><td class="codes" /><pre class="brush: clojure">(defn summarize [strategy]
(str (:name strategy) &quot;: &quot; (reduce + (:points strategy)) &quot; points&quot;))</pre></tr><tr><td class="docs"><p>Calculates the resulting scores for each of the strategies.</p>
(str (:name strategy) &quot;: &quot; (total strategy) &quot; points&quot;))</pre></tr><tr><td class="docs"><p>Calculates the resulting scores for each of the strategies.</p>
</td><td class="codes" /><pre class="brush: clojure">(defn report [strategies]
(clojure.string/join &quot;, &quot; (map summarize strategies)))</pre></tr><tr><td class="docs"><h3>Running the Simulation</h3>

Expand Down
2 changes: 1 addition & 1 deletion project.clj
@@ -1,4 +1,4 @@
(defproject prisoners "0.0.6-SNAPSHOT"
(defproject prisoners "0.0.7-SNAPSHOT"
:description "A Prisoner's Dilemma simulation"
:dev-dependencies [[lein-marginalia "0.6.0"]]
:dependencies [[org.clojure/clojure "1.2.1"]
Expand Down
6 changes: 5 additions & 1 deletion src/prisoners/core.clj
Expand Up @@ -136,9 +136,13 @@
(iterate play-round [{:name x :points [] :plays [] :opponent []}
{:name y :points [] :plays [] :opponent []}]))))

;; Produce a total score as the sum of the points awarded during each round.
(defn total [strategy]
(reduce + (:points strategy)))

;; Summarizes a strategy's score.
(defn summarize [strategy]
(str (:name strategy) ": " (reduce + (:points strategy)) " points"))
(str (:name strategy) ": " (total strategy) " points"))

;; Calculates the resulting scores for each of the strategies.
(defn report [strategies]
Expand Down
41 changes: 38 additions & 3 deletions test/prisoners/test/core.clj
@@ -1,6 +1,41 @@
(ns prisoners.test.core
(:use [prisoners.core])
(:require [prisoners.core :as pri])
(:use [clojure.test]))

(deftest replace-me ;; FIXME: write
(is false "No tests have been written."))
;; Test Helpers

(defn score-round [rounds x y]
(map (fn [m] [(:name m) (pri/total m)]) (pri/play-rounds rounds x y)))

(defn score [x]
(last x))

;; Test Expected Outcomes

(deftest test-cheat-sucker
(let [[x y] (score-round 1 :cheat :sucker)]
(is
(and
(= 5 (score x))
(= 0 (score y))) ":cheat 5, :sucker 0")))

(deftest test-cheat-cheat
(let [[x y] (score-round 1 :cheat :cheat)]
(is
(and
(= 1 (score x))
(= 1 (score y))) ":cheat 1, :cheat 1")))

(deftest test-sucker-sucker
(let [[x y] (score-round 1 :sucker :sucker)]
(is
(and
(= 3 (score x))
(= 3 (score y))) ":sucker 3, :sucker 3")))

(deftest test-cheat-tit-for-tat
(let [[x y] (score-round 2 :cheat :tit-for-tat)]
(is
(and
(= 6 (last x))
(= 1 (last y))) ":cheat 6, :tit-fot-tat 1")))

0 comments on commit a843b20

Please sign in to comment.