Skip to content

Commit

Permalink
Clean up some loop tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodiologist committed Nov 22, 2024
1 parent 2f23327 commit 6707476
Showing 1 changed file with 21 additions and 31 deletions.
52 changes: 21 additions & 31 deletions tests/test_loop.hy
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,30 @@
hyrule [loop])
(import
sys
hyrule [inc dec])
hyrule [inc dec]
pytest)


(defn tco-sum [x y]
(loop [[x x] [y y]]
(cond
(> y 0) (recur (inc x) (dec y))
(< y 0) (recur (dec x) (inc y))
True x)))
(defn test-tco-sum []

; This plain old tail-recursive function should exceed Python's
; default maximum recursion depth.
(defn non-tco-sum [x y]
(cond
(> y 0) (inc (non-tco-sum x (dec y)))
(< y 0) (dec (non-tco-sum x (inc y)))
True x))
(with [(pytest.raises RecursionError)]
(non-tco-sum 100 10,000))

(defn non-tco-sum [x y]
(cond
(> y 0) (inc (non-tco-sum x (dec y)))
(< y 0) (dec (non-tco-sum x (inc y)))
True x))


(defn test-loop []
;; non-tco-sum should fail
(try
(setv n (non-tco-sum 100 10000))
(except [e RuntimeError]
(assert True))
(else
(assert False)))

;; tco-sum should not fail
(try
(setv n (tco-sum 100 10000))
(except [e RuntimeError]
(assert False))
(else
(assert (= n 10100)))))
; With `loop`, it should work.
(defn tco-sum [x y]
(loop [[x x] [y y]]
(cond
(> y 0) (recur (inc x) (dec y))
(< y 0) (recur (dec x) (inc y))
True x)))
(assert (= (tco-sum 100 10,000) 10,100)))


(defn test-recur-in-wrong-loc []
Expand All @@ -54,5 +44,5 @@


(defn test-recur-string []
"test that `loop` doesn't touch a string named `recur`"
"`loop` shouldn't touch a string named `recur`."
(assert (= (loop [] (+ "recur" "1")) "recur1")))

0 comments on commit 6707476

Please sign in to comment.