Skip to content

Commit 00233b7

Browse files
committed
Fix byte division.
The previous fix didn't work so I added tests for the new rounding-divide function I introduced.
1 parent 47a8501 commit 00233b7

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/kaocha/plugin/gc_profiling.clj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525
(- (.totalMemory (Runtime/getRuntime))
2626
(.freeMemory (Runtime/getRuntime))))
2727

28+
(defn rounding-divide
29+
"Divide two numbers and round them.
30+
31+
Unlike quot, this rounds rather than truncates."
32+
[m n]
33+
(Math/round (double (/ m n))))
34+
2835
(defn start [testable]
2936
(assoc testable ::start (get-memory)))
3037

@@ -93,7 +100,7 @@
93100
(doseq [{:keys [::delta :kaocha.testable/id] :as test} largest
94101
:let [n (count (remove ::testable/skip (:kaocha.result/tests test)))]]
95102
(cond
96-
(> n 0) (let [avg (long (Math/round (/ delta n)))]
103+
(> n 0) (let [avg (rounding-divide delta n)]
97104
(println (format "%s%s \n%s\033[1m%s\033[0m average (%s / %d tests)" indentation-str
98105
id (str indentation-str indentation-str)
99106
(convert-bytes avg) (convert-bytes delta) n)))

test/unit/kaocha/plugin/gc_profiling_test.clj

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,36 @@
4040
(is
4141
(= "-11B" (gc/convert-bytes -11)))))
4242

43+
(deftest rounding-divide
44+
(testing "Yielding whole numbers."
45+
(is
46+
(= 2 (gc/rounding-divide 4 2)))
47+
(is
48+
(= 2 (gc/rounding-divide 4.0 2)))
49+
(is
50+
(= 2 (gc/rounding-divide 4 2.0)))
51+
(is
52+
(= 2 (gc/rounding-divide 4.0 2.0))))
53+
(testing "Yielding rational numbers as decimals."
54+
(is
55+
(= 3 (gc/rounding-divide 5 2)))
56+
(is
57+
(= 3 (gc/rounding-divide 5 2.0)))
58+
(is
59+
(= 3 (gc/rounding-divide 5.0 2)))
60+
(is
61+
(= 3 (gc/rounding-divide 5.0 2.0))))
62+
(testing "Yielding rational numbers as decimals."
63+
(is
64+
(= -2 (gc/rounding-divide -5 2)))
65+
(is
66+
(= -2 (gc/rounding-divide -5 2.0)))
67+
(is
68+
(= -2 (gc/rounding-divide -5.0 2)))
69+
(is
70+
(= -2 (gc/rounding-divide -5.0 2.0)))))
71+
72+
4373
(deftest gc-profiling-test
4474
(plugin/with-plugins plugin-chain
4575
(is

0 commit comments

Comments
 (0)