Skip to content

Update luhn and add luhn test template #160

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

Merged
merged 2 commits into from
Feb 26, 2017
Merged
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
9 changes: 9 additions & 0 deletions exercises/luhn/luhn.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(ns luhn-test
(:require [clojure.test :refer [deftest is testing]]
luhn))

(deftest validity-tests
{{#valid}}
(testing "{{{description}}}"
(is ({{{expected}}}? (luhn/valid? "{{{input}}}"))))
{{/valid}})
20 changes: 11 additions & 9 deletions exercises/luhn/src/example.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(ns luhn)
(ns luhn
(:require [clojure.string :as string]))

(defn to-reversed-digits
"returns a lazy sequence of least to most significant digits of n"
Expand All @@ -19,14 +20,15 @@
(apply +))
(mod 10)))

(defn valid?
"whether n has a valid luhn check-digit"
(defn string->long
"Strips any non-digit characters and converts the string into a Long"
[n]
(zero? (checksum n)))
(-> n (string/replace #"[^0-9]+" "") Long/parseLong))

(defn add-check-digit
"given a number, adds a luhn check digit at the end"
(defn valid?
"whether n has a valid luhn check-digit"
[n]
(let [n-shifted (* 10 n)
check-digit (- 10 (checksum n-shifted))]
(+ n-shifted check-digit)))
; Numbers with non digit/whitespace or only 1 digit are invalid
(if (or (re-find #"[^0-9\s]+" n) (>= 1 (count (string/trim n))))
false
(zero? (-> n string->long checksum))))
58 changes: 29 additions & 29 deletions exercises/luhn/test/luhn_test.clj
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
(ns luhn-test
(:require [clojure.test :refer [deftest is]]
(:require [clojure.test :refer [deftest is testing]]
luhn))

(deftest checksum-works
(is (= 2 (luhn/checksum 10)))
(is (= 9 (luhn/checksum 90)))
(is (= 1 (luhn/checksum 100)))
(is (= 2 (luhn/checksum 1000)))
(is (= 1 (luhn/checksum 10000000000000000)))
(is (= 6 (luhn/checksum 1111)))
(is (= 0 (luhn/checksum 8763)))
(is (= 0 (luhn/checksum 2323200577663554))))

(deftest valid?-works
(is (= true (luhn/valid? 18)))
(is (= true (luhn/valid? 59)))
(is (= false (luhn/valid? 63)))
(is (= true (luhn/valid? 8763)))
(is (= false (luhn/valid? 1111)))
(is (= true (luhn/valid? 4242424242424242)))
(is (= true (luhn/valid? 2323200577663554)))
(is (= false (luhn/valid? 2323200577663555)))
(is (= false (luhn/valid? 2223200577663554)))
(is (= false (luhn/valid? 3323200577663554))))

(deftest add-check-digit-works
(is (= 18 (luhn/add-check-digit 1)))
(is (= 59 (luhn/add-check-digit 5)))
(is (= 8763 (luhn/add-check-digit 876)))
(is (= 4242424242424242 (luhn/add-check-digit 424242424242424)))
(is (= 2323200577663554 (luhn/add-check-digit 232320057766355))))
(deftest validity-tests
(testing "single digit strings can not be valid"
(is (false? (luhn/valid? "1"))))
(testing "A single zero is invalid"
(is (false? (luhn/valid? "0"))))
(testing "simple valid sin"
(is (true? (luhn/valid? " 5 9 "))))
(testing "valid Canadian SIN"
(is (true? (luhn/valid? "046 454 286"))))
(testing "invalid Canadian SIN"
(is (false? (luhn/valid? "046 454 287"))))
(testing "invalid credit card"
(is (false? (luhn/valid? "8273 1232 7352 0569"))))
(testing "valid strings with a non-digit added become invalid"
(is (false? (luhn/valid? "046a 454 286"))))
(testing "punctuation is not allowed"
(is (false? (luhn/valid? "055-444-285"))))
(testing "symbols are not allowed"
(is (false? (luhn/valid? "055£ 444$ 285"))))
(testing "single zero with space is invalid"
(is (false? (luhn/valid? " 0"))))
(testing "lots of zeros are valid"
(is (true? (luhn/valid? " 00000"))))
(testing "another valid sin"
(is (true? (luhn/valid? "055 444 285"))))
(testing "nine doubled is nine"
(is (true? (luhn/valid? "091"))))
)