forked from shirok/Gauche
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.scm
1657 lines (1354 loc) · 56.8 KB
/
object.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
;;
;; Test object system
;;
(use gauche.test)
(test-start "object system")
;;----------------------------------------------------------------
(test-section "class definition")
(define-class <x> () (a b c))
(test* "define-class <x>" '<x> (class-name <x>))
(test* "define-class <x>" 3 (slot-ref <x> 'num-instance-slots))
(test* "define-class <x>" <class> (class-of <x>))
(test* "define-class <x>" '(<x> <object> <top>)
(map class-name (class-precedence-list <x>)))
(define-class <y> (<x>) (c d e))
(test* "define-class <y>" 5 (slot-ref <y> 'num-instance-slots))
(test* "define-class <y>" <class> (class-of <y>))
(test* "define-class <y>" '(<y> <x> <object> <top>)
(map class-name (class-precedence-list <y>)))
(define-class <z> (<object>) ())
(test* "define-class <z>" 0 (slot-ref <z> 'num-instance-slots))
(test* "define-class <z>" <class> (class-of <z>))
(test* "define-class <z>" '(<z> <object> <top>)
(map class-name (class-precedence-list <z>)))
(define-class <w> (<z> <y>) (e f))
(test* "define-class <w>" 6 (slot-ref <w> 'num-instance-slots))
(test* "define-class <w>" <class> (class-of <w>))
(test* "define-class <w>" '(<w> <z> <y> <x> <object> <top>)
(map class-name (class-precedence-list <w>)))
(define-class <w2> (<y> <z>) (e f))
(test* "define-class <w2>" '(<w2> <y> <x> <z> <object> <top>)
(map class-name (class-precedence-list <w2>)))
;;----------------------------------------------------------------
(test-section "instancing")
(define x1 (make <x>))
(define x2 (make <x>))
(test* "make <x>" <x> (class-of x1))
(test* "make <x>" <x> (class-of x2))
(slot-set! x1 'a 4)
(slot-set! x1 'b 5)
(slot-set! x1 'c 6)
(slot-set! x2 'a 7)
(slot-set! x2 'b 8)
(slot-set! x2 'c 9)
(test* "slot-ref" '(4 5 6) (map (lambda (slot) (slot-ref x1 slot)) '(a b c)))
(test* "slot-ref" '(7 8 9) (map (lambda (slot) (slot-ref x2 slot)) '(a b c)))
(test* "slot-ref-using-class" '(4 5 6)
(map (lambda (slot) (slot-ref-using-class <x> x1 slot)) '(a b c)))
(test* "slot-ref-using-class" (test-error)
(slot-ref-using-class <y> x1 'a))
(test* "slot-ref-using-accessor" '(7 8 9)
(map (lambda (slot)
(let ((sa (class-slot-accessor <x> slot)))
(and sa (slot-ref-using-accessor x2 sa))))
'(a b c)))
(test* "slot-ref-using-accessor" (test-error)
(let ((sa (class-slot-accessor <y> slot)))
(and sa (slot-ref-using-accessor x2 sa))))
(test* "slot-set-using-class!" '(-4 -5 -6)
(map (lambda (slot)
(slot-set-using-class! <x> x1 slot
(- (slot-ref x1 slot)))
(slot-ref x1 slot))
'(a b c)))
(test* "slot-set-using-class!" (test-error)
(slot-set-using-class! <y> x1 'a 3))
(test* "slot-set-using-accessor!" '(-7 -8 -9)
(map (lambda (slot)
(let ((sa (class-slot-accessor <x> slot)))
(and sa
(slot-set-using-accessor! x2 sa (- (slot-ref x2 slot)))))
(slot-ref x2 slot))
'(a b c)))
(test* "slot-ref-using-accessor!" (test-error)
(let ((sa (class-slot-accessor <y> slot)))
(and sa (slot-set-using-accessor! x2 sa -1))))
(let ((x3 (make <x>)))
(test* "slot-pop! (unbound error)" (test-error)
(slot-pop! x3 'a))
(test* "slot-pop! (unbound but fallback)" 'ok
(slot-pop! x3 'a 'ok))
(test* "slot-push!" '(1)
(begin (slot-set! x3 'a '())
(slot-push! x3 'a 1)
(slot-ref x3 'a)))
(test* "slot-push!" '(2 1)
(begin (slot-push! x3 'a 2)
(slot-ref x3 'a)))
(test* "slot-pop!" 2 (slot-pop! x3 'a 'empty))
(test* "slot-pop!" 1 (slot-pop! x3 'a 'empty))
(test* "slot-pop!" 'empty (slot-pop! x3 'a 'empty))
(test* "slot-push!" '(1 . z)
(begin (slot-set! x3 'a 'z)
(slot-push! x3 'a 1)
(slot-ref x3 'a)))
(test* "slot-pop!" 1 (slot-pop! x3 'a))
(test* "slot-pop!" (test-error) (slot-pop! x3 'a))
(test* "slot-pop!" 'empty (slot-pop! x3 'a 'empty))
)
;;----------------------------------------------------------------
(test-section "slot initialization")
(define-class <r> ()
((a :init-keyword :a :initform 4)
(b :init-keyword :b :init-value 5)))
(define r1 (make <r>))
(define r2 (make <r> :a 9))
(define r3 (make <r> :b 100 :a 20))
(define-method slot-values ((obj <r>))
(map (^s (slot-ref obj s)) '(a b)))
(test* "make <r>" '(4 5) (slot-values r1))
(test* "make <r> :a" '(9 5) (slot-values r2))
(test* "make <r> :a :b" '(20 100) (slot-values r3))
;;----------------------------------------------------------------
(test-section "slot allocations")
(define-class <s> ()
((i :allocation :instance :init-keyword :i :init-value #\i)
(c :allocation :class :init-keyword :c :init-value #\c)
(s :allocation :each-subclass :init-keyword :s :init-value #\s)
(v :allocation :virtual :init-keyword :v
:slot-ref (^o (cons (slot-ref o 'i) (slot-ref o 'c)))
:slot-set! (lambda (o v)
(slot-set! o 'i (car v))
(slot-set! o 'c (cdr v))))
))
(define-method slot-values ((obj <s>))
(map (^s (slot-ref obj s)) '(i c s v)))
(define s1 (make <s>))
(define s2 (make <s>))
(test* "make <s>" '(#\i #\c #\s (#\i . #\c)) (slot-values s1))
(test* "slot-set! :instance"
'((#\I #\c #\s (#\I . #\c)) (#\i #\c #\s (#\i . #\c)))
(begin
(slot-set! s1 'i #\I)
(list (slot-values s1) (slot-values s2))))
(test* "slot-set! :class"
'((#\I #\C #\s (#\I . #\C)) (#\i #\C #\s (#\i . #\C)))
(begin
(slot-set! s1 'c #\C)
(list (slot-values s1) (slot-values s2))))
(test* "slot-set! :each-subclass"
'((#\I #\C #\S (#\I . #\C)) (#\i #\C #\S (#\i . #\C)))
(begin
(slot-set! s1 's #\S)
(list (slot-values s1) (slot-values s2))))
(test* "slot-set! :virtual"
'((i c #\S (i . c)) (#\i c #\S (#\i . c)))
(begin
(slot-set! s1 'v '(i . c))
(list (slot-values s1) (slot-values s2))))
(define-class <ss> (<s>)
())
(define s3 (make <ss> :i "i" :c "c" :s "s"))
(test* "make <ss>"
'(("i" "c" "s" ("i" . "c")) (i "c" #\S (i . "c")))
(list (slot-values s3) (slot-values s1)))
(test* "slot-set! :class"
'(("i" "C" "s" ("i" . "C")) (i "C" #\S (i . "C")))
(begin
(slot-set! s3 'c "C")
(list (slot-values s3) (slot-values s1))))
(test* "slot-set! :each-subclass"
'(("i" "C" "s" ("i" . "C")) (i "C" "S" (i . "C")))
(begin
(slot-set! s1 's "S")
(list (slot-values s3) (slot-values s1))))
(test* "slot-set! :each-subclass"
'(("i" "C" 5 ("i" . "C")) (i "C" "S" (i . "C")))
(begin
(slot-set! s3 's 5)
(list (slot-values s3) (slot-values s1))))
(define s4 (make <ss> :v '(1 . 0)))
(test* "make <ss> :v"
'((1 0 5 (1 . 0)) ("i" 0 5 ("i" . 0)))
(list (slot-values s4) (slot-values s3)))
(test* "class-slot-ref"
'(0 "S" 0 5)
(list (class-slot-ref <s> 'c) (class-slot-ref <s> 's)
(class-slot-ref <ss> 'c) (class-slot-ref <ss> 's)))
(test* "class-slot-set!"
'(100 99 100 5)
(begin
(class-slot-set! <s> 'c 100)
(class-slot-set! <s> 's 99)
(list (class-slot-ref <s> 'c) (class-slot-ref <s> 's)
(class-slot-ref <ss> 'c) (class-slot-ref <ss> 's))))
(test* "class-slot-set!"
'(101 99 101 55)
(begin
(class-slot-set! <ss> 'c 101)
(class-slot-set! <ss> 's 55)
(list (class-slot-ref <s> 'c) (class-slot-ref <s> 's)
(class-slot-ref <ss> 'c) (class-slot-ref <ss> 's))))
(define-class <sss> ()
((v :allocation :virtual
:slot-ref (^o (slot-ref o 'vv))
:slot-set! (lambda (o v) (slot-set! o 'vv v))
:slot-bound? (^o (slot-bound? o 'vv)))
vv))
(define s5 (make <sss>))
(test* "slot-bound? protocol" #f
(slot-bound? s5 'v))
(test* "slot-bound? protocol" '(#t 8)
(begin (slot-set! s5 'v 8)
(list (slot-bound? s5 'v)
(slot-ref s5 'v))))
;; Direct instance-slot access is only for under-the-hood work, and
;; shouldn't be casually used. But that doesn't diminish the importance
;; of tests!
(define-class <ssss> () ((a :init-keyword :a) b))
(let ((z (make <ssss> :a 1)))
(test* "instance-slot-ref a" 1 (instance-slot-ref z 0))
(test* "instance-slot-ref b" (test-error <error> #/unbound/)
(instance-slot-ref z 1))
(test* "instance-slot-ref b fallback" 2
(instance-slot-ref z 1 2))
(test* "instance-slot-set!" 3
(begin (instance-slot-set! z 1 3)
(instance-slot-ref z 1)))
(test* "instance-slot-ref oob" (test-error <error> #/out of bound/)
(instance-slot-ref z 2))
(test* "instance-slot-ref oob" (test-error <error> #/out of bound/)
(instance-slot-ref z -1))
(test* "instance-slot-set! oob" (test-error <error> #/out of bound/)
(instance-slot-set! z 2 0))
)
;;----------------------------------------------------------------
(test-section "next method")
(define (nm obj) 'fallback)
(define-method nm ((obj <x>)) (list 'x-in (next-method) 'x-out))
(define-method nm ((obj <y>)) (list 'y-in (next-method) 'y-out))
(define-method nm ((obj <z>)) (list 'z-in (next-method) 'z-out))
(define-method nm ((obj <w>)) (list 'w-in (next-method) 'w-out))
(define-method nm ((obj <w2>)) (list 'w2-in (next-method) 'w2-out))
(test* "next method"
'(y-in (x-in fallback x-out) y-out)
(nm (make <y>)))
(test* "next-method"
'(w-in (z-in (y-in (x-in fallback x-out) y-out) z-out) w-out)
(nm (make <w>)))
(test* "next-method"
'(w2-in (y-in (x-in (z-in fallback z-out) x-out) y-out) w2-out)
(nm (make <w2>)))
(define-method nm (obj . a)
(if (null? a) (list 't*-in (next-method) 't*-out) 't*))
(define-method nm ((obj <y>) a) (list 'y1-in (next-method) 'y1-out))
(define-method nm ((obj <y>) . a) (list 'y*-in (next-method) 'y*-out))
(test* "next-method"
'(y1-in (y*-in t* y*-out) y1-out)
(nm (make <y>) 3))
(test* "next-method"
'(y-in (y*-in (x-in (t*-in fallback t*-out) x-out) y*-out) y-out)
(nm (make <y>)))
;; check if method-leaf is properly set
(define-method nm-1 (obj) #f)
(define-method nm-2 (obj) (next-method))
(test* "method-leaf" '(#t #f)
(list (method-leaf? (car (slot-ref nm-1 'methods)))
(method-leaf? (car (slot-ref nm-2 'methods)))))
;;----------------------------------------------------------------
(test-section "method sorting")
;; Corner cases of method sorting.
;; 0.8.6 and before had a bug in method-more-specific? when both methods
;; had optional arg.
(define-method ms-1 ((x <string>) . rest) 1)
(define-method ms-1 rest 0)
(define-method ms-1 ((x <string>) (y <string>) . rest) 2)
(test* "method sorting" 2 (ms-1 "a" "a"))
(test* "method sorting" 1 (ms-1 "a"))
;;----------------------------------------------------------------
(test-section "setter method definition")
(define-method s-get-i ((self <s>)) (slot-ref self 'i))
(define-method (setter s-get-i) ((self <s>) v) (slot-set! self 'i v))
(define-method (setter s-get-i) ((self <ss>) v) (slot-set! self 'i (cons v v)))
(test* "setter of s-get-i(<s>)" '("i" "j")
(let* ((s (make <s> :i "i"))
(i (s-get-i s))
(j (begin (set! (s-get-i s) "j") (s-get-i s))))
(list i j)))
(test* "setter of s-get-i(<ss>)" '("i" ("j" . "j"))
(let* ((s (make <ss> :i "i"))
(i (s-get-i s))
(j (begin (set! (s-get-i s) "j") (s-get-i s))))
(list i j)))
;;----------------------------------------------------------------
(test-section "method optional arguments")
(define-method optarg ((a <x>)) 'x)
(define-method optarg ((a <y>) :optional (x 0) (y 1) :key (z 2) :rest r)
(list x y z r))
(test* "method optarg (no optarg)" 'x (optarg (make <x>)))
(test* "method optarg (optarg)" '(0 1 2 ()) (optarg (make <y>)))
(test* "method optarg (optarg)" '(a b 2 ()) (optarg (make <y>) 'a 'b))
(test* "method optarg (optarg)" '(a b 4 (:z 4)) (optarg (make <y>) 'a 'b :z 4))
;;----------------------------------------------------------------
(test-section "accelerated dispatch")
(define-generic acc-dis-1)
(define-generic acc-dis-2)
((with-module gauche.object generic-build-dispatcher!) acc-dis-2 0)
(define-method acc-dis-1 ((a <top>)) 'top)
(define-method acc-dis-2 ((a <top>)) 'top)
(define *acc-dis-count* 25)
(define-macro (gen-acc-dis-classes&methods)
(define (gen-1 n)
(let1 cl (symbol-append '<acc-dis- n '>)
`(begin
(define-class ,cl () ())
(define-method acc-dis-1 ((a ,cl)) `(,',n 0))
(define-method acc-dis-1 ((a ,cl) x) `(,',n 1))
(define-method acc-dis-1 ((a ,cl) x y) `(,',n 2))
(define-method acc-dis-1 ((a ,cl) x y . z) `(,',n 3))
(define-method acc-dis-2 ((a ,cl)) `(,',n 0))
(define-method acc-dis-2 ((a ,cl) x) `(,',n 1))
(define-method acc-dis-2 ((a ,cl) x y) `(,',n 2))
(define-method acc-dis-2 ((a ,cl) x y . z) `(,',n 3))
)))
`(begin ,@(append-map gen-1 (iota *acc-dis-count*))))
(gen-acc-dis-classes&methods)
(define-macro (acc-dis-classes)
`(list ,@(map (^n (symbol-append '<acc-dis- n '>)) (iota *acc-dis-count*))))
(define (test-acc-dis name gf :optional (expect '()))
(test* name expect
(let1 r '()
(do ([cs (acc-dis-classes) (cdr cs)]
[n 0 (+ n 1)])
[(null? cs) r]
(do ([obj (make (car cs))]
[k 0 (+ k 1)]
[args '() (cons #f args)])
[(= k 4)]
(let* ([expected `(,n ,k)]
[actual (apply gf obj args)])
(unless (equal? expected actual)
(push! r `(:expected ,expected :actual ,actual)))))))))
((with-module gauche.object generic-build-dispatcher!) acc-dis-1 0)
(test-acc-dis "batch build" acc-dis-1)
(test-acc-dis "incremental build" acc-dis-2)
(define-method acc-dis-1 ((c <acc-dis-0>)) (cons '(0 0) (next-method)))
(define-method acc-dis-1 ((c <acc-dis-1>)) 'redefined)
(test-acc-dis "redefinition 1" acc-dis-1
'((:expected (1 0) :actual redefined)
(:expected (0 0) :actual ((0 0) . top))))
(define-method acc-dis-1 ((c <acc-dis-0>)) '(0 0))
(define-method acc-dis-1 ((c <acc-dis-1>) (x <integer>)) (cons '(1 1) (next-method)))
(test-acc-dis "redefinition 2" acc-dis-1
'((:expected (1 0) :actual redefined)))
(test* "redefiniton 2'" '((1 1) 1 1)
(acc-dis-1 (make <acc-dis-1>) 2))
(define-method acc-dis-1 ((c <acc-dis-1>) (x <integer>)) 'leaf)
(test* "redefiniton 3" '((1 1) . leaf)
(cons (acc-dis-1 (make <acc-dis-1>) #f)
(acc-dis-1 (make <acc-dis-1>) 2)))
;;----------------------------------------------------------------
(test-section "module and accessor")
;; This test is a contrived example of the case where the
;; superclass has a slot with a getter method whose name isn't
;; exported. Gauche 0.8.5 and before doesn't handle
;; this correctly, since the implicit accessor method of slot a
;; of <ma-class-2> defined in MA doesn't share the generic function
;; with MA.inner#ma-get. Thus, in MA.inner#ma-method, (ma-get <ma-class-1>)
;; is called even when the passed object is <ma-class-2>.
;;
;; The root of the problem is the undesired interaction between
;; module system and generic functions. In 0.8.6, we haven't still
;; solved the root problem, but we fixed this particular problem by
;; adding extra check in accessor methods.
(define-module MA.inner
(export <ma-class-1> ma-getter ma-setter)
(define-class <ma-class-1> ()
((a :accessor ma-get :init-value 'a)))
(define-method ma-getter ((o <ma-class-1>))
(ma-get o))
(define-method ma-setter ((o <ma-class-1>) val)
(set! (ma-get o) val)))
(define-module MA
(import MA.inner)
(export <ma-class-2> ma-g ma-s)
(define-class <ma-class-2> (<ma-class-1>)
((b :init-value 'b)))
(define (ma-g o) (ma-getter o))
(define (ma-s o v) (ma-setter o v)))
(define-module MA.user
(import MA))
(test* "module and accessor" 'a
(with-module MA.user
(ma-g (make <ma-class-2>))))
(test* "module and accessor" 'ei
(with-module MA.user
(let1 m (make <ma-class-2>)
(ma-s m 'ei)
(slot-ref m 'a))))
;; Another tricky interference between modules and implicit setter.
;; The :accessor slot option may create the setter generic function
;; with the weird name |setter of <getter-name>|. The name isn't supposed
;; to be visible from the user ever. However, until 0.9.6, when the
;; MB.base below does not export the setter name, the derived class
;; couldn't find the setter gf and created a separate setter gf, making it
;; incompatible with base class and derived class.
(define-module MB.base
(export <mb-base> mb-a)
(define-class <mb-base> ()
((a :accessor mb-a :init-value 0))))
(define-module MB.derived
(import MB.base)
(export <mb-base> <mb-derived> mb-a)
(define-class <mb-defived> (<mb-base>) ()))
(test* "module and implicit setter" 3
;; This caused 'no applicable method for #<generic |setter of mb-a|>
;; in 0.9.6 and before.
(with-module MB.derived
(let1 m (make <mb-base>)
(set! (mb-a m) 3)
(~ m 'a))))
;;----------------------------------------------------------------
(test-section "class redefinition (part 1)")
;; save original <x> and <y> defined above
(define <x>-orig <x>)
(define <y>-orig <y>)
(define <w>-orig <w>)
(define <w2>-orig <w2>)
;; create some more instances
(define y1 (let ((o (make <y>)))
(for-each (lambda (s v) (slot-set! o s v))
'(a b c d e)
'(0 1 2 3 4))
o))
(define y2 (let ((o (make <y>)))
(for-each (lambda (s v) (slot-set! o s v))
'(a b c d e)
'(5 6 7 8 9))
o))
(define w1 (let ((o (make <w>)))
(for-each (lambda (s v) (slot-set! o s v))
'(a b c d e f)
'(100 101 102 103 104 105))
o))
(define w2 (make <w>))
;; set several methods
(define-method redef-test1 ((x <x>)) 'x)
(define-method redef-test1 ((y <y>)) 'y)
(define-method redef-test1 ((w <w>)) 'w)
(define-method redef-test1 ((w2 <w2>)) 'w2)
(define-method redef-test2 ((x <x>) (y <y>)) 'xyz)
(define-method redef-test2 ((z <z>) (w <w>)) 'yw)
(test* "simple redefinition of <x>" #f
(begin
(eval '(define-class <x> () (a b c x)) (current-module))
(eval '(eq? <x> <x>-orig) (current-module))))
(test* "simple redefinition of <x>" '(#t #f #t #f)
(list (eq? (ref <x>-orig 'redefined) <x>)
(ref <x> 'redefined)
(eq? (ref <y>-orig 'redefined) <y>)
(ref <y> 'redefined)))
(test* "subclass redefinition <y> (links)"
'(#f #f #f)
(list (eq? <y> <y>-orig)
(not (memq <x> (ref <y> 'direct-supers)))
(not (memq <x>-orig (ref <y>-orig 'direct-supers)))))
(test* "subclass redefinition <y> (slots)"
'((a b c) (a b c x) (c d e a b) (c d e a b x))
(map (^c (map (lambda (s) (car s)) (class-slots c)))
(list <x>-orig <x> <y>-orig <y>)))
(test* "subclass redefinition <w> (links)"
'(#f #f #f)
(list (eq? <w> <w>-orig)
(not (memq <y> (ref <w> 'direct-supers)))
(not (memq <y>-orig (ref <w>-orig 'direct-supers)))))
(test* "subclass redefinition <w> (slots)"
'((e f c d a b) (e f c d a b x) (e f c d a b) (e f c d a b x))
(map (^c (map (lambda (s) (car s)) (class-slots c)))
(list <w>-orig <w> <w2>-orig <w2>)))
(test* "subclass redefinition (hierarchy)"
(list (list <x> <object> <top>)
(list <y> <x> <object> <top>)
(list <w> <z> <y> <x> <object> <top>)
(list <w2> <y> <x> <z> <object> <top>))
(map class-precedence-list (list <x> <y> <w> <w2>)))
(test* "subclass redefinition (hierarchy, orig)"
(list (list <x>-orig <object> <top>)
(list <y>-orig <x>-orig <object> <top>)
(list <w>-orig <z> <y>-orig <x>-orig <object> <top>)
(list <w2>-orig <y>-orig <x>-orig <z> <object> <top>))
(map class-precedence-list
(list <x>-orig <y>-orig <w>-orig <w2>-orig)))
;; check link consistency between class-direct-methods and method-specializer.x
(define (method-link-check gf class)
(and (not (null? (class-direct-methods class)))
(let loop ((dmeths (class-direct-methods class)))
(cond ((null? dmeths) #t)
((memq (car dmeths) (slot-ref gf 'methods))
=> (lambda (meth)
(and (memq class (slot-ref (car meth) 'specializers))
(loop (cdr dmeths)))))
(else (loop (cdr dmeths)))))))
(test* "method link fix"
'(#t #t #t #t #t #t #t)
(list (method-link-check redef-test1 <x>)
(method-link-check redef-test1 <y>)
(method-link-check redef-test1 <w>)
(method-link-check redef-test1 <w2>)
(method-link-check redef-test2 <x>)
(method-link-check redef-test2 <y>)
(method-link-check redef-test2 <w>)))
(test* "instance update (x1)" '(#t -4 -5 -6 #f)
(list (is-a? x1 <x>)
(slot-ref x1 'a)
(slot-ref x1 'b)
(slot-ref x1 'c)
(slot-bound? x1 'x)))
(test* "instance update (y1)" '(#f 0 1 2 3 4)
(list (slot-bound? y1 'x)
(slot-ref y1 'a)
(slot-ref y1 'b)
(slot-ref y1 'c)
(slot-ref y1 'd)
(slot-ref y1 'e)))
(test* "redefine <x> again" '(a c x)
(begin
(eval '(define-class <x> () (a c (x :init-value 3))) (current-module))
(eval '(map car (class-slots <x>)) (current-module))))
(test* "instance update (x1)" '(1 #f -6 3)
(begin
(slot-set! x1 'a 1)
(list (slot-ref x1 'a)
(slot-exists? x1 'b)
(slot-ref x1 'c)
(slot-ref x1 'x))))
(test* "instance update (x2) - cascade" '(#t -7 #f -9 3)
(list (is-a? x2 <x>)
(slot-ref x2 'a)
(slot-exists? x2 'b)
(slot-ref x2 'c)
(slot-ref x2 'x)))
(test* "redefine <y>" '(a e c x)
(begin
(eval '(define-class <y> (<x>)
((a :accessor a-of)
(e :init-value -200)))
(current-module))
(eval '(map car (class-slots <y>)) (current-module))))
(test* "instance update (y2) - cascade"
'(5 7 9 3)
(map (^s (slot-ref y2 s)) '(a c e x)))
(test* "redefine <y> without inheriting <x>" '(a e)
(begin
(eval '(define-class <y> ()
((a :init-keyword :a :init-value -30)
(e :init-keyword :e :init-value -40)))
(current-module))
(eval '(map car (class-slots <y>)) (current-module))))
(test* "link consistency <y> vs <x>" '(#f #f #f)
(list (memq <y> (ref <x> 'direct-subclasses))
(memq <y>-orig (ref <x> 'direct-subclasses))
(memq <x> (ref <y> 'direct-supers))))
(test* "instance update (y1)" '(0 4)
(map (^s (slot-ref y1 s)) '(a e)))
(test* "subclass redefinition <w>" '(e f a)
(map car (class-slots <w>)))
(test* "instance update (w1)" '(#f #t #t 100 104 105)
(list (is-a? w1 <x>)
(is-a? w1 <y>)
(is-a? w1 <z>)
(slot-ref w1 'a)
(slot-ref w1 'e)
(slot-ref w1 'f)))
(test* "instance update (w2)" '(#f #t #t -30 #f #f)
(list (is-a? w2 <x>)
(is-a? w2 <y>)
(is-a? w2 <z>)
(slot-ref w2 'a)
(slot-bound? w2 'e)
(slot-bound? w2 'f)))
(test* "method link fix"
'(#t #t #t #t #t #t #t)
(list (method-link-check redef-test1 <x>)
(method-link-check redef-test1 <y>)
(method-link-check redef-test1 <w>)
(method-link-check redef-test1 <w2>)
(method-link-check redef-test2 <x>)
(method-link-check redef-test2 <y>)
(method-link-check redef-test2 <w>)))
;;----------------------------------------------------------------
(test-section "object comparison protocol")
(define-class <cmp> () ((x :init-keyword :x)))
(define-method object-equal? ((x <cmp>) (y <cmp>))
(equal? (slot-ref x 'x) (slot-ref y 'x)))
(define-method object-compare ((x <cmp>) (y <cmp>))
(compare (slot-ref x 'x) (slot-ref y 'x)))
(test* "object-equal?" #t
(equal? (make <cmp> :x 3) (make <cmp> :x 3)))
(test* "object-equal?" #f
(equal? (make <cmp> :x 3) (make <cmp> :x 2)))
(test* "object-equal?" #t
(equal? (make <cmp> :x (list 1 2))
(make <cmp> :x (list 1 2))))
(test* "object-equal?" #f
(equal? (make <cmp> :x 5) 5))
(test* "object-compare" -1 (compare 0 1))
(test* "object-compare" 0 (compare 0 0))
(test* "object-compare" 1 (compare 1 0))
(test* "object-compare" -1 (compare "abc" "abd"))
(test* "object-compare" 0 (compare "abc" "abc"))
(test* "object-compare" 1 (compare "abd" "abc"))
(test* "object-compare" -1 (compare #\a #\b))
(test* "object-compare" 0 (compare #\a #\a))
(test* "object-compare" 1 (compare #\b #\a))
(test* "object-compare" -1 (compare (make <cmp> :x 3) (make <cmp> :x 4)))
(test* "object-compare" 0 (compare (make <cmp> :x 3) (make <cmp> :x 3)))
(test* "object-compare" 1 (compare (make <cmp> :x 4) (make <cmp> :x 3)))
;;----------------------------------------------------------------
(test-section "object hash protocol")
;; Without definining object-hash, it returns a constant value
(test* "object-hash" #t
(eqv? (hash (make <cmp> :x (list 1 2)))
(hash (make <cmp> :x (list 1 2)))))
(define-method object-hash ((obj <cmp>))
(+ (hash (slot-ref obj 'x)) 1))
(test* "object-hash" (+ (hash (list 1 2)) 1)
(hash (make <cmp> :x (list 1 2))))
(test* "object-hash" (hash (make <cmp> :x (list 1 2)))
(hash (make <cmp> :x (list 1 2))))
(test* "object-hash" (+ (hash (vector 'a 'b)) 1)
(hash (make <cmp> :x '#(a b))))
(test* "object-hash" (+ (hash "ab") 1)
(hash (make <cmp> :x "ab")))
;; NB: the following test is not necessarily be false theoretically,
;; but we know the two returns different values in our implementation.
(test* "object-hash" #f
(equal? (hash (make <cmp> :x (cons 1 2)))
(hash (make <cmp> :x (cons 2 1)))))
(use srfi-1)
(define xht (make-hash-table 'equal?))
(test* "a => 8" 8
(begin
(hash-table-put! xht (make <cmp> :x 'a) 8)
(hash-table-get xht (make <cmp> :x 'a))))
(test* "b => non" #t
(hash-table-get xht (make <cmp> :x 'b) #t))
(test* "b => error" (test-error)
(hash-table-get xht (make <cmp> :x 'b)))
(test* "b => \"b\"" "b"
(begin
(hash-table-put! xht (make <cmp> :x 'b) "b")
(hash-table-get xht (make <cmp> :x 'b))))
(test* "2.0 => #\C" #\C
(begin
(hash-table-put! xht (make <cmp> :x 2.0) #\C)
(hash-table-get xht (make <cmp> :x 2.0))))
(test* "2.0 => #\c" #\c
(begin
(hash-table-put! xht (make <cmp> :x 2.0) #\c)
(hash-table-get xht (make <cmp> :x 2.0))))
(test* "87592876592374659237845692374523694756 => 0" 0
(begin
(hash-table-put! xht
(make <cmp> :x 87592876592374659237845692374523694756) 0)
(hash-table-get xht
(make <cmp> :x 87592876592374659237845692374523694756))))
(test* "87592876592374659237845692374523694756 => -1" -1
(begin
(hash-table-put! xht
(make <cmp> :x 87592876592374659237845692374523694756) -1)
(hash-table-get xht
(make <cmp> :x 87592876592374659237845692374523694756))))
(test* "equal? test" 5
(begin
(hash-table-put! xht (make <cmp> :x (string #\d)) 4)
(hash-table-put! xht (make <cmp> :x (string #\d)) 5)
(length (hash-table-keys xht))))
(test* "equal? test" 6
(begin
(hash-table-put! xht (make <cmp> :x (cons 'a 'b)) 6)
(hash-table-put! xht (make <cmp> :x (cons 'a 'b)) 7)
(length (hash-table-keys xht))))
(test* "equal? test" 7
(begin
(hash-table-put! xht (make <cmp> :x (vector (cons 'a 'b) 3+3i)) 60)
(hash-table-put! xht (make <cmp> :x (vector (cons 'a 'b) 3+3i)) 61)
(length (hash-table-keys xht))))
(test* "hash-table-values" #t
(lset= equal? (hash-table-values xht) '(8 "b" #\c -1 5 7 61)))
(test* "delete!" #f
(begin
(hash-table-delete! xht (make <cmp> :x (vector (cons 'a 'b) 3+3i)))
(hash-table-get xht (make <cmp> :x (vector (cons 'a 'b) 3+3i)) #f)))
;; Allowing to use user-defined objects in a hashtable with
;; a comparator with default-hash.
;; We use eq-/eqv-hash regardless of the compoarator's hash setting
;; If the equality predicate is eq?/eqv?. See srfi-125.
;; Also https://github.com/shirok/Gauche/issues/708
(define-class <user-defined-hashed> ()
((a :init-keyword :a)))
(let ([eqtab (make-hash-table (make-eq-comparator))]
[eqvtab (make-hash-table (make-eqv-comparator))]
[x0 (make <user-defined-hashed> :a 1)]
[x1 (make <user-defined-hashed> :a 2)])
(hash-table-put! eqtab x0 1)
(hash-table-put! eqtab x1 2)
(hash-table-put! eqvtab x0 3)
(hash-table-put! eqvtab x1 4)
(test* "user-defined object in eq/eqv comparator"
'(1 2)
(map (cut hash-table-get eqtab <>) (list x0 x1)))
(test* "user-defined object in eq/eqv comparator"
'(3 4)
(map (cut hash-table-get eqvtab <>) (list x0 x1)))
)
;;----------------------------------------------------------------
(test-section "object-apply protocol")
(define-class <applicable> ()
((v :initform (make-vector 5 #f))))
(define-method object-apply ((self <applicable>) (i <integer>))
(vector-ref (ref self 'v) i))
(define-method (setter object-apply) ((self <applicable>) (i <integer>) v)
(vector-set! (ref self 'v) i v))
(define-method object-apply ((self <applicable>) (s <symbol>))
(case s
((list) (vector->list (ref self 'v)))
((vector) (ref self 'v))
(else #f)))
(define applicable (make <applicable>))
(test* "object-apply" #f (applicable 2))
(test* "object-apply" 'a
(begin (set! (applicable 3) 'a) (applicable 3)))
(test* "object-apply" '(d b c a q)
(begin
(for-each (lambda (i v) (set! (applicable i) v))
'(2 4 1 0) '(c q b d))
(map applicable '(0 1 2 3 4))))
(test* "object-apply" '((d b c a q) #(d b c a q))
(map applicable '(list vector)))
;;----------------------------------------------------------------
(test-section "metaclass")
(define-class <listing-class> (<class>)
((classes :allocation :class :init-value '() :accessor classes-of))
)
(define-method initialize ((class <listing-class>) initargs)
(next-method)
(set! (classes-of class) (cons (class-name class) (classes-of class))))
(define-class <xx> ()
()
:metaclass <listing-class>)
(define-class <yy> (<xx>)
())
(test* "metaclass" '(<yy> <xx>)
(class-slot-ref <listing-class> 'classes))
(define-class <auto-accessor-class> (<class>)
())
(define-method initialize ((class <auto-accessor-class>) initargs)
(let ((slots (map (lambda (slot)
(if (get-keyword :accessor (cdr slot) #f)
slot
(cons (car slot)
(list* :accessor
(string->symbol
(format #f "~a-of" (car slot)))
(cdr slot)))))
(get-keyword :slots initargs '()))))
(next-method class (list* :slots slots initargs))))
(define-class <zz> ()
(a b c)
:metaclass <auto-accessor-class>)
(test* "metaclass" '(1 2 3)
(let ((zz (make <zz>)))
(set! (a-of zz) 1)
(set! (b-of zz) 2)
(set! (c-of zz) 3)
(map (^s (slot-ref zz s)) '(a b c))))
(define-class <uu> (<zz>)
(d e f))
(test* "metaclass" '(1 2 3 4 5 6)
(let ((uu (make <uu>)))
(set! (a-of uu) 1)
(set! (b-of uu) 2)
(set! (c-of uu) 3)
(set! (d-of uu) 4)
(set! (e-of uu) 5)
(set! (f-of uu) 6)
(map (^s (slot-ref uu s)) '(a b c d e f))))
(define-class <vv> (<zz> <xx>)
())
(test* "metaclass" '(1 2 3)
(let ((vv (make <vv>)))
(set! (a-of vv) 1)
(set! (b-of vv) 2)
(set! (c-of vv) 3)
(map (^s (slot-ref vv s)) '(a b c))))
(test "metaclass" '(<vv> <yy> <xx>)
(lambda () (class-slot-ref <listing-class> 'classes)))
(define-class <ww> (<uu> <yy>)
())
(test* "metaclass" #t
(eq? (class-of <ww>) (class-of <vv>)))
(test* "metaclass" '(1 2 3 4 5 6)
(let ((ww (make <ww>)))
(set! (a-of ww) 1)
(set! (b-of ww) 2)
(set! (c-of ww) 3)
(set! (d-of ww) 4)
(set! (e-of ww) 5)
(set! (f-of ww) 6)
(map (^s (slot-ref ww s)) '(a b c d e f))))
(test* "metaclass" '(<ww> <vv> <yy> <xx>)
(class-slot-ref <listing-class> 'classes))
;;----------------------------------------------------------------
(test-section "metaclass w/ slots")
(define-class <docu-meta> (<class>)
((sub :accessor sub-of)
(doc :init-keyword :doc :initform #f)))
(define-method initialize ((self <docu-meta>) initargs)
(next-method)
(set! (sub-of self) "sub"))
(define-class <xxx> ()
(a b c)
:metaclass <docu-meta>
:doc "Doc doc")
(test* "class slot in meta" '("Doc doc" "sub")
(list (slot-ref <xxx> 'doc)
(slot-ref <xxx> 'sub)))
(define-class <docu-meta-sub> (<docu-meta>)
((xtra :init-value 'xtra)))
(define-class <xxx-sub> (<xxx>)
(x y z)
:metaclass <docu-meta-sub>)
(test* "class slot in meta (sub)" '(#f "sub" xtra)
(list (slot-ref <xxx-sub> 'doc)
(slot-ref <xxx-sub> 'sub)
(slot-ref <xxx-sub> 'xtra)))