Skip to content

Commit

Permalink
hamming: Sync tests (#796)
Browse files Browse the repository at this point in the history
* update example solution

* add generators and regenerate tests

* update function template

* update contributors

[no important files changed]
  • Loading branch information
tasxatzial authored Jan 22, 2025
1 parent d0db435 commit 74904a5
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 31 deletions.
3 changes: 2 additions & 1 deletion exercises/practice/hamming/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"AndreaCrotti",
"haus",
"heyarne",
"sjwarner-bp"
"sjwarner-bp",
"tasxatzial"
],
"files": {
"solution": [
Expand Down
5 changes: 3 additions & 2 deletions exercises/practice/hamming/.meta/example.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns hamming)

(defn distance [a b]
(when (= (count a) (count b))
(count (filter true? (map not= a b)))))
(if (= (count a) (count b))
(count (filter true? (map not= a b)))
(throw (IllegalArgumentException. "strands must be of equal length"))))
6 changes: 6 additions & 0 deletions exercises/practice/hamming/.meta/generator.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(ns hamming-generator)

(defn update-test-case [test-case]
(if-let [error (get-in test-case [:expected :error])]
(assoc-in test-case [:expected :error] (str "^" error "$"))
test-case))
14 changes: 14 additions & 0 deletions exercises/practice/hamming/.meta/generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(ns hamming-test
(:require [clojure.test :refer [deftest testing is]]
hamming))

{{#test_cases.distance}}
(deftest distance_test_{{idx}}
(testing {{description}}
{{~#if error}}
(is (thrown-with-msg? IllegalArgumentException #{{error}}
(hamming/distance {{input.strand1}} {{input.strand2}})))))
{{else}}
(is (= {{expected}} (hamming/hamming {{input.strand1}} {{input.strand2}})))))
{{/if~}}
{{/test_cases.distance}}
6 changes: 4 additions & 2 deletions exercises/practice/hamming/src/hamming.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(ns hamming)

(defn distance [strand1 strand2] ; <- arglist goes here
;; your code goes here
(defn distance
"Returns the hamming distance between two DNA strands."
[strand1 strand2]
;; function body
)
56 changes: 30 additions & 26 deletions exercises/practice/hamming/test/hamming_test.clj
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
(ns hamming-test
(:require [clojure.test :refer [deftest is testing]]
(:require [clojure.test :refer [deftest testing is]]
hamming))

(deftest empty-strands
(testing "Empty strands"
(deftest distance_test_1
(testing "empty strands"
(is (= 0 (hamming/distance "" "")))))

(deftest single-letter-identical-strands
(testing "Single letter identical strands"
(deftest distance_test_2
(testing "single letter identical strands"
(is (= 0 (hamming/distance "A" "A")))))

(deftest single-letter-different-strands
(testing "Single letter different strands"
(deftest distance_test_3
(testing "single letter different strands"
(is (= 1 (hamming/distance "G" "T")))))

(deftest long-identical-strands
(testing "Long identical strands"
(deftest distance_test_4
(testing "long identical strands"
(is (= 0 (hamming/distance "GGACTGAAATCTG" "GGACTGAAATCTG")))))

(deftest long-different-strands
(testing "Long different strands"
(deftest distance_test_5
(testing "long different strands"
(is (= 9 (hamming/distance "GGACGGATTCTG" "AGGACGGATTCT")))))

(deftest disallow-first-strand-longer
(testing "Disallow first strand longer"
(is (= nil (hamming/distance "AATG" "AAA")))))

(deftest disallow-second-strand-longer
(testing "Disallow second strand longer"
(is (= nil (hamming/distance "ATA" "AGTG")))))

(deftest disallow-empty-first-strand
(testing "Disallow empty first strand"
(is (= nil (hamming/distance "" "G")))))

(deftest disallow-empty-second-strand
(testing "Disallow empty second strand"
(is (= nil (hamming/distance "G" "")))))
(deftest distance_test_6
(testing "disallow first strand longer"
(is (thrown-with-msg? IllegalArgumentException #"^strands must be of equal length$"
(hamming/distance "AATG" "AAA")))))

(deftest distance_test_7
(testing "disallow second strand longer"
(is (thrown-with-msg? IllegalArgumentException #"^strands must be of equal length$"
(hamming/distance "ATA" "AGTG")))))

(deftest distance_test_8
(testing "disallow empty first strand"
(is (thrown-with-msg? IllegalArgumentException #"^strands must be of equal length$"
(hamming/distance "" "G")))))

(deftest distance_test_9
(testing "disallow empty second strand"
(is (thrown-with-msg? IllegalArgumentException #"^strands must be of equal length$"
(hamming/distance "G" "")))))

0 comments on commit 74904a5

Please sign in to comment.