Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
frenchy64 committed Nov 25, 2024
1 parent 67c439c commit 2f0a1c5
Showing 1 changed file with 16 additions and 28 deletions.
44 changes: 16 additions & 28 deletions src/malli/generator.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
:max (or gen-max max)}))

(defn- -solve-each [f {::keys [solutions] :as options}]
(gen-one-of (mapv #(f % (assoc options ::solutions [%])) (or solutions [{}])) options))
(gen-one-of (into [] (keep #(f % (assoc options ::solutions [%]))) (or solutions [{}])) options))

;; double/long bounds coincide with test.check
(def ^:private -max-double #?(:clj Double/MAX_VALUE :cljs (.-MAX_VALUE js/Number)))
Expand Down Expand Up @@ -122,40 +122,28 @@
(update :NaN? #(if (some? %) % false)))))

(defn- -double-gen* [goptions options]
(-solve-each
(fn [solution options]
(let [goptions (-with-number-bounds goptions solution)]
(or (some-> (-reachable-double*-options goptions) gen/double*)
(-never-gen options))))
options))
(-solve-each (fn [solution options]
(some-> goptions (-with-number-bounds solution) -reachable-double*-options gen/double*))
options))

(defn- -reachable-large-integer*-options [{:keys [min max] :as goptions}]
(when (-finite-bounds-between? min max -min-long -max-long)
(cond-> (or goptions {})
;; {:min 1.5} => {:min 2}
min (assoc :min (long (math/ceil min)))
;; {:max 1.5} => {:max 1}
max (assoc :max (long (math/floor max))))))
(let [imin (some-> min math/ceil long)
imax (some-> max math/floor long)]
(when (-finite-bounds-between? imin imax min max)
(cond-> (or goptions {})
imin (assoc :min imin)
imax (assoc :max imax))))))

(defn- -int-gen* [goptions options]
(-solve-each
(fn [solution options]
(let [goptions (-with-number-bounds goptions solution)]
(or (some-> (-reachable-large-integer*-options goptions) gen/large-integer*)
(-never-gen options))))
options))
(-solve-each (fn [solution options]
(some-> goptions (-with-number-bounds solution) -reachable-large-integer*-options gen/large-integer*))
options))

(defn- -number-gen* [goptions options]
(-solve-each
(fn [solution options]
(let [gen-type (fn gen-type [stype]
(case stype
(nil :number) (gen-one-of (mapv gen-type [:int :double]) options)
:int (-int-gen* goptions options)
:double (-double-gen* goptions options)
(-never-gen options)))]
(gen-type (:type solution))))
options))
(gen-one-of [(-int-gen* goptions options)
(-double-gen* goptions options)]
options))

(defn- gen-vector-min [gen min options]
(cond-> (gen/sized #(gen/vector gen min (+ min %)))
Expand Down

0 comments on commit 2f0a1c5

Please sign in to comment.