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 #277

Merged
merged 2 commits into from
Sep 7, 2023
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
2 changes: 1 addition & 1 deletion exercises/practice/collatz-conjecture/.meta/example.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
(define (collatz n)
(if (nand (exact-integer? n)
(positive? n))
(error "number must be a positive integer")
(error "Only positive integers are allowed")
(collatz-length n)))


Expand Down
10 changes: 10 additions & 0 deletions exercises/practice/collatz-conjecture/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ description = "large number of even and odd steps"

[7d4750e6-def9-4b86-aec7-9f7eb44f95a3]
description = "zero is an error"
include = false

[2187673d-77d6-4543-975e-66df6c50e2da]
description = "zero is an error"
reimplements = "7d4750e6-def9-4b86-aec7-9f7eb44f95a3"

[c6c795bf-a288-45e9-86a1-841359ad426d]
description = "negative value is an error"
include = false

[ec11f479-56bc-47fd-a434-bcd7a31a7a2e]
description = "negative value is an error"
reimplements = "c6c795bf-a288-45e9-86a1-841359ad426d"
38 changes: 26 additions & 12 deletions exercises/practice/collatz-conjecture/collatz-conjecture-test.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,44 @@
(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"
"collatz conjecture tests"

(test-eqv? "zero steps for one"
(collatz 1)
0)

(test-eqv? "divide if even"
(collatz 16)
4)

(test-eqv? "even and odd steps"
(collatz 12)
9)

(test-eqv? "Large number of even and odd steps"
(collatz 1000000)
152)
(test-exn "zero is an error"
exn:fail?
(lambda () (collatz 0)))
(test-exn "negative value is an error"
exn:fail?
(lambda () (collatz -15)))
(test-exn "non exact value is an error"
exn:fail?
(lambda () (collatz 3.4)))))

(run-tests suite))

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

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

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

(run-tests suite))