|
165 | 165 | (some-> content-cost (+ container-cost))) |
166 | 166 | (:set-of |
167 | 167 | :sequence-of |
168 | | - :repeat) (let [container-cost (if (#{:set-of :sequence-of} type) 1 0) |
| 168 | + :repeat) (let [container-cost 1 |
169 | 169 | min-count (min-count-value model) |
170 | 170 | elements-model (:elements-model model) |
171 | 171 | elements-model-min-cost (if elements-model |
|
186 | 186 | (reduce max vals))) |
187 | 187 | (:map |
188 | 188 | :sequence |
189 | | - :cat) (let [container-cost (if (or (#{:map :sequence} type) |
190 | | - (:coll-type model) |
191 | | - (not (:inlined model true))) |
192 | | - 1 0) |
| 189 | + :cat) (let [container-cost 1 |
193 | 190 | vals (->> (:entries model) |
194 | 191 | (remove :optional) |
195 | 192 | (map (comp ::min-cost :model))) |
|
259 | 256 | (gen/fmap (fn [rates] |
260 | 257 | (let [budget-factor (/ budget-minus-min-costs (reduce + rates))] |
261 | 258 | (mapv (fn [min-cost rate] |
262 | | - (+ min-cost (* rate budget-factor))) |
| 259 | + (+ min-cost (int (* rate budget-factor)))) |
263 | 260 | min-costs |
264 | 261 | rates))) |
265 | 262 | (gen/vector (gen/choose 1 100) nb-elements))) |
266 | 263 | (gen/return []))) |
267 | 264 |
|
268 | 265 |
|
269 | | -;; TODO: What if ... conditions could only exist as something else's :condition-model? |
270 | | -;; conditions would then only be used for validity testing, not generation. |
271 | | -;; TODO: add :condition-model to :fn nodes and maybe others (all of them?) |
272 | | - |
273 | 266 | (declare generator) |
274 | 267 |
|
275 | 268 | (defn- sequence-generator |
|
289 | 282 | (let [chosen-entry (first (sort-by (comp ::min-cost :model) possible-entries))] |
290 | 283 | (sequence-generator context (:model chosen-entry) budget)))) |
291 | 284 |
|
292 | | - :cat (let [;budget (max 0 (dec budget)) ; the repeat itself costs 1 |
| 285 | + :cat (let [budget (max 0 (dec budget)) ; the cat itself costs 1 |
293 | 286 | entries (:entries model) |
294 | 287 | min-costs (mapv (comp ::min-cost :model) entries)] |
295 | 288 | (gen/let [budgets (budget-split-gen budget min-costs) |
296 | 289 | sequences (apply gen/tuple |
297 | | - (mapv (fn [entry budget] |
298 | | - (sequence-generator context (:model entry) budget)) |
299 | | - entries |
300 | | - budgets))] |
| 290 | + (mapv (fn [entry budget] |
| 291 | + (sequence-generator context (:model entry) budget)) |
| 292 | + entries |
| 293 | + budgets))] |
301 | 294 | (into [] cat sequences))) |
302 | 295 |
|
303 | | - :repeat (let [;budget (max 0 (dec budget)) ; the repeat itself costs 1 |
| 296 | + :repeat (let [budget (max 0 (dec budget)) ; the repeat itself costs 1 |
304 | 297 | min-repeat (:min model) |
305 | 298 | max-repeat (:max model) |
306 | 299 | elements-model (:elements-model model) |
307 | | - elm-min-cost (::min-cost elements-model 1) |
| 300 | + elm-min-cost (::min-cost elements-model) |
308 | 301 | coll-max-size (-> (int (/ budget elm-min-cost)) |
309 | 302 | (min max-repeat))] |
310 | 303 | (gen/let [n-repeat (gen/fmap (fn [size] (+ min-repeat size)) |
|
348 | 341 | :set-of (let [budget (max 0 (dec budget)) ; the collection itself costs 1 |
349 | 342 | elements-model (:elements-model model) |
350 | 343 | count-model (:count-model model) |
351 | | - elm-min-cost (::min-cost elements-model 1) |
| 344 | + elm-min-cost (::min-cost elements-model) |
352 | 345 | coll-sizes-gen (if count-model |
353 | 346 | (if (= (:type count-model) :enum) |
354 | 347 | (gen/shuffle (sort (:values count-model))) |
|
388 | 381 | count-model (:count-model model) |
389 | 382 | keys-model (-> model :keys :model) |
390 | 383 | values-model (-> model :values :model) |
391 | | - entry-min-cost (+ (::min-cost keys-model 1) ;; TODO: is the default values needed? |
392 | | - (::min-cost values-model 1)) |
| 384 | + entry-min-cost (+ (::min-cost keys-model) |
| 385 | + (::min-cost values-model)) |
393 | 386 | coll-max-size (int (/ budget entry-min-cost)) |
394 | 387 | coll-size-gen (if count-model |
395 | 388 | (generator context count-model 0) |
|
437 | 430 | entries (:entries model) |
438 | 431 | coll-gen (if entries |
439 | 432 | ; :sequence ... count-model is not used |
440 | | - (gen/bind (budget-split-gen budget (mapv (fn [entry] |
441 | | - (::min-cost entry 1)) |
442 | | - entries)) |
| 433 | + (gen/bind (budget-split-gen budget (mapv (comp ::min-cost :model) entries)) |
443 | 434 | (fn [budgets] |
444 | 435 | (apply gen/tuple (mapv (fn [entry budget] |
445 | 436 | (generator context (:model entry) budget)) |
446 | 437 | entries budgets)))) |
447 | 438 | ; :sequence-of ... count-model and/or elements-model might be used |
448 | 439 | (let [count-model (:count-model model) |
449 | 440 | elements-model (:elements-model model) |
450 | | - elm-min-cost (::min-cost elements-model 1) |
| 441 | + elm-min-cost (::min-cost elements-model) |
451 | 442 | coll-max-size (int (/ budget elm-min-cost)) |
452 | 443 | coll-size-gen (if count-model |
453 | 444 | (generator context count-model 0) |
|
478 | 469 |
|
479 | 470 | (:cat :repeat) (cond->> (gen/bind gen/boolean |
480 | 471 | (fn [random-bool] |
481 | | - (let [budget (max 0 (dec budget)) ; the collection itself costs 1 |
482 | | - gen (sequence-generator context (dissoc model :inlined) budget) |
| 472 | + (let [gen (sequence-generator context (dissoc model :inlined) budget) |
483 | 473 | inside-list? (case (:coll-type model) |
484 | 474 | :list true |
485 | 475 | :vector false |
|
0 commit comments