Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
experimenting with quickcheck style tests
- Loading branch information
1 parent
ab8b8d2
commit 869dde8
Showing
2 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
(ns circumspec.for-all | ||
(:use [clojure.contrib.seq-utils :only (rand-elt)])) | ||
|
||
(def *size* 100) | ||
|
||
(defmacro defgenerator [sym & forms] | ||
`(def ~sym ~@forms)) | ||
|
||
(defn data-name-for-generator-coll | ||
[generator-sym] | ||
(symbol (str generator-sym "*"))) | ||
|
||
(defmacro generator-coll | ||
[sym coll] | ||
(let [data-name (data-name-for-generator-coll sym)] | ||
`(do | ||
(def ~data-name ~coll) | ||
(defn ~sym [] (rand-elt ~data-name))))) | ||
|
||
(defn choose-from | ||
[& generators] | ||
(fn [] ((rand-elt generators)))) | ||
|
||
(defn list-of | ||
[& generators] | ||
(let [gen (apply choose-from generators)] | ||
(fn [] (take (rand-int *size*) (repeatedly gen))))) | ||
|
||
(defn string-of | ||
[& generators] | ||
(let [gen (apply list-of generators)] | ||
(fn [] (apply str (gen))))) | ||
|
||
(defn symbol-of | ||
[& generators] | ||
(let [gen (apply string-of generators)] | ||
(fn [] (symbol (gen))))) | ||
|
||
(generator-coll famous-string [nil ""]) | ||
(generator-coll famous-whitespace " \t\n") | ||
(generator-coll digits "1234567890") | ||
(generator-coll ascii-upper "ABCDEFGHIJKLMNOPQRSTUVWXYZ") | ||
(generator-coll ascii-lower "abcdefghijklmnopqrstuvwxyz") | ||
(defgenerator ascii-alpha (choose-from ascii-upper ascii-lower)) | ||
|
||
(defmacro for-all | ||
[bindings & body] | ||
`(dotimes [_# *size*] | ||
(let ~bindings | ||
~@body))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters