Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

use clojure.data.generators

  • Loading branch information...
commit 4d384cf40f29f8a044a90f5711e11564218715ac 1 parent ef0e19c
Stuart Halloway authored November 17, 2012
5  pom.xml
@@ -57,6 +57,11 @@
57 57
       <version>0.1.1</version>
58 58
     </dependency>
59 59
     <dependency>
  60
+      <groupId>org.clojure</groupId>
  61
+      <artifactId>data.generators</artifactId>
  62
+      <version>0.1.0</version>
  63
+    </dependency>
  64
+    <dependency>
60 65
       <groupId>ch.qos.logback</groupId>
61 66
       <artifactId>logback-classic</artifactId>
62 67
       <version>1.0.6</version>
2  src/examples/clojure/clojure/test/array_test.clj
@@ -9,7 +9,7 @@
9 9
 
10 10
 (ns clojure.test.array-test
11 11
   (:use clojure.test.generative)
12  
-  (:require [clojure.test.generative.generators :as gen]))
  12
+  (:require [clojure.data.generators :as gen]))
13 13
 
14 14
 (defn nan-or-=
15 15
   [a b]
2  src/examples/clojure/clojure/test/core_test.clj
@@ -9,7 +9,7 @@
9 9
 
10 10
 (ns clojure.test.core-test
11 11
   (:use clojure.test.generative clojure.pprint clojure.data clojure.walk)
12  
-  (:require [clojure.test.generative.generators :as gen]))
  12
+  (:require [clojure.data.generators :as gen]))
13 13
 
14 14
 (def ^:private failed-diff-ref (atom nil))
15 15
 
2  src/examples/clojure/clojure/test/hash_test.clj
@@ -9,7 +9,7 @@
9 9
 
10 10
 (ns clojure.test.hash-test
11 11
   (:use clojure.test.generative)
12  
-  (:require [clojure.test.generative.generators :as gen]))
  12
+  (:require [clojure.data.generators :as gen]))
13 13
 
14 14
 (defn hash-obj
15 15
   [h]
2  src/examples/clojure/clojure/test/math_test.clj
@@ -9,7 +9,7 @@
9 9
 
10 10
 (ns clojure.test.math-test
11 11
   (:use clojure.test.generative)
12  
-  (:require [clojure.test.generative.generators :as gen]))
  12
+  (:require [clojure.data.generators :as gen]))
13 13
 
14 14
 (defn integer
15 15
   "Distribution of integers biased towards the small, but
4  src/main/clojure/clojure/test/generative.clj
@@ -14,14 +14,14 @@
14 14
 
15 15
 (defn- fully-qualified
16 16
   "Qualify a name used in :tag metadata. Unqualified names are
17  
-   interpreted in the 'clojure.test.generative.generators, except
  17
+   interpreted in the 'clojure.data.generators, except
18 18
    for the fn-building symbols fn and fn*."
19 19
   [n]
20 20
   (let [ns (cond
21 21
             (#{'fn*} n) nil
22 22
             (#{'fn} n) 'clojure.core
23 23
             (namespace n) (namespace n)
24  
-            :else 'clojure.test.generative.generators)]
  24
+            :else 'clojure.data.generators)]
25 25
     (if ns
26 26
       (symbol (str ns) (name n))
27 27
       n)))
300  src/main/clojure/clojure/test/generative/generators.clj
... ...
@@ -1,300 +0,0 @@
1  
-;   Copyright (c) Rich Hickey, Stuart Halloway, and contributors.
2  
-;   All rights reserved.
3  
-;   The use and distribution terms for this software are covered by the
4  
-;   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
5  
-;   which can be found in the file epl-v10.html at the root of this distribution.
6  
-;   By using this software in any fashion, you are agreeing to be bound by
7  
-;   the terms of this license.
8  
-;   You must not remove this notice, or any other, from this software.
9  
-
10  
-(ns ^{:author "Stuart Halloway"
11  
-      :doc "Generators for clojure.test.generative.
12  
-
13  
-The functions in this namespace define the DSL for generator
14  
-specs. You do not want to use this namespace directly in most
15  
-cases, because
16  
-
17  
-1. Spec compilation uses these fns for you automatically.
18  
-2. There are lot of collisions with clojure.core!"}
19  
-  clojure.test.generative.generators
20  
-  (:refer-clojure :exclude [byte char long int short float double boolean string symbol keyword list vec set hash-map name rand-nth byte-array boolean-array short-array char-array int-array long-array float-array double-array shuffle])
21  
-  (:require [clojure.core :as core]))
22  
-
23  
-(def ^:dynamic ^java.util.Random
24  
-     *rnd*
25  
-     "Random instance for use in generators. By consistently using this
26  
-instance you can get a repeatable basis for tests."
27  
-     (java.util.Random. 42))
28  
-
29  
-(def ^:dynamic
30  
-  *seed*
31  
-  "Seed for random generator, rebind this to change your basis."
32  
-  42)
33  
-
34  
-(defn- call-through
35  
-  "Recursively call x until it doesn't return a function."
36  
-  [x]
37  
-  (if (fn? x)
38  
-    (recur (x))
39  
-    x))
40  
-
41  
-(defn reps
42  
-  "Returns sizer repetitions of f (or (f) if f is a fn)."
43  
-  [sizer f]
44  
-  (let [count (call-through sizer)]
45  
-    (if (fn? f)
46  
-      (repeatedly count f)
47  
-      (repeat count f))))
48  
-
49  
-(defn geometric
50  
-  "Geometric distribution with mean 1/p."
51  
-  ^long [p]
52  
-  (core/long (Math/ceil (/ (Math/log (.nextDouble *rnd*))
53  
-                           (Math/log (- 1.0 p))))))
54  
-
55  
-(defn uniform
56  
-  "Uniform distribution from lo (inclusive) to high (exclusive).
57  
-   Defaults to range of Java long."
58  
-  (^long [] (.nextLong *rnd*))
59  
-  (^long[lo hi] {:pre [(< lo hi)]}
60  
-         (clojure.core/long (Math/floor (+ lo (* (.nextDouble *rnd*) (- hi lo)))))))
61  
-
62  
-(defn float
63  
-  "Generate a float."
64  
-  ^double []
65  
-  (Float/intBitsToFloat (.nextInt *rnd*)))
66  
-
67  
-(defn double
68  
-  "Generate a double."
69  
-  ^double []
70  
-  (.nextDouble *rnd*))
71  
-
72  
-(defn rand-nth
73  
-  "Replacement of core/rand-nth that allows control of the
74  
-   randomization basis (through binding *seed*)."
75  
-  [coll]
76  
-  (nth coll (uniform 0 (count coll))))
77  
-
78  
-(defn tuple
79  
-  "Generate a tuple with one element from each generator."
80  
-  [& generators]
81  
-  (into [] (map #(%) generators)))
82  
-
83  
-(defn weighted
84  
-  "Given a map of generators and weights, return a value from one of
85  
-   the generators, selecting generator based on weights."
86  
-  [m]
87  
-  (let [weights   (reductions + (vals m))
88  
-        total   (last weights)
89  
-        choices (map vector (keys m) weights)]
90  
-    (let [choice (uniform 0 total)]
91  
-      (loop [[[c w] & more] choices]
92  
-        (when w
93  
-          (if (< choice w)
94  
-            (call-through c)
95  
-            (recur more)))))))
96  
-
97  
-(defn one-of
98  
-  "Generates one of the specs passed in, with equal probability."
99  
-  ([& specs]
100  
-     (weighted (zipmap specs (repeat 1)))))
101  
-
102  
-(def long
103  
-  "Returns a random long. Same as uniform."
104  
-  uniform)
105  
-
106  
-(defn int
107  
-  []
108  
-  "Returns a random long in the int range."
109  
-  (uniform Integer/MIN_VALUE (inc Integer/MAX_VALUE)))
110  
-
111  
-(defn short
112  
-  []
113  
-  "Returns a random long in the short range."
114  
-  (uniform Short/MIN_VALUE (inc (core/long Short/MAX_VALUE))))
115  
-
116  
-(defn byte
117  
-  "Returns a random long in the byte range."
118  
-  ^long []
119  
-  (uniform Byte/MIN_VALUE (inc (core/int Byte/MAX_VALUE))))
120  
-
121  
-(defn boolean
122  
-  
123  
-  "Returns a random bool."
124  
-  []
125  
-  (< (.nextDouble *rnd*) 0.5))
126  
-
127  
-(defn printable-ascii-char
128  
-  "Returns a random printable ascii character."
129  
-  []
130  
-  (core/char (uniform 32 127)))
131  
-
132  
-(defn char
133  
-  "Returns a random character in the range 0-65536."
134  
-  []
135  
-  (core/char (uniform 0 65536)))
136  
-
137  
-(defn default-sizer
138  
-  "Default sizer used to run tests. If you want a specific distribution,
139  
-   create your own and pass it to a fn that wants a sizer."
140  
-  []
141  
-  (dec (geometric 0.02)))
142  
-
143  
-(defn list
144  
-  "Create a list with elements from f and sized from sizer."
145  
-  ([f] (list f default-sizer))
146  
-  ([f sizer]
147  
-     (reps sizer f)))
148  
-
149  
-(defmacro primitive-array
150  
-  [type]
151  
-  (let [fn-name (core/symbol (str type "-array"))
152  
-        factory-name (core/symbol (str "core/" fn-name))
153  
-        cast-name (core/symbol (str "core/" type))]
154  
-    `(defn ~fn-name
155  
-       "Create an array with elements from f and sized from sizer."
156  
-       ([~'f]
157  
-          (~fn-name ~'f default-sizer))
158  
-       ([~'f ~'sizer]
159  
-          (let [~'arr (~factory-name (call-through ~'sizer))]
160  
-            (dotimes [~'i (count ~'arr)]
161  
-              (aset ~'arr ~'i (~cast-name (call-through ~'f))))
162  
-            ~'arr)))))
163  
-
164  
-(defmacro primitive-arrays
165  
-  [types]
166  
-  `(do ~@(map (fn [type] `(primitive-array ~type)) types)))
167  
-
168  
-(primitive-arrays ["byte" "short" "long" "char" "double" "float" "int" "boolean"])
169  
-
170  
-#_(defn byte-array
171  
-      ([f]
172  
-         (byte-array f default-sizer))
173  
-      ([f sizer]
174  
-         (let [arr (core/byte-array (call-through default-sizer))]
175  
-           (dotimes [i (count arr)]
176  
-             (aset arr i (core/byte (call-through f))))
177  
-           arr)))
178  
-
179  
-(defn vec
180  
-  "Create a vec with elements from f and sized from sizer."
181  
-  ([f] (vec f default-sizer))
182  
-  ([f sizer]
183  
-     (into [] (reps sizer f))))
184  
-
185  
-(defn set
186  
-  "Create a set with elements from f and sized from sizer."
187  
-  ([f] (set f default-sizer))
188  
-  ([f sizer]
189  
-     (into #{} (reps sizer f))))
190  
-
191  
-(defn hash-map
192  
-  "Create a hash-map with keys from fk, vals from fv, and
193  
-   sized from sizer."
194  
-  ([fk fv] (hash-map fk fv default-sizer))
195  
-  ([fk fv sizer]
196  
-     (into {}
197  
-           (zipmap (reps sizer fk)
198  
-                   (reps sizer fv)))))
199  
-
200  
-(defn string
201  
-  "Create a string with chars from v and sized from sizer."
202  
-  ([] (string printable-ascii-char))
203  
-  ([f] (string f default-sizer))
204  
-  ([f sizer] (apply str (reps sizer f))))
205  
-
206  
-(def ^:private ascii-alpha
207  
-  (concat (range 65 (+ 65 26))
208  
-          (range 97 (+ 97 26))))
209  
-
210  
-(def ^:private symbol-start
211  
-  (-> (concat ascii-alpha [\* \+ \! \- \_ \?])
212  
-      core/vec))
213  
-
214  
-(def ^:private symbol-char
215  
-  (into symbol-start [\1 \2 \3 \4 \5 \6 \7 \8 \9 \0]))
216  
-
217  
-;; cannot generate every legal prefix, but at least avoids
218  
-;; the illegal "-1"
219  
-(defn- name-prefix
220  
-  []
221  
-  (str (rand-nth [(core/char (rand-nth symbol-start)) ""])
222  
-       (core/char (rand-nth ascii-alpha))))
223  
-
224  
-(defn- name-body
225  
-  [sizer]
226  
-  (string #(core/char (rand-nth symbol-char)) sizer))
227  
-
228  
-(defn- name
229  
-  ([] (name default-sizer))
230  
-  ([sizer]
231  
-     (str (name-prefix)
232  
-          (name-body sizer))))
233  
-
234  
-(defn symbol
235  
-  "Create a symbol sized from sizer."
236  
-  ([] (core/symbol (name)))
237  
-  ([sizer]
238  
-     (core/symbol (name sizer))))
239  
-
240  
-(defn keyword
241  
-  "Create a keyword sized from sizer."
242  
-  ([] (core/keyword (name)))
243  
-  ([sizer]
244  
-     (core/keyword (name sizer))))
245  
-
246  
-(def scalars
247  
-  [(constantly nil)
248  
-   byte
249  
-   long
250  
-   boolean
251  
-   printable-ascii-char
252  
-   string
253  
-   symbol
254  
-   keyword])
255  
-
256  
-(defn scalar
257  
-  "Returns a random scalar."
258  
-  []
259  
-  (call-through (rand-nth scalars)))
260  
-
261  
-(def collections
262  
-  [[vec [scalars]]
263  
-   [set [scalars]]
264  
-   [hash-map [scalars scalars]]])
265  
-
266  
-(defn collection
267  
-  "Returns a random collection of scalar elements."
268  
-  []
269  
-  (let [[coll args] (rand-nth collections)]
270  
-    (apply coll (map rand-nth args))))
271  
-
272  
-(defn anything
273  
-  "Returns a random scalar or collection."
274  
-  []
275  
-  (one-of scalar collection))
276  
-
277  
-(defn ^:private fisher-yates
278  
-  "http://en.wikipedia.org/wiki/Fisher–Yates_shuffle#The_modern_algorithm"
279  
-  [coll]
280  
-  (let [as (object-array coll)]
281  
-    (loop [i (dec (count as))]
282  
-      (if (<= 1 i)
283  
-        (let [j (uniform 0 (inc i))
284  
-              t (aget as i)]
285  
-          (aset as i (aget as j))
286  
-          (aset as j t)
287  
-          (recur (dec i)))
288  
-        (into (empty coll) (seq as))))))
289  
-
290  
-(defn shuffle
291  
-  "Shuffle coll"
292  
-  [coll]
293  
-  ;; using our own fischer-yates instead of core/shuffle so that
294  
-  ;; we'll get the same shuffle, given the same random *seed*.
295  
-  (fisher-yates coll))
296  
-
297  
-
298  
-
299  
-
300  
-
4  src/main/clojure/clojure/test/generative/runner.clj
@@ -14,7 +14,7 @@
14 14
    [clojure.tools.namespace :as ns]
15 15
    [clojure.test.generative.config :as config]
16 16
    [clojure.test.generative.event :as event]
17  
-   [clojure.test.generative.generators :as gen]
  17
+   [clojure.data.generators :as gen]
18 18
    [clojure.test.generative.io :as io]
19 19
    [clojure.test :as ctest]))
20 20
 
@@ -146,7 +146,7 @@
146 146
                                               :name name)))
147 147
                             (event/report :test/test
148 148
                                           :msec (- now start)
149  
-                                          :count (inc iter)
  149
+                                          :count iter
150 150
                                           :tags #{:end :test/inputs-exhausted}
151 151
                                           :test/result (if failed? :test/fail :test/pass)
152 152
                                           :level (if failed? :warn :info)
2  src/test/clojure/clojure/test/generative/generators_test.clj
... ...
@@ -1,7 +1,7 @@
1 1
 (ns clojure.test.generative.generators-test
2 2
   (:use clojure.test.generative
3 3
         [clojure.test :only (deftest) :as ct])
4  
-  (:require [clojure.test.generative.generators :as gen]))
  4
+  (:require [clojure.data.generators :as gen]))
5 5
 
6 6
 (defspec test-repeatable-generation
7 7
   (constantly nil)
2  src/test/clojure/clojure/test/generative_test.clj
@@ -13,7 +13,7 @@
13 13
 
14 14
 (deftest test-fully-qualified
15 15
   (is (= 'a/b (@#'test/fully-qualified 'a/b)))
16  
-  (is (= 'clojure.test.generative.generators/long (@#'test/fully-qualified 'long))))
  16
+  (is (= 'clojure.data.generators/long (@#'test/fully-qualified 'long))))
17 17
 
18 18
 
19 19
 

0 notes on commit 4d384cf

Please sign in to comment.
Something went wrong with that request. Please try again.