|
9 | 9 |
|
10 | 10 | ;; Helper functions: |
11 | 11 |
|
12 | | -(defn bullet-count |
| 12 | +(defn alive-bullet-count |
13 | 13 | [world] |
14 | | - (count (:bullets world))) |
| 14 | + (count (filter #(= (:status %) :alive) (:bullets world)))) |
15 | 15 |
|
16 | | -(defn player-count |
| 16 | +(defn dead-bullets-count |
17 | 17 | [world] |
18 | | - (count (:players world))) |
| 18 | + (count (filter #(= (:status %) :dead) (:bullets world)))) |
19 | 19 |
|
20 | 20 | (defn trace-count |
| 21 | + [bullet] |
| 22 | + (count (:traces bullet))) |
| 23 | + |
| 24 | +(defn alive-player-count |
21 | 25 | [world] |
22 | | - (count (:traces world))) |
| 26 | + (count (:players world))) |
23 | 27 |
|
24 | 28 | ;; Simple tests: |
25 | 29 |
|
|
44 | 48 | (let [world1 (get-test-world) |
45 | 49 | world2 (fire world1 "player-1") |
46 | 50 | world3 (fire world2 "player-2")] |
47 | | - (is (= (bullet-count world1) 0)) |
48 | | - (is (= (bullet-count world2) 1)) |
49 | | - (is (= (bullet-count world3) 2)))) |
| 51 | + (is (= (alive-bullet-count world1) 0)) |
| 52 | + (is (= (alive-bullet-count world2) 1)) |
| 53 | + (is (= (alive-bullet-count world3) 2)))) |
50 | 54 |
|
51 | 55 | (deftest bullet-move-test |
52 | 56 | (let [planet1 (make-planet (make-point 0 0) 1 100) |
53 | 57 | planet2 (make-planet (make-point 0 5) 1 200) |
54 | 58 | planets [planet1 planet2] |
55 | | - bullet (make-bullet (make-point 5 5) (make-vector 1 1)) |
56 | | - acceleration (get-acceleration bullet planets) |
57 | | - new-velocity (vector-sum (:velocity bullet) acceleration) |
58 | | - new-bullet (move-bullet bullet planets)] |
59 | | - (is (almost= (:x (:center new-bullet)) 6)) |
60 | | - (is (almost= (:y (:center new-bullet)) 6)) |
61 | | - (is (almost= (:x (:velocity new-bullet)) |
62 | | - (:x new-velocity))) |
63 | | - (is (almost= (:y (:velocity new-bullet)) |
64 | | - (:y new-velocity))))) |
| 59 | + bullet1 (make-bullet (make-point 5 5) (make-vector 1 1)) |
| 60 | + bullet2 (update-bullet bullet1 planets) |
| 61 | + |
| 62 | + acceleration (get-acceleration bullet1 planets) |
| 63 | + new-velocity (vector-sum (:velocity bullet1) acceleration)] |
| 64 | + (is (points-almost= (:center bullet2) |
| 65 | + (make-point 6 6))) |
| 66 | + (is (vectors-almost= (:velocity bullet2) |
| 67 | + new-velocity)))) |
| 68 | + |
| 69 | +(deftest trace-test |
| 70 | + (let [planet (make-planet (make-point 1 5) 5 100) |
| 71 | + bullet1 (make-bullet (make-point 1 30) (make-vector 1 1)) |
| 72 | + bullet2 (update-bullet bullet1 [planet]) |
| 73 | + bullet3 (update-bullet bullet2 [planet])] |
| 74 | + (is (= (trace-count bullet1) 0)) |
| 75 | + (is (= (trace-count bullet2) 1)) |
| 76 | + (is (= (trace-count bullet3) 2)) |
| 77 | + |
| 78 | + (is (points-almost= (first (:traces bullet2)) |
| 79 | + (:center bullet1))) |
| 80 | + (is (points-almost= (first (:traces bullet3)) |
| 81 | + (:center bullet1))) |
| 82 | + (is (points-almost= (last (:traces bullet3)) |
| 83 | + (:center bullet2))))) |
65 | 84 |
|
66 | 85 | (deftest bullet-destroy-test |
67 | 86 | (let [planet (make-planet (make-point 0 0) 5 100) |
|
72 | 91 |
|
73 | 92 | ;; Complex tests: |
74 | 93 |
|
75 | | -(deftest simple-trace-test |
76 | | - (let [world1 (get-test-world) |
77 | | - world2 (fire world1 "player-1") |
78 | | - bullet1 (first (:bullets world2)) |
79 | | - world3 (update-world world2 1) |
80 | | - bullet2 (first (:bullets world3)) |
81 | | - trace (first (:traces world3))] |
82 | | - (is (= (first (:points trace)) |
83 | | - (:center bullet1))))) |
84 | | - |
85 | | -(deftest trace-test |
| 94 | +(deftest complex-firing-test |
86 | 95 | (let [world1 (get-test-world) |
87 | 96 | world2 (fire world1 "player-1") |
88 | | - world3 (update-world world2 10000) |
89 | | - world4 (update-world (fire world3 "player-1") 10000)] |
90 | | - (is (= (bullet-count world3) 0)) |
91 | | - (is (= (trace-count world3) 1)) |
| 97 | + world3 (update-world world2 10000)] |
| 98 | + (is (= (alive-bullet-count world2) 1)) |
| 99 | + (is (= (dead-bullets-count world2) 0)) |
92 | 100 |
|
93 | | - (is (= (bullet-count world4) 0)) |
94 | | - (is (= (trace-count world4) 2)))) |
| 101 | + (is (= (alive-bullet-count world3) 0)) |
| 102 | + (is (= (dead-bullets-count world3) 1)))) |
95 | 103 |
|
96 | 104 | (deftest kill-test |
97 | 105 | (let [world1 (get-test-world) |
98 | 106 | world2 (update-world (fire world1 "player-2") 10000)] |
99 | | - (is (= (player-count world2) 1)))) |
| 107 | + (is (= (alive-player-count world2) 1)))) |
0 commit comments