-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-arrays.scm
6888 lines (5797 loc) · 279 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
#|
SRFI 231: Intervals and Generalized Arrays
Copyright 2016, 2018, 2020, 2021, 2022 Bradley J Lucier.
All Rights Reserved.
Permission is hereby granted, free of charge,
to any person obtaining a copy of this software
and associated documentation files (the "Software"),
to deal in the Software without restriction,
including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice
(including the next paragraph) shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
|#
;;; A test program for SRFI 231:
;;; Intervals and Generalized Arrays
(begin
;; Uncomment this line to run test-arrays.scm in Gambit.
(include "generic-arrays.scm"))
'(begin
;; To run test-arrays.scm as an R7RS module in Gambit,
;; take the following steps:
;; 1. Put generic-arrays.scm and 231.sld in new directory ./srfi/231.
;; 2. Uncomment this "begin".
;; 2 bis. If you want to compile the library do "gsc . srfi/231".
;; 3. Run "gsi . test-arrays".
(import (srfi 231))
(##namespace
("srfi/231#"
;; Internal SRFI 231 procedures that are either tested or called here.
%%compose-indexers
make-%%array
%%every
%%interval->basic-indexer
%%interval-lower-bounds
%%interval-upper-bounds
%%move-array-elements
%%permutation-invert
%%vector-every
%%vector-permute
%%vector-permute->list
%%order-unknown
%%compute-array-packed?
%%array-domain
%%array-indexer
%%array-getter
%%array-packed?))
)
(declare (standard-bindings)(extended-bindings)(block)(not safe) (mostly-fixnum))
(declare (inlining-limit 0))
(define random-tests 100)
(set! random-tests random-tests)
(define total-tests 0)
(set! total-tests total-tests)
(define failed-tests 0)
(set! failed-tests failed-tests)
;;; The next macros are not hygienic, so don't call any variable
;;; "continuation" ...
(define-macro (test expr value)
`(let* (;(ignore (pretty-print ',expr))
(result (call-with-current-continuation
(lambda (continuation)
(with-exception-catcher
(lambda (args)
(cond ((error-exception? args)
(continuation (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))
((wrong-number-of-arguments-exception? args)
"Wrong number of arguments passed to procedure ")
(else
"piffle")))
(lambda ()
,expr))))))
(set! total-tests (+ total-tests 1))
(if (not (equal? result ,value))
(begin
(set! failed-tests (+ failed-tests 1))
(pp (list ',expr" => " result ", not " ,value))))))
(define-macro (test-multiple-values expr vals)
`(call-with-values
(lambda () ,expr)
(lambda args
(set! total-tests (+ total-tests 1))
(if (not (equal? args ,vals))
(begin
(set! failed-tests (+ failed-tests 1))
(pp (list ',expr " => " args ", not " ,vals #\newline)))))))
;;; requires make-list function
;;; Pseudo-random infrastructure
;;; The idea is to have more reproducibility in the random tests.
;;; Our goal is that if this file is run with random-tests=N and then
;;; run with random-tests=M>N, then the parameters of the first N of M tests in each block
;;; will be the same as the parameters of the tests in the run with random-tests=N.
;;; Call next-test-random-source-state! immediately *after* each loop that is
;;; executed random-tests number of times.
(define test-random-source
(make-random-source))
(define initial-test-random-source-state
(random-source-state-ref test-random-source))
(define next-test-random-source-state!
(let ((j 0))
(lambda ()
(set! j (fx+ j 1))
(random-source-state-set!
test-random-source
initial-test-random-source-state)
(random-source-pseudo-randomize!
test-random-source
0 j))))
(define test-random-integer
(random-source-make-integers
test-random-source))
(define test-random-real
(random-source-make-reals
test-random-source))
(define (random a #!optional b)
(if b
(+ a (test-random-integer (- b a)))
(test-random-integer a)))
(define (random-inclusive a #!optional b)
(if b
(+ a (test-random-integer (- b a -1)))
(test-random-integer (+ a 1))))
(define (random-char)
(let ((n (random-inclusive (##max-char-code))))
(if (or (fx< n #xd800)
(fx< #xdfff n))
(integer->char n)
(random-char))))
(define (random-sample n #!optional (l 4))
(list->vector (map (lambda (i)
(random 1 l))
(iota n))))
(define (random-permutation n)
(let ((result (make-vector n)))
;; fill it
(do ((i 0 (fx+ i 1)))
((fx= i n))
(vector-set! result i i))
;; permute it
(do ((i 0 (fx+ i 1)))
((fx= i n) result)
(let* ((index (random i n))
(temp (vector-ref result index)))
(vector-set! result index (vector-ref result i))
(vector-set! result i temp)))))
(define (vector-permute v permutation)
(let* ((n (vector-length v))
(result (make-vector n)))
(do ((i 0 (+ i 1)))
((= i n) result)
(vector-set! result i (vector-ref v (vector-ref permutation i))))))
(define (filter p l)
(cond ((null? l) l)
((p (car l))
(cons (car l) (filter p (cdr l))))
(else
(filter p (cdr l)))))
(define (in-order < l)
(or (null? l)
(null? (cdr l))
(and (< (car l) (cadr l))
(in-order < (cdr l)))))
(define (foldl op id l)
(if (null? l)
id
(foldl op (op id (car l)) (cdr l))))
(define (foldr op id l)
(if (null? l)
id
(op (car l) (foldr op id (cdr l)))))
(define (indices->string . l)
(apply string-append (number->string (car l))
(map (lambda (n) (string-append "_" (number->string n))) (cdr l))))
;; (include "generic-arrays.scm")
(pp "Interval error tests")
(test (make-interval 1 '#(3 4))
"make-interval: The first argument is not a vector of exact integers: ")
(test (make-interval '#(1 1) 3)
"make-interval: The second argument is not a vector of exact integers: ")
(test (make-interval '#(1 1) '#(3))
"make-interval: The first and second arguments are not the same length: ")
(test (interval-volume (make-interval '#() '#()))
1)
(test (make-interval '#(1.) '#(1))
"make-interval: The first argument is not a vector of exact integers: ")
(test (make-interval '#(1 #f) '#(1 2))
"make-interval: The first argument is not a vector of exact integers: ")
(test (make-interval '#(1) '#(1.))
"make-interval: The second argument is not a vector of exact integers: ")
(test (make-interval '#(1 1) '#(1 #f))
"make-interval: The second argument is not a vector of exact integers: ")
(test (interval-volume (make-interval '#(1) '#(1)))
0)
(test (interval-volume (make-interval '#(1 2 3) '#(4 2 6)))
0)
(test (make-interval 1)
"make-interval: The argument is not a vector of nonnegative exact integers: ")
(test (interval-volume (make-interval '#()))
1)
(test (make-interval '#(1.))
"make-interval: The argument is not a vector of nonnegative exact integers: ")
(test (make-interval '#(-1))
"make-interval: The argument is not a vector of nonnegative exact integers: ")
(test (make-interval '#(1) '#(0))
"make-interval: Each lower-bound must be no greater 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: The 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: The first argument is not an interval: ")
(test (interval-lower-bound (make-interval '#(1 2 3) '#(4 5 6)) #f)
"interval-lower-bound: The second argument is not an exact integer between 0 (inclusive) and (interval-dimension interval) (exclusive): ")
(test (interval-lower-bound (make-interval '#(1 2 3) '#(4 5 6)) 1.)
"interval-lower-bound: The second argument is not an exact integer between 0 (inclusive) and (interval-dimension interval) (exclusive): ")
(test (interval-lower-bound (make-interval '#(1 2 3) '#(4 5 6)) -1)
"interval-lower-bound: The second argument is not an exact integer between 0 (inclusive) and (interval-dimension interval) (exclusive): ")
(test (interval-lower-bound (make-interval '#(1 2 3) '#(4 5 6)) 3)
"interval-lower-bound: The second argument is not an exact integer between 0 (inclusive) and (interval-dimension interval) (exclusive): ")
(test (interval-lower-bound (make-interval '#(1 2 3) '#(4 5 6)) 4)
"interval-lower-bound: The second argument is not an exact integer between 0 (inclusive) and (interval-dimension interval) (exclusive): ")
(test (interval-lower-bound (make-interval '#()) 0)
"interval-lower-bound: The second argument is not an exact integer between 0 (inclusive) and (interval-dimension interval) (exclusive): ")
(pp "interval-upper-bound error tests")
(test (interval-upper-bound 1 0)
"interval-upper-bound: The first argument is not an interval: ")
(test (interval-upper-bound (make-interval '#(1 2 3) '#(4 5 6)) #f)
"interval-upper-bound: The second argument is not an exact integer between 0 (inclusive) and (interval-dimension interval) (exclusive): ")
(test (interval-upper-bound (make-interval '#(1 2 3) '#(4 5 6)) 1.)
"interval-upper-bound: The second argument is not an exact integer between 0 (inclusive) and (interval-dimension interval) (exclusive): ")
(test (interval-upper-bound (make-interval '#(1 2 3) '#(4 5 6)) -1)
"interval-upper-bound: The second argument is not an exact integer between 0 (inclusive) and (interval-dimension interval) (exclusive): ")
(test (interval-upper-bound (make-interval '#(1 2 3) '#(4 5 6)) 3)
"interval-upper-bound: The second argument is not an exact integer between 0 (inclusive) and (interval-dimension interval) (exclusive): ")
(test (interval-upper-bound (make-interval '#(1 2 3) '#(4 5 6)) 4)
"interval-upper-bound: The second argument is not an exact integer between 0 (inclusive) and (interval-dimension interval) (exclusive): ")
(test (interval-upper-bound (make-interval '#()) 0)
"interval-upper-bound: The second argument is not an exact integer 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: The argument is not an interval: ")
(test (interval-lower-bounds->list (make-interval '#()))
'())
(pp "interval-upper-bounds->list error tests")
(test (interval-upper-bounds->list #f)
"interval-upper-bounds->list: The argument is not an interval: ")
(test (interval-upper-bounds->list (make-interval '#()))
'())
(pp "interval-lower-bounds->vector error tests")
(test (interval-lower-bounds->vector 1)
"interval-lower-bounds->vector: The argument is not an interval: ")
(test (interval-lower-bounds->vector (make-interval '#()))
'#())
(pp "interval-upper-bounds->vector error tests")
(test (interval-upper-bounds->vector #f)
"interval-upper-bounds->vector: The argument is not an interval: ")
(test (interval-upper-bounds->vector (make-interval '#()))
'#())
(pp "interval-width, interval-widths error tests")
(test (interval-width 1 0)
"interval-width: The first argument is not an interval: ")
(test (interval-width (make-interval '#(1 2 3) '#(4 5 6)) #f)
"interval-width: The second argument is not an exact integer between 0 (inclusive) and the dimension of the first argument (exclusive): ")
(test (interval-width (make-interval '#(1 2 3) '#(4 5 6)) 1.)
"interval-width: The second argument is not an exact integer between 0 (inclusive) and the dimension of the first argument (exclusive): ")
(test (interval-width (make-interval '#(1 2 3) '#(4 5 6)) -1)
"interval-width: The second argument is not an exact integer between 0 (inclusive) and the dimension of the first argument (exclusive): ")
(test (interval-width (make-interval '#(1 2 3) '#(4 5 6)) 3)
"interval-width: The second argument is not an exact integer between 0 (inclusive) and the dimension of the first argument (exclusive): ")
(test (interval-width (make-interval '#(1 2 3) '#(4 5 6)) 4)
"interval-width: The second argument is not an exact integer between 0 (inclusive) and the dimension of the first argument (exclusive): ")
(test (interval-widths 1)
"interval-widths: The argument is not an interval: ")
(test (interval-widths (make-interval '#()))
'#())
(test (interval-widths (make-interval '#(1 0)))
'#(1 0))
(pp "interval-lower-bound, interval-upper-bound, interval-lower-bounds->list, interval-upper-bounds->list,")
(pp "interval-lower-bounds->vector, interval-upper-bounds->vector, interval-width, interval-widths result tests")
(do ((i 0 (+ i 1)))
((= i random-tests))
(let* ((lower (map (lambda (x) (random 10)) (vector->list (make-vector (random 1 11)))))
(upper (map (lambda (x) (+ (random 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)
(test (interval-lower-bounds->vector interval)
(list->vector lower))
(test (interval-upper-bounds->vector interval)
(list->vector upper))
(test (interval-width interval offset)
(- (list-ref upper offset)
(list-ref lower offset)))
)))
(do ((i 0 (+ i 1)))
((= i random-tests))
(let* ((lower (map (lambda (x) (random 10)) (vector->list (make-vector (random 1 11)))))
(upper (map (lambda (x) (+ (random 11) x)) lower)))
(let ((interval (make-interval (list->vector lower)
(list->vector upper))))
(test (interval-widths interval)
(vector-map -
(interval-upper-bounds->vector interval)
(interval-lower-bounds->vector interval))))))
(next-test-random-source-state!)
(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 second argument is not an exact integer between 0 and the dimension of the first argument (inclusive): ")
(test (interval-projections (make-interval '#(0 0) '#(1 1)) 1/2)
"interval-projections: The second argument is not an exact integer between 0 and the dimension of the first argument (inclusive): ")
(test (interval-projections (make-interval '#(0 0) '#(1 1)) 1.)
"interval-projections: The second argument is not an exact integer between 0 and the dimension of the first argument (inclusive): ")
(test-multiple-values
(interval-projections (make-interval '#(0 0) '#(1 1)) 0)
(list (make-interval '#(1 1))
(make-interval '#()) ))
(test-multiple-values
(interval-projections (make-interval '#(0 0) '#(1 1)) 2)
(list (make-interval '#())
(make-interval '#(1 1))))
(pp "interval-projections result tests")
(do ((i 0 (+ i 1)))
((= i random-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)))))))
(next-test-random-source-state!)
(pp "interval-volume error tests")
(test (interval-volume #f)
"interval-volume: The argument is not an interval: ")
(pp "interval-volume result tests")
(do ((i 0 (+ i 1)))
((= i random-tests))
(let* ((lower (map (lambda (x) (random 6)) (vector->list (make-vector (random 6)))))
(upper (map (lambda (x) (+ (random 6) x)) lower)))
(test (interval-volume (make-interval (list->vector lower)
(list->vector upper)))
(apply * (map - upper lower)))))
(next-test-random-source-state!)
(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 random-tests))
(let* ((lower1 (map (lambda (x) (random 2)) (vector->list (make-vector (random 4)))))
(upper1 (map (lambda (x) (+ (random 3) x)) lower1))
(lower2 (map (lambda (x) (random 2)) lower1))
(upper2 (map (lambda (x) (+ (random 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)))))
(next-test-random-source-state!)
(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 random-tests))
(let* ((lower1 (map (lambda (x) (random 2)) (vector->list (make-vector (random 6)))))
(upper1 (map (lambda (x) (+ (random 3) x)) lower1))
(lower2 (map (lambda (x) (random 2)) lower1))
(upper2 (map (lambda (x) (+ (random 3) x)) lower2)))
(test (interval-subset? (make-interval (list->vector lower1)
(list->vector upper1))
(make-interval (list->vector lower2)
(list->vector upper2)))
(and (%%every (lambda (x) (>= (car x) (cdr x))) (map cons lower1 lower2))
(%%every (lambda (x) (<= (car x) (cdr x))) (map cons upper1 upper2))))))
(pp "interval-empty? tests")
(test (interval-empty? 'a)
"interval-empty?: The argument is not an interval: ")
(test (interval-empty? (make-interval '#(1) '#(1)))
#t)
(test (interval-empty? (make-interval '#(1) '#(2)))
#f)
(test (interval-empty? (make-interval '#()))
#f)
(next-test-random-source-state!)
(pp "interval-contains-multi-index? error tests")
(test (interval-contains-multi-index? 1 1)
"interval-contains-multi-index?: The first argument is not an interval: ")
(test (interval-contains-multi-index? (make-interval '#(1 2 3) '#(4 5 6)) 1)
"interval-contains-multi-index?: The dimension of the first argument (an interval) does not match number of indices: ")
(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-left (lambda (result x)
(and result (apply interval-contains-multi-index? interval x)))
#t
(make-array interval list)))
(error "these should all be true"))
(if (not (array-fold-left (lambda (result x)
(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: The second argument is not a interval: ")
(test (interval-for-each 1 (make-interval '#(3) '#(4)))
"interval-for-each: The first 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 random-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))))))
(next-test-random-source-state!)
(pp "interval-fold-left and interval-fold-right error tests")
(test (interval-fold-left 1 2 3 4)
"interval-fold-left: The fourth argument is not an interval: ")
(test (interval-fold-left 1 2 3 (make-interval '#(2 2)))
"interval-fold-left: The second argument is not a procedure: ")
(test (interval-fold-left 1 values 3 (make-interval '#(2 2)))
"interval-fold-left: The first argument is not a procedure: ")
(test (interval-fold-right 1 2 3 4)
"interval-fold-right: The fourth argument is not an interval: ")
(test (interval-fold-right 1 2 3 (make-interval '#(2 2)))
"interval-fold-right: The second argument is not a procedure: ")
(test (interval-fold-right 1 values 3 (make-interval '#(2 2)))
"interval-fold-right: The first argument is not a procedure: ")
;;; We'll mainly rely on tests for array-fold[lr] to test interval-fold[lr]
(test (interval-fold-left identity + 0 (make-interval '#(5)))
10)
(test (interval-fold-right identity + 0 (make-interval '#(5)))
10)
(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: Some resulting lower bounds are greater than corresponding upper bounds: "))
;;; 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 use-bignum-intervals #f)
(define (random-interval #!optional (min 0) (max 6))
;; a random interval with min <= dimension < max
;; positive and negative lower bounds
(let* ((lower
(map (lambda (x)
(if use-bignum-intervals
(random (- (expt 2 90)) (expt 2 90))
(random -10 10)))
(iota (random min max))))
(upper
(map (lambda (x)
(+ (random 0 8) x))
lower)))
(make-interval (list->vector lower)
(list->vector upper))))
(define (random-nonempty-interval #!optional (min 0) (max 6))
;; a random interval with min <= dimension < max
;; positive and negative lower bounds
(let* ((lower
(map (lambda (x)
(if use-bignum-intervals
(random (- (expt 2 90)) (expt 2 90))
(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-inclusive lowers uppers))
(new-uppers (vector-map random-inclusive new-lowers uppers))
(subinterval (make-interval new-lowers new-uppers)))
subinterval))
(define (random-nonnegative-interval #!optional (min 1) (max 6))
;; a random interval with min <= dimension < max
;; positive and negative lower bounds
(let* ((lower
(make-vector (random min max) 0))
(upper
(vector-map (lambda (x) (random 1 7)) lower)))
(make-interval lower 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)))
(define (array-display A)
(define (display-item x)
(display x) (display "\t"))
(newline)
(case (array-dimension A)
((1) (array-for-each display-item A) (newline))
((2) (array-for-each (lambda (row)
(array-for-each display-item row)
(newline))
(array-curry A 1)))
(else
(error "array-display can't handle > 2 dimensions: " A))))
(pp "storage-class tests")
(define storage-class-names
(list (list u1-storage-class 'u1-storage-class 'u16vector make-u16vector)
(list u8-storage-class 'u8-storage-class 'u8vector make-u8vector)
(list u16-storage-class 'u16-storage-class 'u16vector make-u16vector)
(list u32-storage-class 'u32-storage-class 'u32vector make-u32vector)
(list u64-storage-class 'u64-storage-class 'u64vector make-u64vector)
(list s8-storage-class 's8-storage-class 's8vector make-s8vector)
(list s16-storage-class 's16-storage-class 's16vector make-s16vector)
(list s32-storage-class 's32-storage-class 's32vector make-s32vector)
(list s64-storage-class 's64-storage-class 's64vector make-s64vector)
(list f16-storage-class 'f16-storage-class 'u16vector make-u16vector)
(list f32-storage-class 'f32-storage-class 'f32vector make-f32vector)
(list f64-storage-class 'f64-storage-class 'f64vector make-f64vector)
(list char-storage-class 'char-storage-class 'string make-string)
(list c64-storage-class 'c64-storage-class 'f32vector make-f32vector)
(list c128-storage-class 'c128-storage-class 'f64vector make-f64vector)
(list generic-storage-class 'generic-storage-class 'vector make-vector)
))
(define uniform-storage-classes
(list u8-storage-class u16-storage-class u32-storage-class u64-storage-class
s8-storage-class s16-storage-class s32-storage-class s64-storage-class
f16-storage-class f32-storage-class f64-storage-class
char-storage-class
c64-storage-class c128-storage-class))
(for-each (lambda (storage-class)
(test ((storage-class-data? storage-class)
((storage-class-maker storage-class)
8 (storage-class-default storage-class)))
#t))
uniform-storage-classes)
(test ((storage-class-data? u1-storage-class) (u16vector 0))
#t)
(for-each (lambda (class-name-data-maker)
(let* ((class
(car class-name-data-maker))
(name
(cadr class-name-data-maker))
(data
(caddr class-name-data-maker))
(maker
(cadddr class-name-data-maker))
(message
(string-append "Expecting a "
(symbol->string data)
(if (memq class (list c64-storage-class c128-storage-class))
" with an even number of elements passed to "
" passed to ")
"(storage-class-data->body "
(symbol->string name)
"): ")))
(test ((storage-class-data->body class) 'a)
message)))
storage-class-names)
(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: ")
(test (make-array 1 values values)
"make-array: The first argument is not an interval: ")
(test (make-array (make-interval '#(3) '#(4)) 1 values)
"make-array: The second argument is not a procedure: ")
(test (make-array (make-interval '#(3) '#(4)) list 1)
"make-array: The third argument is not a procedure: ")
(pp "array result tests")
(define (myarray= array1 array2 #!optional (compare equal?))
(and (interval= (array-domain array1)
(array-domain array2))
(array-every compare array1 array2)))
(let ((getter (lambda args 1.)))
(test (myarray= (make-array (make-interval '#(3) '#(4)) getter)
(make-%%array (make-interval '#(3) '#(4))
getter
#f
#f
#f
#f
#f
%%order-unknown))
#t))
(pp "array-domain and array-getter error tests")
(test (array-domain #f)
"array-domain: The argument is not an array: ")
(test (array-getter #f)
"array-getter: The argument is not an array: ")
(pp "array?, array-domain, and array-getter result tests")
(let* ((getter (lambda args 1.))
(domain (make-interval '#(3) '#(4)))
(array (make-array domain getter)))
(test (array? #f)
#f)
(test (array? array)
#t)
(test (array-domain array)
domain)
(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 domain
getter
setter
#f
#f
#f
#f
%%order-unknown))))
(pp "array-setter error tests")
(test (array-setter #f)
"array-setter: The argument 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))))
(pp "array-freeze! tests")
(test (array-freeze! 'a)
"array-freeze!: The argument is not an array: ")
(let ((A (make-specialized-array (make-interval '#()))))
(test (mutable-array? A)
#t)
(let ((B (array-freeze! A)))
(test (mutable-array? B)
#f)
(test (eq? A B)
#t))
(test (mutable-array? A)
#f))
(define (myindexer= indexer1 indexer2 interval)
(array-fold-left (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 random-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