From 384688fa95abaf0fcbeb1c187b1574ebd6cec456 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Tue, 25 Jan 2022 08:42:20 -0800 Subject: Fix the bugs in loop. It works! Sometimes it emits nested loops! --- csc/loop-test.csc | 169 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 157 insertions(+), 12 deletions(-) (limited to 'csc/loop-test.csc') diff --git a/csc/loop-test.csc b/csc/loop-test.csc index df2c970..eb9345b 100644 --- a/csc/loop-test.csc +++ b/csc/loop-test.csc @@ -6,43 +6,43 @@ (csc loop)) -(test for-collect +(test loop-for-collect (assert-equal '(1 2 3 4 5) (loop for x in '(1 2 3 4 5) collect x))) -(test for-collect-add +(test loop-for-collect-add (assert-equal '(2 3 4 5 6) (loop for x in '(1 2 3 4 5) collect (+ 1 x)))) -(test for-arithmetic +(test loop-for-arithmetic (assert-equal '(1 2 3 4 5) (loop for x from 1 to 5 collect x))) -(test for-arithmetic-step +(test loop-for-arithmetic-step (assert-equal '(1 3 5 7 9) (loop for x from 1 to 10 by 2 collect x))) -(test finally-noop +(test loop-finally-noop (assert-equal '(1 2 3) (loop for x from 1 to 3 - finally #t + finally (if #f #f) collect x))) -(test collect-into +(test loop-collect-into (assert-equal '((1 2 3) (1 2 3) (1 2 3)) (loop for x from 1 to 3 @@ -50,7 +50,7 @@ collect l))) -(test finally-set +(test loop-finally-set (assert-equal '(1 2 3) (let ((res #f)) @@ -69,13 +69,13 @@ res))) -(test return +(test loop-return (assert-equal '(1 2 3) (loop do (return '(1 2 3))))) -(test finally-return +(test loop-finally-return (assert-equal '(1 2 3) (loop for x from 1 to 3 @@ -83,7 +83,7 @@ finally (return l)))) -(test for-as-equals-then +(test loop-for-as-equals-then (assert-equal '((1 2 3) (2 3) (3)) (loop for i from 1 to 3 @@ -91,7 +91,15 @@ collect tail))) -(test return-multiple-values +(test loop-for-as-equals + (assert-equal + '((1) (2) (3)) + (loop for i from 1 to 3 + for j = (list i) + collect j))) + + +(test loop-return-multiple-values (let-values (((x1 x2) (loop do (return (values 1 2))))) (assert-equal 1 @@ -106,3 +114,140 @@ '(1 2 3) (loop do (define x (loop do (return '(1 2 3)))) (return x)))) + + +(test loop-with-return + (assert-equal + 5 + (loop with x = 5 + return x))) + + +(test loop-for-x-on-l + (assert-equal + '((1 2 3) (2 3) (3)) + (loop for x on '(1 2 3) + collect x))) + + +(test loop-for-across + (assert-equal + '(1 2 3) + (loop for x across #(1 2 3) + collect x))) + + +(test loop-for-downfrom + (assert-equal + '(3 2 1) + (loop for x to 1 downfrom 3 + collect x))) + + +(test loop-for-to + (assert-equal + '(0 1 2 3) + (loop for x to 3 + collect x))) + + +(test loop-for-downto + (assert-equal + '(3 2 1) + (loop for x downto 1 from 3 + collect x))) + + +(test loop-for-below + (assert-equal + '(0 1 2) + (loop for x below 3 + collect x))) + + +(test loop-for-above + (assert-equal + '(3 2 1) + (loop for x above 0 from 3 + collect x))) + + +(test loop-for-by + (assert-equal + '(0 2 4) + (loop for x by 2 to 4 + collect x))) + + +(test loop-append + (assert-equal + '(1 2 3 4) + (loop for x in '((1 2) (3 4)) + append x))) + + +(test loop-count + (assert-equal + 50 + (loop for x from 1 to 100 + count (even? x)))) + + +(test loop-sum + (assert-equal + 15 + (loop for x from 1 to 5 + sum x))) + + +(test loop-maximize + (assert-equal + 10 + (loop for x in '(3 10 1 4) + maximize x))) + + +(test loop-minimize + (assert-equal + 1 + (loop for x in '(3 10 1 4) + minimize x))) + + +(test loop-if + (assert-equal + 5 + (loop for x from 0 + if (>= x 5) return x))) + + +(test loop-when + (assert-equal + 5 + (loop for x from 0 + when (>= x 5) return x))) + + +(test loop-else + (assert-equal + '((0 2 4) . (1 3 5)) + (loop for x to 5 + if (even? x) collect x into evens + else collect x into odds + finally (return (cons evens odds))))) + + +(test loop-if-compound + (assert-equal + '((0 2 4) . (1 3 5)) + (loop for x to 5 + if (even? x) collect x into list1 + and collect (+ 1 x) into list2 + finally (return (cons list1 list2))))) + + +(test loop-if-end + (assert-equal + 5 + (loop for x from 0 + if (>= x 5) return x end))) -- cgit v1.3.1