This repository has been archived by the owner on Jan 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cs-test.rkt
137 lines (134 loc) · 4.71 KB
/
cs-test.rkt
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
131
132
133
134
135
136
137
#lang racket
;; Copyright (C) 2017 Zaoqi
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as published
;; by the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU Affero General Public License for more details.
;; You should have received a copy of the GNU Affero General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
(require rackunit)
(require "cs.rkt")
(define-syntax-rule (test [c r] ...)
(begin
(check-equal? (run-sexp (quote c)) (quote r)) ...))
(define-syntax-rule (load/test [f ...] [c r] ...)
(check-equal?
(run-xs (map sexp-> (append (include/quote/list f) ...
(list (quote (list c ...))))))
(list (sexp-> (quote r)) ...)))
(test
[`(list ,(+ 1 2) 4) (list 3 4)]
[(let ((name 'a)) `(list ,name ',name)) (list a (quote a))]
[`(a ,(+ 1 2) ,@(map abs '(4 -5 6)) b) (a 3 4 5 6 b)]
[`(( foo ,(- 10 3)) ,@(cdr '(c)) . ,(car '(cons))) ((foo 7) . cons)]
[(let ((foo '(foo bar)) (@baz 'baz))
`(list ,@foo , @baz)) (list foo bar baz)]
[`(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f) (a `(b ,(+ 1 2) ,(foo 4 d) e) f)]
[(let ((name1 'x)
(name2 'y))
`(a `(b ,,name1 ,',name2 d) e)) (a `(b ,x ,'y d) e)]
)
(test
[(add-between '(x y z) 'and) (x and y and z)]
[(add-between '(x) 'and) (x)])
(test
[(begin
(define-record-type <pare>
(kons x y)
pare?
(x kar set-kar!)
(y kdr))
(list (pare? (kons 1 2))
(pare? (cons 1 2))
(kar (kons 1 2))
(kdr (kons 1 2))
(let ((k (kons 1 2)))
(set-kar! k 3)
(kar k)))) (#t #f 1 2 3)])
(test
[(begin
(define d (delay 0))
(list (force d) (promise-forced? d))) (0 #t)])
(test
[(begin
(struct cell ([content #:mutable]))
(define a-cell (cell 0))
(let ([x (cell-content a-cell)])
(set-cell-content! a-cell 1)
(list
x
(cell-content a-cell)
(cell? a-cell)))) (0 1 #t)])
(load/test
["macroexpand.cscm"]
[(macroexpand '(begin
(defmacro (k a)
`(+ ,a ,a))
(k 0)))
(letrec ((first car)
(second (λ (x) (car (cdr x))))
(third (λ (x) (car (cdr (cdr x)))))
(fourth (λ (x) (car (cdr (cdr (cdr x))))))
(add-between
(λ (xs v)
(cond
((null? xs) '())
((null? (cdr xs)) xs)
(else (cons (car xs) (cons v (add-between (cdr xs) v)))))))
(zero? (λ (x) (eq? x 0)))
(abs (λ (x) (if (< x 0) (- 0 x) x)))
(not (λ (x) (if x #f #t)))
(force
(λ (x)
((λ (r)
(if (pair? r)
(car r)
(car (atom-map! (λ (v) (list (r))) (%force x)))))
(atom-get/set!
(%force x)
(λ (v)
(cond
((pair? v) (cons v v))
((not v) (error '|force: halted| x))
(else (cons v #f))))))))
(promise-running? (λ (x) (not (atom-get (%force x)))))
(promise-forced? (λ (x) (pair? (atom-get (%force x)))))
(error (λ (x) (raise (ERROR x))))
(++
(λ xs
(cond
((null? xs) '||)
((list? (car xs)) (apply ++ (append (car xs) (cdr xs))))
(else (string-append (car xs) (apply ++ (cdr xs)))))))
(symbol? string?)
(partition
(λ (f xs)
(if (null? xs)
(cons '() '())
((λ (r)
(if (f (car xs))
(cons (cons (car xs) (car r)) (cdr r))
(cons (car r) (cons (car xs) (cdr r)))))
(partition f (cdr xs))))))
(filter
(λ (f xs)
(if (null? xs)
'()
(if (f (car xs))
(cons (car xs) (filter f (cdr xs)))
(filter f (cdr xs))))))
(foldl
(λ (f x xs) (if (null? xs) x (foldl f (f (car xs) x) (cdr xs))))))
(define-record-type promise (%delay x) promise? (x %force))
(define-record-type ERROR (ERROR x) ERROR? (x ERROR-x))
(+ 0 0))]
)
(load/test
["js.cscm"]
[(js '(struct a (b c)))
|var aT=function(){};var a=function(x){return x instanceof aT;};var b=function(c){var v_=new aT();v_.c=c;return v_;};|]
)