Skip to content

Commit 80d4b00

Browse files
committed
Some changes:
* almost= is a chatty checker now * removed some unnecessary assertions
1 parent 02a6d98 commit 80d4b00

4 files changed

Lines changed: 50 additions & 54 deletions

File tree

src/hyperspace/geometry.clj

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@
1919
[vtr scalar]
2020
(mapv #(* scalar %) vtr))
2121

22+
(defn polar->cartesian
23+
"Converts polar coordinates to the cartesian ones."
24+
[[a, d]]
25+
[(* d (Math/cos a))
26+
(* d (Math/sin a))])
27+
28+
(defn cartesian->polar
29+
"Converts cartesian cooradinates to the polar ones."
30+
[[x, y]]
31+
[(Math/atan2 y x)
32+
(Math/sqrt (+ (* x x) (* y y)))])
33+
2234
(defn vector-length
2335
"Returns the length of vtr."
2436
[vtr]
@@ -36,18 +48,6 @@
3648
[p1 p2]
3749
(vector-length (vector-subtract p1 p2)))
3850

39-
(defn polar->cartesian
40-
"Converts polar coordinates to the cartesian ones."
41-
[[a, d]]
42-
[(* d (Math/cos a))
43-
(* d (Math/sin a))])
44-
45-
(defn cartesian->polar
46-
"Converts cartesian cooradinates to the polar ones."
47-
[[x, y]]
48-
[(Math/atan2 y x)
49-
(Math/sqrt (+ (* x x) (* y y)))])
50-
5151
(defn circle-X-circle?
5252
"Does the first circle intersects the second one?"
5353
[{position1 :position radius1 :radius}

test/hyperspace/test/checkers.clj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(ns hyperspace.test.checkers
2+
(:use [midje.sweet]))
3+
4+
(defchecker almost= [expected]
5+
(chatty-checker [actual]
6+
(< (Math/abs (- expected actual)) 1e-6)))

test/hyperspace/test/geometry.clj

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,40 @@
11
(ns hyperspace.test.geometry
22
(:use [hyperspace.geometry]
3-
[hyperspace.test.utils]
3+
[hyperspace.test.checkers]
44
[midje.sweet]))
55

6-
(fact "about vector-sum function"
7-
(vector-sum [2] [2]) => [4]
8-
(vector-sum [5, 5] [2, 3]) => [7, 8]
9-
(vector-sum [1, 2, 3] [4, 5, 6]) => [5, 7, 9]
10-
(vector-sum [0.2, 0.4] [0.1, 0.3]) => (just [(almost= 0.3)
11-
(almost= 0.7)])
12-
(vector-sum [12, 62]) => [12, 62]
13-
(vector-sum [1, 2] [3, 4] [5, 6] [7, 8]) => [16, 20])
6+
(facts "about vector-sum function"
7+
(vector-sum [5, 5] [2, 3]) => [7, 8]
8+
(vector-sum [0.2, 0.4] [0.1, 0.3]) => (just [(almost= 0.3)
9+
(almost= 0.7)])
10+
(vector-sum [12, 62]) => [12, 62])
1411

15-
(fact "about vector-subtract function"
16-
(vector-subtract [3] [2]) => [1]
17-
(vector-subtract [3, 2] [2, 1]) => [1, 1]
18-
(vector-subtract [4, 5, 6] [6, 5, 4]) => [-2, 0, 2]
19-
(vector-subtract [0.4 0.2] [0.3 0.2]) => (just [(almost= 0.1)
20-
(almost= 0.0)])
21-
(vector-subtract [4, 6]) => [-4, -6]
22-
(vector-subtract [100, 100] [2, 5] [5, 2] [9, 3]) => [84, 90])
12+
(facts "about vector-subtract function"
13+
(vector-subtract [3, 2] [2, 1]) => [1, 1]
14+
(vector-subtract [0.4 0.2] [0.3 0.2]) => (just [(almost= 0.1)
15+
(almost= 0.0)])
16+
(vector-subtract [4, 6]) => [-4, -6])
2317

24-
(fact "about multiply-by-scalar function"
25-
(multiply-by-scalar [2] 10) => [20]
26-
(multiply-by-scalar [1, 2] 2) => [2, 4]
27-
(multiply-by-scalar [5, 2, 1] 5) => [25, 10, 5]
28-
(multiply-by-scalar [0.2, 0.5] 3) => (just [(almost= 0.6),
29-
(almost= 1.5)]))
18+
(facts "about multiply-by-scalar function"
19+
(multiply-by-scalar [1, 2] 2) => [2, 4]
20+
(multiply-by-scalar [0.2, 0.5] 3) => (just [(almost= 0.6),
21+
(almost= 1.5)]))
3022

31-
(fact "about vector-length function"
32-
(vector-length [7]) => (almost= 7)
33-
(vector-length [10, 10]) => (almost= 14.142135623730951)
34-
(vector-length [34, 123, 431]) => (almost= 449.4952725001677)
35-
(vector-length [1.23, 10.32]) => (almost= 10.393040940937354))
23+
(facts "about vector-length function"
24+
(vector-length [10, 10]) => (almost= 14.142135623730951)
25+
(vector-length [1.23, 10.32]) => (almost= 10.393040940937354))
3626

37-
(fact "about normilize-vector function"
38-
(vector-length (normilize-vector [7])) => (almost= 1.0)
39-
(vector-length (normilize-vector [10, 10])) => (almost= 1.0)
40-
(vector-length (normilize-vector [34, 123, 431])) => (almost= 1.0)
41-
(vector-length (normilize-vector [1.23, 10.32])) => (almost= 1.0))
27+
(facts "about normilize-vector function"
28+
(cartesian->polar (normilize-vector [10, 10]))
29+
=>
30+
(just [(almost= (-> [10, 10] cartesian->polar first))
31+
(almost= 1.0)])
32+
33+
(cartesian->polar (normilize-vector [1.23, 10.32]))
34+
=>
35+
(just [(almost= (-> [1.23, 10.32] cartesian->polar first))
36+
(almost= 1.0)]))
37+
38+
(facts "about distance function"
39+
(distance [0, 0] [10, 10]) => (almost= (vector-length [10, 10]))
40+
(distance [34, 12] [-24, 42]) => (almost= 65.29931086925804))

test/hyperspace/test/utils.clj

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)