aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/macro/poly/equivalence.lux
blob: a73d71112b7378baada5aa113e250d850930e29e (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
(.module:
  [lux #*
   [control
    [monad (#+ do Monad)]
    [equivalence (#+ Equivalence)]]
   [data
    ["." bit]
    ["." maybe]
    [number
     ["." int ("#/." number)]]
    ["." text
     format]
    [collection
     ["." list]]]
   [math
    ["r" random]]
   ["." macro
    [poly (#+ derived:)
     ["&" equivalence]]]]
  lux/test)

(type: Variant
  (#Case0 Bit)
  (#Case1 Int)
  (#Case2 Frac))

(type: #rec Recursive
  (#Number Frac)
  (#Addition Frac Recursive))

(type: Record
  {#bit Bit
   #int Int
   #frac Frac
   #text Text
   #maybe (Maybe Int)
   #list (List Int)
   #variant Variant
   #tuple [Int Frac Text]
   #recursive Recursive})

(def: gen-recursive
  (r.Random Recursive)
  (r.rec (function (_ gen-recursive)
           (r.or r.frac
                 (r.and r.frac gen-recursive)))))

(def: gen-record
  (r.Random Record)
  (do r.monad
    [size (:: @ map (n/% 2) r.nat)
     #let [gen-int (|> r.int (:: @ map (|>> int/abs (i/% +1,000,000))))]]
    ($_ r.and
        r.bit
        gen-int
        r.frac
        (r.unicode size)
        (r.maybe gen-int)
        (r.list size gen-int)
        ($_ r.or r.bit gen-int r.frac)
        ($_ r.and gen-int r.frac (r.unicode size))
        gen-recursive)))

(derived: (&.Equivalence<?> Record))

(context: "Equivalence polytypism"
  (<| (times 100)
      (do @
        [sample gen-record
         #let [(^open "&/.") ..equivalence]]
        (test "Every instance equals itself."
              (&/= sample sample)))))