From 6707476390796eba13f74bc3e0e12ebb2c684cd4 Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Fri, 22 Nov 2024 14:40:53 -0500 Subject: [PATCH] Clean up some `loop` tests --- tests/test_loop.hy | 52 +++++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 31 deletions(-) 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")))