Skip to content

Commit 02a6d98

Browse files
committed
Added tests for some geometry functions.
1 parent f1e8053 commit 02a6d98

2 files changed

Lines changed: 36 additions & 11 deletions

File tree

src/hyperspace/geometry.clj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
(Math/sqrt (reduce #(+ %1 (* %2 %2)) 0.0 vtr)))
2626

2727
(defn normilize-vector
28+
"Normilizes vector, that is returns a vector with the same
29+
direction, but with the length equal 1."
2830
[vtr]
2931
(let [length (vector-length vtr)]
3032
(mapv #(/ % length) vtr)))

test/hyperspace/test/geometry.clj

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,38 @@
44
[midje.sweet]))
55

66
(fact "about vector-sum function"
7-
;; One-dimensional integer vectors
8-
(vector-sum [2] [2]) => [4]
9-
10-
;; Two-dimensional integer vectors
11-
(vector-sum [5, 5] [2, 3]) => [7, 8]
12-
13-
;; Tree-dimenstional integer vectors
14-
(vector-sum [1, 2, 3] [4, 5, 6]) => [5, 7, 9]
15-
16-
;; Floating point vectors
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]
1710
(vector-sum [0.2, 0.4] [0.1, 0.3]) => (just [(almost= 0.3)
18-
(almost= 0.7)]))
11+
(almost= 0.7)])
12+
(vector-sum [12, 62]) => [12, 62]
13+
(vector-sum [1, 2] [3, 4] [5, 6] [7, 8]) => [16, 20])
14+
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])
23+
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)]))
30+
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))
36+
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))

0 commit comments

Comments
 (0)