Kibitised, and improved test coverage. Cloverage currently failing, don't

know why.
This commit is contained in:
Simon Brooke 2014-08-13 20:02:37 +01:00
parent 3b1d28d3cd
commit 1030ece3ff
5 changed files with 33 additions and 9 deletions

View file

@ -40,7 +40,7 @@
"Set the `gradient` property of this `cell` of this `world` to the difference in "Set the `gradient` property of this `cell` of this `world` to the difference in
altitude between its highest and lowest neghbours." altitude between its highest and lowest neghbours."
[world cell] [world cell]
(let [heights (remove nil? (map #(:altitude %) (get-neighbours world cell))) (let [heights (remove nil? (map :altitude (get-neighbours world cell)))
highest (cond (empty? heights) 0 ;; shouldn't happen highest (cond (empty? heights) 0 ;; shouldn't happen
true (apply max heights)) true (apply max heights))
lowest (cond (empty? heights) 0 ;; shouldn't lowest (cond (empty? heights) 0 ;; shouldn't

View file

@ -11,7 +11,7 @@
* `n` a number, on the set of real numbers." * `n` a number, on the set of real numbers."
[n] [n]
(cond (< n 0) (- 0 n) true n)) (cond (neg? n) (- 0 n) true n))
(defn member? (defn member?
"True if elt is a member of col." "True if elt is a member of col."
@ -161,7 +161,7 @@
(or (property downstream) default)) (first cells) (or (property downstream) default)) (first cells)
true downstream)))) true downstream))))
([cells property] ([cells property]
(get-least-cell cells property (. Integer MAX_VALUE)))) (get-least-cell cells property (Integer/MAX_VALUE))))
(defn- set-cell-property (defn- set-cell-property

View file

@ -8,7 +8,8 @@
;; that every cell's :x and :y properties reflect its place in the matrix. ;; that every cell's :x and :y properties reflect its place in the matrix.
(ns mw-engine.world (ns mw-engine.world
(:use mw-engine.utils)) (:use mw-engine.utils)
(:require [clojure.string :as string :only [join]]))
(defn- make-cell (defn- make-cell
"Create a minimal default cell at x, y "Create a minimal default cell at x, y
@ -27,7 +28,7 @@
[index width height] [index width height]
(cond (= index width) nil (cond (= index width) nil
true (cons (make-cell index height) true (cons (make-cell index height)
(make-world-row (+ index 1) width height)))) (make-world-row (inc index) width height))))
(defn- make-world-rows [index width height] (defn- make-world-rows [index width height]
"Make the (remaining) rows in a world of this width and height, from this "Make the (remaining) rows in a world of this width and height, from this
@ -38,7 +39,7 @@
* `height` total height of the matrix, in cells." * `height` total height of the matrix, in cells."
(cond (= index height) nil (cond (= index height) nil
true (cons (apply vector (make-world-row 0 width index)) true (cons (apply vector (make-world-row 0 width index))
(make-world-rows (+ index 1) width height)))) (make-world-rows (inc index) width height))))
(defn make-world (defn make-world
"Make a world width cells from east to west, and height cells from north to "Make a world width cells from east to west, and height cells from north to
@ -53,7 +54,7 @@
"Truncate the print name of the state of this cell to at most limit characters." "Truncate the print name of the state of this cell to at most limit characters."
[cell limit] [cell limit]
(let [s (:state cell)] (let [s (:state cell)]
(cond (> (count (.toString s)) 10) (subs s 0 10) (cond (> (count (str s)) limit) (subs s 0 limit)
true s))) true s)))
(defn format-cell (defn format-cell
@ -67,8 +68,7 @@
(defn- format-world-row (defn- format-world-row
"Format one row in the state of a world for printing." "Format one row in the state of a world for printing."
[row] [row]
(apply str (string/join (map format-cell row)))
(map format-cell row)))
(defn print-world (defn print-world
"Print the current state of this world, and return nil. "Print the current state of this world, and return nil.

View file

@ -2,6 +2,7 @@
(:use clojure.java.io) (:use clojure.java.io)
(:require [clojure.test :refer :all] (:require [clojure.test :refer :all]
[mw-engine.heightmap :refer :all] [mw-engine.heightmap :refer :all]
[mw-engine.world :as world :only [make-world]]
[clojure.math.combinatorics :as combo])) [clojure.math.combinatorics :as combo]))
(deftest apply-heightmap-test (deftest apply-heightmap-test
@ -11,6 +12,21 @@
gradients (map #(:gradient %) (flatten world))] gradients (map #(:gradient %) (flatten world))]
(is (= (count world) 9) "World should be 9x9") (is (= (count world) 9) "World should be 9x9")
(is (= (count (first world)) 9) "World should be 9x9") (is (= (count (first world)) 9) "World should be 9x9")
(is (= (count (remove nil? altitudes)) 81)
"All cells should have altitude")
(is (= (count (remove nil? gradients)) 81)
"All cells should have gradient")
(is (> (apply max altitudes)
(apply min altitudes))
"There should be a range of altitudes")
(is (> (apply + gradients) 0)
"At least some gradients must be positive, none should be negative"))
;; alternate means of making the world, same tests.
(let [world (apply-heightmap (world/make-world 9 9) (as-file "resources/heightmaps/test9x9.png"))
altitudes (map #(:altitude %) (flatten world))
gradients (map #(:gradient %) (flatten world))]
(is (= (count world) 9) "World should be 9x9")
(is (= (count (first world)) 9) "World should be 9x9")
(is (= (count (remove nil? altitudes)) 81) (is (= (count (remove nil? altitudes)) 81)
"All cells should have altitude") "All cells should have altitude")
(is (= (count (remove nil? gradients)) 81) (is (= (count (remove nil? gradients)) 81)

View file

@ -4,6 +4,14 @@
[clojure.math.combinatorics :as combo] [clojure.math.combinatorics :as combo]
[mw-engine.utils :refer :all])) [mw-engine.utils :refer :all]))
(deftest abs-test
(testing "Absolute value function"
(is (= (abs 0) 0) "Corner case: nothing comes of nothing, nothing ever could.")
(is (= (abs 1) 1) "Corner case: one is one and all alone and ever more shall be so.")
(is (= (abs -1) 1) "Corner case: when others are cast down and afflicted, thou shalt be able to raise them up.")
(is (= (abs -90371) 90371) "Random check")
(is (= (abs 30971) 30971) "Another random check")))
(deftest get-neighbours-test (deftest get-neighbours-test
(testing "Gross functionality of get-neighbours: checks the right number of (testing "Gross functionality of get-neighbours: checks the right number of
neighbours returned, doesn't actually check they're the right ones." neighbours returned, doesn't actually check they're the right ones."