aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/data/collection/dictionary.lux
blob: c28ff6f51f6a1c8057db9db2179f09904e624c55 (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
(.module:
  [library
   [lux #*
    ["_" test (#+ Test)]
    [abstract
     [hash (#+ Hash)]
     [monad (#+ do)]
     [\\spec
      ["$." equivalence]
      ["$." functor (#+ Injection)]]]
    [control
     ["." try]
     ["." exception]]
    [data
     ["." product]
     ["." maybe]
     [collection
      ["." list ("#\." functor)]]]
    [math
     ["." random]
     [number
      ["n" nat]]]]]
  [\\library
   ["." /]])

(def: injection
  (Injection (/.Dictionary Nat))
  (|>> [0] list (/.from_list n.hash)))

(def: for_dictionaries
  Test
  (do {! random.monad}
    [#let [capped_nat (\ random.monad map (n.% 100) random.nat)]
     size capped_nat
     dict (random.dictionary n.hash size random.nat capped_nat)
     non_key (random.filter (|>> (/.key? dict) not)
                            random.nat)
     test_val (random.filter (|>> (list.member? n.equivalence (/.values dict)) not)
                             random.nat)]
    ($_ _.and
        (_.cover [/.size]
                 (n.= size (/.size dict)))
        
        (_.cover [/.empty?]
                 (case size
                   0 (/.empty? dict)
                   _ (not (/.empty? dict))))
        
        (_.cover [/.new]
                 (let [sample (/.new n.hash)]
                   (and (n.= 0 (/.size sample))
                        (/.empty? sample))))

        (do !
          [constant random.nat
           #let [hash (: (Hash Nat)
                         (implementation
                          (def: &equivalence n.equivalence)
                          (def: (hash _)
                            constant)))]]
          (_.cover [/.key_hash]
                   (is? hash (/.key_hash (/.new hash)))))
        
        (_.cover [/.entries /.keys /.values]
                 (\ (list.equivalence (product.equivalence n.equivalence n.equivalence)) =
                    (/.entries dict)
                    (list.zip/2 (/.keys dict)
                                (/.values dict))))

        (_.cover [/.merge]
                 (let [merging_with_oneself (let [(^open ".") (/.equivalence n.equivalence)]
                                              (= dict (/.merge dict dict)))
                       overwritting_keys (let [dict' (|> dict /.entries
                                                         (list\map (function (_ [k v]) [k (inc v)]))
                                                         (/.from_list n.hash))
                                               (^open ".") (/.equivalence n.equivalence)]
                                           (= dict' (/.merge dict' dict)))]
                   (and merging_with_oneself
                        overwritting_keys)))
        
        (_.cover [/.merge_with]
                 (list.every? (function (_ [x x*2]) (n.= (n.* 2 x) x*2))
                              (list.zip/2 (/.values dict)
                                          (/.values (/.merge_with n.+ dict dict)))))

        (_.cover [/.from_list]
                 (let [(^open ".") (/.equivalence n.equivalence)]
                   (and (= dict dict)
                        (|> dict /.entries (/.from_list n.hash) (= dict)))))
        )))

(def: for_entries
  Test
  (do random.monad
    [#let [capped_nat (\ random.monad map (n.% 100) random.nat)]
     size capped_nat
     dict (random.dictionary n.hash size random.nat capped_nat)
     non_key (random.filter (|>> (/.key? dict) not)
                            random.nat)
     test_val (random.filter (|>> (list.member? n.equivalence (/.values dict)) not)
                             random.nat)]
    ($_ _.and
        (_.cover [/.key?]
                 (list.every? (/.key? dict)
                              (/.keys dict)))
        
        (_.cover [/.get]
                 (and (list.every? (function (_ key) (case (/.get key dict)
                                                       (#.Some _) true
                                                       _          false))
                                   (/.keys dict))
                      (case (/.get non_key dict)
                        (#.Some _) false
                        _          true)))
        
        (_.cover [/.put]
                 (and (n.= (inc (/.size dict))
                           (/.size (/.put non_key test_val dict)))
                      (case (/.get non_key (/.put non_key test_val dict))
                        (#.Some v) (n.= test_val v)
                        _          true)))
        
        (_.cover [/.try_put /.key_already_exists]
                 (let [can_put_new_keys!
                       (case (/.try_put non_key test_val dict)
                         (#try.Success dict)
                         (case (/.get non_key dict)
                           (#.Some v) (n.= test_val v)
                           _          true)

                         (#try.Failure _)
                         false)
                       
                       cannot_put_old_keys!
                       (or (n.= 0 size)
                           (let [first_key (|> dict /.keys list.head maybe.assume)]
                             (case (/.try_put first_key test_val dict)
                               (#try.Success _)
                               false
                               
                               (#try.Failure error)
                               (exception.match? /.key_already_exists error))))]
                   (and can_put_new_keys!
                        cannot_put_old_keys!)))
        
        (_.cover [/.remove]
                 (and (let [base (/.put non_key test_val dict)]
                        (and (/.key? base non_key)
                             (not (/.key? (/.remove non_key base) non_key))))
                      (case (list.head (/.keys dict))
                        #.None
                        true
                        
                        (#.Some known_key)
                        (n.= (dec (/.size dict))
                             (/.size (/.remove known_key dict))))))
        
        (_.cover [/.update]
                 (let [base (/.put non_key test_val dict)
                       updt (/.update non_key inc base)]
                   (case [(/.get non_key base) (/.get non_key updt)]
                     [(#.Some x) (#.Some y)]
                     (n.= (inc x) y)

                     _
                     false)))
        
        (_.cover [/.upsert]
                 (let [can_upsert_new_key!
                       (case (/.get non_key (/.upsert non_key test_val inc dict))
                         (#.Some inserted)
                         (n.= (inc test_val) inserted)

                         #.None
                         false)

                       can_upsert_old_key!
                       (case (list.head (/.entries dict))
                         #.None
                         true
                         
                         (#.Some [known_key known_value])
                         (case (/.get known_key (/.upsert known_key test_val inc dict))
                           (#.Some updated)
                           (n.= (inc known_value) updated)

                           #.None
                           false))]
                   (and can_upsert_new_key!
                        can_upsert_old_key!)))

        (_.cover [/.select]
                 (|> dict
                     (/.put non_key test_val)
                     (/.select (list non_key))
                     /.size
                     (n.= 1)))
        
        (_.cover [/.re_bind]
                 (or (n.= 0 size)
                     (let [first_key (|> dict /.keys list.head maybe.assume)
                           rebound (/.re_bind first_key non_key dict)]
                       (and (n.= (/.size dict) (/.size rebound))
                            (/.key? rebound non_key)
                            (not (/.key? rebound first_key))
                            (n.= (maybe.assume (/.get first_key dict))
                                 (maybe.assume (/.get non_key rebound)))))))
        )))

(def: #export test
  Test
  (<| (_.covering /._)
      (_.for [/.Dictionary])
      (do random.monad
        [#let [capped_nat (\ random.monad map (n.% 100) random.nat)]
         size capped_nat
         dict (random.dictionary n.hash size random.nat capped_nat)
         non_key (random.filter (|>> (/.key? dict) not)
                                random.nat)
         test_val (random.filter (|>> (list.member? n.equivalence (/.values dict)) not)
                                 random.nat)]
        ($_ _.and
            (_.for [/.equivalence]
                   ($equivalence.spec (/.equivalence n.equivalence)
                                      (random.dictionary n.hash size random.nat random.nat)))
            
            (_.for [/.functor]
                   ($functor.spec ..injection /.equivalence /.functor))

            ..for_dictionaries
            ..for_entries
            ))))