source: project/release/5/coops-utils/trunk/tests/coops-utils-test.scm @ 37388

Last change on this file since 37388 was 37388, checked in by Kon Lovett, 21 months ago

rm dup issue, update test

File size: 5.3 KB
Line 
1;;;; coops-utils test  -*- Scheme -*-
2;;;; Kon Lovett, Jul '18
3
4(import test)
5
6(test-begin "Coops Utils")
7
8;;;
9
10(import
11  (chicken syntax)
12  (srfi 1)
13  (srfi 13)
14  coops
15  coops-utils)
16
17;;
18
19(test-assert (class? <standard-class>))
20
21(test-assert (class? <standard-object>))
22
23;; setup test reference environment
24
25(define-class <s1xy> () ((x 's1xy-x) (y 's1xy-y)))
26(define-class <s1ab> () ((a 's1ab-a) (b 's1ab-b)))
27(define-class <s2xz> () ((x 's2xz-x) (z 's2xz-z)))
28(define-class <s2ac> () ((a 's2ac-a) (c 's2ac-c)))
29
30(define-class <s1xys1ab> (<s1xy> <s1ab>)
31  ((x 's1xys1ab-x) (y 's1xys1ab-y) (a 's1xys1ab-a) (b 's1xys1ab-b) (me '<s1xys1ab>)))
32(define-class <s2xzs2ac> (<s2xz> <s2ac>)
33  ((x 's2xzs2ac-x) (z 's2xzs2ac-z) (a 's2xzs2ac-a) (c 's2xzs2ac-c)))
34
35(define-class <s1xys2xz> (<s1xy> <s2xz>) ())
36(define-class <s2xzs1ab> (<s2xz> <s1ab>) ())
37
38(define-generic (foo abc xyz))
39
40(define-method (foo primary: (abc <s1xy>) (xyz <s1ab>)) 'primary-<s1xy><s1ab>)
41(define-method (foo before: (abc <s1xy>) (xyz <s1ab>)) 'before-<s1xy><s1ab>)
42(define-method (foo after: (abc <s1xy>) (xyz <s1ab>)) 'after-<s1xy><s1ab>)
43(define-method (foo around: (abc <s1xy>) (xyz <s1ab>)) 'around-<s1xy><s1ab>)
44
45(define-method (foo primary: (abc <s2xz>) (xyz <s2ac>)) 'primary-<s2xz><s2ac>)
46(define-method (foo before: (abc <s2xz>) (xyz <s2ac>)) 'before-<s2xz><s2ac>)
47(define-method (foo after: (abc <s2xz>) (xyz <s2ac>)) 'after-<s2xz><s2ac>)
48(define-method (foo around: (abc <s2xz>) (xyz <s2ac>)) 'around-<s2xz><s2ac>)
49
50(test (list <s2xz> <standard-object> <s1ab>) (class-precedence-list <s2xzs1ab>))
51(test '(x z a b) (class-slots <s2xzs1ab>))
52
53(test-assert (class? <s2xzs2ac>))
54(test (list <standard-object>) (class-direct-supers <s2ac>))
55(test '(a c) (class-direct-slots <s2ac>))
56
57(test-assert (class? <s1xys1ab>))
58(test (list <s1xy> <s1ab>) (class-direct-supers <s1xys1ab>))
59(test '(me) (class-direct-slots <s1xys1ab>))
60
61(test 'foo (generic-name foo))
62(test '(abc xyz) (generic-specialized-arguments foo))
63(test-assert (eq? 2 (length (generic-primary-methods foo))))
64(let ((primaries (generic-primary-methods foo)))
65  (for-each
66    (lambda (x)
67      (let ((specials (method-specializers x)))
68        (test-assert (list? specials))
69        (test-assert (every class? specials)) )
70      (test-assert (procedure? (method-procedure x)))
71      (test-assert (method? x)) )
72    primaries) )
73
74(define s1xy-inst (make <s1xy> 'x 1 'y 2))
75(test-assert (instance? s1xy-inst))
76(test-assert (instance-of? s1xy-inst <s1xy>))
77;(describe-object s1xy-inst)
78
79(define-class <first> () (next))
80(define-class <second> (<first>) ())
81(define-class <third> (<second>) ())
82
83(define 1st (make <first> 'next (make <second> 'next (make <third> 'next "the end"))))
84
85(test "the end" (slot@ 1st next next next))
86(slot@ 1st next next next = "still the end")
87(test "still the end" (slot@ 1st next next next))
88
89;make-copy
90(let ((inst (make-copy s1xy-inst 'y 23)))
91  (test "make-copy" 23 (slot@ inst y)) )
92
93(let ()
94  (define-generic (city-market-class obj))
95  (define-generic (city-goods obj))
96  (define-class city () (name (market-class reader: city-market-class) sellers buyers (goods accessor: city-goods)))
97  (define temphawa (make city 'name "Hawa" 'market-class 2))
98  (test-assert "make-copy temphawa" (make-copy temphawa)) )
99
100;primitive-instance?
101
102;---
103#|
104;; Named (has a name) "concept"
105
106(define-generic (name obj))
107(define-class <named> () (
108  (namsym #:reader name) ) )
109
110;; Moves foreward thru a set of values "concept"
111
112(define-generic (step-function obj))
113(define-class <stepper> () (
114  (nxtval #:reader step-function) ) )
115(define-generic (next-value obj))
116(define-method (next-value (obj <stepper>)) ((step-function obj)))
117
118;; Parameterized extension "concept"
119
120(define-generic (parameters obj))
121(define-generic (basis obj))
122(define-class <parameterized> () (
123  (parms #:reader parameters)
124  (src #:reader basis) ) )
125
126;; Parameterized generative set of random values "concept"
127
128(define-class <random-distribution> (<named> <parameterized> <stepper>) (
129  temp ) )
130
131;; Create an instance of <random-distribution> where the arguments are
132;; the same as the documented procedural distribution API.
133;;
134;; SRFI 27 API: ({some distribution constructor} arg...)
135;;      OO API: (make-random-distribution {some distribution constructor} arg...)
136
137(define-syntax make-random-distribution
138  (syntax-rules ()
139    ((_ ?ctor ?arg0 ...)
140      (make <random-distribution> 'temp (?ctor ?arg0 ...)) ) ) )
141
142(define-method (initialize-instance (obj <random-distribution>))
143  ;Reconstruct distribution api ctor invocation parameters
144  ;(The 'ctor' must be a globally defined procedure compiled
145  ;with procedure-information. So if following nomenclature then the last
146  ;procedure name element will be the kind of distribution.)
147  (let* (
148    (temp (slot@ obj temp))
149    (ctor (car temp))
150    (procinfo (procedure-information ctor))
151    (name (and (pair? procinfo) (symbol->string (car procinfo))))
152    (name
153      (and-let* (
154        (name)
155        (kndpos (string-index-right name #\-)))
156        (substring/shared name (+ kndpos 1)) ) )
157    (dstr-vals (receive (apply ctor (cdr temp))))
158    (parms (and (<= 2 (length dstr-vals)) (receive ((second dstr-vals))))) )
159    (slot@ obj temp = #f) ;"free" the "any" slot
160    (slot@ obj namsym = (string->symbol name))
161    (slot@ obj nxtval = (first dstr-vals))
162    (slot@ obj parms = (and parms (drop-right parms 1))) ) )
163|#
164
165;;;
166
167(test-end "Coops Utils")
168
169(test-exit)
Note: See TracBrowser for help on using the repository browser.