diff --git a/tests/test_loop.hy b/tests/test_loop.hy index 6d67f87d..59a01041 100644 --- a/tests/test_loop.hy +++ b/tests/test_loop.hy @@ -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 [] @@ -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")))