aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/data/lazy.lux
blob: 957ce0c3417bfac7c88d6b0f6c1c7deea0ae22d0 (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
(.module:
  [lux #*
   ["_" test (#+ Test)]
   [control
    [monad (#+ do)]
    {[0 #test]
     [/
      [".T" functor (#+ Injection Comparison)]
      [".T" apply]
      [".T" monad]
      [".T" equivalence]]}]
   [data
    text/format
    [number
     ["." nat]]]
   [math
    ["r" random (#+ Random)]]]
  {1
   ["." / (#+ Lazy)]})

(def: injection
  (Injection Lazy)
  (|>> /.freeze))

(def: comparison
  (Comparison Lazy)
  (function (_ ==)
    (:: (/.equivalence ==) =)))

(def: #export lazy
  (All [a] (-> (Random a) (Random (Lazy a))))
  (:: r.functor map (|>> /.freeze)))

(def: #export test
  Test
  (<| (_.context (%name (name-of /.Lazy)))
      (do r.monad
        [left r.nat
         right r.nat
         #let [lazy (/.freeze (n/* left right))
               expected (n/* left right)]]
        ($_ _.and
            (_.test "Freezing does not alter the expected value."
                    (n/= expected
                         (/.thaw lazy)))
            (_.test "Lazy values only evaluate once."
                    (and (not (is? expected
                                   (/.thaw lazy)))
                         (is? (/.thaw lazy)
                              (/.thaw lazy))))
            (equivalenceT.test (/.equivalence nat.equivalence) (..lazy r.nat))
            (functorT.laws ..injection ..comparison /.functor)
            (applyT.laws ..injection ..comparison /.apply)
            (monadT.laws ..injection ..comparison /.monad)
            ))))