Skip to content

Commit

Permalink
Update collatz conjecture tests (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras authored Sep 7, 2023
1 parent 37dfbcc commit a0c5f57
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
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))

0 comments on commit a0c5f57

Please sign in to comment.