-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtesting.rkt
141 lines (126 loc) · 5.1 KB
/
testing.rkt
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
#lang racket/base
(require racket/contract/base)
(provide
(contract-out
[observing-transduction-events
(-> transducer? (transducer/c any/c transduction-event?))]
[transduction-event? (-> any/c boolean?)]
[start-event transduction-event?]
[half-close-event transduction-event?]
[finish-event transduction-event?]
[consume-event (-> any/c consume-event?)]
[consume-event? (-> any/c boolean?)]
[consume-event-value (-> consume-event? any/c)]
[emit-event (-> any/c emit-event?)]
[emit-event? (-> any/c boolean?)]
[emit-event-value (-> emit-event? any/c)]
[half-closed-emit-event (-> any/c half-closed-emit-event?)]
[half-closed-emit-event? (-> any/c boolean?)]
[half-closed-emit-event-value (-> half-closed-emit-event? any/c)]))
(require guard
rebellion/base/variant
rebellion/private/static-name
rebellion/streaming/transducer/base
rebellion/streaming/transducer/private/contract
rebellion/type/record
rebellion/type/singleton
rebellion/type/wrapper)
;@------------------------------------------------------------------------------
(define-singleton-type start-event)
(define-singleton-type half-close-event)
(define-singleton-type finish-event)
(define-wrapper-type consume-event)
(define-wrapper-type emit-event)
(define-wrapper-type half-closed-emit-event)
(define (transduction-event? v)
(or (start-event? v)
(half-close-event? v)
(finish-event? v)
(consume-event? v)
(emit-event? v)
(half-closed-emit-event? v)))
(define-record-type materialized-transduction-step (event original-state))
(define/name (observing-transduction-events original)
(define original-starter (transducer-starter original))
(define original-consumer (transducer-consumer original))
(define original-emitter (transducer-emitter original))
(define original-half-closer (transducer-half-closer original))
(define original-half-closed-emitter
(transducer-half-closed-emitter original))
(define original-finisher (transducer-finisher original))
(define (start)
(define step
(materialized-transduction-step #:event start-event
#:original-state (original-starter)))
(variant #:emit step))
(define (consume original-state element)
(define step
(materialized-transduction-step
#:event (consume-event element)
#:original-state (original-consumer original-state element)))
(variant #:emit step))
(define (emit step)
(define event (materialized-transduction-step-event step))
(define original-state (materialized-transduction-step-original-state step))
(define next-state
(case (variant-tag original-state)
[(#:consume) original-state]
[(#:emit)
(define em (original-emitter (variant-value original-state)))
(define next-step
(materialized-transduction-step
#:event (emit-event (emission-value em))
#:original-state (emission-state em)))
(variant #:emit next-step)]
[(#:half-closed-emit)
(define em
(original-half-closed-emitter (variant-value original-state)))
(define next-step
(materialized-transduction-step
#:event (half-closed-emit-event (half-closed-emission-value em))
#:original-state (half-closed-emission-state em)))
(variant #:half-closed-emit next-step)]
[else
(original-finisher (variant-value original-state))
(define next-step
(materialized-transduction-step
#:event finish-event
#:original-state #f))
(variant #:half-closed-emit next-step)]))
(emission next-state event))
(define (half-close original-state)
(define step
(materialized-transduction-step
#:event half-close-event
#:original-state (original-half-closer original-state)))
(variant #:half-closed-emit step))
(define (half-closed-emit step)
(define event (materialized-transduction-step-event step))
(define original-state (materialized-transduction-step-original-state step))
(define next-state
(guarded-block
(guard (variant? original-state) #:else
(variant #:finish #false))
(guard (equal? (variant-tag original-state) '#:half-closed-emit) #:else
(original-finisher (variant-value original-state))
(define next-step
(materialized-transduction-step
#:event finish-event
#:original-state #false))
(variant #:half-closed-emit next-step))
(define em
(original-half-closed-emitter (variant-value original-state)))
(define next-step
(materialized-transduction-step
#:event (half-closed-emit-event (half-closed-emission-value em))
#:original-state (half-closed-emission-state em)))
(variant #:half-closed-emit next-step)))
(half-closed-emission next-state event))
(make-transducer
#:starter start
#:consumer consume
#:emitter emit
#:half-closer half-close
#:half-closed-emitter half-closed-emit
#:finisher void
#:name enclosing-function-name))