aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/time/instant.lux
diff options
context:
space:
mode:
Diffstat (limited to 'stdlib/source/test/lux/time/instant.lux')
-rw-r--r--stdlib/source/test/lux/time/instant.lux99
1 files changed, 99 insertions, 0 deletions
diff --git a/stdlib/source/test/lux/time/instant.lux b/stdlib/source/test/lux/time/instant.lux
new file mode 100644
index 000000000..c9d7aad55
--- /dev/null
+++ b/stdlib/source/test/lux/time/instant.lux
@@ -0,0 +1,99 @@
+(.module:
+ [lux #*
+ [io]
+ [control
+ [monad (#+ do Monad)]
+ pipe]
+ [data
+ ["." text
+ format]
+ [error]]
+ [math
+ ["r" random]]
+ [time
+ ["@" instant]
+ ["@d" duration]
+ ["@date" date]]]
+ lux/test
+ [//
+ ["_." duration]])
+
+(def: boundary Int +99_999_999_999_999)
+
+(def: #export instant
+ (r.Random @.Instant)
+ (|> r.int (:: r.monad map (|>> (i/% boundary) @.from-millis))))
+
+(context: "Conversion."
+ (<| (times 100)
+ (do @
+ [millis r.int]
+ (test "Can convert from/to milliseconds."
+ (|> millis @.from-millis @.to-millis (i/= millis))))))
+
+(context: "Equivalence."
+ (<| (times 100)
+ (do @
+ [sample instant
+ #let [(^open "@/.") @.equivalence]]
+ (test "Every instant equals itself."
+ (@/= sample sample)))))
+
+(context: "Order"
+ (<| (times 100)
+ (do @
+ [reference instant
+ sample instant
+ #let [(^open "@/.") @.order]]
+ (test "Can compare instants."
+ (and (or (@/< reference sample)
+ (@/>= reference sample))
+ (or (@/> reference sample)
+ (@/<= reference sample)))))))
+
+(context: "Enum"
+ (<| (times 100)
+ (do @
+ [sample instant
+ #let [(^open "@/.") @.enum]]
+ (test "Valid Enum."
+ (and (not (@/= (@/succ sample)
+ sample))
+ (not (@/= (@/pred sample)
+ sample))
+ (|> sample @/succ @/pred (@/= sample))
+ (|> sample @/pred @/succ (@/= sample)))))))
+
+(context: "Arithmetic"
+ (<| (times 100)
+ (do @
+ [sample instant
+ span _duration.duration
+ #let [(^open "@/.") @.equivalence
+ (^open "@d/.") @d.equivalence]]
+ ($_ 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 9863552679229274604)
+## ## (times 100)
+## (do @
+## [sample instant
+## #let [(^open "@/.") @.equivalence
+## (^open "@/.") @.codec]]
+## (test "Can encode/decode instants."
+## (|> sample
+## @/encode
+## @/decode
+## (case> (#error.Success decoded)
+## (@/= sample decoded)
+
+## (#error.Failure error)
+## #0))))))