Commits

Meikel Brandmeyer committed aad6847

Numeric generators

Comments (0)

Files changed (1)

src/main/clojure/clojurecheck/core.clj

     {:type :summary, :test 2, :pass 6, :fail 1, :error 0}"}
   clojurecheck.core
   (:refer-clojure
-    :exclude (int float list vec set sorted-set hash-map sorted-map))
+    :exclude (long int double float
+              list vec
+              set sorted-set
+              hash-map sorted-map))
   (:use clojure.test))
 
 ;; # Interfaces
   Object
   (arbitrary [this size] (arbitrary (constantly this) size)))
 
-(deftype NumberDomain
+;; ## Numeric generators
+;;
+;; The first a little more complex generators are the numeric ones.
+;; We define a common generator type, which handles generating
+;; numeric values based on a custom provided `random` function.
+(deftype NumericGenerator
   [random lower upper]
   Generator
-  (arbitrary
-    [this size]
+  (arbitrary [this size]
     (let [[low high] (if size
                        [(max (- size) lower) (min size upper)]
                        [lower upper])]
-      (DomainValue. this (+ low (random (- high low)))))))
+      (SimpleValue. (+ low (random (- high low)))))))
 
-(defn int
+(defn long
   "Generates a random integral number between lower and upper.
   The interval is limited by the size guidance."
-  {:added "2.0"}
+  {:added "2.1"}
   [& {:keys [lower upper] :or {lower -32768 upper 32767}}]
-  (NumberDomain. rand-int lower upper))
+  (NumericGenerator. rand-int lower upper))
 
-(defn float
+(def #^{:doc   "Alternative name for `long`."
+        :added "2.0"}
+  int
+  long)
+(alter-meta! #'int merge (meta #'long) (meta #'int))
+
+(defn double
   "Generates a random floating point number between lower and upper.
   The interval is limited by the size guidance."
-  {:added "2.0"}
+  {:added "2.1"}
   [& {:keys [lower upper] :or {lower -32768.0 upper 32767.0}}]
-  (NumberDomain. rand lower upper))
+  (NumericGenerator. rand lower upper))
+
+(def #^{:doc   "Alternative name for `double`."
+        :added "2.0"}
+  float
+  double)
+(alter-meta! #'float merge (meta #'double) (meta #'float))
 
 (def #^{:doc "Generates a random boolean value. Ignores the size guidance"
         :added "2.0"}