aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/data/collection/set/ordered.lux
blob: 1734d80c4fc86bfbcf509a2b662c63a16c67ef4e (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
(.module:
  [lux #*
   ["_" test (#+ Test)]
   [abstract
    [monad (#+ do)]
    [order (#+ Order)]
    {[0 #spec]
     [/
      ["$." equivalence]]}]
   [data
    ["." bit ("#\." equivalence)]
    [number
     ["n" nat]]
    [collection
     ["." list]]]
   [math
    ["." random (#+ Random) ("#\." monad)]]]
  {1
   ["." / (#+ Set)
    ["." //]]})

(def: size
  (random.Random Nat)
  (:: random.monad map (n.% 100) random.nat))

(def: #export (random size &order gen-value)
  (All [a] (-> Nat (Order a) (Random a) (Random (Set a))))
  (case size
    0
    (random\wrap (/.new &order))

    _
    (do random.monad
      [partial (random (dec size) &order gen-value)
       value (random.filter (|>> (/.member? partial) not)
                            gen-value)]
      (wrap (/.add value partial)))))

(def: #export test
  Test
  (<| (_.covering /._)
      (_.with-cover [/.Set])
      (do {! random.monad}
        [sizeL ..size
         sizeR ..size
         usetL (random.set n.hash sizeL random.nat)
         non-memberL (random.filter (|>> (//.member? usetL) not)
                                    random.nat)
         #let [listL (//.to-list usetL)]
         listR (|> (random.set n.hash sizeR random.nat) (:: ! map //.to-list))
         #let [(^open "/\.") /.equivalence
               setL (/.from-list n.order listL)
               setR (/.from-list n.order listR)
               empty (/.new n.order)]]
        (`` ($_ _.and
                (_.with-cover [/.equivalence]
                  ($equivalence.spec /.equivalence (..random sizeL n.order random.nat)))

                (_.cover [/.size]
                         (n.= sizeL (/.size setL)))
                (_.cover [/.empty?]
                         (bit\= (n.= 0 (/.size setL))
                                (/.empty? setL)))
                (_.cover [/.new]
                         (/.empty? (/.new n.order)))
                (_.cover [/.to-list]
                         (:: (list.equivalence n.equivalence) =
                             (/.to-list (/.from-list n.order listL))
                             (list.sort (:: n.order <) listL)))
                (_.cover [/.from-list]
                         (|> setL
                             /.to-list (/.from-list n.order)
                             (/\= setL)))
                (~~ (template [<coverage> <comparison>]
                      [(_.cover [<coverage>]
                                (case (<coverage> setL)
                                  (#.Some value)
                                  (|> setL /.to-list (list.every? (<comparison> value)))

                                  #.None
                                  (/.empty? setL)))]

                      [/.min n.>=]
                      [/.max n.<=]
                      ))
                (_.cover [/.member?]
                         (let [members-are-identified!
                               (list.every? (/.member? setL) (/.to-list setL))

                               non-members-are-not-identified!
                               (not (/.member? setL non-memberL))]
                           (and members-are-identified!
                                non-members-are-not-identified!)))
                (_.cover [/.add]
                         (let [setL+ (/.add non-memberL setL)]
                           (and (not (/.member? setL non-memberL))
                                (/.member? setL+ non-memberL)
                                (n.= (inc (/.size setL))
                                     (/.size setL+)))))
                (_.cover [/.remove]
                         (|> setL
                             (/.add non-memberL)
                             (/.remove non-memberL)
                             (:: /.equivalence = setL)))
                (_.cover [/.sub?]
                         (let [self!
                               (/.sub? setL setL)

                               empty!
                               (/.sub? setL empty)]
                           (and self!
                                empty!)))
                (_.cover [/.super?]
                         (let [self!
                               (/.super? setL setL)

                               empty!
                               (/.super? empty setL)

                               symmetry!
                               (bit\= (/.super? setL setR)
                                      (/.sub? setR setL))]
                           (and self!
                                empty!
                                symmetry!)))
                (~~ (template [<coverage> <relation> <empty?>]
                      [(_.cover [<coverage>]
                                (let [self!
                                      (:: /.equivalence =
                                          setL
                                          (<coverage> setL setL))

                                      super!
                                      (and (<relation> (<coverage> setL setR) setL)
                                           (<relation> (<coverage> setL setR) setR))

                                      empty!
                                      (:: /.equivalence =
                                          (if <empty?> empty setL)
                                          (<coverage> setL empty))

                                      idempotence!
                                      (:: /.equivalence =
                                          (<coverage> setL (<coverage> setL setR))
                                          (<coverage> setR (<coverage> setL setR)))]
                                  (and self!
                                       super!
                                       empty!
                                       idempotence!)))]

                      [/.union /.sub? false]
                      [/.intersection /.super? true]
                      ))
                (_.cover [/.difference]
                         (let [self!
                               (|> setL
                                   (/.difference setL)
                                   (:: /.equivalence = empty))

                               empty!
                               (|> setL
                                   (/.difference empty)
                                   (:: /.equivalence = setL))

                               difference!
                               (not (list.any? (/.member? (/.difference setL setR))
                                               (/.to-list setL)))

                               idempotence!
                               (:: /.equivalence =
                                   (/.difference setL setR)
                                   (/.difference setL (/.difference setL setR)))]
                           (and self!
                                empty!
                                difference!
                                idempotence!)))
                )))))