aboutsummaryrefslogtreecommitdiffstats
path: root/csc/loop-test.csc
diff options
context:
space:
mode:
Diffstat (limited to 'csc/loop-test.csc')
-rw-r--r--csc/loop-test.csc108
1 files changed, 108 insertions, 0 deletions
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))))