eval-tests.lisp 21.6 KB
Newer Older
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
;;; Copyright 2011-2012 Vladimir Sedach

;;; SPDX-License-Identifier: BSD-3-Clause

;;; Redistribution and use in source and binary forms, with or
;;; without modification, are permitted provided that the following
;;; conditions are met:

;;; 1. Redistributions of source code must retain the above copyright
;;; notice, this list of conditions and the following disclaimer.

;;; 2. Redistributions in binary form must reproduce the above
;;; copyright notice, this list of conditions and the following
;;; disclaimer in the documentation and/or other materials provided
;;; with the distribution.

;;; 3. Neither the name of the copyright holder nor the names of its
;;; contributors may be used to endorse or promote products derived
;;; from this software without specific prior written permission.

;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
;;; CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
;;; INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
;;; MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
;;; DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
;;; BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
;;; EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
;;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
;;; ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
;;; OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
;;; OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
;;; POSSIBILITY OF SUCH DAMAGE.
34

35
(in-package #:parenscript.eval-tests)
36
37
(named-readtables:in-readtable :parenscript)

38
39
#+sbcl (declaim (sb-ext:muffle-conditions sb-ext:compiler-note))

40
(fiveam:in-suite parenscript.tests::eval-tests)
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

(test-js-eval number
  3
  3)

(test-js-eval float
  (< 123.12 123.123 123.124)
  t)

(test-js-eval string
  "foobar"
  "foobar")

(test-js-eval statements-and-expressions-2
  ((lambda () (if 1 2 3)))
  2)

(test-js-eval statements-and-expressions-1
  ((lambda () (+ 2 (if 1 2 3))))
  4)

(test-js-eval empty-array
  (array)
64
  #())
65

66
67
68
69
(test-js-eval funargs-let1
  ((lambda (x)
     (let ((x 10))
       (incf x))
70
71
     (incf x))
   0)
72
  1)
73

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
(test-js-eval times-rem
  (* 12 (rem 10 3))
  12)

(test-js-eval divide-rem
  (rem 10 (/ 12 4))
  1)

(test-js-eval rem-divide
  (/ 9 (rem 11 4))
  3)

(test-js-eval rem-multiply
  (* 5 (rem 4 3))
  5)
89
90

(test-js-eval case-return-break-null
91
92
93
94
  ((lambda (x) (case x
                 (1)
                 (2 3)))
   1)
95
  :null)
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
138
139

(test-js-eval defun-return1
  (progn (defun bar (x) (1+ x))
         (bar 1))
  2)

(test-js-eval defun-return2
  (progn (defun bar (fn)
           (funcall fn))

         (defun foo ()
           (bar (lambda () (return-from foo 123))))

         (foo))
  123)

(test-js-eval defvar
  (progn (defvar foo 1)
         foo)
  1)

(test-js-eval block-dynamic-lambda
  ((lambda () (block nil (return 4) (+ 1 2))))
  4)

(test-js-eval block-dynamic-lambda1
  ((lambda () (block nil (return 4) (+ 1 2)) 5))
  5)

(test-js-eval loop-sum
  (+ 1 (loop for i from 0 to 10 sum i))
  56)

(test-js-eval loop-funcall-sum
  ((lambda (x) (1+ x)) (loop for i from 0 to 10 sum i))
  56)

(test-js-eval loop-funcall-if-sum
  (progn (defun one-plus (x)
           (1+ x))

         (one-plus (if false 1 (loop for i from 0 to 10 sum i))))
  56)

140
141
142
143
144
145
(test-js-eval case-return-break1-broken-return
  (progn (defun foo (x y)
           (case x
             ("bar" (if y (return-from foo 1)))
             ("baz" 2)))
         (list (foo "bar" t) (foo "bar" nil) (foo "baz" nil)))
146
  '(1 :null 2))
147
148
149
150

(test-js-eval funcall-loop-doing
  ((lambda (x) x)
   (loop for i from 0 to 10 do (1+ i)))
151
  :undefined)
152
153
154
155
156
157
158
159
160
161

(test-js-eval block-dynamic-lambda2
  ((lambda () (1+ (block nil (return 4) (+ 1 2)))))
  5)

(test-js-eval block-dynamic-setf
  (progn (defvar foo (block nil (return 4) (+ 1 2)))
         foo)
  4)

162
163
164
165
166
167
168
169
(test-js-eval block-dynamic-return1
  (progn (defvar foo ((lambda ()
                        (block nil
                          ((lambda () (return 6)))
                          (+ 1 2)))))
         foo)
  6)

170
171
172
173
174
175
176
177
(test-js-eval block-lambda-dynamic-setf
  (progn (defvar foo (block nil ((lambda () (return 4))) (+ 1 2)))
         foo)
  4)

(test-js-eval block-lambda-dynamic-lambda
  ((lambda () (block nil ((lambda () (return 4))) (+ 1 2))))
  4)
178
179
180
181
182
183
184
185
186
187
188
189

(test-js-eval return-from-flet
  (progn (defun foo ()
           (flet ((bar () (return-from foo 42)))
             (bar)))
         (foo))
  42)

(test-js-eval plus-block
  (1+ (block nil (return 4) (+ 1 2)))
  5)

190
191
192
193
194
195
196
197
198
199
200
201
(test-js-eval let-closures-rename
  (progn (defun make-closures ()
           (list
            (let ((x 1)) (lambda () (1+ x)))
            (let ((x 2)) (lambda () (1+ x)))))

         (defvar foo (make-closures))

         (list (funcall (aref foo 0))
               (funcall (aref foo 1))))
  '(2 3))

202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
(test-js-eval labels-return
  ((lambda ()
     (labels ((bar (x)
                (when (evenp x)
                  (return-from bar "even"))
                1))
       (bar 9))))
  1)

(test-js-eval labels-return1
  (progn (defun foo ()
           (labels ((bar (x)
                      (when (evenp x)
                        (return-from foo "even"))
                      1))
             (bar 8)
             2))
         (foo))
  "even")

(test-js-eval labels-return2
  (progn (defun foo ()
           (labels ((bar (x)
                      (flet ((foo (y)
                               (when (evenp (+ x y))
                                 (return-from bar "even"))))
                        (foo 4)
                        5)
                      1))
             (bar 8)))
         (foo))
  "even")

(test-js-eval labels-return3
  (progn (defun foo ()
           (labels ((bar (x)
                      (flet ((baz (y)
                               (when (evenp (+ x y))
                                 (return-from foo "even"))))
                        (baz 4)
                        5)
                      1))
             (bar 8)
             2))
         (foo))
  "even")

249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
(test-js-eval toplevel-local-scope
  (progn (defvar foo (create "fn" (let ((x 5)) (lambda () x))))
         (funcall (getprop foo "fn")))
  5)

(test-js-eval special-var2
  (progn (defvar *foo*)
         (let* ((*baz* 3)
                (*foo* 2))
           (* *foo* 2 *baz*)))
  12)

(test-js-eval special-var3
  (progn (defvar *foo* 1)
         (+ *foo* (let* ((*baz* 3)
                         (*foo* 2))
                    (* *foo* 2 *baz*))))
  13)

(test-js-eval let3
  (let ((x 3)
        (y 2))
    (+ x x))
  6)

(test-js-eval operator-expressions-array-nested-let
  (list (let ((y 1)) y) 2)
  '(1 2))

278
279
280
281
282
283
284
285
286
287
288
289
290
291
(test-js-eval block-return-plus
  (+ 1 (block foobar (return-from foobar 2) 1))
  3)

(test-js-eval block-return-plus1
  (+ 1 (block foobar (+ 4 ((lambda (x) (return-from foobar x)) 2)) 1))
  3)

(test-js-eval block-let
  (block foobar
    (let ((x 1))
      (return-from foobar x)
      2))
  1)
292
293
294
295
296
297
298
299
300

(test-js-eval block-dynamic-return
  (block nil (return 4) (+ 1 2))
  4)

(test-js-eval block-lambda-dynamic-return
  (block nil ((lambda () (return 4))) (+ 1 2))
  4)

301
302
303
304
(test-js-eval nil-block-return-1
  (block nil (return 1) 2)
  1)

305
306
307
(test-js-eval dolist-return
  (dolist (x '(5 2 3))
    (return (1+ x)))
308
309
  6)

310
311
312
313
314
315
316
317
318
319
320
321
(test-js-eval let-defun-toplevel
  (progn (let ((foo 0))
           (defun bar () foo))
         (bar))
  0)

(test-js-eval let-defvar-toplevel
  (progn (let ((foo 0))
           (defvar bar (1+ foo)))
         bar)
  1)

322
323
324
325
326
(test-js-eval values-not-returned
  (let ((x 1))
    (setf x (+ x (values 2 (incf x))))
    x)
  3)
327

328
329
330
331
(test-js-eval equality-nary1
  (let ((x 10) (y 10) (z 10))
    (= x y z))
  t)
332
333
334
335
336

(test-js-eval values-not-returned1
  (let ((x 1))
    (incf x (+ x (values 1 (incf x))))
    x)
337
  4)
338
339
340
341
342

(test-js-eval incf-incf
  (let ((x 1))
    (incf x (incf x))
    x)
343
  4)
344
345
346
347
348

(test-js-eval incf-setf
  (let ((x 1))
    (incf x (setf x 4))
    x)
349
  8)
350
351
352
353
354
355
356
357
358
359

(test-js-eval values0
  ((lambda () (values)))
  :null)

(test-js-eval mv-return1
  (progn (defun foo ()
           (values 1 2 3))
         (multiple-value-bind (a b c) (foo)
           (list a b c)))
360
  '(1 2 3))
361
362
363
364
365
366
367

(test-js-eval dynamic-extent-function-return-values
  (progn (defun foo ()
           ((lambda ()
              (return-from foo (values 1 2 3)))))
         (multiple-value-bind (a b c) (foo)
           (list a b c)))
368
  '(1 2 3))
369

370
371
372
373
374
(test-js-eval plus-not-associative
  (let ((str "a")
        (n 1))
    (+ str (+ n 1)))
  "a2")
375
376
377
378
379
380
381
382

(test-js-eval loop-return
  (progn (defun foo ()
           (* 10 (loop for i from 0 below 10 do
                      (when (> i 3)
                        (return i)))))
         (foo))
  40)
383

384
385
386
387
388
389
390
(test-js-eval for-arr-place-in
  (loop :for (a b) :in '((2 3) (4 5)) :sum (+ a b))
  14)

(test-js-eval for-obj-place-in
  (loop :for (:a :b) :in (list (create :a 2 :b 3)
                               (create :a 4 :b 5))
391
    :summing (+ a b))
392
393
394
395
396
397
398
399
400
401
402
403
  14)

(test-js-eval for-arr-place-=
  (flet ((foo () '(2 3)))
    (loop :repeat 3 :for (a b) = (foo) :sum (+ a b)))
  15)

(test-js-eval for-obj-place-=
  (flet ((foo () (create :a 2 :b 3)))
    (loop :repeat 3 :for (:a :b) = (foo) :sum (+ a b)))
  15)

404
405
(test-js-eval loop-until1
   (let ((x 0))
406
     (loop :doing (incf x) :until t)
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
     x)
   1)

(test-js-eval loop-until2
  (let ((b nil) (c nil))
    (loop :for a :in '(4 9 10) :do (setf b a) :until (> a 5))
    (loop :for a :in '(4 9 10) :until (> a 5) :do (setf c a))
    (list b c))
  '(9 4))

(test-js-eval loop-until3
   (let ((x 0) (y 0))
     (loop :do (incf x) :until (= x 5)
       :do (incf y) :until (= y 3))
     (list x y))
   '(3 3))

424
425
426
427
428
429
430
(test-js-eval loop-with-clause
  (loop for i :from 0 :to 5
        with x
        unless (< i 3)
     do (return (setf x i)))
  3)

Daniel Gackle's avatar
Daniel Gackle committed
431
432
433
434
(test-js-eval loop-append
   (loop :for i :from 0 :below 10 :by 3 :append (list i (* i 100)))
   '(0 0 3 300 6 600 9 900))

435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
(test-js-eval loop-appending
   (loop :for i :from 0 :below 10 :by 3 :appending (list i (* i 100)))
   '(0 0 3 300 6 600 9 900))

(test-js-eval loop-maximize
   (loop :for i :in (list 1 5 3 2) :maximize i)
   5)

(test-js-eval loop-maximizing
   (loop :for i :in (list 1 5 3 7) :maximizing i)
   7)

(test-js-eval loop-minimize
   (loop :for i :in (list 1 5 3 2) :minimize i)
   1)

(test-js-eval loop-minimizing
   (loop :for i :in (list 3 5 3 2) :minimizing i)
   2)

Daniel Gackle's avatar
Daniel Gackle committed
455
456
457
458
(test-js-eval loop-for-on
   (loop :for (a b) :on '(10 20 30 40 50 60) :by 2 :collect (list b a))
   '((20 10) (40 30) (60 50)))

459
460
461
(test-js-eval loop-parallel-clauses-with-return
   (loop :for i :from 0 :below 10 :for x = (* i 10)
     :when (> i 5) :do (return x)
462
     :collecting i)
463
464
   60)

465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
(test-js-eval loop-extended-conditional-clauses
  (loop for i :from 0 :to 5
     for x := (1+ i)
     when x
       collect x
       and if (oddp x)
         collect (1+ x)
       else
         collect (/ x 2)
       end)
  '(1 2 2 1 3 4 4 2 5 6 6 3))

(test-js-eval loop-extended-conditional-clauses1
  (let* ((foo 0)
         (bar (loop for i :from 0 :to 5
                 for x := (1+ i)
                 when x
                   collect x
                   and if (oddp x)
                     collect (1+ x)
                   else
                     collect (/ x 2)
                   end
                   and do (incf foo x))))
    (funcall (@ bar push) foo)
    bar)
  '(1 2 2 1 3 4 4 2 5 6 6 3 21))
492

493
494
495
496
497
498
499
500
501
502
(test-js-eval for-loop-downfrom-to
  (loop for i :downfrom 5 :to 0
    collect i)
  '(5 4 3 2 1 0))

(test-js-eval for-loop-downfrom-above
  (loop for i :downfrom 5 :above 0
    collect i)
  '(5 4 3 2 1))

503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
(test-js-eval loop-conditional-return-works
  (block nil
    (dotimes (i 10)
      (if (< 10 i) (return i)))
    (return -1))
  -1)

(test-js-eval return-from-loop
  (let ((a 0))
    (dolist (x '(2 1 3))
      (when (= x 1)
        (return))
      (incf a))
    a)
  1)

(test-js-eval for-loop-var-init-exp
  ((lambda (x)
     (do* ((y (if x 0 1) (1+ y))
           (z 0 (1+ z)))
          ((= y 3) z)))
   true)
  3)

(test-js-eval dolist-return1
  (dolist (x '(5 2 3))
    (return (1+ x)))
  6)

(test-js-eval lambda-nil-return-implicit-nested
  (progn (defun foo (x)
           (block nil
             (if x
                 (return 1)
                 (dotimes (i 4)
                   (return 1)))
             2))

         (list (foo t) (foo nil)))
  '(1 2))
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560

(test-js-eval case-clauses0
  (let* ((foo :bar)
         (bar :baz)
         (x bar))
    (case x
      ((:foo) 1)
      ((:bar :baz) 2)))
  2)

(test-js-eval case-clauses-false
  (* 2 (case (= 1 2)
         (1 1)
         (false 3)
         (2 5)
         (otherwise 7)))
  6)

561
562
563
564
565
566
567
568
(test-js-eval case-clauses-false-nil
  (* 2 (case (= 1 2)
         (1 1)
         ((nil) 3)
         (2 5)
         (otherwise 7)))
  6)

569
570
571
572
573
574
(test-js-eval case-clauses-true
  (* 2 (case (= 2 2)
         (1 1)
         ((t) 3)
         (otherwise 7)))
  6)
575

576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
;;; needs MV pass-through to work
;; (test-js-eval multiple-value-call-twice
;;   (progn
;;     (defun foo (x &optional y z)
;;       (if z
;;           (values x y z)
;;           (values x y)))

;;     (defun bar ()
;;       (foo 1 2 3)
;;       (foo 4 5))

;;     (multiple-value-bind (a b c) (bar)
;;       (list a b c)))
;;   '(4 5 :undefined))
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642

(test-js-eval recursive-values
  (progn
    (defun foo (x)
      (if (= 0 x)
          (values 1 2 3 4)
          (progn
            (foo 0)
            23)))

    (defun bar ()
      (foo 1))

    (multiple-value-bind (a b c d) (bar)
      (list a b c d)))
  '(23 :undefined :undefined :undefined))

(test-js-eval recursive-values1
  (progn
    (defun foo (x)
      (if (= 0 x)
          (values 1 2 3)
          (baz)))

    (defun baz (x)
      (foo 0)
      23)

    (defun bar ()
      (foo 1))

    (multiple-value-bind (a b c d) (bar)
      (list a b c d)))
  '(23 :undefined :undefined :undefined))

(test-js-eval values-nonlocal-return
  (progn
    (defun foo (x)
      (if (= 0 x)
          (values 1 2 3)
          (progn
            (foo 0)
            (throw 23))))

    (defun bar ()
      (try (foo 1)
           (:catch (e)
             27)))

    (multiple-value-bind (a b c) (bar)
      (list a b c)))
  '(27 :undefined :undefined))
Vladimir Sedach's avatar
Vladimir Sedach committed
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663

(test-js-eval values-nonlocal-return1
  (progn
    (defun foo (x)
      (if (= 0 x)
          (values 1 2 3)
          (bar 0)))

    (defun bar (x)
      (try
       (if (= x 0)
           (progn
             (foo 0)
             (throw 11))
           (foo x))
       (:catch (e)
         27)))

    (multiple-value-bind (a b c) (bar 1)
      (list a b c)))
  '(27 :undefined :undefined))
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680

(test-js-eval values-nonlocal-return2
  (progn
    (defun foo (x)
      (try
       (if (= 0 x)
           (values 1 2 3)
           (bar))
       (:catch (e) 27)))

    (defun bar ()
      (foo 0)
      (throw 13))

    (multiple-value-bind (a b c) (foo 1)
      (list a b c)))
  '(27 :undefined :undefined))
681
682
683
684
685
686
687
688

(test-js-eval case-symbol-macro-key
  (symbol-macrolet ((x 1))
    (let ((blah 1))
      (case blah
        (0 3)
        (x 7)
        (t 13))))
689
690
691
692
693
694
695
696
  13)

(test-js-eval case-symbol
  (let ((blah 'x))
    (case blah
      (0 3)
      (x 7)
      (t 13)))
697
  7)
Vladimir Sedach's avatar
Vladimir Sedach committed
698

699
700
701
702
703
(test-js-eval symbol-macro-funcall
  (symbol-macrolet ((bar (getprop Math 'min)))
    (funcall bar -1 2))
  -1)

Vladimir Sedach's avatar
Vladimir Sedach committed
704
705
706
707
(test-js-eval negative-mod
  (mod -12 7)
  2)

708
709
710
711
712
713
714
715
(test-js-eval negative-mod1
  (mod -12 (funcall (@ Math floor) 7.5))
  2)

(test-js-eval negative-mod2
  (1+ (mod -12 7))
  3)

Vladimir Sedach's avatar
Vladimir Sedach committed
716
717
718
(test-js-eval negative-rem
  (rem -12 7)
  -5)
719
720
721
722

(test-js-eval negative-rem1
  (rem -12 (funcall (@ Math floor) 7.5))
  -5)
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770

(test-js-eval-epsilon trig1
  (sinh 3.14)
  11.530293)

(test-js-eval-epsilon trig2
  (sinh (floor 3.14))
  10.017875)

(test-js-eval-epsilon trig3
  (cosh 3.14)
  11.573576)

(test-js-eval-epsilon trig4
  (cosh (floor 3.14))
  10.067662)

(test-js-eval-epsilon trig5
  (tanh 3.14)
  0.9962602)

(test-js-eval-epsilon trig6
  (tanh (floor 3.14))
  0.9950548)

(test-js-eval-epsilon trig7
  (asinh 3.14)
  1.8618126)

(test-js-eval-epsilon trig8
  (asinh (floor 3.14))
  1.8184465)

(test-js-eval-epsilon trig7
  (acosh 3.14)
  1.8109914)

(test-js-eval-epsilon trig8
  (acosh (floor 3.14))
  1.7627472)

(test-js-eval-epsilon trig7
  (atanh 0.71)
  0.88718385)

(test-js-eval-epsilon trig8
  (atanh (expt -0.71 3))
  -0.37448788)
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792

(test-js-eval let-let
  (let ((x (let ((y 12))
             (+ 1 2)
             y)))
    (1+ x))
  13)

(test-js-eval let-let1
  (let ((x (let ((y 12))
             (dolist (x '(1 2 3))
               (* x x))
             y)))
    (1+ x))
  13)

(test-js-eval array-init-1
  (make-array 2 :initial-contents '(10 20))
  '(10 20))

(test-js-eval array-init-2
  (make-array 5 :initial-element 10)
793
  '(10 10 10 10 10))
794
795
796
797

(test-js-eval dotimes-block-return
  (1+ (dotimes (x 3) (if (= x 2) (return (+ x x)))))
  5)
Vladimir Sedach's avatar
Vladimir Sedach committed
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818

(test-js-eval labels-factorial
  (progn
    (defun fac (n)
      (labels ((f (n a)
                 (if (< n 2)
                     a
                     (f (- n 1) (* n a)))))
        (f n 1)))
    (fac 5))
  120)

(test-js-eval destructuring-bind1
  ((lambda (a)
     (when a
       (destructuring-bind (b . c)
           a
         (list b c))))
   '(1 2 3))
  '(1 (2 3)))

819
820
821
822
823
824
825
826
827
828
(test-js-eval defun-not-a-docstring
  (progn
    (defun foo ()
      "bar")
    (foo))
  "bar")

(test-js-eval lambda-not-a-docstring
  ((lambda () "bar"))
  "bar")
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850

(test-js-eval loop-variable-capture1
  (let ((x (make-array 10)))
    (dotimes (i 10) (setf (aref x i) (lambda () i)))
    (loop for x across x sum (funcall x)))
  100)

(test-js-eval loop-variable-capture2
  (let ((x (make-array 10)))
    (dotimes (i 10)
      (let ((y i))
        (setf (aref x i) (lambda () y))))
    (loop for x across x sum (funcall x)))
  45)

(test-js-eval loop-variable-capture3
  (let ((x (make-array 10)))
    (dotimes (i 10)
      (let ((i i))
        (setf (aref x i) (lambda () i))))
    (loop for x across x sum (funcall x)))
  45)
Vladimir Sedach's avatar
Vladimir Sedach committed
851

852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
(test-js-eval loop-variable-capture4
  (let ((x (make-array 10 :initial-element (lambda () 0))))
    (dotimes (i 10)
      (let ((i i))
        (setf (aref x i) (lambda () i))
        (when (= i 5) (return))))
    (loop for x across x sum (funcall x)))
  15)

(test-js-eval loop-variable-capture5
  (let ((A (make-array 10)))
    (dotimes (i 10)
      (flet ((foo (x) (+ i x)))
        (setf (aref A i) (lambda () (foo i)))))
    (loop for x across A sum (funcall x)))
  200)

869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
(test-js-eval loop-variable-capture6
  (labels ((make-cl (i) (lambda () (* i 10))))
    (let ((closures (loop :for i :from 1 :to 3
                       :collect (make-cl i))))
      (loop :for fn :in closures :collect (funcall fn))))
  '(10 20 30))

(test-js-eval loop-variable-capture7
  (let ((closures (loop :for i :from 1 :to 3
                     :collect (let* ((j i)
                                     (cl (lambda () (* j 10))))
                                cl))))
    (loop :for fn :in closures :collect (funcall fn)))
  '(10 20 30))

Vladimir Sedach's avatar
Vladimir Sedach committed
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
(test-js-eval nested-let
  (let ((x (let ((y 94))
             y)))
    x)
  94)

(test-js-eval lambda-apply
  ((lambda (x)
     (apply (lambda (y) (1+ y))
            x))
   (list 6))
  7)

(test-js-eval subtract-associative
  (list (- 1 2 3)
        (- 1 (- 2 3)))
  '(-4 2))

(test-js-eval logand1
  (let ((x 4583))
    (setf x (logand x 947))
    x)
  419)

(test-js-eval cons-cdr-clause-empty
  ((lambda () (cond (923))))
  923)

(test-js-eval labels-return-from
  (labels ((bar (x)
             (when (evenp x)
               (return-from bar "even"))
             "odd"))
    (bar 9))
  "odd")
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936

(test-js-eval random-float-const
  (< 0 (rem (random 123.456) 1) 1)
  t)

(test-js-eval random-int-const
  (rem (random 234) 1)
  0)

(test-js-eval random-float-fun-once-only
  (let ((x t))
    (flet ((foo ()
             (prog1 (if x
                        123.123
                        (throw "foobar"))
               (setf x nil))))
      (< 0 (rem (random (foo)) 1) 1)))
  t)
Vladimir Sedach's avatar
Vladimir Sedach committed
937
938
939
940
941
942
943
944

(test-js-eval let-setf-side-effects
  (let ((x 10))
    (defun side-effect()
      (setf x 4)
      3)
    (setf x (+ 2 (side-effect) x 5)))
  14)
945
946
947
948
949
950
951
952
953
954

(test-js-eval dolist-result-bind-nil
  (dolist (i '(1 2 3) (list i 9))
    (list i))
  '(nil 9))

(test-js-eval dotimes-result-bind-nil
  (dotimes (i '(1 2 3) (list 9 i))
    (list i))
  '(9 nil))
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041

(test-js-eval progn-of-block-closure
  ((lambda ()
     (progn
       (block nil
         ((lambda ()
            ((lambda ()
               (return 1)))
            2))))))
  1)

(test-js-eval progn-of-block-closure1
  ((lambda ()
     (progn
       (block X
         ((lambda ()
            ((lambda ()
               (return-from X 1)))
            2))))))
  1)

(test-js-eval progn-of-block-closure2
  ((lambda ()
     (1+
       (block X
         ((lambda ()
            ((lambda ()
               (return-from X 1)))
            3))))))
  2)

(test-js-eval progn-of-block-closure3
  ((lambda ()
     (1+
       (block nil
         ((lambda ()
            ((lambda ()
               (return 1)))
            3))))))
  2)

(test-js-eval block-closure-no-progn
  ((lambda ()
     (block nil
       ((lambda ()
          ((lambda ()
             (return 1)))
          2)))))
  1)

(test-js-eval function-clears-current-block-tag
  ((lambda ()
     (block X
       ((lambda ()
          ((lambda ()
             (return-from X 1)))
          2)))
     5))
  5)

(test-js-eval defvar-dynamic-return
  (progn
    (defvar foo ((lambda ()
                   (block nil
                     ((lambda () (return 6)))
                     (+ 1 2)))))
    foo)
  6)

(test-js-eval defvar-dynamic-return1
  (progn
    (defvar foo ((lambda ()
                   (block nil
                     ((lambda () (return 6)))
                     (+ 1 2))
                   (+ 4 5))))
    foo)
  9)

(test-js-eval defvar-dynamic-return-expression
  (progn
    (defvar foo ((lambda ()
                   (+ 1 (block nil
                          ((lambda () (return 6)))
                          (+ 1 2))))))
    foo)
  7)
Vladimir Sedach's avatar
Vladimir Sedach committed
1042
1043
1044
1045
1046
1047
1048
1049

(test-js-eval defun-when-if-return
  (progn
    (defun foobar ()
      (when t
        (loop if t return 10)))
    (foobar))
  10)