aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/data/sum.lux
blob: fd3f4fecccef64e3ac5741fe92bac9e0146cdb26 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
(.using
 [library
  [lux (.except)
   ["_" test (.only Test)]
   [abstract
    [monad (.only do)]
    [\\specification
     ["$[0]" equivalence]
     ["$[0]" hash]]]
   [control
    ["[0]" pipe]]
   [data
    ["[0]" text]
    [collection
     ["[0]" list (.open: "[1]#[0]" functor)]]]
   [math
    ["[0]" random]
    [number
     ["n" nat]
     ["i" int]]]]]
 [\\library
  ["[0]" /]])

(def .public test
  Test
  (<| (_.covering /._)
      (_.for [.Union .Or])
      (do [! random.monad]
        [expected random.nat
         shift random.nat]
        (all _.and
             (_.for [/.equivalence]
                    ($equivalence.spec (/.equivalence n.equivalence n.equivalence)
                                       (random.or random.nat random.nat)))
             (_.for [/.hash]
                    ($hash.spec (/.hash n.hash n.hash)
                                (random.or random.nat random.nat)))

             (_.coverage [/.left]
               (|> (/.left expected)
                   (is (Or Nat Nat))
                   (pipe.case
                     {0 #0 actual} (n.= expected actual)
                     _ false)))
             (_.coverage [/.right]
               (|> (/.right expected)
                   (is (Or Nat Nat))
                   (pipe.case
                     {0 #1 actual} (n.= expected actual)
                     _ false)))
             (_.coverage [/.either]
               (and (|> (/.left expected)
                        (is (Or Nat Nat))
                        (/.either (n.+ shift) (n.- shift))
                        (n.= (n.+ shift expected)))
                    (|> (/.right expected)
                        (is (Or Nat Nat))
                        (/.either (n.+ shift) (n.- shift))
                        (n.= (n.- shift expected)))))
             (_.coverage [/.then]
               (and (|> (/.left expected)
                        (is (Or Nat Nat))
                        (/.then (n.+ shift) (n.- shift))
                        (pipe.case {0 #0 actual} (n.= (n.+ shift expected) actual) _ false))
                    (|> (/.right expected)
                        (is (Or Nat Nat))
                        (/.then (n.+ shift) (n.- shift))
                        (pipe.case {0 #1 actual} (n.= (n.- shift expected) actual) _ false))))
             (do !
               [size (at ! each (n.% 5) random.nat)
                expected (random.list size random.nat)]
               (all _.and
                    (_.coverage [/.lefts]
                      (let [actual (is (List (Or Nat Nat))
                                       (list#each /.left expected))]
                        (and (at (list.equivalence n.equivalence) =
                                 expected
                                 (/.lefts actual))
                             (at (list.equivalence n.equivalence) =
                                 (list)
                                 (/.rights actual)))))
                    (_.coverage [/.rights]
                      (let [actual (is (List (Or Nat Nat))
                                       (list#each /.right expected))]
                        (and (at (list.equivalence n.equivalence) =
                                 expected
                                 (/.rights actual))
                             (at (list.equivalence n.equivalence) =
                                 (list)
                                 (/.lefts actual)))))
                    (_.coverage [/.partition]
                      (let [[lefts rights] (|> expected
                                               (list#each (function (_ value)
                                                            (if (n.even? value)
                                                              (/.left value)
                                                              (/.right value))))
                                               (is (List (Or Nat Nat)))
                                               /.partition)]
                        (and (at (list.equivalence n.equivalence) =
                                 (list.only n.even? expected)
                                 lefts)
                             (at (list.equivalence n.equivalence) =
                                 (list.only (|>> n.even? not) expected)
                                 rights))))
                    ))
             ))))