diff options
author | Eduardo Julian | 2022-12-09 17:18:41 -0400 |
---|---|---|
committer | Eduardo Julian | 2022-12-09 17:18:41 -0400 |
commit | fd3f02c024687bc5c2b9741f6386719a0affb7bb (patch) | |
tree | 3bf0b87aae836645bfe0e5bffcd68a8a4aceaf3e /stdlib/source/test/lux/world | |
parent | 94e5802f594a73245fce0fbd885103b8bf210d57 (diff) |
Added machinery for averaging a time-series.
Diffstat (limited to '')
-rw-r--r-- | stdlib/source/test/lux/world/net/http/response.lux | 11 | ||||
-rw-r--r-- | stdlib/source/test/lux/world/time/series.lux | 213 | ||||
-rw-r--r-- | stdlib/source/test/lux/world/time/series/average.lux | 100 |
3 files changed, 210 insertions, 114 deletions
diff --git a/stdlib/source/test/lux/world/net/http/response.lux b/stdlib/source/test/lux/world/net/http/response.lux index 4e4e0baac..d25d3ce7f 100644 --- a/stdlib/source/test/lux/world/net/http/response.lux +++ b/stdlib/source/test/lux/world/net/http/response.lux @@ -17,17 +17,18 @@ [encoding ["[0]" utf8 (.use "[1]#[0]" codec)]]] [format - ["[0]" html] - ["[0]" css (.only) - ["[0]" selector] - ["[0]" property] - ["[0]" value]] ["[0]" json (.use "[1]#[0]" codec) ["[1]T" \\test]]]] [math ["[0]" random (.only Random)] [number ["n" nat]]] + [web + ["[0]" html] + ["[0]" css (.only) + ["[0]" selector] + ["[0]" property] + ["[0]" value]]] [test ["_" property (.only Test)] ["[0]" unit]]]] diff --git a/stdlib/source/test/lux/world/time/series.lux b/stdlib/source/test/lux/world/time/series.lux index 806241c2b..47432df08 100644 --- a/stdlib/source/test/lux/world/time/series.lux +++ b/stdlib/source/test/lux/world/time/series.lux @@ -15,7 +15,8 @@ ["[0]" product] [collection ["[0]" list (.use "[1]#[0]" functor mix)] - ["[0]" set]]] + ["[0]" set] + ["[0]" sequence]]] [math ["[0]" random (.only Random) (.use "[1]#[0]" functor)] [number @@ -24,129 +25,123 @@ [world [time ["[0]" instant (.only Instant) (.use "[1]#[0]" order)] - ["[0]" duration]]] + ["[0]" duration (.only Duration)]]] [test ["_" property (.only Test)]]]] [\\library - ["[0]" /]]) + ["[0]" /]] + ["[0]" / + ["[1][0]" average]]) -(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) +(def .public (random events 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)))) + (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 when) - (-> Instant +(def (injection start interval) + (-> Instant Duration (Injection /.Series)) - (|>> [/.#when when - /.#what] - list - /.series - try.trusted)) + (|>> sequence.sequence + [/.#start start + /.#interval interval + /.#data])) (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_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 - 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)))] + 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] + (equivalenceS.spec (/.equivalence n.equivalence) (..random expected_size random.nat))) + (_.for [/.mix] + (mixS.spec (..injection expected_start expected_interval) /.equivalence /.mix)) + (_.for [/.functor] + (functorS.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] - )) - ))) - (<| (_.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] + )) + (_.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] - )))) - (,, (with_template [<exception> <left> <right>] - [(_.coverage [<exception>] - (|> (/.series (list <left> <right>)) - (|.when - {try.#Failure error} - (exception.match? <exception> 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)))) - [/.disordered after before] - [/.duplicated before before] - )) - ))) - ))) + /average.test + )))) diff --git a/stdlib/source/test/lux/world/time/series/average.lux b/stdlib/source/test/lux/world/time/series/average.lux new file mode 100644 index 000000000..5cd02181a --- /dev/null +++ b/stdlib/source/test/lux/world/time/series/average.lux @@ -0,0 +1,100 @@ +(.require + [library + [lux (.except) + [abstract + [monad (.only do)]] + [control + ["[0]" try] + ["[0]" exception]] + [math + ["[0]" random (.only Random)] + [number + ["n" nat]]] + [world + [time + ["[0]" instant (.use "[1]#[0]" order)] + ["[0]" duration (.use "[1]#[0]" equivalence)]]] + [test + ["_" property (.only Test)]]]] + [\\library + ["[0]" / (.only) + ["/[1]" //]]]) + +(def (series events) + (-> Nat + (Random (/.Series Frac))) + (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 random.safe_frac)] + (in [//.#start start + //.#interval interval + //.#data data]))) + +(def .public test + Test + (<| (_.covering /._) + (do [! random.monad] + [expected_events (of ! each (|>> (n.% 10) ++) random.nat) + input (series expected_events) + expected_window_extras (of ! each (n.% expected_events) random.nat)]) + (all _.and + (_.coverage [/.cumulative] + (let [output (/.cumulative input)] + (and (instant#= (//.start input) + (//.start output)) + (n.= (//.size input) + (//.size output))))) + (_.coverage [/.windows] + (<| (try.else false) + (do try.monad + [output (/.windows expected_window_extras input)] + (in (and (instant#= (//.start input) + (//.start output)) + (n.= (n./ (++ expected_window_extras) (//.size input)) + (//.size output))))))) + (_.coverage [/.window_size_is_too_large] + (when (/.windows (++ expected_events) input) + {try.#Failure error} + (exception.match? /.window_size_is_too_large error) + + {try.#Success _} + false)) + (<| (_.for [/.Average /.moving]) + (all _.and + (_.coverage [/.Factor /.simple_factor /.exponential] + (<| (try.else false) + (do try.monad + [output (/.moving (/.exponential /.simple_factor) + expected_window_extras + input)] + (in (and (instant#< (//.start output) + (//.start input)) + (n.= (n.- expected_window_extras (//.size input)) + (//.size output))))))) + (_.coverage [/.simple] + (<| (try.else false) + (do try.monad + [output (/.moving /.simple + expected_window_extras + input)] + (in (and (instant#< (//.start output) + (//.start input)) + (n.= (n.- expected_window_extras (//.size input)) + (//.size output))))))) + (_.coverage [/.weighted] + (<| (try.else false) + (do try.monad + [output (/.moving /.weighted + expected_window_extras + input)] + (in (and (instant#< (//.start output) + (//.start input)) + (n.= (n.- expected_window_extras (//.size input)) + (//.size output))))))) + )) + ))) |