forked from scheme-requests-for-implementation/srfi-122
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-arrays.scm
3037 lines (2601 loc) · 113 KB
/
test-arrays.scm
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
(declare (standard-bindings)(extended-bindings)(block)(safe) (mostly-fixnum))
(include "generic-arrays.scm")
(declare (inlining-limit 0))
(define tests 100)
(set! tests tests)
(define-macro (test expr value)
`(let* (;(ignore (pretty-print ',expr))
(result (call-with-current-continuation
(lambda (c)
(with-exception-catcher
(lambda (args)
(cond ((error-exception? args)
(c (error-exception-message args)))
;; I don't expect any of these, but it sure makes debugging easier
((unbound-global-exception? args)
(unbound-global-exception-variable args))
(else
"piffle")))
(lambda ()
,expr))))))
(if (not (equal? result ,value))
(pp (list ',expr" => " result ", not " ,value)))))
(define-macro (test-multiple-values expr vals)
`(call-with-values
(lambda () ,expr)
(lambda args
(if (not (equal? args ,vals))
(pp (list ',expr " => " args ", not " ,vals #\newline))))))
(define-macro (do-times var n . body)
;; not so useful when the debugger can't report line numbers inside macros.
`(do ((,var 0 (+ ,var 1)))
((= ,var ,n))
,@body))
;;; requires make-list function
(define (random a #!optional b)
(if b
(+ a (random-integer (- b a)))
(random-integer a)))
;; (include "generic-arrays.scm")
(pp "Interval error tests")
(test (make-interval 1 '#(3 4))
"make-interval: The first argument is not a nonempty vector of exact integers: ")
(test (make-interval '#(1 1) 3)
"make-interval: The second argument is not a nonempty vector of exact integers: ")
(test (make-interval '#(1 1) '#(3))
"make-interval: The first and second arguments are not the same length: ")
(test (make-interval '#() '#())
"make-interval: The first argument is not a nonempty vector of exact integers: ")
(test (make-interval '#(1.) '#(1))
"make-interval: The first argument is not a nonempty vector of exact integers: ")
(test (make-interval '#(1 #f) '#(1 2))
"make-interval: The first argument is not a nonempty vector of exact integers: ")
(test (make-interval '#(1) '#(1.))
"make-interval: The second argument is not a nonempty vector of exact integers: ")
(test (make-interval '#(1 1) '#(1 #f))
"make-interval: The second argument is not a nonempty vector of exact integers: ")
(test (make-interval '#(1) '#(1))
"make-interval: Each lower-bound must be less than the associated upper-bound: ")
(test (make-interval '#(1 2 3) '#(4 2 6))
"make-interval: Each lower-bound must be less than the associated upper-bound: ")
(pp "interval result tests")
(test (make-interval '#(11111) '#(11112))
(make-interval '#(11111) '#(11112)))
(test (make-interval '#(1 2 3) '#(4 5 6))
(make-interval '#(1 2 3) '#(4 5 6)))
(pp "interval? result tests")
(test (interval? #t)
#f)
(test (interval? (make-interval '#(1 2 3) '#(4 5 6)))
#t)
(pp "interval-dimension error tests")
(test (interval-dimension 1)
"interval-dimension: argument is not an interval: ")
(pp "interval-dimension result tests")
(test (interval-dimension (make-interval '#(1 2 3) '#(4 5 6)))
3)
(pp "interval-lower-bound error tests")
(test (interval-lower-bound 1 0)
"interval-lower-bound: argument is not an interval: ")
(test (interval-lower-bound (make-interval '#(1 2 3) '#(4 5 6)) #f)
"interval-lower-bound: argument is not an exact integer: ")
(test (interval-lower-bound (make-interval '#(1 2 3) '#(4 5 6)) 1.)
"interval-lower-bound: argument is not an exact integer: ")
(test (interval-lower-bound (make-interval '#(1 2 3) '#(4 5 6)) -1)
"interval-lower-bound: index is not between 0 (inclusive) and (interval-dimension interval) (exclusive): ")
(test (interval-lower-bound (make-interval '#(1 2 3) '#(4 5 6)) 3)
"interval-lower-bound: index is not between 0 (inclusive) and (interval-dimension interval) (exclusive): ")
(test (interval-lower-bound (make-interval '#(1 2 3) '#(4 5 6)) 4)
"interval-lower-bound: index is not between 0 (inclusive) and (interval-dimension interval) (exclusive): ")
(pp "interval-upper-bound error tests")
(test (interval-upper-bound 1 0)
"interval-upper-bound: argument is not an interval: ")
(test (interval-upper-bound (make-interval '#(1 2 3) '#(4 5 6)) #f)
"interval-upper-bound: argument is not an exact integer: ")
(test (interval-upper-bound (make-interval '#(1 2 3) '#(4 5 6)) 1.)
"interval-upper-bound: argument is not an exact integer: ")
(test (interval-upper-bound (make-interval '#(1 2 3) '#(4 5 6)) -1)
"interval-upper-bound: index is not between 0 (inclusive) and (interval-dimension interval) (exclusive): ")
(test (interval-upper-bound (make-interval '#(1 2 3) '#(4 5 6)) 3)
"interval-upper-bound: index is not between 0 (inclusive) and (interval-dimension interval) (exclusive): ")
(test (interval-upper-bound (make-interval '#(1 2 3) '#(4 5 6)) 4)
"interval-upper-bound: index is not between 0 (inclusive) and (interval-dimension interval) (exclusive): ")
(pp "interval-lower-bounds->list error tests")
(test (interval-lower-bounds->list 1)
"interval-lower-bounds->list: argument is not an interval: ")
(pp "interval-upper-bounds->list error tests")
(test (interval-upper-bounds->list #f)
"interval-upper-bounds->list: argument is not an interval: ")
(pp "interval-lower-bound, interval-upper-bound, interval-lower-bounds->list, and interval-upper-bounds->list result tests")
(do ((i 0 (+ i 1)))
((= i tests))
(let* ((lower (map (lambda (x) (random 10)) (vector->list (make-vector (random 1 11)))))
(upper (map (lambda (x) (+ (random 1 11) x)) lower)))
(let ((interval (make-interval (list->vector lower)
(list->vector upper)))
(offset (random (length lower))))
(test (interval-lower-bound interval offset)
(list-ref lower offset))
(test (interval-upper-bound interval offset)
(list-ref upper offset))
(test (interval-lower-bounds->list interval)
lower)
(test (interval-upper-bounds->list interval)
upper))))
(pp "interval-lower-bounds->vector error tests")
(test (interval-lower-bounds->vector 1)
"interval-lower-bounds->vector: argument is not an interval: ")
(pp "interval-upper-bounds-> error tests")
(test (interval-upper-bounds->vector #f)
"interval-upper-bounds->vector: argument is not an interval: ")
(pp "interval-lower-bound, interval-upper-bound, interval-lower-bounds->vector, and interval-upper-bounds->vector result tests")
(do ((i 0 (+ i 1)))
((= i tests))
(let* ((lower (map (lambda (x) (random 10)) (vector->list (make-vector (random 1 11)))))
(upper (map (lambda (x) (+ (random 1 11) x)) lower)))
(let ((interval (make-interval (list->vector lower)
(list->vector upper)))
(offset (random (length lower))))
(test (interval-lower-bound interval offset)
(list-ref lower offset))
(test (interval-upper-bound interval offset)
(list-ref upper offset))
(test (interval-lower-bounds->vector interval)
(list->vector lower))
(test (interval-upper-bounds->vector interval)
(list->vector upper)))))
(pp "interval-projections error tests")
(test (interval-projections 1 1)
"interval-projections: The first argument is not an interval: ")
(test (interval-projections (make-interval '#(0) '#(1)) #t)
"interval-projections: The dimension of the first argument is not greater than 1: " )
(test (interval-projections (make-interval '#(0 0) '#(1 1)) 1/2)
"interval-projections: The second argument is not an exact integer: ")
(test (interval-projections (make-interval '#(0 0) '#(1 1)) 1.)
"interval-projections: The second argument is not an exact integer: ")
(test (interval-projections (make-interval '#(0 0) '#(1 1)) 0)
"interval-projections: The second argument is not between 0 and the dimension of the first argument (exclusive): ")
(test (interval-projections (make-interval '#(0 0) '#(1 1)) 2)
"interval-projections: The second argument is not between 0 and the dimension of the first argument (exclusive): ")
(pp "interval-projections result tests")
(do ((i 0 (+ i 1)))
((= i tests))
(let* ((lower (map (lambda (x) (random 10)) (vector->list (make-vector (random 3 11)))))
(upper (map (lambda (x) (+ (random 1 11) x)) lower))
(left-dimension (random 1 (- (length lower) 1)))
(right-dimension (- (length lower) left-dimension)))
(test-multiple-values
(interval-projections (make-interval (list->vector lower)
(list->vector upper))
right-dimension)
(list (make-interval (list->vector (take lower left-dimension))
(list->vector (take upper left-dimension)))
(make-interval (list->vector (drop lower left-dimension))
(list->vector (drop upper left-dimension)))))))
(pp "interval-contains-multi-index? error tests")
(pp "interval-volume error tests")
(test (interval-volume #f)
"interval-volume: argument is not an interval: ")
(pp "interval-volume result tests")
(do ((i 0 (+ i 1)))
((= i tests))
(let* ((lower (map (lambda (x) (random 10)) (vector->list (make-vector (random 1 11)))))
(upper (map (lambda (x) (+ (random 1 11) x)) lower)))
(test (interval-volume (make-interval (list->vector lower)
(list->vector upper)))
(apply * (map - upper lower)))))
(pp "interval= error tests")
(test (interval= #f (make-interval '#(1 2 3) '#(4 5 6)))
"interval=: Not all arguments are intervals: ")
(test (interval= (make-interval '#(1 2 3) '#(4 5 6)) #f)
"interval=: Not all arguments are intervals: ")
(pp "interval= result tests")
(do ((i 0 (+ i 1)))
((= i tests))
(let* ((lower1 (map (lambda (x) (random 2)) (vector->list (make-vector (random 1 6)))))
(upper1 (map (lambda (x) (+ (random 1 3) x)) lower1))
(lower2 (map (lambda (x) (random 2)) lower1))
(upper2 (map (lambda (x) (+ 1 (random 1 3) x)) lower2)))
(test (interval= (make-interval (list->vector lower1)
(list->vector upper1))
(make-interval (list->vector lower2)
(list->vector upper2)))
(and (equal? lower1 lower2) ;; the probability of this happening is about 1/16
(equal? upper1 upper2)))))
(pp "interval-subset? error tests")
(test (interval-subset? #f (make-interval '#(1 2 3) '#(4 5 6)))
"interval-subset?: Not all arguments are intervals: ")
(test (interval-subset? (make-interval '#(1 2 3) '#(4 5 6)) #f)
"interval-subset?: Not all arguments are intervals: ")
(test (interval-subset? (make-interval '#(1) '#(2))
(make-interval '#(0 0) '#(1 2)))
"interval-subset?: The arguments do not have the same dimension: ")
(pp "interval-subset? result tests")
(do ((i 0 (+ i 1)))
((= i tests))
(let* ((lower1 (map (lambda (x) (random 2)) (vector->list (make-vector (random 1 6)))))
(upper1 (map (lambda (x) (+ (random 1 3) x)) lower1))
(lower2 (map (lambda (x) (random 2)) lower1))
(upper2 (map (lambda (x) (+ (random 1 3) x)) lower2)))
(test (interval-subset? (make-interval (list->vector lower1)
(list->vector upper1))
(make-interval (list->vector lower2)
(list->vector upper2)))
(and (##every >= lower1 lower2)
(##every <= upper1 upper2)))))
(pp "interval-contains-multi-index? error tests")
(test (interval-contains-multi-index? 1 1)
"interval-contains-multi-index?: argument is not an interval: ")
(test (interval-contains-multi-index? (make-interval '#(1 2 3) '#(4 5 6)) 1)
"interval-contains-multi-index?: dimension of interval does not match number of arguments: ")
(test (interval-contains-multi-index? (make-interval '#(1 2 3) '#(4 5 6)) 1 1/2 0.1)
"interval-contains-multi-index?: at least one multi-index component is not an exact integer: ")
(pp "interval-contains-multi-index? result tests")
(let ((interval (make-interval '#(1 2 3) '#(4 5 6)))
(interval-2 (make-interval '#(10 11 12) '#(13 14 15))))
(if (not (array-fold (lambda (x result)
(and result (apply interval-contains-multi-index? interval x)))
#t
(make-array interval list)))
(error "these should all be true"))
(if (not (array-fold (lambda (x result)
(and result (not (apply interval-contains-multi-index? interval x))))
#t
(make-array interval-2 list)))
(error "these should all be false")))
(pp "interval-for-each error tests")
(test (interval-for-each (lambda (x) x) 1)
"interval-for-each: Argument is not a interval: ")
(test (interval-for-each 1 (make-interval '#(3) '#(4)))
"interval-for-each: Argument is not a procedure: ")
(define (local-iota a b)
(if (= a b)
'()
(cons a (local-iota (+ a 1) b))))
(define (all-elements lower upper)
(if (null? (cdr lower))
(map list (local-iota (car lower) (car upper)))
(apply append (map (lambda (x)
(map (lambda (y)
(cons x y))
(all-elements (cdr lower) (cdr upper))))
(local-iota (car lower) (car upper))))))
(pp "interval-for-each result tests")
(do ((i 0 (+ i 1)))
((= i tests))
(let* ((lower (map (lambda (x) (random 10))
(vector->list (make-vector (random 1 7)))))
(upper (map (lambda (x) (+ (random 1 4) x))
lower)))
(let ((result '()))
(define (f . args)
(set! result (cons args result)))
(test (let ()
(interval-for-each f
(make-interval (list->vector lower)
(list->vector upper)))
result)
(reverse (all-elements lower upper))))))
(pp "interval-dilate error tests")
(let ((interval (make-interval '#(0 0) '#(100 100))))
(test (interval-dilate interval 'a '#(-10 10))
"interval-dilate: The second argument is not a vector of exact integers: ")
(test (interval-dilate 'a '#(10 10) '#(-10 -10))
"interval-dilate: The first argument is not an interval: ")
(test (interval-dilate interval '#(10 10) 'a)
"interval-dilate: The third argument is not a vector of exact integers: " )
(test (interval-dilate interval '#(10) '#(-10 -10))
"interval-dilate: The second and third arguments must have the same length as the dimension of the first argument: ")
(test (interval-dilate interval '#(10 10) '#( -10))
"interval-dilate: The second and third arguments must have the same length as the dimension of the first argument: ")
(test (interval-dilate interval '#(100 100) '#(-100 -100))
"interval-dilate: The resulting interval is empty: "))
;;; define random-interval, random-multi-index
(define (random-multi-index interval)
(apply values
(apply map
random
(map (lambda (bounds)
(bounds interval))
(list interval-lower-bounds->list
interval-upper-bounds->list)))))
(define (random-interval #!optional (min 1) (max 8) )
;; a random interval with min <= dimension < max
;; positive and negative lower bounds
(let* ((lower
(map (lambda (x)
(random -10 10))
(vector->list (make-vector (random min max)))))
(upper
(map (lambda (x)
(+ (random 1 8) x))
lower)))
(make-interval (list->vector lower)
(list->vector upper))))
(define (random-subinterval interval)
(let* ((lowers (interval-lower-bounds->vector interval))
(uppers (interval-upper-bounds->vector interval))
(new-lowers (vector-map random lowers uppers))
(new-uppers (vector-map (lambda (x) (+ x 1))
(vector-map random new-lowers uppers)))
(subinterval (make-interval new-lowers new-uppers)))
subinterval))
(define (random-nonnegative-interval #!optional (min 1) (max 11) )
;; a random interval with min <= dimension < max
;; positive and negative lower bounds
(let* ((lower
(map (lambda (x)
0)
(vector->list (make-vector (random min max)))))
(upper
(map (lambda (x)
(+ (random 1 11) x))
lower)))
(make-interval (list->vector lower)
(list->vector upper))))
(define (random-positive-vector n #!optional (max 5))
(vector-map (lambda (x)
(random 1 max))
(make-vector n)))
(define (random-boolean)
(zero? (random 2)))
(pp "array error tests")
(test (make-array 1 values)
"make-array: The first argument is not an interval: ")
(test (make-array (make-interval '#(3) '#(4)) 1)
"make-array: The second argument is not a procedure: ")
(pp "array result tests")
(let ((getter (lambda args 1.)))
(test (make-array (make-interval '#(3) '#(4)) getter)
(make-##array-base (make-interval '#(3) '#(4))
getter
#f
#f
#f
#f
#f)))
(pp "array-domain and array-getter error tests")
(test (array-domain #f)
"array-domain: object is not an array: ")
(test (array-getter #f)
"array-getter: object is not an array: ")
(pp "array?, array-domain, and array-getter result tests")
(let* ((getter (lambda args 1.))
(array (make-array (make-interval '#(3) '#(4)) getter)))
(test (array? #f)
#f)
(test (array? array)
#t)
(test (array-domain array)
(make-interval '#(3) '#(4)))
(test (array-getter array)
getter))
(pp "mutable-array result tests")
(let ((result #f))
(let ((getter (lambda (i) result))
(setter (lambda (v i) (set! result v)))
(domain (make-interval '#(3) '#(4))))
(test (make-array domain
getter
setter)
(make-##array-base domain
getter
setter
#f
#f
#f
#f))))
(pp "array-setter error tests")
(test (array-setter #f)
"array-setter: object is not an mutable array: ")
(pp "mutable-array? and array-setter result tests")
(let ((result (cons #f #f)))
(let ((getter (lambda (i) (car result)))
(setter (lambda (v i) (set-car! result v)))
(domain (make-interval '#(3) '#(4))))
(let ((array (make-array domain
getter
setter)))
(test (array? array)
#t)
(test (mutable-array? array)
#t)
(test (mutable-array? 1)
#f)
(test (array-setter array)
setter)
(test (array-getter array)
getter)
(test (array-domain array)
domain))))
(define (myindexer= indexer1 indexer2 interval)
(array-fold (lambda (x y) (and x y))
#t
(make-array interval
(lambda args
(= (apply indexer1 args)
(apply indexer2 args))))))
(define (my-indexer base lower-bounds increments)
(lambda indices
(apply + base (map * increments (map - indices lower-bounds)))))
(pp "new-indexer result tests")
(define (random-sign)
(- 1 (* 2 (random 2))))
(do ((i 0 (+ i 1)))
((= i tests))
(let* ((lower-bounds
(map (lambda (x) (random 2))
(vector->list (make-vector (random 1 7)))))
(upper-bounds
(map (lambda (x) (+ x (random 1 3)))
lower-bounds))
(new-domain
(make-interval (list->vector lower-bounds)
(list->vector upper-bounds)))
(new-domain-dimension
(interval-dimension new-domain))
(old-domain-dimension
(random 1 7))
(base
(random 100))
(coefficients
(map (lambda (x) (* (random-sign)
(random 20)))
(local-iota 0 old-domain-dimension)))
(old-indexer
(lambda args
(apply + base (map * args coefficients))))
(new-domain->old-domain-coefficients
(map (lambda (x)
(map (lambda (x) (* (random-sign) (random 10)))
(local-iota 0 new-domain-dimension)))
(local-iota 0 old-domain-dimension)))
(new-domain->old-domain
(lambda args
(apply values (map (lambda (row)
(apply + (map * row args)))
new-domain->old-domain-coefficients)))))
(if (not (and (myindexer= (lambda args
(call-with-values
(lambda () (apply new-domain->old-domain args))
old-indexer))
(##compose-indexers old-indexer new-domain new-domain->old-domain)
new-domain)))
(pp (list new-domain
old-domain-dimension
base
coefficients
new-domain->old-domain-coefficients)))))
(define (myarray= array1 array2)
(and (interval= (array-domain array1)
(array-domain array2))
(array-fold (lambda (vs result)
(and (equal? (car vs)
(cadr vs))
result))
#t
(array-map list array1 array2))))
(pp "array body, indexer, storage-class, and safe? error tests")
(let ((a (make-array (make-interval '#(0 0) '#(1 1)) ;; not valid
values
values)))
(test (array-body a)
"array-body: argument is not a specialized array: ")
(test (array-indexer a)
"array-indexer: argument is not a specialized array: ")
(test (array-storage-class a)
"array-storage-class: argument is not a specialized array: ")
(test (array-safe? a)
"array-safe?: argument is not a specialized array: "))
(pp "specialized-array error tests")
(test (make-specialized-array 'a)
"make-specialized-array: The first argument is not an interval: ")
(test (make-specialized-array (make-interval '#(0) '#(10)) 'a)
"make-specialized-array: The second argument is not a storage-class: ")
(test (make-specialized-array (make-interval '#(0) '#(10)) generic-storage-class 'a)
"make-specialized-array: The third argument is not a boolean: ")
(pp "array->specialized-array error tests")
(test (array->specialized-array #f generic-storage-class)
"array->specialized-array: Argument is not an array: ")
(test (array->specialized-array (make-array (make-interval '#(1) '#(2))
list)
#f)
"array->specialized-array: result-storage-class is not a storage-class: ")
(test (array->specialized-array (make-array (make-interval '#(1) '#(2))
list)
generic-storage-class
'a)
"array->specialized-array: safe? is not a boolean: ")
;; We gotta make sure than the error checks work in all dimensions ...
(test (array->specialized-array (make-array (make-interval '#(1) '#(2))
list)
u16-storage-class)
"array->specialized-array: not all elements of the array can be manipulated by the storage class: ")
(test (array->specialized-array (make-array (make-interval '#(1 1) '#(2 2))
list)
u16-storage-class)
"array->specialized-array: not all elements of the array can be manipulated by the storage class: ")
(test (array->specialized-array (make-array (make-interval '#(1 1 1) '#(2 2 2))
list)
u16-storage-class)
"array->specialized-array: not all elements of the array can be manipulated by the storage class: ")
(test (array->specialized-array (make-array (make-interval '#(1 1 1 1) '#(2 2 2 2))
list)
u16-storage-class)
"array->specialized-array: not all elements of the array can be manipulated by the storage class: ")
(test (array->specialized-array (make-array (make-interval '#(1 1 1 1 1) '#(2 2 2 2 2))
list)
u16-storage-class)
"array->specialized-array: not all elements of the array can be manipulated by the storage class: ")
(test (specialized-array-default-safe? 'a)
"specialized-array-default-safe?: The argument is not a boolean: ")
(pp "array->specialized-array result tests")
(specialized-array-default-safe? #t)
(pp "Safe tests")
(do ((i 0 (+ i 1)))
((= i tests))
(let* ((lower-bounds
(map (lambda (x) (random 4))
(vector->list (make-vector (random 1 7)))))
(upper-bounds
(map (lambda (x) (+ x (random 1 5)))
lower-bounds))
(domain
(make-interval (list->vector lower-bounds)
(list->vector upper-bounds)))
(array1
(let ((alist '()))
(make-array
domain
(lambda indices
(cond ((assoc indices alist)
=> cdr)
(else
indices)))
(lambda (value . indices)
(cond ((assoc indices alist)
=>(lambda (entry)
(set-cdr! entry value)))
(else
(set! alist (cons (cons indices value)
alist))))))))
(array2
(array->specialized-array array1 generic-storage-class))
(setter1
(array-setter array1))
(setter2
(array-setter array2)))
(do ((j 0 (+ j 1)))
((= j 25))
(let ((v (random 1000))
(indices (map random lower-bounds upper-bounds)))
(apply setter1 v indices)
(apply setter2 v indices)))
(or (myarray= array1 array2) (pp "test1"))
(or (myarray= (array->specialized-array array1 generic-storage-class ) array2) (pp "test3"))
))
(specialized-array-default-safe? #f)
(pp "Unsafe tests")
(do ((i 0 (+ i 1)))
((= i tests))
(let* ((lower-bounds
(map (lambda (x) (random 4))
(vector->list (make-vector (random 1 7)))))
(upper-bounds
(map (lambda (x) (+ x (random 1 5)))
lower-bounds))
(domain
(make-interval (list->vector lower-bounds)
(list->vector upper-bounds)))
(array1
(let ((alist '()))
(make-array
domain
(lambda indices
(cond ((assoc indices alist)
=> cdr)
(else
indices)))
(lambda (value . indices)
(cond ((assoc indices alist)
=>(lambda (entry)
(set-cdr! entry value)))
(else
(set! alist (cons (cons indices value)
alist))))))))
(array2
(array->specialized-array array1 generic-storage-class ))
(setter1
(array-setter array1))
(setter2
(array-setter array2)))
(do ((j 0 (+ j 1)))
((= j 25))
(let ((v (random 1000))
(indices (map random lower-bounds upper-bounds)))
(apply setter1 v indices)
(apply setter2 v indices)))
(or (myarray= array1 array2) (pp "test1"))
(or (myarray= (array->specialized-array array1 generic-storage-class ) array2) (pp "test3"))
))
(pp "array-map error tests")
(test (array-map 1 #f)
"array-map: The first argument is not a procedure: ")
(test (array-map list 1 (make-array (make-interval '#(3) '#(4))
list))
"array-map: Not all arguments after the first are arrays: ")
(test (array-map list (make-array (make-interval '#(3) '#(4))
list) 1)
"array-map: Not all arguments after the first are arrays: ")
(test (array-map list
(make-array (make-interval '#(3) '#(4))
list)
(make-array (make-interval '#(3 4) '#(4 5))
list))
"array-map: Not all arguments after the first have the same domain: ")
(pp "array-every and array-any error tests")
(test (array-every 1 2)
"array-every: The first argument is not a procedure: ")
(test (array-every list 1)
"array-every: Not all arguments after the first are arrays: ")
(test (array-every list
(make-array (make-interval '#(3) '#(4))
list)
1)
"array-every: Not all arguments after the first are arrays: ")
(test (array-every list
(make-array (make-interval '#(3) '#(4))
list)
(make-array (make-interval '#(3 4) '#(4 5))
list))
"array-every: Not all arguments after the first have the same domain: ")
(test (array-any 1 2)
"array-any: The first argument is not a procedure: ")
(test (array-any list 1)
"array-any: Not all arguments after the first are arrays: ")
(test (array-any list
(make-array (make-interval '#(3) '#(4))
list)
1)
"array-any: Not all arguments after the first are arrays: ")
(test (array-any list
(make-array (make-interval '#(3) '#(4))
list)
(make-array (make-interval '#(3 4) '#(4 5))
list))
"array-any: Not all arguments after the first have the same domain: ")
(pp "array-every and array-any")
(define (multi-index< ind1 ind2)
(and (not (null? ind1))
(not (null? ind2))
(or (< (car ind1)
(car ind2))
(and (= (car ind1)
(car ind2))
(multi-index< (cdr ind1)
(cdr ind2))))))
(define (indices-in-proper-order l)
(or (null? l)
(null? (cdr l))
(and (multi-index< (car l)
(cadr l))
(indices-in-proper-order (cdr l)))))
(do ((i 0 (+ i 1)))
((= i tests))
(let* ((interval
(random-nonnegative-interval 1 6))
(n
(interval-volume interval))
(separator
;; I want to make sure that the last item is chosen at least
;; once for each random
(random (max 0 (- n 10)) n))
(indexer
(##interval->basic-indexer interval))
(arguments-1
'())
(array-1
(make-array interval
(lambda args
(set! arguments-1 (cons args
arguments-1))
(let ((index (apply indexer args)))
(cond ((< index separator)
#f)
((= index separator)
1)
(else
(error "The array should never be called with these args"
interval
separator
args
index)))))))
(arguments-2
'())
(array-2
(make-array interval
(lambda args
(set! arguments-2 (cons args
arguments-2))
(let ((index (apply indexer args)))
(cond ((< index separator)
#t)
((= index separator)
#f)
(else
(error "The array should never be called with these args"
interval
separator
args
index))))))))
(test (array-any values array-1)
1)
(test (array-every values array-2)
#f)
(if (not (indices-in-proper-order (reverse arguments-1)))
(error "arrghh arguments-1" arguments-1))
(if (not (indices-in-proper-order (reverse arguments-2)))
(error "arrghh arguments-2" arguments-2))
))
(pp "array-fold error tests")
(test (array-fold 1 1 1)
"array-fold: The first argument is not a procedure: ")
(test (array-fold list 1 1)
"array-fold: The third argument is not an array: ")
(test (array-fold-right 1 1 1)
"array-fold-right: The first argument is not a procedure: ")
(test (array-fold-right list 1 1)
"array-fold-right: The third argument is not an array: ")
(pp "array-for-each error tests")
(test (array-for-each 1 #f)
"array-for-each: The first argument is not a procedure: ")
(test (array-for-each list 1 (make-array (make-interval '#(3) '#(4))
list))
"array-for-each: Not all arguments after the first are arrays: ")
(test (array-for-each list (make-array (make-interval '#(3) '#(4))
list) 1)
"array-for-each: Not all arguments after the first are arrays: ")
(test (array-for-each list
(make-array (make-interval '#(3) '#(4))
list)
(make-array (make-interval '#(3 4) '#(4 5))
list))
"array-for-each: Not all arguments after the first have the same domain: ")
(pp "array-map, array-fold, and array-for-each result tests")
(specialized-array-default-safe? #t)
(let ((array-builders (vector (list u1-storage-class (lambda indices (random 0 (expt 2 1))))
(list u8-storage-class (lambda indices (random 0 (expt 2 8))))
(list u16-storage-class (lambda indices (random 0 (expt 2 16))))
(list u32-storage-class (lambda indices (random 0 (expt 2 32))))
(list u64-storage-class (lambda indices (random 0 (expt 2 64))))
(list s8-storage-class (lambda indices (random (- (expt 2 7)) (expt 2 7))))
(list s16-storage-class (lambda indices (random (- (expt 2 15)) (expt 2 15))))
(list s32-storage-class (lambda indices (random (- (expt 2 31)) (expt 2 31))))
(list s64-storage-class (lambda indices (random (- (expt 2 63)) (expt 2 63))))
(list f32-storage-class (lambda indices (random-real)))
(list f64-storage-class (lambda indices (random-real)))
(list c64-storage-class (lambda indices (make-rectangular (random-real) (random-real))))
(list c128-storage-class (lambda indices (make-rectangular (random-real) (random-real))))
(list generic-storage-class (lambda indices indices)))))
(do ((i 0 (+ i 1)))
((= i tests))
(let* ((lower-bounds
(map (lambda (x) (random 4))
(vector->list (make-vector (random 1 10)))))
(upper-bounds
(map (lambda (x) (+ x (random 1 4)))
lower-bounds))
(array-length
(lambda (a)
(let ((upper-bounds (interval-upper-bounds->list (array-domain a)))
(lower-bounds (interval-lower-bounds->list (array-domain a))))
(apply * (map - upper-bounds lower-bounds)))))
(domain
(make-interval (list->vector lower-bounds)