aboutsummaryrefslogtreecommitdiff
path: root/stdlib/test
diff options
context:
space:
mode:
authorEduardo Julian2017-07-24 20:04:59 -0400
committerEduardo Julian2017-07-24 20:04:59 -0400
commitb86a223c911edf2580b46d9892f344531616695d (patch)
tree4a8653a1a1295fbb02ce3f480274ab24dc8a5fef /stdlib/test
parent6928bc80cfc265628215b0111751e072f6f2e640 (diff)
- Refactored Time into Instant and Duration.
Diffstat (limited to '')
-rw-r--r--stdlib/test/test/lux/time.lux68
-rw-r--r--stdlib/test/test/lux/time/duration.lux57
-rw-r--r--stdlib/test/test/lux/time/instant.lux74
-rw-r--r--stdlib/test/tests.lux3
4 files changed, 133 insertions, 69 deletions
diff --git a/stdlib/test/test/lux/time.lux b/stdlib/test/test/lux/time.lux
deleted file mode 100644
index 3d477f4ea..000000000
--- a/stdlib/test/test/lux/time.lux
+++ /dev/null
@@ -1,68 +0,0 @@
-(;module:
- lux
- (lux [io]
- (control [monad #+ do Monad]
- pipe)
- (data [text]
- text/format
- ["R" result]
- [number "Int/" Number<Int>])
- (math ["r" random])
- ["@" time])
- lux/test)
-
-(def: (limited-int size)
- (-> Nat (r;Random Int))
- (do r;Monad<Random>
- [sample r;int]
- (wrap (|> sample
- Int/abs
- (i.% (nat-to-int size))
- (i.* (Int/signum sample))))))
-
-(def: boundary Int 99_999_999_999_999)
-
-(def: time (r;Random @;Time)
- (|> r;int (:: r;Monad<Random> map (i.% boundary))))
-
-(context: "Equality"
- [sample time
- #let [(^open) @;Eq<Time>]]
- (test "Every time equals itself."
- (= sample sample)))
-
-(context: "Arithmetic"
- [subject time
- param time]
- ($_ seq
- (test "Can add and subtract times."
- (and (|> subject (@;t.+ param) (@;t.- param) (@;t.= subject))
- (|> subject (@;t.- param) (@;t.+ param) (@;t.= subject))))
- (test "Subtracting a time from itself results in the epoch."
- (@;t.= @;epoch
- (@;t.- subject subject)))
- ))
-
-(context: "Order"
- [reference time
- sample time
- #let [(^open) @;Order<Time>]]
- (test "Can compare times."
- (and (or (< reference sample)
- (>= reference sample))
- (or (> reference sample)
- (<= reference sample)))))
-
-(context: "Codec"
- #seed +16366082068080165840
- [sample time
- #let [(^open "&/") @;Codec<Text,Time>]]
- (test "Can encode/decode times."
- (|> sample
- &/encode
- &/decode
- (case> (#R;Success decoded)
- (@;t.= sample decoded)
-
- (#R;Error error)
- false))))
diff --git a/stdlib/test/test/lux/time/duration.lux b/stdlib/test/test/lux/time/duration.lux
new file mode 100644
index 000000000..18ec46090
--- /dev/null
+++ b/stdlib/test/test/lux/time/duration.lux
@@ -0,0 +1,57 @@
+(;module:
+ lux
+ (lux [io]
+ (control [monad #+ do Monad])
+ (math ["r" random])
+ (time ["@" duration]))
+ lux/test)
+
+(def: duration
+ (r;Random @;Duration)
+ (|> r;int (:: r;Monad<Random> map @;from-millis)))
+
+(context: "Conversion."
+ [millis r;int]
+ (test "Can convert from/to milliseconds."
+ (|> millis @;from-millis @;to-millis (i.= millis))))
+
+(context: "Equality"
+ [sample duration
+ #let [(^open "@/") @;Eq<Duration>]]
+ (test "Every duration equals itself."
+ (@/= sample sample)))
+
+(context: "Order"
+ [reference duration
+ sample duration
+ #let [(^open "@/") @;Order<Duration>]]
+ (test "Can compare times."
+ (and (or (@/< reference sample)
+ (@/>= reference sample))
+ (or (@/> reference sample)
+ (@/<= reference sample)))))
+
+(context: "Arithmetic."
+ #seed +16674263968423793
+ [sample (|> duration (:: @ map (@;frame @;day)))
+ frame duration
+ factor (|> r;int (:: @ map (|>. (i.% 10) (i.max 1))))
+ #let [(^open "@/") @;Order<Duration>]]
+ ($_ seq
+ (test "Can scale a duration."
+ (|> sample (@;scale factor) (@;query sample) (i.= factor)))
+ (test "Scaling a duration by one does not change it."
+ (|> sample (@;scale 1) (@/= sample)))
+ (test "Merging with the empty duration changes nothing."
+ (|> sample (@;merge @;empty) (@/= sample)))
+ (test "Merging a duration with it's opposite yields an empty duration."
+ (|> sample (@;merge (@;scale -1 sample)) (@/= @;empty)))
+ (test "Can frame a duration in terms of another."
+ (if (or (and (@;positive? frame) (@;positive? sample))
+ (and (@;negative? frame) (@;negative? sample)))
+ (|> sample (@;frame frame) (@/< frame))
+ (or (or (@;neutral? frame) (@;neutral? sample))
+ (|> sample (@;frame frame) (@;scale -1) (@/< (if (@;negative? frame)
+ (@;scale -1 frame)
+ frame))))))
+ ))
diff --git a/stdlib/test/test/lux/time/instant.lux b/stdlib/test/test/lux/time/instant.lux
new file mode 100644
index 000000000..eda4e4ebe
--- /dev/null
+++ b/stdlib/test/test/lux/time/instant.lux
@@ -0,0 +1,74 @@
+(;module:
+ lux
+ (lux [io]
+ (control [monad #+ do Monad]
+ pipe)
+ (data [text]
+ text/format
+ ["R" result]
+ [number "Int/" Number<Int>])
+ (math ["r" random])
+ (time ["@" instant]
+ ["@d" duration]))
+ lux/test)
+
+(def: boundary Int 99_999_999_999_999)
+
+(def: instant
+ (r;Random @;Instant)
+ (|> r;int (:: r;Monad<Random> map (|>. (i.% boundary) @;from-millis))))
+
+(def: duration
+ (r;Random @d;Duration)
+ (|> r;int (:: r;Monad<Random> map @d;from-millis)))
+
+(context: "Conversion."
+ [millis r;int]
+ (test "Can convert from/to milliseconds."
+ (|> millis @;from-millis @;to-millis (i.= millis))))
+
+(context: "Equality"
+ [sample instant
+ #let [(^open "@/") @;Eq<Instant>]]
+ (test "Every instant equals itself."
+ (@/= sample sample)))
+
+(context: "Order"
+ [reference instant
+ sample instant
+ #let [(^open "@/") @;Order<Instant>]]
+ (test "Can compare instants."
+ (and (or (@/< reference sample)
+ (@/>= reference sample))
+ (or (@/> reference sample)
+ (@/<= reference sample)))))
+
+(context: "Arithmetic"
+ [sample instant
+ span duration
+ #let [(^open "@/") @;Eq<Instant>
+ (^open "@d/") @d;Eq<Duration>]]
+ ($_ seq
+ (test "The span of a instant and itself has an empty duration."
+ (|> sample (@;span sample) (@d/= @d;empty)))
+ (test "Can shift a instant by a duration."
+ (|> sample (@;shift span) (@;span sample) (@d/= span)))
+ (test "Can obtain the time-span between the epoch and an instant."
+ (|> sample @;relative @;absolute (@/= sample)))
+ (test "All instants are relative to the epoch."
+ (|> @;epoch (@;shift (@;relative sample)) (@/= sample)))))
+
+(context: "Codec"
+ #seed +4428624921609897635
+ [sample instant
+ #let [(^open "@/") @;Eq<Instant>
+ (^open "@/") @;Codec<Text,Instant>]]
+ (test "Can encode/decode instants."
+ (|> sample
+ @/encode
+ @/decode
+ (case> (#R;Success decoded)
+ (@/= sample decoded)
+
+ (#R;Error error)
+ false))))
diff --git a/stdlib/test/tests.lux b/stdlib/test/tests.lux
index 7c8258bc6..3004190c1 100644
--- a/stdlib/test/tests.lux
+++ b/stdlib/test/tests.lux
@@ -9,7 +9,8 @@
(lux ["_;" cli]
["_;" host]
["_;" io]
- ["_;" time]
+ (time ["_;" instant]
+ ["_;" duration])
(concurrency ["_;" actor]
["_;" atom]
["_;" frp]