Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isbn-verifier: Sync tests #706

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion exercises/practice/isbn-verifier/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"AndreaCrotti",
"haus",
"jgomo3",
"sjwarner-bp"
"sjwarner-bp",
"tasxatzial"
],
"files": {
"solution": [
Expand Down
33 changes: 20 additions & 13 deletions exercises/practice/isbn-verifier/.meta/src/example.clj
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
(ns isbn-verifier)

(defn is-in? [xs x] (some #(= x %) xs))
(defn- get-value
[ch]
(if (= ch \X)
10
(Character/digit ^char ch 10)))

(defn isbn-chars [isbn]
(filter #(is-in? [\0 \1 \2 \3 \4 \5 \6 \7 \8 \9 \X] %) isbn))
(defn- valid-isbn-value?
[s]
(->> (remove #{\-} s)
(map get-value)
(map * (range 10 0 -1))
(reduce +)
(#(mod % 11))
zero?))

(defn isbn? [isbn]
(let [chars (isbn-chars isbn)
nums (map #(if (= \X %) 10 (Character/digit % 10)) chars)]
(and (-> chars butlast (is-in? \X) not)
(= 10 (count chars))
(as-> nums x
(map #(* %1 %2) (range 10 0 -1) x)
(reduce + x)
(mod x 11)
(zero? x)))))
(defn valid-isbn-pattern?
[s]
(boolean (re-matches #"\d{9}(X|\d)|\d-\d{3}-\d{5}-(X|\d)" s)))

(defn isbn?
[s]
(and (valid-isbn-pattern? s) (valid-isbn-value? s)))
27 changes: 20 additions & 7 deletions exercises/practice/isbn-verifier/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[0caa3eac-d2e3-4c29-8df8-b188bc8c9292]
description = "valid isbn number"
description = "valid isbn"

[19f76b53-7c24-45f8-87b8-4604d0ccd248]
description = "invalid isbn check digit"

[4164bfee-fb0a-4a1c-9f70-64c6a1903dcd]
description = "valid isbn number with a check digit of 10"
description = "valid isbn with a check digit of 10"

[3ed50db1-8982-4423-a993-93174a20825c]
description = "check digit is a character other than X"

[9416f4a5-fe01-4b61-a07b-eb75892ef562]
description = "invalid check digit in isbn is not treated as zero"

[c19ba0c4-014f-4dc3-a63f-ff9aefc9b5ec]
description = "invalid character in isbn"
description = "invalid character in isbn is not treated as zero"

[28025280-2c39-4092-9719-f3234b89c627]
description = "X is only valid as a check digit"
Expand Down Expand Up @@ -48,7 +58,10 @@ description = "empty isbn"
description = "input is 9 characters"

[ed6e8d1b-382c-4081-8326-8b772c581fec]
description = "invalid characters are not ignored"
description = "invalid characters are not ignored after checking length"

[daad3e58-ce00-4395-8a8e-e3eded1cdc86]
description = "invalid characters are not ignored before checking length"

[fb5e48d8-7c03-4bfb-a088-b101df16fdc3]
description = "input is too long but contains a valid isbn"
8 changes: 5 additions & 3 deletions exercises/practice/isbn-verifier/src/isbn_verifier.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(ns isbn-verifier)

(defn isbn? [isbn] ;; <- arglist goes here
;; your code goes here
)
(defn isbn?
"Returns true if the given isbn is valid; otherwise, returns false"
[isbn]
;; function body
)
94 changes: 64 additions & 30 deletions exercises/practice/isbn-verifier/test/isbn_verifier_test.clj
Original file line number Diff line number Diff line change
@@ -1,45 +1,79 @@
(ns isbn-verifier-test
(:require [clojure.test :refer [deftest is]]
[isbn-verifier :refer [isbn?]]))
(:require [clojure.test :refer [deftest testing is]]
isbn-verifier))

(deftest valid-isbn-number
(is (= true (isbn? "3-598-21508-8"))))
(deftest test-0caa3eac-d2e3-4c29-8df8-b188bc8c9292
(testing "valid isbn"
(is (true? (isbn-verifier/isbn? "3-598-21508-8")))))

(deftest invalid-isbn-check-digit
(is (= false (isbn? "3-598-21508-9"))))
(deftest test-19f76b53-7c24-45f8-87b8-4604d0ccd248
(testing "invalid isbn check digit"
(is (false? (isbn-verifier/isbn? "3-598-21508-9")))))

(deftest valid-isbn-number-with-a-check-digit-of-10
(is (= true (isbn? "3-598-21507-X"))))
(deftest test-4164bfee-fb0a-4a1c-9f70-64c6a1903dcd
(testing "valid isbn with a check digit of 10"
(is (true? (isbn-verifier/isbn? "3-598-21507-X")))))

(deftest check-digit-is-a-character-other-than-X
(is (= false (isbn? "3-598-21507-A"))))
(deftest test-3ed50db1-8982-4423-a993-93174a20825c
(testing "check digit is a character other than X"
(is (false? (isbn-verifier/isbn? "3-598-21507-A")))))

(deftest invalid-character-in-isbn
(is (= false (isbn? "3-598-2K507-0"))))
(deftest test-9416f4a5-fe01-4b61-a07b-eb75892ef562
(testing "invalid check digit in isbn is not treated as zero"
(is (false? (isbn-verifier/isbn? "4-598-21507-B")))))

(deftest X-is-only-valid-as-a-check-digit
(is (= false (isbn? "3-598-2X507-9"))))
(deftest test-c19ba0c4-014f-4dc3-a63f-ff9aefc9b5ec
(testing "invalid character in isbn is not treated as zero"
(is (false? (isbn-verifier/isbn? "3-598-P1581-X")))))

(deftest valid-isbn-without-separating-dashes
(is (= true (isbn? "3598215088"))))
(deftest test-28025280-2c39-4092-9719-f3234b89c627
(testing "X is only valid as a check digit"
(is (false? (isbn-verifier/isbn? "3-598-2X507-9")))))

(deftest isbn-without-separating-dashes-and-X-as-check-digit
(is (= true (isbn? "359821507X"))))
(deftest test-f6294e61-7e79-46b3-977b-f48789a4945b
(testing "valid isbn without separating dashes"
(is (true? (isbn-verifier/isbn? "3598215088")))))

(deftest isbn-without-check-digit-and-dashes
(is (= false (isbn? "359821507"))))
(deftest test-185ab99b-3a1b-45f3-aeec-b80d80b07f0b
(testing "isbn without separating dashes and X as check digit"
(is (true? (isbn-verifier/isbn? "359821507X")))))

(deftest too-long-isbn-and-no-dashes
(is (= false (isbn? "3598215078X"))))
(deftest test-7725a837-ec8e-4528-a92a-d981dd8cf3e2
(testing "isbn without check digit and dashes"
(is (false? (isbn-verifier/isbn? "359821507")))))

(deftest too-short-isbn
(is (= false (isbn? "00"))))
(deftest test-47e4dfba-9c20-46ed-9958-4d3190630bdf
(testing "too long isbn and no dashes"
(is (false? (isbn-verifier/isbn? "3598215078X")))))

(deftest isbn-without-check-digit
(is (= false (isbn? "3-598-21507"))))
(deftest test-737f4e91-cbba-4175-95bf-ae630b41fb60
(testing "too short isbn"
(is (false? (isbn-verifier/isbn? "00")))))

(deftest too-long-isbn
(is (= false (isbn? "3-598-21507-XX"))))
(deftest test-5458a128-a9b6-4ff8-8afb-674e74567cef
(testing "isbn without check digit"
(is (false? (isbn-verifier/isbn? "3-598-21507")))))

(deftest check-digit-of-X-should-not-be-used-for-0
(is (= false (isbn? "3-598-21515-X"))))
(deftest test-70b6ad83-d0a2-4ca7-a4d5-a9ab731800f7
(testing "check digit of X should not be used for 0"
(is (false? (isbn-verifier/isbn? "3-598-21515-X")))))

(deftest test-94610459-55ab-4c35-9b93-ff6ea1a8e562
(testing "empty isbn"
(is (false? (isbn-verifier/isbn? "")))))

(deftest test-7bff28d4-d770-48cc-80d6-b20b3a0fb46c
(testing "input is 9 characters"
(is (false? (isbn-verifier/isbn? "134456729")))))

(deftest test-ed6e8d1b-382c-4081-8326-8b772c581fec
(testing "invalid characters are not ignored after checking length"
(is (false? (isbn-verifier/isbn? "3132P34035")))))

(deftest test-daad3e58-ce00-4395-8a8e-e3eded1cdc86
(testing "invalid characters are not ignored before checking length"
(is (false? (isbn-verifier/isbn? "3598P215088")))))

(deftest test-fb5e48d8-7c03-4bfb-a088-b101df16fdc3
(testing "input is too long but contains a valid isbn"
(is (false? (isbn-verifier/isbn? "98245726788")))))
Loading