aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/world/time/series.lux
blob: 26ca412defb4064123b2c4d670296f66c4db854d (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
(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]
    ["[0]" functor
     ["[1]T" \\test (.only Injection)]]
    ["[0]" mix
     ["[1]T" \\test]]
    ["[0]" equivalence
     ["[1]T" \\test]]]
   [control
    ["|" pipe]
    ["[0]" try (.use "[1]#[0]" functor)]
    ["[0]" exception]]
   [data
    ["[0]" product]
    [collection
     ["[0]" list (.use "[1]#[0]" functor mix)]
     ["[0]" set]
     ["[0]" sequence]]]
   [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 (.only Duration)]]]
   [test
    ["_" property (.only Test)]]]]
 [\\library
  ["[0]" /]]
 ["[0]" /
  ["[1][0]" average]])

(def .public (random events it)
  (All (_ of)
    (-> Nat (Random of)
        (Random (/.Series of))))
  (do [! random.monad]
    [.let [duration (random.only duration.positive? random.duration)]
     offset (of ! each (duration.framed (duration.up 100 duration.normal_year))
                duration)
     .let [start (instant.after offset instant.epoch)]
     interval (of ! each (duration.framed duration.week)
                  duration)
     data (random.sequence events it)]
    (in [/.#start start
         /.#interval interval
         /.#data data])))

(def (injection start interval)
  (-> Instant Duration
      (Injection /.Series))
  (|>> sequence.sequence
       [/.#start start
        /.#interval interval
        /.#data]))

(def .public test
  Test
  (<| (_.covering /._)
      (do [! random.monad]
        [expected_size (of ! each (|>> (n.% 10) ++) random.nat)
         expected_series (..random expected_size random.nat)

         before random.nat
         after random.nat
         expected_start random.instant
         expected_interval random.duration

         window_size (of ! each (|>> (n.% expected_size) ++) random.nat)
         window_offset (of ! each (n.% (++ (n.- window_size expected_size))) random.nat)])
      (_.for [/.Series
              /.#start /.#interval /.#data])
      (`` (all _.and
               (_.for [/.equivalence]
                      (equivalenceT.spec (/.equivalence n.equivalence) (..random expected_size random.nat)))
               (_.for [/.mix]
                      (mixT.spec (..injection expected_start expected_interval) /.equivalence /.mix))
               (_.for [/.functor]
                      (functorT.spec (..injection expected_start expected_interval) /.equivalence /.functor))
               
               (_.coverage [/.size]
                 (n.= expected_size
                      (/.size expected_series)))
               (_.coverage [/.start /.end]
                 (instant#< (/.end expected_series)
                            (/.start expected_series)))
               (_.coverage [/.at]
                 (and (instant#= (/.at 0 expected_series)
                                 (/.start expected_series))
                      (instant#< (/.at (-- expected_size) expected_series)
                                 (/.start expected_series))))
               (,, (with_template [<event> <expected>]
                     [(_.coverage [<event>]
                        (|> (do try.monad
                              [.let [it [/.#start expected_start
                                         /.#interval expected_interval
                                         /.#data (sequence.sequence before after)]]
                               actual (<event> it)]
                              (in (same? <expected> actual)))
                            (try.else false)))]

                     [/.earliest before]
                     [/.latest after]
                     ))
               (_.coverage [/.empty]
                 (and (,, (with_template [<event> <expected>]
                            [(|> (do try.monad
                                   [.let [it [/.#start expected_start
                                              /.#interval expected_interval
                                              /.#data (sequence.sequence)]]]
                                   (<event> it))
                                 (|.when
                                   {try.#Failure error}
                                   (exception.match? /.empty error)
                                   
                                   _
                                   false))]

                            [/.earliest before]
                            [/.latest after]
                            ))))
               (_.coverage [/.window]
                 (|> (do try.monad
                       [it (/.window window_offset window_size expected_series)]
                       (in (n.= window_size (/.size it))))
                     (try.else false)))
               (_.coverage [/.window_goes_out_of_bounds]
                 (and (|> (/.window expected_size window_size expected_series)
                          (|.when
                            {try.#Failure error}
                            (exception.match? /.window_goes_out_of_bounds error)
                            
                            _
                            false))
                      (|> (/.window (++ window_offset) expected_size expected_series)
                          (|.when
                            {try.#Failure error}
                            (exception.match? /.window_goes_out_of_bounds error)
                            
                            _
                            false))))

               /average.test
               ))))