aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/world/time/series.lux
blob: 806241c2b2deecaa20e53e7192b3dead7ed45620 (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
(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]
    [\\specification
     ["[0]S" equivalence]
     ["[0]S" mix]
     ["[0]S" functor (.only Injection)]]]
   [control
    ["|" pipe]
    ["[0]" try (.use "[1]#[0]" functor)]
    ["[0]" exception]]
   [data
    ["[0]" product]
    [collection
     ["[0]" list (.use "[1]#[0]" functor mix)]
     ["[0]" set]]]
   [math
    ["[0]" random (.only Random) (.use "[1]#[0]" functor)]
    [number
     ["n" nat]
     ["i" int]]]
   [world
    [time
     ["[0]" instant (.only Instant) (.use "[1]#[0]" order)]
     ["[0]" duration]]]
   [test
    ["_" property (.only Test)]]]]
 [\\library
  ["[0]" /]])

(def .public (event it)
  (All (_ of)
    (-> (Random of)
        (Random (/.Event of))))
  (do random.monad
    [when random.instant
     what it]
    (in [/.#when when
         /.#what what])))

(def .public (random size it)
  (All (_ of)
    (-> Nat (Random of)
        (Random (/.Series of))))
  (|> it
      (random.list size)
      (random#each (|>> (list#mix (function (_ what [when events])
                                    [(instant.before duration.milli_second when)
                                     (list.partial [/.#when when
                                                    /.#what what]
                                                   events)])
                                  [instant.latest (list)])
                        product.right))
      (random.one (|>> /.series
                       try.maybe))))

(def (injection when)
  (-> Instant
      (Injection /.Series))
  (|>> [/.#when when
        /.#what]
       list
       /.series
       try.trusted))

(def .public test
  Test
  (<| (_.covering /._)
      (do [! random.monad]
        [before (..event random.nat)
         after (random.only (|>> (the /.#when)
                                 (instant#= (the /.#when before))
                                 not)
                            (..event random.nat))
         .let [[before after] (if (instant#< (the /.#when after)
                                             (the /.#when before))
                                [before after]
                                [after before])]

         expected_instant random.instant
         expected_size (of ! each (n.% 10) random.nat)
         events (is (Random (List (/.Event Int)))
                    (|> random.int
                        (random.set i.hash expected_size)
                        (of ! each (|>> set.list
                                        (list.sorted i.<)
                                        (list#each (function (_ it)
                                                     [/.#when (instant.of_millis it)
                                                      /.#what it]))))))])
      (all _.and
           (<| (_.for [/.Event
                       /.#what /.#when])
               (`` (all _.and
                        (,, (with_template [<event> <expected>]
                              [(_.coverage [<event>]
                                 (|> (do try.monad
                                       [it (/.series (list before after))
                                        actual (<event> it)]
                                       (in (same? <expected> actual)))
                                     (try.else false)))]

                              [/.earliest before]
                              [/.latest after]
                              ))
                        )))
           (<| (_.for [/.Series])
               (`` (all _.and
                        (_.for [/.equivalence]
                               (equivalenceS.spec (/.equivalence n.equivalence) (..random expected_size random.nat)))
                        (_.for [/.mix]
                               (mixS.spec (..injection expected_instant) /.equivalence /.mix))
                        (_.for [/.functor]
                               (functorS.spec (..injection expected_instant) /.equivalence /.functor))
                        
                        (_.coverage [/.series /.size]
                          (|> (do try.monad
                                [it (/.series events)]
                                (in (/.size it)))
                              (try#each (n.= expected_size))
                              (try.else false)))
                        (_.coverage [/.empty]
                          (and (,, (with_template [<event> <expected>]
                                     [(|> (do try.monad
                                            [it (/.series (list))]
                                            (<event> it))
                                          (|.when
                                            {try.#Failure error}
                                            (exception.match? /.empty error)
                                            
                                            _
                                            false))]

                                     [/.earliest before]
                                     [/.latest after]
                                     ))))
                        (,, (with_template [<exception> <left> <right>]
                              [(_.coverage [<exception>]
                                 (|> (/.series (list <left> <right>))
                                     (|.when
                                       {try.#Failure error}
                                       (exception.match? <exception> error)
                                       
                                       _
                                       false)))]

                              [/.disordered after before]
                              [/.duplicated before before]
                              ))
                        )))
           )))