Skip to content

Commit

Permalink
Update collatz-conjecture tests to allow use of contract (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras authored Jan 3, 2024
1 parent f913b0e commit 0d79175
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
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))

0 comments on commit 0d79175

Please sign in to comment.