aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/abstract/interval.lux
blob: 55f97f711f15608e3d93750bb1d8bdb4e2e7b085 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]
    ["[0]" order]
    [\\specification
     ["$[0]" equivalence]]]
   [control
    ["[0]" pipe]]
   [data
    [collection
     ["[0]" set]
     ["[0]" list]]]
   [math
    ["[0]" random (.only Random)]
    [number
     ["n" nat]]]
   [test
    ["_" property (.only Test)]]]]
 [\\library
  ["[0]" / (.only Interval) (.use "[1]#[0]" equivalence)]])

(with_template [<name> <cmp>]
  [(def .public <name>
     (Random (Interval Nat))
     (do random.monad
       [bottom random.nat
        top (random.only (|>> (n.= bottom) not)
                         random.nat)]
       (if (<cmp> top bottom)
         (in (/.between n.enum bottom top))
         (in (/.between n.enum top bottom)))))]

  [inner n.<]
  [outer n.>]
  )

(def .public singleton
  (Random (Interval Nat))
  (do random.monad
    [point random.nat]
    (in (/.singleton n.enum point))))

(def .public interval
  (Random (Interval Nat))
  (all random.either
       ..inner
       ..outer
       ..singleton))

(def types
  Test
  (do random.monad
    [inner ..inner
     outer ..outer
     singleton ..singleton]
    (all _.and
         (_.coverage [/.inner?]
           (/.inner? inner))
         (_.coverage [/.outer?]
           (/.outer? outer))
         (_.coverage [/.singleton /.singleton?]
           (/.singleton? singleton))
         )))

(def boundaries
  Test
  (do random.monad
    [bottom random.nat
     top random.nat
     .let [interval (/.between n.enum bottom top)]]
    (all _.and
         (_.coverage [/.between /.within?]
           (and (/.within? interval bottom)
                (/.within? interval top)))
         (_.coverage [/.starts_with?]
           (/.starts_with? bottom interval))
         (_.coverage [/.ends_with?]
           (/.ends_with? top interval))
         (_.coverage [/.borders?]
           (and (/.borders? interval bottom)
                (/.borders? interval top)))
         )))

(def union
  Test
  (do random.monad
    [some_interval ..interval
     left_inner ..inner
     right_inner ..inner
     left_singleton ..singleton
     right_singleton ..singleton
     left_outer ..outer
     right_outer ..outer]
    (all _.and
         (_.test "The union of an interval to itself yields the same interval."
           (/#= some_interval (/.union some_interval some_interval)))
         (_.test "The union of 2 inner intervals is another inner interval."
           (/.inner? (/.union left_inner right_inner)))
         (_.test "The union of 2 outer intervals yields an inner interval when their complements don't overlap, and an outer when they do."
           (if (/.overlaps? (/.complement left_outer) (/.complement right_outer))
             (/.outer? (/.union left_outer right_outer))
             (/.inner? (/.union left_outer right_outer))))
         )))

(def intersection
  Test
  (do random.monad
    [some_interval ..interval
     left_inner ..inner
     right_inner ..inner
     left_singleton ..singleton
     right_singleton ..singleton
     left_outer ..outer
     right_outer ..outer]
    (all _.and
         (_.test "The intersection of an interval to itself yields the same interval."
           (/#= some_interval (/.intersection some_interval some_interval)))
         (_.test "The intersection of 2 inner intervals yields an inner interval when they overlap, and an outer when they don't."
           (if (/.overlaps? left_inner right_inner)
             (/.inner? (/.intersection left_inner right_inner))
             (/.outer? (/.intersection left_inner right_inner))))
         (_.test "The intersection of 2 outer intervals is another outer interval."
           (/.outer? (/.intersection left_outer right_outer)))
         )))

(def complement
  Test
  (do random.monad
    [some_interval ..interval]
    (all _.and
         (_.test "The complement of a complement is the same as the original."
           (/#= some_interval (|> some_interval /.complement /.complement)))
         (_.test "The complement of an interval does not overlap it."
           (not (/.overlaps? some_interval (/.complement some_interval))))
         )))

(def location
  Test
  (do [! random.monad]
    [[l m r] (|> (random.set n.hash 3 random.nat)
                 (of ! each (|>> set.list
                                 (list.sorted n.<)
                                 (pipe.when
                                   (list b t1 t2)
                                   [b t1 t2]

                                   _
                                   (undefined)))))
     .let [left (/.singleton n.enum l)
           right (/.singleton n.enum r)]]
    (all _.and
         (_.coverage [/.precedes? /.succeeds?]
           (and (/.precedes? right left)
                (/.succeeds? left right)))
         (_.coverage [/.before? /.after?]
           (and (/.before? m left)
                (/.after? m right)))
         )))

(def touch
  Test
  (do [! random.monad]
    [[b t1 t2] (|> (random.set n.hash 3 random.nat)
                   (of ! each (|>> set.list
                                   (list.sorted n.<)
                                   (pipe.when
                                     (list b t1 t2)
                                     [b t1 t2]

                                     _
                                     (undefined)))))
     .let [int_left (/.between n.enum t1 t2)
           int_right (/.between n.enum b t1)]]
    (all _.and
         (_.coverage [/.meets?]
           (/.meets? int_left int_right))
         (_.coverage [/.touches?]
           (/.touches? int_left int_right))
         (_.coverage [/.starts?]
           (/.starts? (/.between n.enum b t2)
                      (/.between n.enum b t1)))
         (_.coverage [/.finishes?]
           (/.finishes? (/.between n.enum b t2)
                        (/.between n.enum t1 t2)))
         )))

(def nested
  Test
  (do [! random.monad]
    [some_interval ..interval
     [x0 x1 x2 x3] (|> (random.set n.hash 4 random.nat)
                       (of ! each (|>> set.list
                                       (list.sorted n.<)
                                       (pipe.when
                                         (list x0 x1 x2 x3)
                                         [x0 x1 x2 x3]

                                         _
                                         (undefined)))))]
    (all _.and
         (_.test "Every interval is nested into itself."
           (/.nested? some_interval some_interval))
         (let [small_inner (/.between n.enum x1 x2)
               large_inner (/.between n.enum x0 x3)]
           (_.test "Inner intervals can be nested inside one another."
             (and (/.nested? large_inner small_inner)
                  (not (/.nested? small_inner large_inner)))))
         (let [small_outer (/.between n.enum x2 x1)
               large_outer (/.between n.enum x3 x0)]
           (_.test "Outer intervals can be nested inside one another."
             (and (/.nested? small_outer large_outer)
                  (not (/.nested? large_outer small_outer)))))
         (let [left_inner (/.between n.enum x0 x1)
               right_inner (/.between n.enum x2 x3)
               outer (/.between n.enum x0 x3)]
           (_.test "Inners can be nested inside outers."
             (and (/.nested? outer left_inner)
                  (/.nested? outer right_inner))))
         )))

(def overlap
  Test
  (do [! random.monad]
    [some_interval ..interval
     [x0 x1 x2 x3] (|> (random.set n.hash 4 random.nat)
                       (of ! each (|>> set.list
                                       (list.sorted n.<)
                                       (pipe.when
                                         (list x0 x1 x2 x3)
                                         [x0 x1 x2 x3]

                                         _
                                         (undefined)))))]
    (all _.and
         (_.test "No interval overlaps with itself."
           (not (/.overlaps? some_interval some_interval)))
         (let [left_inner (/.between n.enum x0 x2)
               right_inner (/.between n.enum x1 x3)]
           (_.test "Inner intervals can overlap one another."
             (and (/.overlaps? left_inner right_inner)
                  (/.overlaps? right_inner left_inner))))
         (let [left_inner (/.between n.enum x0 x2)
               right_inner (/.between n.enum x1 x3)
               outer (/.between n.enum x1 x2)]
           (_.test "Inners can overlap outers."
             (and (/.overlaps? outer left_inner)
                  (/.overlaps? outer right_inner))))
         )))

(def .public test
  Test
  (<| (_.covering /._)
      (all _.and
           (_.for [/.equivalence]
                  ($equivalence.spec /.equivalence ..interval))
           
           ..types
           ..boundaries
           (_.for [/.union]
                  ..union)
           (_.for [/.intersection]
                  ..intersection)
           (_.for [/.complement]
                  ..complement)
           ..location
           ..touch
           (_.for [/.nested?]
                  ..nested)
           (_.for [/.overlaps?]
                  ..overlap)
           )))