aboutsummaryrefslogtreecommitdiffstats
path: root/csc/loop-test.csc
blob: df2c970503777b8309eb92ea1de671cd4e71881c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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))))