aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/data/collection/queue/priority.lux
blob: da99e408d6345f99dc0d225a545bed71f1b92843 (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
(.module:
  [library
   [lux #*
    ["_" test (#+ Test)]
    [abstract
     ["." monad (#+ do)]]
    [control
     ["." maybe ("#\." functor)]]
    [data
     ["." bit ("#\." equivalence)]]
    [math
     ["." random (#+ Random)]
     [number
      ["n" nat]]]]]
  [\\library
   ["." / (#+ Queue)]])

(def: .public (random size)
  (-> Nat (Random (Queue Nat)))
  (do {! random.monad}
    [inputs (random.list size random.nat)]
    (monad.mix ! (function (_ head tail)
                   (do !
                     [priority random.nat]
                     (in (/.end priority head tail))))
               /.empty
               inputs)))

(def: .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Queue])
      (do {! random.monad}
        [size (\ ! each (n.% 100) random.nat)
         sample (..random size)
         non_member_priority random.nat
         non_member (random.only (|>> (/.member? n.equivalence sample) not)
                                 random.nat)

         max_member random.nat
         min_member random.nat]
        ($_ _.and
            (_.cover [/.size]
                     (n.= size (/.size sample)))
            (_.cover [/.empty?]
                     (bit\= (n.= 0 (/.size sample))
                            (/.empty? sample)))
            (_.cover [/.empty]
                     (/.empty? /.empty))
            (_.cover [/.front]
                     (case (/.front sample)
                       (#.Some first)
                       (n.> 0 (/.size sample))
                       
                       #.None
                       (/.empty? sample)))
            (_.cover [/.member?]
                     (case (/.front sample)
                       (#.Some first)
                       (/.member? n.equivalence sample first)
                       
                       #.None
                       (/.empty? sample)))
            (_.cover [/.end]
                     (let [sample+ (/.end non_member_priority non_member sample)]
                       (and (not (/.member? n.equivalence sample non_member))
                            (n.= (++ (/.size sample))
                                 (/.size sample+))
                            (/.member? n.equivalence sample+ non_member))))
            (_.cover [/.next]
                     (let [sample- (/.next sample)]
                       (or (and (/.empty? sample)
                                (/.empty? sample-))
                           (n.= (-- (/.size sample))
                                (/.size sample-)))))
            (_.for [/.Priority]
                   ($_ _.and
                       (_.cover [/.max]
                                (|> /.empty
                                    (/.end /.min min_member)
                                    (/.end /.max max_member)
                                    /.front
                                    (maybe\each (n.= max_member))
                                    (maybe.else false)))
                       (_.cover [/.min]
                                (|> /.empty
                                    (/.end /.max max_member)
                                    (/.end /.min min_member)
                                    /.next
                                    /.front
                                    (maybe\each (n.= min_member))
                                    (maybe.else false)))
                       ))
            ))))