From a54f483b1f9ae3f03f1f00124eec296c2d9218d7 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Sat, 22 Jan 2022 17:51:20 -0800 Subject: Add a loop macro. --- csc/loop-test.csc | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 csc/loop-test.csc (limited to 'csc/loop-test.csc') diff --git a/csc/loop-test.csc b/csc/loop-test.csc new file mode 100644 index 0000000..df2c970 --- /dev/null +++ b/csc/loop-test.csc @@ -0,0 +1,108 @@ +(import (scheme base) + (only (csc format) printf) + (only (csc testing) + assert-equal + test) + (csc loop)) + + +(test for-collect + (assert-equal + '(1 2 3 4 5) + (loop for x in '(1 2 3 4 5) + collect x))) + + +(test 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 + (assert-equal + '(1 2 3 4 5) + (loop for x from 1 to 5 + collect x))) + + +(test for-arithmetic-step + (assert-equal + '(1 3 5 7 9) + (loop for x from 1 to 10 by 2 + collect x))) + + +(test finally-noop + (assert-equal + '(1 2 3) + (loop for x from 1 to 3 + finally #t + collect x))) + + +(test collect-into + (assert-equal + '((1 2 3) (1 2 3) (1 2 3)) + (loop for x from 1 to 3 + collect x into l + collect l))) + + +(test finally-set + (assert-equal + '(1 2 3) + (let ((res #f)) + (loop for x from 1 to 3 + collect x into l + finally (set! res l)) + res))) + + +(test loop-do + (assert-equal + '(3 2 1) + (let ((res '())) + (loop for x from 1 to 3 + do (set! res (cons x res))) + res))) + + +(test return + (assert-equal + '(1 2 3) + (loop do (return '(1 2 3))))) + + +(test finally-return + (assert-equal + '(1 2 3) + (loop for x from 1 to 3 + collect x into l + finally (return l)))) + + +(test for-as-equals-then + (assert-equal + '((1 2 3) (2 3) (3)) + (loop for i from 1 to 3 + for tail = '(1 2 3) then (cdr tail) + collect tail))) + + +(test return-multiple-values + (let-values (((x1 x2) (loop do (return (values 1 2))))) + (assert-equal + 1 + x1) + (assert-equal + 2 + x2))) + + +(test nested-loops + (assert-equal + '(1 2 3) + (loop do (define x (loop do (return '(1 2 3)))) + (return x)))) -- cgit v1.3.1