-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.l1
1006 lines (876 loc) · 25.2 KB
/
tests.l1
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
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
492
493
494
495
496
497
498
499
500
501
502
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
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
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
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
;; Unit tests in l1-proper
(test '(basic assertions)
(is t)
(is 3)
(is +)
(is (not ()))
(is (and t t))
(is (not (and () t)))
(errors '(assertion failed)
(is ()))
(errors '(assertion failed)
(is (and t ())))
(is (= 1 1))
(errors '(is not equal to)
(is (= 1 2)))
(is (= (+ 2 3 4) (+ 4 3 1 1)))
(errors '(is not equal to)
(is (= (+ 2 3 4) 4))))
(test '(basic stuff
cons
len)
(errors '(is not a function)
(t))
(errors '(is not a function)
(1))
(errors '(is not a function)
(()))
(test '(len)
(is (= 3 (len '(1 2 3))))
(is (= 0 (len ())))
(errors '(is not a list) (len 3)))
(errors '(missing argument) (cons))
(errors '(expects a single argument) (atom?))
(errors '(missing argument) (cdr))
(is (= '(1 2 3 4) (cons 1 '(2 3 4))))
(is (= '((1 2) 3 4) (cons (cons 1 (cons 2 ())) '(3 4))))
(is (= '(()) (cons () ())))
(is (= '(1 . 2) (cons 1 2)))
(is (= '(1 2) (cons 1 '(2))))
(is (= '((1 . 2)) (cons (cons 1 2) ())))
(is (= '((1) . 2) (cons '(1) 2))))
(test '(car and cdr)
(is (= (car ()) ()))
(is (= (cdr ()) ()))
(is (= (cdr '(is not common lisp))
'(not common lisp)))
(is (= (car '(is not common lisp))
'is))
(is (= 1
(car '(1 2 3))))
(is (= '(2 3) (cdr '(1 2 3))))
(errors '(missing argument) (car))
(errors '(not a list) (car t))
(errors '(not a list) (cdr t))
(is (= 1 (car '(1 . 2))))
(is (= 2 (cdr '(1 . 2))))
(is (= 1 (caar '((1 2) (3 4)))))
(is (= 3 (caadr '((1 2) (3 4)))))
(is (= 4 (cadadr '((1 2) (3 4)))))
(errors '(unknown symbol)
(cadoopdydoopdr '((1 2) (3 4)))))
(test '(boolean logic)
(is t)
(is (not ()))
(is (not (= t ())))
(is (and))
(is (and t t))
(is (not (or)))
(is (or t ()))
(is (or () t))
(is (not (or () ())))
(is (or () () () () () t () () () ()))
(is (not (and () () () ()))))
(test '(types)
(is (atom? (quote foo)))
(is (not (atom? (quote (foo bar)))))
(is (atom? (quote atom?)))
(is (not (atom? atom?)))
(is (not (number? 'a)))
(is (number? 3))
(is (not (list? 'a)))
(is (list? ()))
(is (list (range 3)))
(is (not (atom? '(jack))))
(is (atom? 'a)))
(test '(equality)
(is (= t t))
(is (= (not t) ()))
(is (= (not ()) t))
(is (= (cons t ()) '(t)))
(is (= (cons (quote hello) (quote (world)))
'(hello world)))
(is (= (quote foo) 'foo))
(is (= (quote 3) 3))
(is (= (quote (1 2 3)) '(1 2 3)))
(is (= '(1 2 3) '(1 2 3)))
(is (= (quote ()) ()))
(is (= (quote foo) (quote foo)))
(is (= () ()))
(is (not (= (quote foo) (quote bar))))
;; P.G.'s interpretation of McCarthy says this is (), but
;; it's simpler to have just one equality operator for now,
;; which works for numbers, lists and atoms:
(is (not (= (quote foo) (quote (foo bar)))))
(is (= (quote (foo bar)) (quote (foo bar))))
(is (= 2 (+ 1 1)))
(is (= 2 (+ 1 1) (- 3 1)))
(is (not (= (quote (1 2 3)) ()))))
(test '(cond)
(is (not (cond)))
(is (not (cond (() 3))))
(is (= 3 (cond (3 3))))
(is (not (cond)))
(is (= ()
(cond (() 3)
(() 4))))
(is (= (cond (t 3)
(t 4))
3))
(is (= t (cond (t t) ((-) t))))
(errors '(requires a list of pairs) (cond ()))
(errors '(requires a list of pairs) (cond (t)))
(errors '(missing argument) (cond ((-) t)))
(errors '(missing argument) (cond (t (-))))
(errors '(missing argument) (cond (() t) ((-) t)))
(errors '(missing argument) (cond (() t) (t (-)))))
(test '(lambdas)
(is (= () ((lambda ()))))
(is (= 333 ((lambda () 333))))
(is (= 1 ((lambda (_) 1) '(stop ignoring me))))
(is (= 2 ((lambda (x) (+ 1 x)) 1)))
(is (= 1 ((lambda () 1 2 1))))
(is (= 3 ((lambda () 1 2 3))))
(errors '(division by zero) ((lambda ()
(/ 1 0))))
(errors '(division by zero) ((lambda ()
1
(/ 1 0))))
(is (= 1 ((lambda (a b c d) d)
4 3 2 1)))
;; rest args / n-ary lambdas:
(is (= (list 2 3 4) ((lambda (a . d) d)
1 2 3 4)))
(errors '(not enough arguments) ((lambda (a . d) d)))
(errors '(not enough arguments) ((lambda (a b . d) d) 1))
(errors '(not enough arguments) ((lambda (a b) d) 1))
(errors '(too many arguments) ((lambda (a b) b) 1 2 3))
(errors '(requires an argument list) (lambda 3))
(errors '(argument list item is not an atom) (lambda (3)))
(errors '(requires a rest argument) ((lambda (()))))
(errors '(cannot bind or set t) ((lambda (t)) 2))
;; rest-only lambdas:
(= 3 ((lambda (a . l) (len l)) 0 1 2 3))
(= 0 ((lambda (a . l) (len l)) 'hi))
(= 3 ((lambda (() . l) (len l)) 1 2 3))
(= 0 ((lambda (() . l) (len l))))
;; named lambdas:
(is (not ((lambda foo ()))))
(is (= 3 ((lambda count (l)
(cond (l (inc (count (cdr l))))
(t 0)))
(range 3))))
;; lambda compound bodies:
(let* ((x 0)
(y 1)
(f (lambda ()
(set! x 2)
(set! y 3))))
(f)
(is (= x 2))
(is (= y 3)))
;; repeated invocations of same:
(let* ((n 0)
(new-n!
(lambda ()
(set! n (inc n))
'lastthing)))
(dotimes 5
(new-n!))
(is (= 5 n))))
(test '(def and defn)
(defn foo ())
(is (= () (foo)))
(defn foo (a))
(is (= () (foo 1)))
(defn foo (a) a)
(is (= 1 (foo 1)))
(errors '(name must be an atom) (defn (bazzy)))
(errors '(requires a function name) (defn))
(errors '(requires an argument list) (defn foo))
;; rest params:
(defn wierd-len (() . l)
(len l))
(is (= 0 (wierd-len)))
(is (= 5 (wierd-len 1 1 1 1 1)))
(defn wierder-len (a . l) (len l))
(errors '(not enough arguments) (wierder-len))
(is (= 0 (wierder-len 1)))
(is (= 1 (wierder-len 1 2)))
(is (= 10 (wierder-len 1 1 2 3 4 5 6 7 8 9 10)))
;; doclists:
(defn a () 3)
(is (= 3 (a)))
(is (not (doc a)))
(errors '(missing argument) (doc))
(errors '(is not a function) (doc 3))
(def b (lambda ()))
(is (not (doc b)))
(def bdoc (lambda () (doc (it floats))))
(is (= '((it floats)) (doc bdoc)))
(is (not (bdoc)))
(defn a () (doc (do something)) 3)
(is (= 3 (a)))
(is (= '((do something)) (doc a)))
(defn c () (doc (something 1)
(something 2)
(something 3)
(example (= 'c (c))))
'c)
(is (= 'c (c)))
(test '(defn mutates toplevel, not inner scope)
(let ()
(defn b () 4))
(is (= 4 (b))))
(test '(def mutates toplevel, not inner scope)
(let ()
(def b 99))
(is (= 99 b))))
(test '(split and fuse)
(is (= '(1) (split 1)))
(is (= '(-1) (split -1)))
(is (= '(-3 2 1) (split -321)))
(is (= '(a) (split (quote a))))
(is (= '(g r e e n s p u n) (split 'greenspun)))
(is (= '(8 3 8 1 0 2 0 5 0) (split (* 12345 67890))))
(is (= 15 (len (split (* 99999 99999 99999)))))
(errors '(expects a single argument)
(split))
(errors '(expects a single argument)
(split 1 1))
(errors '(expects an atom or a number)
(split '(a b c)))
(is (= '() (fuse ())))
(is (= 'a (fuse (quote (a)))))
(is (= 'aa (fuse (quote (aa)))))
(is (= 'ab (fuse (quote (a b)))))
(is (= 1 (fuse (quote (1)))))
(is (= 12 (fuse (quote (1 2)))))
(is (= 125 (+ 2 (fuse (quote (1 2 3))))))
(is (= 1295807125987 (fuse (split 1295807125987))))
(errors '(expects a single argument)
(fuse)))
(test '(randomness)
(is (= 1000 (len (randigits 1000))))
(errors '(not enough arguments)
(randigits))
(is (= 1 (randchoice (list 1 1 1 1 1)))))
(test '(list function)
(is (= '(1 2 3)
(list 1 2 3)))
(is (= '(1 2 3) (list 1 (+ 1 1) (+ 4 -1))))
(is (= () (list))))
(test '(concat)
(is (= () (concat)))
(is (= () (concat ())))
(is (= () (concat () ())))
(is (= '(a) (concat '(a))))
(is (= '(a b c d)
(concat '(a b) '(c d))))
(is (= '(a b c d e f) (concat '(a b) '(c d) '(e f)))))
(test '(range)
(is (not (range 0)))
(is (= '(0) (range 1)))
(is (= '(0 1) (range 2)))
(is (= 100 (len (range 100)))))
(test '(reduce)
(is (= 0 (reduce + ())))
(is (= 1 (reduce * ())))
(is (= 0 (reduce + '(0))))
(is (= 0 (reduce + 0 ())))
(is (= 1 (reduce + 0 (range 2))))
(is (= 6 (reduce + 0 (range 4))))
(is (= 362880 (reduce * 1 (split 123456789))))
(is (= (range 10)
(reduce concat (map list (range 10))))))
(test '(apply)
(is (= 6 (apply + '(1 2 3))))
(is (= 0 (apply + ())))
(is (= 1 (apply + '(1))))
(is (= 6 (apply * '(1 2 3))))
(is (= 6 (apply * (split 123))))
(is (= 362880 (apply * (split 123456789))))
(is (= 1 (apply / (split 1111))))
(is (= t (apply = (split 'ooooooooooooooooo))))
(is (= () (apply = (split 'foo))))
(is (= t (apply (lambda (x y) (= x y)) (split 11))))
(is (= () (apply (lambda (x y z) (= x y z)) (split 121))))
(is (= 12 (apply (lambda (x y z) (+ x y z))
(list (+ 1 1) (+ 2 2) (+ 3 3)))))
(is (= 0 (apply + ())))
(is (= 0 (apply + 0 ())))
(is (= 10 (apply + 1 2 '(3 4))))
(is (= 362880 (apply * (cdr (range 10)))))
(is (= () (apply concat (map list ()))))
(is (= () (apply concat '(()))))
(is (= () (apply concat ())))
(is (= '(1 2) (concat '(1) '(2))))
(is (= '(1 2) (concat '(1) '(2))))
(is (= '(1 2) (apply concat '((1) (2)))))
(is (= '(1 2) (apply concat '((1) (2)))))
(is (= '(1 2) (concat '(1) '(2))))
(is (= (range 10) (apply concat (map list (range 10)))))
(errors '(not enough arguments) (apply))
(errors '(not enough arguments) (apply +))
(errors '(too many arguments)
(apply (lambda (x y) (= x y)) (split 123))))
(test '(mapcat)
(is (= '(0 10 40 90 160 250 360 490 640 810)
;; FIXME: use comp and partial when available:
(mapcat (lambda (x)
(list (* x x 10)))
(range 10)))))
(test '(functions as values)
(is (zero? ((cond (t +)))))
(is (= 6 ((car (cons + ())) 1 2 3)))
(def a +)
(is (= 2 (a 1 1))))
(test '(zero? pos? and neg?)
(is (not (pos? 0)))
(is (pos? 1))
(is (neg? -1))
(errors '(not enough arguments) (pos?))
(errors '(too many arguments) (pos? 1 1))
(errors '(is not a number) (pos? 'one)))
(test '(inequalities <= < > >=)
(is (< 1 2))
(is (< 1 2 3))
(is (not (< 1 2 3 3)))
(is (<= 1 2 3 3))
(is (not (< 1 2 1)))
(is (not (< 2 1)))
(is (not (< 2 1 3)))
(is (> 1))
(is (> 3 2 1 0))
(is (not (< 0 0)))
(is (not (> 3 2 1 1)))
(is (> 3 2 1 0))
(is (>= 3 2 1 1))
(errors '(is not a number) (< t 1))
(errors '(is not a number) (< 1 t)))
(test '(arithmetic)
(is (zero? 0))
(is (not (zero? (quote zero))))
(is (= 0))
(is (= 0 0))
(is (= 0 0 0))
(is (not (= 0 0 1)))
(is (= 1 (+ 1)))
(is (= -1 (+ -1)))
(is (= 0 (+ 0)))
(is (= 2 (+ 1 1)))
(is (= 3 (+ 1 2)))
(is (= 7 (+ 1 1 2 3)))
(is (= 0 (+)))
(is (= (* 12349807213490872130987
12349807213490872130987)
152517738210391179737088822267441718485594169))
(is (= 55 (+ 1 2 3 4 5 6 7 8 9 10)))
(is (= 1000000000000000 (+ 999999999999999 1)))
(is (= 1000000000000000 (+ 1 999999999999999)))
(is (= 1 (+ (+ 1))))
(is (= 21 (+ (+ 1 2 3) 4 5 6)))
(is (= -1 (- 1)))
(is (= 0 (- 1 1)))
(is (= 31489071430987532109487513094875031984750983147
31489071430987532109487513094875031984750983147))
(is (= 0 (- 12349807213490872130987
12349807213490872130987)))
(is (= -9 (- (+ 1 2 3) 4 5 6)))
(is (= 1 (*)))
(is (= 1 (* 1 1)))
(is (= 2432902008176640000
(* 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20)))
(is (= 1 (/ 1 1)))
(is (= 2 (/ 4 2)))
(is (= 0 (/ 1 2)))
(is (= 1 (* 1 1 1 (*) (*) (*))))
(is (= 3 (+ 1 1 1 (+) (+) (+))))
(is (zero? (rem 1 1)))
(is (zero? (rem 10 10)))
(is (= 1 (rem 5 2)))
(errors '(division by zero) (rem 1 0))
(errors '(requires two arguments) (rem))
(errors '(expected number) (rem t t))
(errors '(expected number) (rem t 1))
(errors '(expected number) (rem 1 t))
(errors '(missing argument) (=))
(errors '(missing argument) (-))
(errors '(missing argument) (/))
(errors '(division by zero) (/ 1 0))
(errors '(expected number) (+ 0 (cond)))
(errors '(expected number) (+ t 1))
(errors '(expected number) (- t))
(errors '(expected number) (- t 1))
(errors '(expected number) (- 1 t))
(errors '(expected number) (* t))
(errors '(expected number) (/ t))
(is (= 1 (/ 1)))
(errors '(expected number) (/ 1 t)))
(test '(basic def)
(def x 3)
(is (= 3 x))
(errors '(unknown symbol)
(def do-not-die unknown-symbol)))
(test '(closures scope and shadowing)
(def plus3-maker
(lambda ()
((lambda (a)
(lambda (x)
(+ a x)))
3)))
(is (= 7 ((plus3-maker) 4)))
(defn incrementer (n)
(lambda (x)
(+ x n)))
(def inc (incrementer 1))
(is (= 6 (inc 5)))
(def add2 (incrementer 2))
(is (= 7 (add2 5)))
(is (= 0 (def x 0)))
(zero? x)
(is (zero? (cond ((= x 0) 0)
(t x))))
(is (= 1 (def x 1)))
(is (= 1 (cond ((= x 0) 0)
(t x))))
(def a 1)
(def b 2)
(is (= 6 ((lambda (x) (+ x a b)) 3)))
(defn f (x) (+ 1 x))
(is (= 3 (f 2)))
(defn f (x) (cond (x 3) (t 4)))
(is (= 3
(f t)
(f 1)
(f '(1 2 3))
(f f)))
(is (= 4 (f ())))
(defn f (x)
(cond ((= x 3) 1)
(t (+ 1 (f 3)))))
(is (= 1 (f 3)))
(is (= 2 (f 4)))
;; This one found a tricky bug while figuring out envs / scope:
(def f (lambda (x) (+ x (g (- x 1)))))
(def g (lambda (x) 0))
(is (= 1 (f 1)))
(test '(shadowing)
(def a 1)
(def f (lambda (a) (+ a a)))
(is (= 6 (f 3)))
(def a 999)
(is (= 4 (f 2)))))
(test '(slow fibonacci)
(def fib
(lambda (n)
(cond ((zero? n) 0)
((= 1 n) 1)
(t (+ (fib (- n 1))
(fib (- n 2)))))))
(is (= 0 (fib 0)))
(is (= 1 (fib 1)))
(is (= 13 (fib 7)))
(is (= 55 (fib 10)))
(is (= 6765 (fib 20))))
(test '(factorial)
(defn fact (n)
(if (zero? n)
1
(* n (fact (- n 1)))))
(is (= 30414093201713378043612608166064768844377641568960512000000000000
(fact 50)))
(is (= 2568 (len (split (fact 1000))))))
;; This case helped me (JJ) find a subtle math/pointer bug:
(test '(oddcase)
(def f
(lambda (x)
(cond ((= x 0) 0)
(t (+ x (f (- x 1)))))))
(is (zero? (f 0)))
(is (= 1 (f 1))))
(test '(let)
(is (not (let ())))
(is (= 2 (let ((_ 1))
2)))
(is (zero? (let ((_ 1))
1
2
3
0)))
(is (zero? (let ((a 0))
a)))
(errors '(let bindings must be a list) (let 3))
(errors '(a let binding must be a list)
(let ((a 0)
3)))
(test '(shadowing inside let)
(is (= 4
(let ((a 1))
(+ a (let ((a 2))
(+ a 1))))))))
(test '(version)
(is (< 2 (len (version)))))
(test '(alpha / word stuff)
(is (= 10 (len (randalpha 10))))
(is (= 1000 (len (split (fuse (randalpha 1000))))))
(is (= 'Capoeira (capitalize 'capoeira)))
(is (= 'KARATE (upcase 'kaRatE)))
(is (= 'break-dancing (downcase 'BREAK-dancing)))
(is (= 'Nope. (period (capitalize 'nope))))
(is (= 'Well, (comma (capitalize 'well))))
(is (= 'bang! (bang 'bang)))
(is (= '(You.) (tosentence '(you))))
(is (= '(You hoo.) (tosentence '(you hoo))))
(is (= '(Boo hoo hoo.) (tosentence '(boo hoo hoo))))
(is (= '(Go away!) (exclaim '(go away)))))
(test '(core / l1 functions)
(is (= () (reverse ())))
(is (= 'apogee (fuse (reverse (split 'eegopa)))))
(is (= '(1 2 3) (concat '(1 2 3) ())))
(is (= '(1 2 3 4 5 6) (concat '(1 2 3) '(4 5 6))))
(is (= '(1 2 3 4 5 6) (concat '(1 2) '(3 4) '(5 6))))
(is (= 100 (len (range 100))))
(is (zero? (car (range 5))))
(is (= 4 (last (range 5))))
(is (not (last ())))
(is (= 'abc (fuse (take 3 (split 'abcdefg)))))
(is (= (range 10) (take 10 (range 100))))
(is (= (range 3) (take 10 (range 3))))
(is (= (range 10) (drop 0 (range 10))))
(is (= '(5 6 7 8 9) (drop 5 (range 10))))
(is (= () (drop 1000 (range 10))))
(is (even? 2))
(is (even? 2000))
(is (odd? 3))
(is (odd? 2001))
(is ((complement odd?) 2))
(is (= () (flatten '(()))))
(is (= (split 'abcdefgh) (flatten '(a b (c d (e f) g h)))))
(is (= 3 ((constantly 3))))
(is (= 3 ((constantly 3) '(ignore me))))
(is (= '(t t t t t))) (repeat 5 t)
(is (= '(1 2 3 4 5)
(map inc (range 5))))
(is (= '(0 2 4) (filter even? (range 5))))
(is (= '(1 3))) (remove even? (range 5)))
(test '(math)
(is (= 1024 (** 2 10)))
(is (= 10000000000 (** 10 10)))
(is (= 18446744073709551616 (** 2 64)))
;; (** 3 60935681) stack overflowed in a fuzzing test, fixed, but
;; testing that takes too long!
(** 3 6093)
(is (= 2 (isqrt 4)))
(let ((biggie (** 18446744073709551616 5)))
(is (= biggie
(isqrt (** biggie 2)))))
(is (= 2 (abs -2)))
(is (= 3 (abs 3))))
(test '(macros)
(defmacro f (zzz) (list '+ 2 3))
(is (= 5 (f 888)))
(defmacro f (zzz) (list '+ 2 zzz))
(is (= 12 (f 10)))
(is (= 3 (macroexpand-1 3)))
(is (= 3 (macroexpand-1 '3)))
(defmacro x () 1)
(is (= 1 (macroexpand-1 '(x))))
(defmacro x () 1 2)
(is (= 2 (macroexpand-1 '(x))))
;; Test for bug, #89:
(defmacro prepend! (l x)
`(set! ~l (cons ~x ~l)))
(let ((x ()))
(prepend! x 1)
(is (= '(1) x)))
;; Another test for bug, #89:
(defmacro identity! (x) `(set! ~x ~x))
(let ((x 5))
(identity! x)
(is (= 5 x))))
(test '(if when and when-not macro)
(is (= 1 (if t 1 2)))
(is (= 2 (if () 1 2)))
(is (= '(cond (() 1) (t 2))
(macroexpand-1 '(if () 1 2))))
(is (not (when () 3)))
(is (= 3 (when-not () 3)))
(is (= 3 (when-not (list? 3) 3)))
(is (= 3 (if ()
1
(when t
3)))))
(test '(list*)
(is (= '(a b c 0 1 2 3 4)
(list* 'a 'b 'c (range 5)))))
(test '(progn)
;; FIXME: add set! and test side effects w/ progn.
(is (= 6 (progn
1
2
3
6))))
(test '(syntax-quote)
(is (= (quote foo) (syntax-quote foo)))
(is (= 'foo `foo))
(is (= '(a list by any other name)
`(a list by any other name)))
(is (= 3 `~3))
(let ((a 1))
(is (= 1 `~a))
(is (= '(hello 1 2)
`(hello ~a ~(+ a 1)))))
(let ((l (range 3)))
(is (= '(a b c 0 1 2 d e)
`(a b c ~@l d e)))))
(test '(some and every)
(is (every pos? (cdr (range 5))))
(is (every odd? '(1 3 5)))
(is (not (some neg? (cdr (range 5))))))
(test '(docs and forms)
(is (< 10 (len (forms))))
(is (some (comp (partial = 'forms) first)
(forms)))
(is (every (comp (partial = 6) len) (forms)))
(is (= '(Return a new atom with all characters in lower case)
(doc downcase))))
(test '(shuffle)
(is (= 100 (len (shuffle (range 100))))))
(test '(error)
(errors '(requires a non-empty argument)
(error))
(errors '(3)
(error 3))
(errors '(i fail)
(error '(i fail))))
(test '(swallow)
(is (not (swallow)))
(is (not (swallow 1 2 3)))
(is (swallow 1 2 (error '(boom)) 3 4)))
(test '(set!)
(errors '(not bound in any environment)
(set! abcd 5))
(def abcd 5)
(= abcd 5)
(set! abcd 6)
(= abcd 6))
(test '(comment sugar)
#_(/ 1 0))
(test '(gensym)
(is (= '<gensym- (fuse (take 8 (split (gensym))))))
(is (= '<gensym-foo (fuse (take 11 (split (gensym 'foo))))))
(is (not (= (gensym) (gensym))))
(is (not (= (gensym 'foo) (gensym 'foo))))
(errors '(expects an atom)
(gensym (range 5)))
(errors '(0 or 1 arguments)
(gensym 'really 'unique)))
(test '(fuzz found these strange birds, each of which crashed
the interpreter)
(errors '(needs an argument)
(quote))
(defmacro x () () ())
(errors '(argument must be an atom)
(def 833))
(errors '(missing argument)
(lambda t))
(errors '(missing argument)
(lambda))
(errors '(missing argument)
(def))
(errors '(missing argument)
(def t))
(errors '(must be a list of binding pairs)
(let (())))
(errors '(must be a list of binding pairs)
(let ((0))))
(errors '(needs an argument)
(syntax-quote))
(errors '(expects a nonempty list)
(randchoice ()))
(errors '(screen not initialized)
(screen-write 30 87 ()))
(errors '(expected list)
(let ((lll (lambda ())))
(if . lll)))
(errors '(doc form is not a list)
(let ((lll (lambda ())))
(lambda t () (doc . lll) 5)))
(is (not (repeat (- 1) 'a)))
;; Issue #79:
(errors '(error not found)
(errors (list* () t)))
(errors '(error not found)
(errors (list* () 695)))
(is (not (dotimes (- 100)))))
(test '(second)
(is (not (second ())))
(is (not (second '(a))))
(is (= 2 (second '(1 2))))
(is (= 2 (second '(1 2 3)))))
(test '(comp)
(is (= 1 ((comp) 1)))
(is (= 2 ((comp inc) 1)))
(is (= 3 ((comp inc inc) 1)))
(is (= 4 ((comp inc inc inc) 1)))
(is (= 5 ((comp inc inc inc inc) 1)))
(is (= 1 ((comp dec inc) 1)))
(is (= 6 ((comp inc +) 2 3))))
(test '(partial)
(is (partial +))
(is (= 1 ((partial +) 1)))
(is (= 3 ((partial + 2) 1))))
(test '(min and max)
(errors '(needs at least one number)
(max))
(is (= -5 (max -5)))
(is (= 1 (max 1)))
(is (= 1 (max 1 0)))
(is (= 9 (apply max (range 10))))
(errors '(needs at least one number)
(min))
(is (= -5 (min -5)))
(is (= 1 (min 1)))
(is (= 0 (min 1 0)))
(is (= 0 (apply min (range 10)))))
(defn member (x l)
(some (partial = x) l))
(test '(exceptions, stacktraces and their contents)
(is (not (try)))
(is (= 1 (try 1)))
(is (= 3 (try 1 2 3)))
(is (not (try (catch e))))
(is (not (try (catch e e))))
(is (= 1 (try 1 (catch e))))
(is (= 1 (try 1 (catch e 2))))
(is (= 2 (try 1 2 (catch e 3 4))))
(is (= 2 (try (/ 1 0) (catch e 2))))
(is (= 4 (try 1 (/ 1 0) 2 (catch e 3 4))))
(try (/ 1 0) (catch e))
(is (pos? (try (/ 1 0) (catch e (len e)))))
(try
(/ 1 0)
(catch e
(is (member '(division by zero) e))))
(is (= 4
(try
1
(/ 1 0)
2
(catch e
3
(is (member '(division by zero) e))
4))))
(errors '(inner error)
(try
(/ 1 0)
(catch e
(error '(inner error)))))
(try
1
(+ 1 (/ 1 0))
2
(catch e
3
(is (member '(division by zero) e))
(is (member '(builtin function /) e))
4))
(let ((e (try
(/ 1 0)
(catch e e))))
(is (member '(division by zero) e))
(is (member '(builtin function /) e)))
(defn wrap-divide-by-zero ()
(/ 1 0)
1)
(defn intermediate-fn ()
(wrap-divide-by-zero)
1)
(try
(intermediate-fn)
(catch e
(is (member '(division by zero) e))
(is (member '(builtin function /) e))
(is (some (lambda (l) (member 'lambda l)) e))))
(defn f () 0)
(defn g () 1)
(defn d (x y) (/ x y))
(defn a () (+ 3 (d (g) (f))))
(try
(a)
(catch e
(is (member '(division by zero) e))
(is (member '(builtin function /) e)))))
(test '(source function)
(defn funfun (x)
1
2
(+ 5 x))
(is (= '(lambda (x) 1 2 (+ 5 x))
(source funfun)))
(defn restyfun (x . z)
'(something else))
(is (= '(lambda (x . z)
'(something else))
(source restyfun)))
(errors '(not a function)
(source 3))
(errors '(not a function)
(source 'a))
(errors '(cannot get source of builtin)
(source +)))
(test 'unicode
(def 水 'water)
(is (= 水 'water))
(is (= '▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▐▖▗■□▢▣▥▧▨▩
(fuse'(▁ ▂ ▃ ▄ ▅ ▆ ▇ █ ▉ ▊
▋ ▌ ▍ ▎ ▏ ▐ ▖ ▗ ■
□ ▢ ▣ ▥ ▧ ▨ ▩))))
(is (= 2 (len (split '水果)))))
(test 'shell
(errors '(argument must be a nonempty list of strings)
(shell 'pwd))
(errors '(argument must be a nonempty list of strings)
(shell '()))
(errors '(file not found) (shell '(asdfkhjasdfjkh)))
(is (= 3 (len (shell '(pwd))))))
(test 'sort
(errors '(expects a single argument) (sort))
(errors '(expects a single argument) (sort () ()))
(errors '(is not a list) (sort 3))
(is (not (sort ())))
(is (= '(1 2) (sort '(1 2))))
(is (= '(1 2) (sort '(2 1))))
(is (= '(2 11) (sort '(11 2))))
(is (= '(a b) (sort '(b a))))
(errors '(is not same type) (sort '(1 a))))
(test 'sort-by
(errors '(expects two arguments) (sort-by))
(errors '(expects two arguments) (sort-by () () ()))
(is (not (sort-by 3 ())))
(errors '(is not a function) (sort-by 3 (range 10)))
(errors '(is not a list) (sort-by first 3))
(is (not (sort-by identity ())))
(is (not (sort-by < ())))
(is (= (range 10) (sort-by < (range 10))))
(is (= (range 10) (sort-by > (range 10))))
(is (= (map list (range 10))
(sort-by first (reverse (map list (range 10))))))
(is (= '(a bb ccc)
(sort-by (comp len split)
'(ccc a bb))))
(is (= '(() (1) (2 2) (3 3 3))
(sort-by len '(() (1) (3 3 3) (2 2)))))
(errors '(is not a list)
(sort-by len '(() (1) (3 3 3) a (2 2)))))
(test 'interpose
(is (not (interpose 'FOO ())))
(is (= '(0) (interpose 'FOO (range 1))))
(is (= '(0 FOO 1) (interpose 'FOO (range 2))))
(is (= '(0 FOO 1 FOO 2) (interpose 'FOO (range 3)))))