Skip to content

Commit 85f7565

Browse files
committed
Bug fix and code cleanup.
1 parent 29b4a0c commit 85f7565

File tree

2 files changed

+17
-46
lines changed

2 files changed

+17
-46
lines changed

src/minimallist/generator.cljc

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@
165165
(some-> content-cost (+ container-cost)))
166166
(:set-of
167167
:sequence-of
168-
:repeat) (let [container-cost (if (#{:set-of :sequence-of} type) 1 0)
168+
:repeat) (let [container-cost 1
169169
min-count (min-count-value model)
170170
elements-model (:elements-model model)
171171
elements-model-min-cost (if elements-model
@@ -186,10 +186,7 @@
186186
(reduce max vals)))
187187
(:map
188188
: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
193190
vals (->> (:entries model)
194191
(remove :optional)
195192
(map (comp ::min-cost :model)))
@@ -259,17 +256,13 @@
259256
(gen/fmap (fn [rates]
260257
(let [budget-factor (/ budget-minus-min-costs (reduce + rates))]
261258
(mapv (fn [min-cost rate]
262-
(+ min-cost (* rate budget-factor)))
259+
(+ min-cost (int (* rate budget-factor))))
263260
min-costs
264261
rates)))
265262
(gen/vector (gen/choose 1 100) nb-elements)))
266263
(gen/return [])))
267264

268265

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-
273266
(declare generator)
274267

275268
(defn- sequence-generator
@@ -289,22 +282,22 @@
289282
(let [chosen-entry (first (sort-by (comp ::min-cost :model) possible-entries))]
290283
(sequence-generator context (:model chosen-entry) budget))))
291284

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
293286
entries (:entries model)
294287
min-costs (mapv (comp ::min-cost :model) entries)]
295288
(gen/let [budgets (budget-split-gen budget min-costs)
296289
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))]
301294
(into [] cat sequences)))
302295

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
304297
min-repeat (:min model)
305298
max-repeat (:max model)
306299
elements-model (:elements-model model)
307-
elm-min-cost (::min-cost elements-model 1)
300+
elm-min-cost (::min-cost elements-model)
308301
coll-max-size (-> (int (/ budget elm-min-cost))
309302
(min max-repeat))]
310303
(gen/let [n-repeat (gen/fmap (fn [size] (+ min-repeat size))
@@ -348,7 +341,7 @@
348341
:set-of (let [budget (max 0 (dec budget)) ; the collection itself costs 1
349342
elements-model (:elements-model model)
350343
count-model (:count-model model)
351-
elm-min-cost (::min-cost elements-model 1)
344+
elm-min-cost (::min-cost elements-model)
352345
coll-sizes-gen (if count-model
353346
(if (= (:type count-model) :enum)
354347
(gen/shuffle (sort (:values count-model)))
@@ -388,8 +381,8 @@
388381
count-model (:count-model model)
389382
keys-model (-> model :keys :model)
390383
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))
393386
coll-max-size (int (/ budget entry-min-cost))
394387
coll-size-gen (if count-model
395388
(generator context count-model 0)
@@ -437,17 +430,15 @@
437430
entries (:entries model)
438431
coll-gen (if entries
439432
; :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))
443434
(fn [budgets]
444435
(apply gen/tuple (mapv (fn [entry budget]
445436
(generator context (:model entry) budget))
446437
entries budgets))))
447438
; :sequence-of ... count-model and/or elements-model might be used
448439
(let [count-model (:count-model model)
449440
elements-model (:elements-model model)
450-
elm-min-cost (::min-cost elements-model 1)
441+
elm-min-cost (::min-cost elements-model)
451442
coll-max-size (int (/ budget elm-min-cost))
452443
coll-size-gen (if count-model
453444
(generator context count-model 0)
@@ -478,8 +469,7 @@
478469

479470
(:cat :repeat) (cond->> (gen/bind gen/boolean
480471
(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)
483473
inside-list? (case (:coll-type model)
484474
:list true
485475
:vector false

test/minimallist/generator_test.cljc

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
::mg/min-cost 1}}
187187
{:model {:type :fn
188188
::mg/min-cost 1}}]
189-
::mg/min-cost 2}
189+
::mg/min-cost 3}
190190

191191
(h/in-vector (h/cat (h/fn int?) (h/fn string?)))
192192
{:type :cat
@@ -497,23 +497,4 @@
497497
(h/ref 'node))]
498498
(is (thrown? #?(:clj Exception :cljs js/Object) (tcg/sample (gen model))))))
499499

500-
#_(let [model (h/let ['tree (h/alt [:leaf (-> (h/fn int?)
501-
(h/with-test-check-gen tcg/nat))]
502-
[:branch (h/vector (h/ref 'tree)
503-
(h/ref 'tree))])]
504-
(h/ref 'tree))]
505-
(tcg/sample (gen model)))
506500

507-
#_(let [model (h/let ['tree (h/alt [:leaf (-> (h/fn int?)
508-
(h/with-test-check-gen tcg/nat))]
509-
[:branch (h/vector (h/ref 'tree)
510-
(h/ref 'tree))])]
511-
(h/ref 'tree))
512-
visitor (fn [model stack path]
513-
(-> model
514-
(mg/assoc-leaf-distance-visitor stack path)
515-
(mg/assoc-min-cost-visitor stack path)))
516-
walker (fn [model]
517-
(mg/postwalk model visitor))
518-
walked-model (util/iterate-while-different walker model 100)]
519-
walked-model)

0 commit comments

Comments
 (0)