Skip to content

Commit

Permalink
Enhance test coverage (#164)
Browse files Browse the repository at this point in the history
* Enhance test coverage

* Remove splint from lint sequence in bb.edn

The changes of this commit are made to the lint sequence inside bb.edn file. The 'splint' style check was removed from the sequencing. Now, the lint sequence now includes only 'cljfmt', 'cljstyle', 'eastwood', and 'kondo'. This simplifies the sequence and prioritizes the rest of the style checks.

* Refactor complex number functionality and add tests

The code was restructured to reorder, rename, and modify complex number functions in Clojure for two models ("Ben's way" and "Alyssa's way"). Float typecasting was added to some functions to ensure accurate calculations. Corresponding tests were also implemented in a new test file to validate and ensure the correctness of these revised functions. These changes enhance code clarity and testing comprehensiveness in handling complex numbers.

* Reformat imports in book_2_4_test.clj

The :require block in the testing file book_2_4_test.clj has been reformatted to improve readability. No functionality was changed in this commit, just the way the code is organized for aesthetically pleasing and more manageable structure.

* Refactor exception handling across multiple test files

Refactored exception handling in tests across multiple modules, replacing previous implementations with the centralized and enhanced `is-exception?` macro. This macro captures the exception type and message and eliminates repeated code, enhancing code cleanliness and maintainability.

* Modify exception checking in tests

Refactored the way exceptions are checked in tests across various modules. This was done by changing from direct exception checks to using the `is-exception?` macro. This improves code readability and makes the test scripts more maintainable.

* Correct naming for test coverage workflow

Fixed a typo and improved the naming pattern for the "test coverage" workflow in the main GitHub actions YAML file. This change contributes to better workflow organization and execution clarity.

* Add test files for ex-2-73 and ex-2-75 and improve derivation functions

Created new test files for exercises 2-73 and 2-75. The test cases for these exercises are now fully covered. Also, made some improvements in the derivation functions in exercise 2-73 by replacing `put` with `put-op`
and `get` with `get-op` methods to manage operations in a more optimized way.

* Update clj-kondo configuration and clojure.test import

Updated the clj-kondo command in the lint task to run in parallel, improving performance. Also simplified the clojure.test import statement in the ex_2_75_test file to only include 'deftest' and 'is', making the dependencies clearer and more specific.

* Update clj-kondo settings and refactor test file imports

The clj-kondo command in the lint task has been updated to run in parallel, setting the fail level as a warning. This amendment should improve performance by running in a non-blocking mode. The clojure.test import usage in the book_2_

* Refactor exception handling and update test cases

Simplified exception handling by removing explicit checks for exception types, now only checks for expected messages. Updated multiple test cases correspondingly. Also made minor alterations to error messages for better clarity.

* Refactor error message formatting in misc.clj

The error message formatting has been adjusted for clarity in the misc.clj file. An extraneous check in the 'apply-generic-test' in the book_2_5_test.clj file has also been removed for improved simplicity.
  • Loading branch information
SmetDenis authored Feb 11, 2024
1 parent 3fe2e5f commit 03626ed
Show file tree
Hide file tree
Showing 28 changed files with 359 additions and 131 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
run: lein test

tests-coverage:
name: Test Coverge
name: Tests - Coverge
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand Down
5 changes: 2 additions & 3 deletions bb.edn
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
style:cljfmt (shell "cljfmt check")
style:cljstyle (shell "cljstyle check")
style:splint (shell "lein splint ./src ./test")
lint (do (run 'style:splint)
(run 'style:cljfmt)
lint (do (run 'style:cljfmt)
(run 'style:cljstyle)
(run 'lint:eastwood)
(run 'lint:kondo))
Expand All @@ -15,7 +14,7 @@
(run 'fix:cljstyle))
; Linters
lint:eastwood (shell "clojure -M:test:eastwood")
lint:kondo (shell "clj-kondo --lint test src")
lint:kondo (shell "clj-kondo --parallel --fail-level warning --lint test src")
; Local development
pre-push (do (run 'fix)
(run 'lint)
Expand Down
2 changes: 1 addition & 1 deletion src/sicp/chapter_2/part_1/ex_2_10.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
(if (< (* (m/lower-bound interval-2)
(m/upper-bound interval-2))
0)
(m/error "interval-2 is spanning zero")
(m/error "Interval-2 is spanning zero")
(m/mul-interval
interval-1
(m/make-interval (/ 1.0 (m/lower-bound interval-2))
Expand Down
5 changes: 2 additions & 3 deletions src/sicp/chapter_2/part_2/book_2_2.clj
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,8 @@
(defn my-filter
[predicate sequence]
(cond (m/list-empty? sequence) nil
(predicate (m/car sequence))
(cons (m/car sequence)
(my-filter predicate (m/cdr sequence)))
(predicate (m/car sequence)) (cons (m/car sequence)
(my-filter predicate (m/cdr sequence)))
:else (my-filter predicate (m/cdr sequence))))

(defn accumulate
Expand Down
82 changes: 41 additions & 41 deletions src/sicp/chapter_2/part_4/book_2_4.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,81 +4,81 @@

(comment "2.4.1 Representations for Complex Numbers ----------------------------------------------")

; Ben's way
(defn real-part-Ben
; Ben's way ----------------------------------------------------------------------------------------
(defn Ben-real-part
[z]
(first z))
(float (first z)))

(defn imag-part-Ben
(defn Ben-imag-part
[z]
(second z))
(float (second z)))

(defn magnitude-Ben
(defn Ben-magnitude
[z]
(Math/sqrt (+ (Math/pow (real-part-Ben z) 2)
(Math/pow (imag-part-Ben z) 2))))
(Math/sqrt (+ (Math/pow (Ben-real-part z) 2)
(Math/pow (Ben-imag-part z) 2))))

(defn angle-v1
(defn Ben-angle
[z]
(Math/atan2 (imag-part-Ben z) (real-part-Ben z)))
(Math/atan2 (Ben-imag-part z) (Ben-real-part z)))

(defn make-from-real-imag-Ben
([x y] [x y])
([z] [(real-part-Ben z) (imag-part-Ben z)]))
(defn Ben-make-from-real-imag
([x y] [(float x) (float y)])
([z] [(Ben-real-part z) (Ben-imag-part z)]))

(defn make-from-mag-ang-Ben
(defn Ben-make-from-mag-ang
([r a] [(* r (Math/cos a)) (* r (Math/sin a))])
([z] [(magnitude-Ben z) (angle-v1 z)]))
([z] [(Ben-magnitude z) (Ben-angle z)]))

(defn add-complex-Ben
(defn Ben-add-complex
[z1 z2]
(make-from-real-imag-Ben
(+ (real-part-Ben z1) (real-part-Ben z2))
(+ (imag-part-Ben z1) (imag-part-Ben z2))))
(Ben-make-from-real-imag
(+ (Ben-real-part z1) (Ben-real-part z2))
(+ (Ben-imag-part z1) (Ben-imag-part z2))))

(defn sub-complex-Ben
(defn Ben-sub-complex
[z1 z2]
(make-from-real-imag-Ben
(- (real-part-Ben z1) (real-part-Ben z2))
(- (imag-part-Ben z1) (imag-part-Ben z2))))
(Ben-make-from-real-imag
(- (Ben-real-part z1) (Ben-real-part z2))
(- (Ben-imag-part z1) (Ben-imag-part z2))))

(defn mul-complex-Ben
(defn Ben-mul-complex
[z1 z2]
(make-from-mag-ang-Ben
(* (magnitude-Ben z1) (magnitude-Ben z2))
(+ (angle-v1 z1) (angle-v1 z2))))
(Ben-make-from-mag-ang
(* (Ben-magnitude z1) (Ben-magnitude z2))
(+ (Ben-angle z1) (Ben-angle z2))))

(defn div-complex-Ben
(defn Ben-div-complex
[z1 z2]
(make-from-mag-ang-Ben
(/ (magnitude-Ben z1) (magnitude-Ben z2))
(- (angle-v1 z1) (angle-v1 z2))))
(Ben-make-from-mag-ang
(/ (Ben-magnitude z1) (Ben-magnitude z2))
(- (Ben-angle z1) (Ben-angle z2))))

; Alyssa's way
(defn magnitude-Alyssa
; Alyssa's way -------------------------------------------------------------------------------------
(defn Alyssa-magnitude
[z]
(first z))

(defn angle-Alyssa
(defn Alyssa-angle
[z]
(second z))

(defn real-part-Alyssa
(defn Alyssa-real-part
[z]
(* (magnitude-Alyssa z) (Math/cos (angle-Alyssa z))))
(* (Alyssa-magnitude z) (Math/cos (Alyssa-angle z))))

(defn imag-part-Alyssa
(defn Alyssa-imag-part
[z]
(* (magnitude-Alyssa z) (Math/sin (angle-Alyssa z))))
(* (Alyssa-magnitude z) (Math/sin (Alyssa-angle z))))

(defn make-from-real-imag-Alyssa
(defn Alyssa-make-from-real-imag
[x y]
[(Math/sqrt (+ (Math/pow x 2) (Math/pow y 2)))
(Math/atan2 y x)])

(defn make-from-mag-ang-Alyssa
(defn Alyssa-make-from-mag-ang
[r a]
[r a])
[(float r) (float a)])

(comment "2.4.2")

Expand Down
23 changes: 14 additions & 9 deletions src/sicp/chapter_2/part_4/ex_2_73.clj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@
; ((get (operator exp) 'deriv)
; (operands exp) var)

(def derivations (atom {}))

(defn put-op
[op method deriv-fn]
(swap! derivations assoc-in [op method] deriv-fn))

(defn get-op
[op method]
(get-in @derivations [op method]))

(defn deriv
[exp var]
(cond
Expand Down Expand Up @@ -87,7 +97,7 @@
(cond
(number? exp) 0
(b23/variable? exp) (if (b23/same-variable? exp var) 1 0)
:else ((get :deriv (operator exp))
:else ((get-op :deriv (operator exp))
(operands exp)
var)))

Expand All @@ -96,11 +106,6 @@
; Sorry, I'm lazy and took examples of code here and rewrite it to Clojure
; https://github.com/ivanjovanovic/sicp/blob/master/2.4/e-2.73.scm

(defn put
[param1 param2 deriver]
; Just for linter
(println param1 param2 deriver))

(defn make-sum
([a b] (list '+ a b))
([a b c] (list '+ a b c)))
Expand All @@ -118,7 +123,7 @@
(make-sum (deriv (addend operands) var)
(deriv (augend operands) var)))]
; and methods for putting the thing in the table
(put '+ 'deriv derive-sum)))
(put-op '+ :deriv derive-sum)))

(defn install-product-derivation
[]
Expand All @@ -133,7 +138,7 @@
(multiplicand operands))
var))]
; put that into table
(put '* 'deriv derive-product)))
(put-op '* :deriv derive-product)))

(defn install-exponent-derivation
[]
Expand All @@ -146,4 +151,4 @@
(make-product (power operands)
(make-exponent (base operands) (dec (power operands))))
(deriv (base operands) var)))]
(put '** 'deriv derive-exponent)))
(put-op '** :deriv derive-exponent)))
2 changes: 1 addition & 1 deletion src/sicp/chapter_2/part_5/book_2_5.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
operation (get-op op type-tags)]
(if operation
(apply operation (map #(:contents %) args))
(throw (IllegalArgumentException. (str "No method for: " op " " type-tags))))))
(throw (IllegalArgumentException. (str "No method for: " op))))))

; --------------------------------------------------------------------------------------------------

Expand Down
17 changes: 16 additions & 1 deletion src/sicp/misc.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
(ns sicp.misc)
(ns sicp.misc
(:require
[clojure.test :refer [is]]))

(comment "Chapter #1 The Elements of Programming -------------------------------------------------")

Expand Down Expand Up @@ -191,3 +193,16 @@
(if (empty? list1)
(if (empty? list2) '() list2)
(cons (first list1) (append (rest list1) list2))))

(defmacro is-exception?
[test-code & [expected-message]]
`(try
(do
~test-code
(is false "Execution of the code expects any type of exception to be thrown"))
(catch Exception exception#
(cond
(not (nil? ~expected-message)) (is (= (.getMessage exception#) ~expected-message)
(str "Expected message: " ~expected-message ", but got: " (.getMessage exception#)))
; For valid test cases, the expected exception type and message should be nil
:else true))))
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns sicp.chapter-1.part-1.ex-1-3-test
(ns sicp.chapter-1.part-1.ex-1-03-test
(:require
[clojure.test :refer [deftest is]]
[sicp.chapter-1.part-1.ex-1-03 :refer [sum-larger-numbers-square]]))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns sicp.chapter-1.part-1.ex-1-4-test
(ns sicp.chapter-1.part-1.ex-1-04-test
(:require
[clojure.test :refer [deftest is]]
[sicp.chapter-1.part-1.ex-1-04 :refer [a-plus-abs-b]]))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns sicp.chapter-1.part-1.ex-1-5-test
(ns sicp.chapter-1.part-1.ex-1-05-test
(:require
[clojure.test :refer [deftest is]]
[sicp.chapter-1.part-1.ex-1-05 :refer [p test_1_5]]))
Expand All @@ -7,4 +7,5 @@
; Normal: Operand "p" will not be evaluated until it's needed by some primitive operation. So result is 0.
(is (= 0 (test_1_5 0 p)))
; Applicative: Operand "y" will be by default evaluated. Then it will end up in recursion since (p) points to itself.
(is (= p (test_1_5 1 p))))
(is (= p (test_1_5 1 p)))
(is (= p (test_1_5 1 (p)))))
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns sicp.chapter-1.part-1.ex-1-6-test
(ns sicp.chapter-1.part-1.ex-1-06-test
(:require
[clojure.test :refer [deftest is]]
[sicp.chapter-1.part-1.book-1-1 :as b11]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns sicp.chapter-1.part-1.ex-1-7-test
(ns sicp.chapter-1.part-1.ex-1-07-test
(:require
[clojure.test :refer [deftest is]]
[sicp.chapter-1.part-1.book-1-1 :as b11]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns sicp.chapter-1.part-1.ex-1-8-test
(ns sicp.chapter-1.part-1.ex-1-08-test
(:require
[clojure.test :refer [deftest is]]
[sicp.chapter-1.part-1.ex-1-08 :refer [cube-root-iter]]))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
(ns sicp.chapter-1.part-2.ex-1-9-test
(ns sicp.chapter-1.part-2.ex-1-09-test
(:require
[clojure.test :refer [deftest is]]
[sicp.chapter-1.part-2.ex-1-09 :refer [plus plus-v2]]))

(deftest plus-test
(is (= 3 (plus 1 2))))
(is (= 3 (plus 1 2)))
(is (= 2 (plus 0 2))))

(deftest plus-v2-test
(is (= 3 (plus-v2 1 2))))
(is (= 3 (plus-v2 1 2)))
(is (= 2 (plus-v2 0 2))))
7 changes: 6 additions & 1 deletion test/sicp/chapter_1/part_2/ex_1_25_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
(ns sicp.chapter-1.part-2.ex-1-25-test
(:require
[clojure.test :refer [deftest is]]
[sicp.chapter-1.part-2.ex-1-25 :refer [expmod expmod-alyssa]]))
[sicp.chapter-1.part-2.ex-1-25 :refer [expmod expmod-alyssa fast-expt]]))

(deftest fast-expt-test
(is (= 1 (fast-expt 2 0)))
(is (= 16 (fast-expt 2 4)))
(is (= 3125 (fast-expt 5 5))))

(deftest expmod-test
(is (= 1 (expmod 2 0 3)))
Expand Down
5 changes: 4 additions & 1 deletion test/sicp/chapter_1/part_2/ex_1_26_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
[sicp.chapter-1.part-2.ex-1-26 :refer [expmod]]))

(deftest expmod-test
(is (= 1 (expmod 2 0 3))))
(is (= 1 (expmod 2 0 3)))
(is (= 1 (expmod 2 4 3)))
(is (= 25 (expmod 5 10 40)))
(is (= 0 (expmod 10 5 40))))
13 changes: 12 additions & 1 deletion test/sicp/chapter_1/part_3/book_1_3_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@
(is (= 0.24998750000000042 (b13/integral m/cube 0 1 0.01)))
(is (= 0.249999875000001 (b13/integral m/cube 0 1 0.001))))

(deftest f-1-test
(is (= 4 (b13/f-1 1 2)))
(is (= 603 (b13/f-1 2 10)))
(is (= 178 (b13/f-1 2 5))))

(deftest f-2-test
(is (= 4 (b13/f-2 1 2)))
(is (= 603 (b13/f-1 2 10)))
(is (= 178 (b13/f-2 2 5))))

(comment "1.3.2 Constructing Procedures Using Lambda ---------------------------------------------")

(deftest pi-sum-lamda-test
Expand All @@ -76,7 +86,8 @@
(deftest half-interval-method-test
(is (= 6.103515625E-5 (b13/half-interval-method m/cube -1.0 9.0)))
(is (= 3.14111328125 (b13/half-interval-method #(Math/sin %) 2.0 4.0)))
(is (= 1.89306640625 (b13/half-interval-method #(- (m/cube %) (* 2 %) 3) 1.0 2.0))))
(is (= 1.89306640625 (b13/half-interval-method #(- (m/cube %) (* 2 %) 3) 1.0 2.0)))
(is (= true (m/is-exception? (b13/half-interval-method m/cube 1 1) "Values are not of opposite sign 1 1"))))

(deftest fixed-point-test
(is (= 0.7390822985224024 (b13/fixed-point #(Math/cos %) -1.0)))
Expand Down
6 changes: 4 additions & 2 deletions test/sicp/chapter_2/part_1/book_2_1_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns sicp.chapter-2.part-1.book-2-1-test
(:require
[clojure.test :refer [deftest is]]
[sicp.chapter-2.part-1.book-2-1 :as b21]))
[sicp.chapter-2.part-1.book-2-1 :as b21]
[sicp.misc :as m]))

(comment "2 Building Abstractions with Data ------------------------------------------------------")

Expand Down Expand Up @@ -75,4 +76,5 @@

(deftest pair-alt-test
(is (= 1 (b21/car-alt (b21/pair-alt 1 2))))
(is (= 2 (b21/cdr-alt (b21/pair-alt 1 2)))))
(is (= 2 (b21/cdr-alt (b21/pair-alt 1 2))))
(is (= true (m/is-exception? ((b21/pair-alt 1 2) 2) "Argument not 0 or 1: CONS"))))
Loading

0 comments on commit 03626ed

Please sign in to comment.