aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/concurrency/frp.lux
blob: 4d9632b8c714b22097fddf7d7cbcabb9728bac7d (plain)
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
(.module:
  [lux #*
   ["_" test (#+ Test)]
   [abstract
    [monad (#+ do)]
    {[0 #spec]
     [/
      ["$." functor (#+ Injection Comparison)]
      ["$." apply]
      ["$." monad]]}]
   [control
    ["." try]
    ["." exception]
    ["." io (#+ IO io)]]
   [data
    [number
     ["n" nat]]
    [collection
     ["." list ("#@." functor fold)]
     ["." row (#+ Row)]]]
   [math
    ["." random]]]
  {1
   ["." /
    [//
     ["." promise ("#@." monad)]
     ["." atom (#+ Atom atom)]]]})

(def: injection
  (Injection /.Channel)
  (|>> promise.resolved
       /.from-promise))

(def: comparison
  (Comparison /.Channel)
  (function (_ == left right)
    (io.run
     (do io.monad
       [?left (promise.poll left)
        ?right (promise.poll right)]
       (wrap (case [?left ?right]
               [(#.Some (#.Some [left _]))
                (#.Some (#.Some [right _]))]
               (== left right)
               
               _
               false))))))

(def: #export test
  Test
  (<| (_.covering /._)
      (let [(^open "list@.") (list.equivalence n.equivalence)]
        (do random.monad
          [inputs (random.list 5 random.nat)
           sample random.nat
           distint/0 random.nat
           distint/1 (|> random.nat (random.filter (|>> (n.= distint/0) not)))
           distint/2 (|> random.nat (random.filter (function (_ value)
                                                     (not (or (n.= distint/0 value)
                                                              (n.= distint/1 value))))))
           shift random.nat]
          ($_ _.and
              (_.with-cover [/.functor]
                ($functor.spec ..injection ..comparison /.functor))
              (_.with-cover [/.apply]
                ($apply.spec ..injection ..comparison /.apply))
              (_.with-cover [/.monad]
                ($monad.spec ..injection ..comparison /.monad))
              
              (_.cover [/.Channel /.Sink /.channel]
                       (case (io.run
                              (do (try.with io.monad)
                                [#let [[channel sink] (/.channel [])]
                                 _ (:: sink feed sample)
                                 _ (:: sink close)]
                                (wrap channel)))
                         (#try.Success channel)
                         (io.run
                          (do io.monad
                            [?actual (promise.poll channel)]
                            (wrap (case ?actual
                                    (#.Some (#.Some [actual _]))
                                    (n.= sample actual)
                                    
                                    _
                                    false))))
                         
                         (#try.Failure error)
                         false))
              (_.cover [/.channel-is-already-closed]
                       (case (io.run
                              (do (try.with io.monad)
                                [#let [[channel sink] (/.channel [])]
                                 _ (:: sink close)]
                                (:: sink feed sample)))
                         (#try.Success _)
                         false
                         
                         (#try.Failure error)
                         (exception.match? /.channel-is-already-closed error)))
              (wrap (do promise.monad
                      [output (|> sample
                                  promise.resolved
                                  /.from-promise
                                  /.consume)]
                      (_.claim [/.from-promise /.consume]
                               (list@= (list sample)
                                       output))))
              (wrap (do promise.monad
                      [output (|> inputs
                                  (/.sequential 0)
                                  /.consume)]
                      (_.claim [/.sequential]
                               (list@= inputs
                                       output))))
              (wrap (do promise.monad
                      [output (|> inputs
                                  (/.sequential 0)
                                  (/.filter n.even?)
                                  /.consume)]
                      (_.claim [/.filter]
                               (list@= (list.filter n.even? inputs)
                                       output))))
              (wrap (do {@ promise.monad}
                      [#let [sink (: (Atom (Row Nat))
                                     (atom.atom row.empty))
                             channel (/.sequential 0 inputs)]
                       _ (promise.future (/.listen (function (_ value)
                                                     (do io.monad
                                                       [_ (atom.update (row.add value) sink)]
                                                       (wrap [])))
                                                   channel))
                       output (/.consume channel)
                       listened (|> sink
                                    atom.read
                                    promise.future
                                    (:: @ map row.to-list))]
                      (_.claim [/.listen]
                               (and (list@= inputs
                                            output)
                                    (list@= output
                                            listened)))))
              (wrap (do promise.monad
                      [actual (/.fold (function (_ input total)
                                        (promise.resolved (n.+ input total)))
                                      0
                                      (/.sequential 0 inputs))]
                      (_.claim [/.fold]
                               (n.= (list@fold n.+ 0 inputs)
                                    actual))))
              (wrap (do promise.monad
                      [actual (|> inputs
                                  (/.sequential 0)
                                  (/.folds (function (_ input total)
                                             (promise.resolved (n.+ input total)))
                                           0)
                                  /.consume)]
                      (_.claim [/.folds]
                               (list@= (list.folds n.+ 0 inputs)
                                       actual))))
              (wrap (do promise.monad
                      [actual (|> (list distint/0 distint/0 distint/0
                                        distint/1
                                        distint/2 distint/2)
                                  (/.sequential 0)
                                  (/.distinct n.equivalence)
                                  /.consume)]
                      (_.claim [/.distinct]
                               (list@= (list distint/0 distint/1 distint/2)
                                       actual))))
              (let [polling-delay 10
                    wiggle-room (n.* 3 polling-delay)
                    amount-of-polls 5
                    total-delay (|> polling-delay
                                    (n.* amount-of-polls)
                                    (n.+ wiggle-room))]
                ($_ _.and
                    (wrap (do promise.monad
                            [#let [[channel sink] (/.poll polling-delay (: (IO Nat) (io.io sample)))]
                             _ (promise.schedule total-delay (io.io []))
                             _ (promise.future (:: sink close))
                             actual (/.consume channel)]
                            (_.claim [/.poll]
                                     (and (list.every? (n.= sample) actual)
                                          (n.>= amount-of-polls (list.size actual))))))
                    (wrap (do promise.monad
                            [#let [[channel sink] (/.periodic polling-delay)]
                             _ (promise.schedule total-delay (io.io []))
                             _ (promise.future (:: sink close))
                             actual (/.consume channel)]
                            (_.claim [/.periodic]
                                     (n.>= amount-of-polls (list.size actual)))))))
              (wrap (do promise.monad
                      [#let [max-iterations 10]
                       actual (|> [0 sample]
                                  (/.iterate (function (_ [iterations current])
                                               (promise.resolved
                                                (if (n.< max-iterations iterations)
                                                  (#.Some [[(inc iterations) (n.+ shift current)]
                                                           current])
                                                  #.None))))
                                  /.consume)]
                      (_.claim [/.iterate]
                               (and (n.= max-iterations (list.size actual))
                                    (list@= (list.folds n.+ sample (list.repeat (dec max-iterations) shift))
                                            actual)))))
              )))))