-
-
Save abrooks/e1ed21f7d59f3bd09339 to your computer and use it in GitHub Desktop.
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
(ns qcheck.core | |
(:require [clojure.test.check :as tc] | |
[clojure.test.check.properties :as prop] | |
[clojure.test.check.generators :as gen])) | |
(defn gen-dag | |
([] (gen-dag [#{}] 10)) | |
([nodes] (gen-dag [#{}] nodes)) | |
([so-far nodes] | |
(gen/bind (gen/frequency [[80 (gen/return 1)] | |
[19 (gen/return 2)] | |
[1 (gen/return 0)]]) | |
(fn [parent-count] | |
(gen/bind (gen/such-that | |
(fn [& parents] | |
(when (not (empty? parents)) | |
(apply distinct? parents))) | |
(apply gen/tuple | |
(repeat (min (count so-far) parent-count) | |
(gen/choose 0 (dec (count so-far)))))) | |
(fn [parents] | |
(if (< 0 nodes) | |
(gen-dag (conj so-far (set parents)) | |
(dec nodes)) | |
(gen/return so-far)))))))) | |
;; qcheck.core> (time (clojure.pprint/pprint (gen/sample (gen-dag)))) | |
;; ([#{} #{} #{0 1} #{1} #{1} #{3} #{1} #{6} #{7 3} #{1 5} #{3 2}] | |
;; [#{} #{0} #{0} #{1} #{2} #{4} #{0} #{5} #{0} #{3} #{9}] | |
;; [#{} #{0} #{1} #{2} #{0} #{4} #{2} #{2} #{4} #{1} #{8}] | |
;; [#{} #{0} #{0} #{0} #{1} #{3} #{2} #{5} #{7} #{7} #{6}] | |
;; [#{} #{0} #{1} #{1} #{0} #{0} #{2} #{5} #{2 5} #{3 8} #{8}] | |
;; [#{} #{0} #{1} #{2} #{2} #{1} #{2} #{3} #{4} #{1 8} #{3}] | |
;; [#{} #{0} #{1} #{1} #{0} #{3} #{0} #{3} #{6} #{4} #{7}] | |
;; [#{} #{0} #{1} #{0} #{1} #{2} #{4} #{4} #{2} #{3} #{2}] | |
;; [#{} #{0} #{0} #{1} #{0 3} #{} #{1 4} #{2} #{2} #{} #{7}] | |
;; [#{} #{0} #{1} #{2} #{3} #{1} #{0} #{6} #{5} #{7} #{7}]) | |
;; "Elapsed time: 55.31 msecs" | |
;; nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment