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

Update collatz-conjecture tests to allow use of contract #326

Merged
merged 1 commit into from
Jan 3, 2024
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
11 changes: 3 additions & 8 deletions exercises/practice/collatz-conjecture/.meta/example.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
(add1 (* 3 n)))
(add1 acc))))

(define (collatz n)
(if (nand (exact-integer? n)
(positive? n))
(error "Only positive integers are allowed")
(collatz-length n)))



(define/contract (collatz n)
(-> exact-positive-integer? exact-integer?)
(collatz-length n))
26 changes: 9 additions & 17 deletions exercises/practice/collatz-conjecture/collatz-conjecture-test.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
(module+ test
(require rackunit rackunit/text-ui)

(define (exn-msg-matches? msg f)
(with-handlers ([exn:fail? (lambda (exn)
(string=? (exn-message exn) msg))])
(f)))

(define suite
(test-suite
"collatz conjecture tests"
Expand All @@ -30,19 +25,16 @@
(collatz 1000000)
152)

(test-true "zero is an error"
(exn-msg-matches?
"Only positive integers are allowed"
(lambda () (collatz 0))))
(test-exn "zero is an error"
exn:fail?
(lambda () (collatz 0)))

(test-true "negative value is an error"
(exn-msg-matches?
"Only positive integers are allowed"
(lambda () (collatz -15))))
(test-exn "negative value is an error"
exn:fail?
(lambda () (collatz -15)))

(test-true "non exact value is an error"
(exn-msg-matches?
"Only positive integers are allowed"
(lambda () (collatz 3.4))))))
(test-exn "non exact value is an error"
exn:fail?
(lambda () (collatz 3.4)))))

(run-tests suite))