File: test-optional.scm

package info (click to toggle)
chicken 5.3.0-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 32,892 kB
  • sloc: ansic: 580,083; lisp: 71,987; tcl: 1,445; sh: 588; makefile: 60
file content (130 lines) | stat: -rw-r--r-- 3,810 bytes parent folder | download | duplicates (7)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
;;;; test-optional.scm - by Alan Post


(define (test baseline result)
  (assert (equal? baseline result)))

;;
;; basic optional arguments with default value.
;;

(define (foo0 #!optional a0 a1 a2 a3)
  (list a0 a1 a2 a3))

(define (foo1 a0 #!optional a1 a2 a3)
  (list a0 a1 a2 a3))

(define (foo2 a0 a1 #!optional a2 a3)
  (list a0 a1 a2 a3))

(define (foo3 a0 a1 a2 #!optional a3)
  (list a0 a1 a2 a3))

(test '(#f #f #f #f) (foo0))
(test '(1  #f #f #f) (foo0 1))
(test '(1  2  #f #f) (foo0 1 2))
(test '(1  2   3 #f) (foo0 1 2 3))
(test '(1  2   3  4) (foo0 1 2 3 4))

;(test '(#f #f #f #f) (foo1)) ; invalid, too few arguments.
(test '(1  #f #f #f) (foo1 1))
(test '(1  2  #f #f) (foo1 1 2))
(test '(1  2   3 #f) (foo1 1 2 3))
(test '(1  2   3  4) (foo1 1 2 3 4))

;(test '(#f #f #f #f) (foo2)) ; invalid, too few arguments.
;(test '(1 #f  #f #f) (foo2 0)) ; invalid, too few arguments.
(test '(1  2  #f #f) (foo2 1 2))
(test '(1  2  #f #f) (foo2 1 2))
(test '(1  2   3 #f) (foo2 1 2 3))
(test '(1  2   3  4) (foo2 1 2 3 4))

;(test '(#f #f #f #f) (foo3)) ; invalid, too few arguments.
;(test '(1  #f #f #f) (foo3 1)) ; invalid, too few arguments.
;(test '(1  2  #f #f) (foo3 1 2)) ; invalid, too few arguments.
(test '(1  2   3 #f) (foo3 1 2 3))
(test '(1  2   3  4) (foo3 1 2 3 4))

;;
;; basic optional arguments with manual default value.
;;

(define (foo0 #!optional (a0 -1) (a1 -2) (a2 -3) (a3 -4))
  (list a0 a1 a2 a3))

(define (foo1 a0 #!optional (a1 -2) (a2 -3) (a3 -4))
  (list a0 a1 a2 a3))

(define (foo2 a0 a1 #!optional (a2 -3) (a3 -4))
  (list a0 a1 a2 a3))

(define (foo3 a0 a1 a2 #!optional (a3 -4))
  (list a0 a1 a2 a3))


(test '(-1 -2 -3 -4) (foo0))
(test '(1  -2 -3 -4) (foo0 1))
(test '(1  2  -3 -4) (foo0 1 2))
(test '(1  2   3 -4) (foo0 1 2 3))
(test '(1  2   3  4) (foo0 1 2 3 4))

;(test '(-1 -2 -3 -4) (foo1)) ; invalid, too few arguments.
(test '(1  -2 -3 -4) (foo1 1))
(test '(1  2  -3 -4) (foo1 1 2))
(test '(1  2   3 -4) (foo1 1 2 3))
(test '(1  2   3  4) (foo1 1 2 3 4))

;(test '(-1 -2 -3 -4) (foo2)) ; invalid, too few arguments.
;(test '(1 -2  -3 -4) (foo2 0)) ; invalid, too few arguments.
(test '(1  2  -3 -4) (foo2 1 2))
(test '(1  2  -3 -4) (foo2 1 2))
(test '(1  2   3 -4) (foo2 1 2 3))
(test '(1  2   3  4) (foo2 1 2 3 4))

;(test '(-1 -2 -3 -4) (foo3)) ; invalid, too few arguments.
;(test '(1  -2 -3 -4) (foo3 1)) ; invalid, too few arguments.
;(test '(1  2  -3 -4) (foo3 1 2)) ; invalid, too few arguments.
(test '(1  2   3 -4) (foo3 1 2 3))
(test '(1  2   3  4) (foo3 1 2 3 4))

;;
;; optional arguments with default value set from previous default.
;;
;; NOTE: these currently fail.

(define (foo0 #!optional (a0 -1) (a1 (- a0 1)) (a2 (- a1 1)) (a3 (- a2 1)))
  (list a0 a1 a2 a3))

(define (foo1 a0 #!optional (a1 -2) (a2 (- a1 1)) (a3 (- a2 1)))
  (list a0 a1 a2 a3))

(define (foo2 a0 a1 #!optional (a2 -3) (a3 (- a2 1)))
  (list a0 a1 a2 a3))

(define (foo3 a0 a1 a2 #!optional (a3 -4))
  (list a0 a1 a2 a3))


(test '(-1 -2 -3 -4) (foo0))
(test '(1  0 -1 -2) (foo0 1))
(test '(1  2  1  0) (foo0 1 2))
(test '(1  2  3  2) (foo0 1 2 3))
(test '(1  2  3  4) (foo0 1 2 3 4))

;(test '(-1 -2 -3 -4) (foo1)) ; invalid, too few arguments.
(test '(1  -2 -3 -4) (foo1 1))
(test '(1  2  1  0) (foo1 1 2))
(test '(1  2  3  2) (foo1 1 2 3))
(test '(1  2  3  4) (foo1 1 2 3 4))

;(test '(-1 -2 -3 -4) (foo2)) ; invalid, too few arguments.
;(test '(1 -2  -3 -4) (foo2 0)) ; invalid, too few arguments.
(test '(1  2  -3 -4) (foo2 1 2))
(test '(1  2   3  2) (foo2 1 2 3))
(test '(1  2   3  4) (foo2 1 2 3 4))

;(test '(-1 -2 -3 -4) (foo3)) ; invalid, too few arguments.
;(test '(1  -2 -3 -4) (foo3 1)) ; invalid, too few arguments.
;(test '(1  2  -3 -4) (foo3 1 2)) ; invalid, too few arguments.
(test '(1  2   3 -4) (foo3 1 2 3))
(test '(1  2   3  4) (foo3 1 2 3 4))