### Clojure Nested Collection Size Example Source: https://github.com/clojure/test.check/blob/master/doc/growth-and-shrinking.md Illustrates the potential for very large output sizes when generating nested collections with default settings. This example shows the maximum size of nested vectors of integers generated by `gen/sample`. ```clojure (defn max-size [colls] (->> colls (map flatten) (map count) (apply max))) (-> gen/nat (gen/vector) (gen/vector) (gen/sample 200) (max-size)) ``` ```clojure (-> gen/nat (gen/vector) (gen/vector) (gen/vector) (gen/sample 200) (max-size)) ``` -------------------------------- ### Clojure gen/resize Example Source: https://github.com/clojure/test.check/blob/master/doc/growth-and-shrinking.md Shows how `gen/resize` fixes a generator to a specific size, overriding the current size context. The output string indicates the generated value and the fixed size used. ```clojure (def g (gen/sized (fn [size] (gen/let [x (gen/resize 100 gen/large-integer)] (format "I generated %d, even though size=%d!" x size))))) (gen/sample g) ``` -------------------------------- ### Clojure gen/sized Example Source: https://github.com/clojure/test.check/blob/master/doc/growth-and-shrinking.md Demonstrates how `gen/sized` allows a generator to access the current size parameter during generation. The generated string includes the value and the size used. ```clojure (def g (gen/sized (fn [size] (gen/let [x gen/large-integer] (format "I generated %d using size=%d!" x size))))) (gen/sample g) ``` -------------------------------- ### Define and Run a Property Test Source: https://github.com/clojure/test.check/blob/master/doc/intro.md Defines a property for a sort function and runs it using quick-check. This example demonstrates checking if sorting a vector preserves its count and order. ```clojure (require '[clojure.test.check :as tc] '[clojure.test.check.generators :as gen] '[clojure.test.check.properties :as prop #?@(:cljs [:include-macros true])]) (def property (prop/for-all [v (gen/vector gen/small-integer)] (let [s (sort v)] (and (= (count v) (count s)) (or (empty? s) (apply <= s)))))) ;; test our property (tc/quick-check 100 property) ``` -------------------------------- ### Clojure Quick Check with Scaled Generator Source: https://github.com/clojure/test.check/blob/master/doc/growth-and-shrinking.md Demonstrates using `gen/scale` to adjust the size progression for `quick-check`, ensuring better coverage when running fewer trials. This example scales the generator to use sizes 0, 20, 40, etc. ```clojure ;; uses sizes 0,20,40,60,80,100,120,140,160,180 (tc/quick-check 10 (prop/for-all [x (gen/scale #(* 20 %) g)] (f x))) ``` -------------------------------- ### Sample Small Integers Source: https://github.com/clojure/test.check/blob/master/doc/intro.md Generates and displays a sample of small integers using the gen/small-integer generator. This shows how to get random values for a specific type. ```clojure (require '[clojure.test.check.generators :as gen]) (gen/sample gen/small-integer) ``` -------------------------------- ### Generate Samples from a Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Use `gen/sample` to get a specified number of smallish values from a generator. If the number of samples is omitted, it defaults to 10. ```clojure (gen/sample g) ``` ```clojure (gen/sample g n) ``` -------------------------------- ### Clojure gen/scale Example Source: https://github.com/clojure/test.check/blob/master/doc/growth-and-shrinking.md Illustrates `gen/scale` for modifying the size seen by a generator, useful for controlling nested collection sizes or scaling test counts. This example scales vector elements and the vector size itself. ```clojure (def gen-small-vectors-of-large-numbers (gen/scale #(max 0 (Math/log %)) (gen/vector (gen/scale #(* % 100) gen/large-integer)))) (gen/sample gen-small-vectors-of-large-numbers 20) ``` -------------------------------- ### Generate strictly increasing integers Source: https://github.com/clojure/test.check/blob/master/doc/growth-and-shrinking.md This snippet shows how to generate strictly increasing integers using gen/bind. It takes a natural number as input and generates a sequence of strictly increasing integers starting from 0. ```clojure (def gen-strictly-increasing-integers (gen/bind gen/nat #(gen-strictly-increasing-integers* % 0))) ``` -------------------------------- ### Sample Generator Sizing with test.check Source: https://github.com/clojure/test.check/blob/master/doc/growth-and-shrinking.md Demonstrates how the 'size' parameter affects different generators like gen/nat, gen/large-integer, and gen/vector. Shows how collection generators grow both the collection size and element size unless the collection size is fixed. ```clojure (defn sizing-sample [g] (into {} (for [size [0 5 25 200]] [size (repeatedly 5 #(gen/generate g size))]))) ;; with gen/nat, the integer is roughly proportional to the `size` (sizing-sample gen/nat) ;; => {0 (0 0 0 0 0), ;; 5 (4 1 3 3 5), ;; 25 (12 8 24 25 22), ;; 200 (63 143 31 199 7)} ;; with gen/large-integer, the integer can be much larger (sizing-sample gen/large-integer) ;; => {0 (-1 0 -1 -1 -1), ;; 5 (1 6 7 4 2), ;; 25 (-1 55798 23198 -11 6124159), ;; 200 (8371567737 ;; -393642130983883 ;; -56826587109 ;; 114071285698586153 ;; 5723723802814291)} ;; a collection generator grows the collection size and the ;; size of its elements (dissoc (sizing-sample (gen/vector gen/nat)) 200) ;; => {0 ([] [] [] [] []), ;; 5 ([1 0 4 3 4] [3] [] [2 0] [2 2 4]), ;; 25 ([8 10 0 16 7 7 2 19 16 10] ;; [16 8 0 18 11 23 11 7 1 19 10 4 0 23 17 2 17 12 1 20] ;; [15 19 7 6 4] ;; [6 5 14 12 19 12 7 13 17 10 16 6 9 1] ;; [19 16 5 22 2 5 10 3 6 7 22 19 21 10 4 22 23 5 9 21 19 16 2])} ;; unless we fix the size of the collection (sizing-sample (gen/vector gen/large-integer 3)) ;; => {0 ([-1 -1 -1] [-1 0 -1] [-1 0 -1] [0 0 -1] [0 0 -1]), ;; 5 ([-10 -1 -1] [4 -14 15] [2 2 -1] [-3 -7 2] [-2 0 0]), ;; 25 ([-4417 32 189] ;; [12886 576 -2] ;; [0 -2 -89799] ;; [108 -250318 -1218212] ;; [-10 -27 -5]), ;; 200 ([-8526639064861 -44 2311] ;; [819670069072907 -4481451104804003250 -81] ;; [-1985273 781374 -480118376] ;; [-2038 2593 -5355] ;; [143974988 4 209260326382094708])} ;; gen/uuid completely ignores `size` (sizing-sample gen/uuid) ;; => {0 (#uuid "29ec3e6f-e35c-466f-b9d5-fa27e043743d" ;; #uuid "7bb1c53d-0b12-4be0-a2c5-b7d6a406f64f" ;; #uuid "8f07cab1-4e3d-4bd1-a699-6d653b353588" ;; #uuid "b2e65dcb-fad1-4f1e-afe6-5645d1759a9d" ;; #uuid "83d9ca17-cc07-4515-bf22-625e1e537943"), ;; 5 (#uuid "f1a35527-128a-4cda-b9ba-d0fec4255674" ;; #uuid "f7a7f621-5e84-4d09-b3e3-849bebfea048" ;; #uuid "d92aa9f9-7be6-4e02-80a7-ab89d9074b48" ;; #uuid "c5f24f29-1472-454a-9171-34a2d74074b1" ;; #uuid "c14ac2f6-a31a-4c0b-8e50-273feac6dbda"), ;; 25 (#uuid "008a8dcb-11b1-41cc-87b7-5b7b1b704e8e" ;; #uuid "f89c245a-7667-4d2f-9a88-b33c92ad09ca" ;; #uuid "dc209d21-cfbc-4e3e-a338-7a8b146084b4" ;; #uuid "c9585173-a4f5-4f3b-9a98-7eecc4801007" ;; #uuid "b10196fb-9cdb-4148-8d99-dea80be6d7fa"), ;; 200 (#uuid "b9a70100-4b02-404b-9de4-611ad5a2cefe" ;; #uuid "32ca9f24-3248-476a-ab9f-d58c9aa5df9c" ;; #uuid "9de09d09-0121-4ffe-b7a7-37cb95191bcb" ;; #uuid "bcaeeaf8-4225-40d9-a2b4-7ba2c417a3f0" ;; #uuid "6153d4e4-038c-4e73-be0c-2394b3078e25")} ``` -------------------------------- ### Sample Maps with Keywords and Booleans Source: https://github.com/clojure/test.check/blob/master/doc/intro.md Generates and displays a sample of maps with keywords as keys and booleans as values. This shows the gen/map combinator with different generator types. ```clojure (gen/sample (gen/map gen/keyword gen/boolean) 5) ``` -------------------------------- ### Sample from a Generator Sequence Source: https://github.com/clojure/test.check/blob/master/doc/intro.md Takes the first element from a lazy sequence of generated small integers. This shows how to sample values incrementally. ```clojure (take 1 (gen/sample-seq gen/small-integer)) ``` -------------------------------- ### Clojure Integer Generator Samples Source: https://github.com/clojure/test.check/blob/master/doc/growth-and-shrinking.md Provides sample outputs from various Clojure integer generators, highlighting potential issues with bounded ranges and confusing naming conventions. `gen/large-integer` is recommended to avoid size limitations. ```clojure (gen/sample (gen/tuple gen/nat gen/int gen/pos-int gen/neg-int gen/s-pos-int gen/s-neg-int)) ``` -------------------------------- ### Sample Lists of Booleans Source: https://github.com/clojure/test.check/blob/master/doc/intro.md Generates and displays a sample of lists containing booleans. This demonstrates the gen/list combinator. ```clojure (gen/sample (gen/list gen/boolean)) ``` -------------------------------- ### Sample generated even-sized integer collections Source: https://github.com/clojure/test.check/blob/master/doc/growth-and-shrinking.md This snippet shows how to generate sample data using the gen-an-even-number-of-integers generator. It produces a list of collections, each guaranteed to have an even number of integers. ```clojure (gen/sample gen-an-even-number-of-integers) ;; => ([] ;; [-1 -1] ;; [] ;; [] ;; [-1 -4 0 -1 -2 2] ;; [0 2] ;; [6 -1 1 4 8 30 -2 0 21 -2 -1 0] ;; [2 -2] ;; [0 -1 7 -33 9 -49 14 14 1 1 -1 0 1 -2] ;; [0 2 -1 -9]) ``` -------------------------------- ### Generate a collection of an even number of integers Source: https://github.com/clojure/test.check/blob/master/doc/growth-and-shrinking.md This snippet demonstrates a way to generate a collection with an even number of integers using gen/let. It first generates an even number for the length and then uses gen/vector. This approach can lead to suboptimal shrinking. ```clojure (def gen-an-even-number-of-integers (gen/let [even-number (gen/fmap #(* 2 %) gen/nat)] (gen/vector gen/large-integer even-number)) ;; or, rewritten without gen/let: #_ (gen/bind (gen/fmap #(* 2 %) gen/nat) (fn [even-number] (gen/vector gen/large-integer even-number)))) ``` -------------------------------- ### Run a Failing Property Test Source: https://github.com/clojure/test.check/blob/master/doc/intro.md Demonstrates a property that incorrectly assumes input is sorted and runs it with quick-check. This highlights test.check's ability to shrink failing inputs. ```clojure (def bad-property (prop/for-all [v (gen/vector gen/small-integer)] (or (empty? v) (apply <= v)))) (tc/quick-check 100 bad-property) ``` -------------------------------- ### Sample More Small Integers Source: https://github.com/clojure/test.check/blob/master/doc/intro.md Generates and displays a larger sample of small integers using the gen/small-integer generator. This demonstrates requesting a specific number of samples. ```clojure (gen/sample gen/small-integer 20) ``` -------------------------------- ### Sample Tuples of Mixed Types Source: https://github.com/clojure/test.check/blob/master/doc/intro.md Generates and displays a sample of tuples containing a natural number, a boolean, and a ratio. This illustrates the gen/tuple combinator for heterogeneous collections. ```clojure (gen/sample (gen/tuple gen/nat gen/boolean gen/ratio)) ``` -------------------------------- ### Configure defspec with options map Source: https://github.com/clojure/test.check/blob/master/CHANGELOG.markdown Use the `defspec` macro with an optional map argument to set `:num-tests` and `:seed`. ```clojure (defspec run-with-map {:num-tests 1} (prop/for-all* [gen/int] (constantly true))) (defspec run-with-map {:num-tests 1 :seed 1} my-prop) ``` -------------------------------- ### Generate a Vector and a Random Element from It Source: https://github.com/clojure/test.check/blob/master/doc/generator-examples.md Generates a non-empty vector of small integers and then selects a random element from that vector. `gen/bind` is used to chain generators where the output of one influences the next. ```clojure (def vector-and-elem (gen/bind (gen/not-empty (gen/vector gen/small-integer)) #(gen/tuple (gen/return %) (gen/elements %)))) (gen/sample vector-and-elem) ``` -------------------------------- ### Sample Vectors of Natural Numbers Source: https://github.com/clojure/test.check/blob/master/doc/intro.md Generates and displays a sample of vectors containing natural numbers. This illustrates the use of the gen/vector combinator. ```clojure (gen/sample (gen/vector gen/nat)) ``` -------------------------------- ### Update namespace declarations using sed Source: https://github.com/clojure/test.check/blob/master/doc/migrating-from-simple-check.md Automate the renaming of simple-check namespaces to clojure.test.check across your project files. This command modifies .clj files in place, creating .bak backups. ```shell find test -name '*.clj' -print0 | xargs -0 sed -i.bak \ -e 's/simple-check.core/clojure.test.check/' \ -e 's/simple-check/clojure.test.check/' ``` -------------------------------- ### Generate User Records Source: https://github.com/clojure/test.check/blob/master/doc/intro.md Creates a generator for a 'User' record by mapping a tuple of generated arguments (username, ID, email, active status) to the '->User' constructor. Useful for generating complex data structures for testing. ```clojure (def user-gen (gen/fmap (partial apply ->User) (gen/tuple (gen/not-empty gen/string-alphanumeric) gen/nat email-gen gen/boolean))) (last (gen/sample user-gen)) ;; => #user.User{:user-name "kWodcsE2", ;; :user-id 1, ;; :email "r2ed3VE@computer.org", ;; :active? true}) ``` -------------------------------- ### CLI/deps.edn Dependency Source: https://github.com/clojure/test.check/blob/master/README.md Dependency information for using test.check with Clojure's CLI and deps.edn. ```clojure org.clojure/test.check {:mvn/version "1.1.3"} ``` -------------------------------- ### Generate Integers from 5 to 9 Source: https://github.com/clojure/test.check/blob/master/doc/generator-examples.md Generates a sequence of integers within a specified range (inclusive). Use `gen/choose` for this purpose. ```clojure (def five-through-nine (gen/choose 5 9)) (gen/sample five-through-nine) ``` -------------------------------- ### Generate an Even Number of Integers without gen/bind Source: https://github.com/clojure/test.check/blob/master/doc/growth-and-shrinking.md This snippet shows how to generate a vector of integers with an even number of elements using `gen/vector` and `gen/fmap`, avoiding `gen/bind`. It includes a commented-out alternative using `gen/let`. ```clojure (def gen-an-even-number-of-integers (gen/let [xs (gen/vector gen/large-integer)] (cond-> xs (odd? (count xs)) pop)) ;; or, rewritten without gen/let: #_ (gen/fmap (fn [xs] (cond-> xs (odd? (count xs)) pop)) (gen/vector gen/large-integer))) ``` ```clojure (gen/sample gen-an-even-number-of-integers) ``` ```clojure ;; => ([] ;; [] ;; [] ;; [0 0] ;; [-6 0] ;; [0 6] ;; [22 -2 -2 0] ;; [] ;; [0 -2 15 95] ;; [0 -7 -205 -9]) ``` ```clojure ;; => {:result false, ;; :seed 1482064393801, ;; :failing-size 160, ;; :num-tests 161, ;; :fail [[-20 3758 -174 2908907278 7767028 6628657334113049 -7409556399 ;; -3379156667294 -473722 760549137 -7137938397056 124401939 ;; 1590227088 174 482329 4972338 -53955167617312 -237816 ;; 1 -13159175 2 1911311087865 -2675112025 -391133804902 ;; -1444282617174675 1477509406066 138075 -3555024567808 ;; 0 -26579022516 0 5182 -82958251 -1287 -35417824257454314 ;; -129794819488 42 1642761942897 975833887255494324 701657767868417 ;; 3940940 2 458 -1337864380187855428 -6716451 23621121 ;; 1 -826778832808 -2 -137892 6996928807632 -1 -506146269826334582 ;; -23783 -419873644169 3928808977969 0 -3595621317791 ;; -66706208260298 -13099314 10721686280827793 -50904466 ;; -6134528453735 24779423757 -43 1042490490 134213823314 -29]], ;; :shrunk {:total-nodes-visited 115, :depth 68, :result false, :smallest [[42 0]]}} ``` -------------------------------- ### Generate a Sorted Sequence of Integers Source: https://github.com/clojure/test.check/blob/master/doc/generator-examples.md Creates a generator for sorted vectors of small integers. It uses `gen/fmap` to apply the `sort` function to vectors generated by `gen/vector`. ```clojure (def sorted-vec (gen/fmap sort (gen/vector gen/small-integer))) (gen/sample sorted-vec) ``` -------------------------------- ### Update project.clj dependency Source: https://github.com/clojure/test.check/blob/master/doc/migrating-from-simple-check.md Replace the simple-check dependency with test.check in your project.clj file. Ensure you use the correct version numbers for your project. ```clojure ; Replace with: [org.clojure/test.check "0.6.2"] ``` -------------------------------- ### Generate Vector and Select Element Source: https://github.com/clojure/test.check/blob/master/doc/intro.md Creates a generator for a non-empty vector of keywords and then generates a tuple containing a random element from the vector and the vector itself. Useful for testing functions that operate on a collection and one of its elements. ```clojure (def keyword-vector (gen/such-that not-empty (gen/vector gen/keyword))) (def vec-and-elem (gen/bind keyword-vector (fn [v] (gen/tuple (gen/elements v) (gen/return v))))) (gen/sample vec-and-elem 4) ;; => ([:va [:va :b4]] [:Zu1 [:w :Zu1]] [:2 [:2]] [:27X [:27X :KW]]) ``` -------------------------------- ### Generate Integers 90% of the Time, Nil 10% Source: https://github.com/clojure/test.check/blob/master/doc/generator-examples.md Generates small integers with a 90% probability and nil with a 10% probability. `gen/frequency` is used to assign weights to different generators. ```clojure (def mostly-ints (gen/frequency [[9 gen/small-integer] [1 (gen/return nil)]])) (gen/sample mostly-ints) ``` -------------------------------- ### Generate Powers of Two Source: https://github.com/clojure/test.check/blob/master/doc/generator-examples.md Generates powers of two by applying a function to exponents generated by `gen/nat`. This demonstrates using `gen/fmap` for mathematical transformations. ```clojure (def powers-of-two (gen/fmap #(int (Math/pow 2 %)) gen/nat)) (gen/sample powers-of-two) ``` -------------------------------- ### Maven Dependency Source: https://github.com/clojure/test.check/blob/master/README.md Dependency information for using test.check with Maven. ```xml org.clojure test.check 1.1.3 ``` -------------------------------- ### Leiningen Dependency Source: https://github.com/clojure/test.check/blob/master/README.md Dependency information for using test.check with Leiningen. ```clojure [org.clojure/test.check "1.1.3"] ``` -------------------------------- ### scale for Generator Sizing Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Creates a variant of a generator `g` where the size parameter is determined by applying a function `f` to the current size. ```clojure (gen/scale f g) ``` -------------------------------- ### Ratio Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates ratios (which may sometimes be integers). Uses `gen/small-integer` for components. A variant uses `gen/size-bounded-bigint` for larger ratios. ```clojure gen/ratio ``` ```clojure gen/big-ratio ``` -------------------------------- ### resize for Generator Sizing Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Creates a variant of a generator `g` that always uses a specific size `n`. ```clojure (gen/resize n g) ``` -------------------------------- ### let Combinator for Generators Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Use `gen/let` for defining generators where bindings depend on generated values, similar to `clojure.core/let`. ```clojure (gen/let [x g] y) ``` -------------------------------- ### Generate an Integer or Nil Source: https://github.com/clojure/test.check/blob/master/doc/generator-examples.md Creates a generator that can produce either a small integer or nil. This is achieved using `gen/one-of` to combine different generator types. ```clojure (def int-or-nil (gen/one-of [gen/small-integer (gen/return nil)])) (gen/sample int-or-nil) ``` -------------------------------- ### Keyword Generators Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates keywords. `gen/keyword` generates unqualified keywords, while `gen/keyword-ns` generates namespaced keywords. ```clojure gen/keyword ``` ```clojure gen/keyword-ns ``` -------------------------------- ### Generate Even, Positive Integers Source: https://github.com/clojure/test.check/blob/master/doc/generator-examples.md Generates even, positive integers by mapping the natural numbers generator. `gen/fmap` applies a function to the output of another generator. ```clojure (def even-and-positive (gen/fmap #(* 2 %) gen/nat)) (gen/sample even-and-positive 20) ``` -------------------------------- ### Property test for collections without the number 42 Source: https://github.com/clojure/test.check/blob/master/doc/growth-and-shrinking.md This snippet demonstrates a property test using quick-check to ensure that no generated collection (using gen-an-even-number-of-integers) contains the number 42. It highlights potential shrinking issues with complex generators. ```clojure (tc/quick-check 10000 (prop/for-all [xs gen-an-even-number-of-integers] (not-any? #{42} xs))) ;; => {:result false, ;; :seed 1482063539636, ;; :failing-size 176, ;; :num-tests 177, ;; :fail [[-92431438766962 63530 -164135493216497125 -3270829858185774102 ;; -260351529 -59352395648111 -4 -17469 -31636041044035 ;; 7336711261875630 -1636343167264 -20912505735276 -23753842660 ;; 13368897139830488 -1 -250220724 24370059524 -8266208340 ;; 949778971431 -2233935110 -10 -226980 -166150097914784515 ;; 1446375390291034 17977873032 -306481593932634684 2 321887 ;; 1535082621176844 24757631603 -15034747392805020 -248163661633 ;; -2272021814312959965 -1045247795284 177163345 13467 ;; -355036687887336641 -4098005768175 -8055 -6317647 133903089 ;; 3881 42630713210694061 -2673915744452669 421802903098966 ;; -34741965 1630280301 231213827 858102836152006 5282 ;; -269037059 -4985695680423 -187884359879 -68958514179 ;; 1356369075861 -1 5701573467 -9 3993 -66360585914444 ;; 1796329244719094 -9139976096708138 -11216 908965 17156900 ;; 5559124946 13403 -2345413999 42 1 -76248253307297 222887742816784 ;; 1274360 -68929 1 -213900 -122103507959521 2767011893757957 ;; -3626024977 84758031 461767131016 -122390014709033 -1052250928741535 ;; 1 383 -575550 -8793837628976134 -540423902910181208 ;; 7896218 -49725987 -68869268253 -470133169 -7407245227931 ;; -2266127667584039 -60700760 7759 14242030181 -565807123157122480 ;; -21599378358624 1000368132 -1 109045164 23447410579428773 ;; 1966123182 949341425 16444393 60598 340542 -187842543295 ;; 3676708478 -236529145197024202 -791408585920527 -3452127625272781 ;; -132208027103 25 -17500698053396417 -3375613232 -88206409961854 ;; -3368 7 -179071081209 16894761949763 -132946664 -30990191248478947 ;; 402283570687771 29732288327985 -6211 -885340544041821 ;; -2134764587 -16103 518432298883356507 -30801 -311015444053486 ;; -52408941698 -2282761018048237612 438556242]], ;; :shrunk {:total-nodes-visited 97, ;; :depth 72, ;; :result false, ;; :smallest [[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;; 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;; 0 0 0 0 0 0 0 0 0 0 0 0 42 0]]}} ``` -------------------------------- ### String Generators Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates strings using various character generators. `gen/string` generates any character, while others use specific ASCII subsets. ```clojure gen/string ``` ```clojure gen/string-ascii ``` ```clojure gen/string-alphanumeric ``` -------------------------------- ### Clojure Generator for Event Sequences Source: https://github.com/clojure/test.check/blob/master/doc/old-confluence-notes.md Defines a generator for sequences of events based on an initial state and a reduce function. Useful for scenarios where event generation depends on the current state. ```clojure (defn gen-events "Given an init-state and a reduce function for determining the current state from a sequence of events, together with a function that takes a state and returns a generator of a new event, returns a generator of sequences of events." [reduce-func init-state state->event-gen] ...) ``` -------------------------------- ### Generate Email Addresses Source: https://github.com/clojure/test.check/blob/master/doc/intro.md Constructs a generator for email addresses by combining a random alphanumeric string with a predefined domain. This is a custom generator for email formats. ```clojure (def domain (gen/elements ["gmail.com" "hotmail.com" "computer.org"])) (def email-gen (gen/fmap (fn [[name domain-name]] (str name "@" domain-name)) (gen/tuple (gen/not-empty gen/string-alphanumeric) domain))) (last (gen/sample email-gen)) ;; => "CW6161Q6@hotmail.com" ``` -------------------------------- ### Heterogeneous Map Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates maps with specified keys and values drawn from corresponding generators. ```clojure (gen/hash-map k1 g1, k2 g2, ...) ``` -------------------------------- ### Generate Sets of Natural Numbers Source: https://github.com/clojure/test.check/blob/master/doc/intro.md Uses fmap to apply the 'set' function to a generated vector of natural numbers. Useful for creating sets with varying elements. ```clojure (gen/sample (gen/fmap set (gen/vector gen/nat))) ;; => (#{} #{1} #{1} #{3} #{0 4} #{1 3 4 5} #{0 6} #{3 4 5 7} #{0 3 4 5 7} #{1 5}) ``` -------------------------------- ### Generate a Single Value from a Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Use `gen/generate` to create a single value from a generator. You can optionally provide a size to control the complexity of the generated value. ```clojure (gen/generate g) ``` ```clojure (gen/generate g size) ``` -------------------------------- ### Symbol Generators Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates symbols. `gen/symbol` generates unqualified symbols, while `gen/symbol-ns` generates namespaced symbols. ```clojure gen/symbol ``` ```clojure gen/symbol-ns ``` -------------------------------- ### frequency Combinator for Generators Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates elements from a list of generators, using specified weights to determine the probability of selecting each generator. ```clojure (gen/frequency [[2 g1] [7 g2] ...]) ``` -------------------------------- ### Byte Array Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates a byte array. ```clojure gen/bytes ``` -------------------------------- ### Any Clojure Value Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates any valid Clojure value. ```clojure gen/any ``` -------------------------------- ### Printable Clojure Value Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates any Clojure value that can be printed. ```clojure gen/any-printable ``` -------------------------------- ### Generate an Integer and a Boolean Source: https://github.com/clojure/test.check/blob/master/doc/generator-examples.md Combines a generator for small integers and a generator for booleans into a single tuple. `gen/tuple` is used to create generators for multiple values. ```clojure (def int-and-boolean (gen/tuple gen/small-integer gen/boolean)) (gen/sample int-and-boolean) ``` -------------------------------- ### Select a Random Element from a Vector Source: https://github.com/clojure/test.check/blob/master/doc/generator-examples.md Picks a random element from a given vector. Useful for generating test data based on a predefined set of options. ```clojure (def languages (gen/elements ["clojure" "haskell" "erlang" "scala" "python"])) (gen/sample languages) ``` -------------------------------- ### Size-Bounded BigInt Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates bigints with a maximum value determined by the size parameter, up to 2^(6*size). ```clojure gen/size-bounded-bigint ``` -------------------------------- ### one-of Combinator for Generators Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Selects and generates elements from a list of provided generators, choosing each generator randomly. ```clojure (gen/one-of [g1 g2 ...]) ``` -------------------------------- ### Sorted Set Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates sorted sets with elements drawn from a single generator, similar to `gen/set`. ```clojure (gen/sorted-set g) ``` -------------------------------- ### Small Integer Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates small integers, including negative values. ```clojure gen/small-integer ``` -------------------------------- ### Distinct Element List by Key Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates lists of elements where uniqueness is determined by applying a key function. Supports options similar to `gen/set`. ```clojure (gen/list-distinct-by key-fn g) ``` -------------------------------- ### Generate Non-Empty Lists Source: https://github.com/clojure/test.check/blob/master/doc/intro.md Filters generated lists to ensure they are not empty using the 'such-that' function with a 'not-empty' predicate. Suitable for scenarios requiring lists with at least one element. ```clojure (gen/sample (gen/such-that not-empty (gen/list gen/boolean))) ;; => ((true) (true) (false) (true false) (false) (true) (false false true true) (false) (true) (false)) ``` -------------------------------- ### Distinct Element List Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates lists of distinct elements drawn from a single generator. Supports options similar to `gen/set`. ```clojure (gen/list-distinct g) ``` -------------------------------- ### Constant Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md The `gen/return` generator always produces the specified constant value. ```clojure (gen/return x) ``` -------------------------------- ### Generate JSON-like Data Structures Source: https://github.com/clojure/test.check/blob/master/doc/intro.md Creates a recursive generator for JSON-like data structures including lists, maps, integers, and booleans. This demonstrates combining multiple compound and scalar generators. ```clojure (def compound (fn [inner-gen] (gen/one-of [(gen/list inner-gen) (gen/map inner-gen inner-gen)]))) (def scalars (gen/one-of [gen/small-integer gen/boolean])) (def my-json-like-thing (gen/recursive-gen compound scalars)) (last (gen/sample my-json-like-thing 20)) ``` -------------------------------- ### Printable Simple Type Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates any printable Clojure value, excluding collections. ```clojure gen/simple-type-printable ``` -------------------------------- ### Homogeneous Map Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates maps with keys from one generator and values from another. Supports options similar to `gen/set`. ```clojure (gen/map key-gen val-gen) ``` -------------------------------- ### Test Sorted Vector First vs. Last Element Source: https://github.com/clojure/test.check/blob/master/doc/intro.md Tests if the first element of a sorted vector is less than the last. This property is designed to fail and demonstrate shrinking. It uses `gen/not-empty` to ensure the vector has elements. ```clojure (def prop-sorted-first-less-than-last (prop/for-all [v (gen/not-empty (gen/vector gen/small-integer))] (let [s (sort v)] (< (first s) (last s))))) (tc/quick-check 100 prop-sorted-first-less-than-last) ``` -------------------------------- ### Heterogeneous Tuple Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates vectors where each element is drawn from a corresponding generator. ```clojure (gen/tuple g1 g2 ...) ``` -------------------------------- ### Integer Choose Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates uniformly distributed integers within a specified inclusive range. ```clojure (gen/choose) ``` -------------------------------- ### Printable and Equatable Clojure Value Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates any Clojure value that is both printable and equatable. ```clojure gen/any-printable-equatable ``` -------------------------------- ### Shuffled Collection Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates a vector containing the elements of the input collection in a random order. ```clojure (gen/shuffle coll) ``` -------------------------------- ### Generate Any Number Except 5 Source: https://github.com/clojure/test.check/blob/master/doc/generator-examples.md Generates small integers, excluding the value 5. `gen/such-that` filters the output of a generator based on a predicate. Use with caution for predicates unlikely to match. ```clojure (def anything-but-five (gen/such-that #(not= % 5) gen/small-integer)) (gen/sample anything-but-five) ``` -------------------------------- ### Character Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates a single character. ```clojure gen/char ``` -------------------------------- ### recursive-gen Combinator for Generators Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates tree-like structures using a container generator and a scalar generator for leaf values. ```clojure (gen/recursive-gen container-gen scalar-gen) ``` -------------------------------- ### Element from Collection Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates a single element chosen uniformly at random from the provided non-empty collection. ```clojure (gen/elements coll) ``` -------------------------------- ### bind Combinator for Generators Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Similar to `gen/fmap`, but used when the function `f` itself returns a generator. It generates values from the generator returned by `f`. ```clojure (gen/bind g f) ``` -------------------------------- ### Define Test Property with clojure.test Source: https://github.com/clojure/test.check/blob/master/doc/intro.md Use `defspec` to define a property that will be run by the `clojure.test` runner. Specify the number of iterations for `test.check` to use. ```clojure (defspec first-element-is-min-after-sorting ;; the name of the test 100 ;; the number of iterations for test.check to test (prop/for-all [v (gen/not-empty (gen/vector gen/small-integer))] (= (apply min v) (first (sort v))))) ``` -------------------------------- ### Printable and Equatable Simple Type Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates any printable and equatable Clojure value, excluding collections. ```clojure gen/simple-type-printable-equatable ``` -------------------------------- ### Small Non-Negative Integer Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates small non-negative integers, often useful for sizes. ```clojure gen/nat ``` -------------------------------- ### Distinct Element Vector by Key Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates vectors of elements where uniqueness is determined by applying a key function. Supports options similar to `gen/set`. ```clojure (gen/vector-distinct-by key-fn g) ``` -------------------------------- ### Double Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates a wide range of double-precision floating-point numbers, including infinities and NaN. Options allow specifying bounds and inclusion of special values. ```clojure gen/double ``` ```clojure (gen/double* {:min x, :max y, :infinite? true, :NaN? true}) ``` -------------------------------- ### fmap Combinator for Generators Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Creates a generator that applies a function `f` to values generated by `g`. This is useful for transforming generated data. ```clojure (gen/fmap f g) ``` -------------------------------- ### Distinct Element Vector Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates vectors of distinct elements drawn from a single generator. Supports options similar to `gen/set`. ```clojure (gen/vector-distinct g) ``` -------------------------------- ### Test Sort Function Idempotency Source: https://github.com/clojure/test.check/blob/master/doc/intro.md Defines a property to test if sorting a vector twice yields the same result as sorting it once. Requires `clojure.test.check` and its generators and properties. ```clojure (require '[clojure.test.check :as tc]) (require '[clojure.test.check.generators :as gen]) (require '[clojure.test.check.properties :as prop #?@(:cljs [:include-macros true])]) (def sort-idempotent-prop (prop/for-all [v (gen/vector gen/small-integer)] (= (sort v) (sort (sort v))))) (tc/quick-check 100 sort-idempotent-prop) ``` -------------------------------- ### Homogeneous List Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates lists where all elements are drawn from a single generator. ```clojure (gen/list g) ``` -------------------------------- ### Byte Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates a single Java Byte value. ```clojure gen/byte ``` -------------------------------- ### Generate Nested Vectors of Booleans Source: https://github.com/clojure/test.check/blob/master/doc/intro.md Defines a recursive generator for nested vectors containing booleans. Use `gen/sample` to generate test data. ```clojure (def nested-vector-of-boolean (gen/recursive-gen gen/vector gen/boolean)) (last (gen/sample nested-vector-of-boolean 20)) ``` -------------------------------- ### Simple Type Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates any Clojure value, excluding collections. ```clojure gen/simple-type ``` -------------------------------- ### ASCII Character Generators Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates specific subsets of ASCII characters: printable, alphanumeric, or alphabetic. ```clojure gen/char-ascii ``` ```clojure gen/char-alphanumeric ``` ```clojure gen/char-alpha ``` -------------------------------- ### no-shrink for Generators Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Creates a variant of a generator `g` that disables the shrinking process for generated values. ```clojure (gen/no-shrink g) ``` -------------------------------- ### such-that Combinator for Generators Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Filters elements from a generator `g` based on a predicate `pred`. An optional `max-tries` argument can limit the number of attempts. ```clojure (gen/such-that pred g) ``` ```clojure (gen/such-that pred g max-tries) ``` -------------------------------- ### UUID Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates uniformly random UUIDs. Note that this generator does not support shrinking. ```clojure gen/uuid ``` -------------------------------- ### Homogeneous Set Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates sets where all elements are drawn from a single generator. Variants allow specifying the number of elements, element range, and maximum tries. ```clojure (gen/set g) ``` ```clojure (gen/set g {:num-elements x, :max-tries 20}) ``` ```clojure (gen/set g {:min-elements x, :max-elements y, :max-tries 20}) ``` -------------------------------- ### Check if a Value is a Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Use `gen/generator?` to determine if a given value is a valid generator. ```clojure (gen/generator? g) ``` -------------------------------- ### Homogeneous Vector Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates vectors where all elements are drawn from a single generator. Variants allow specifying the number of elements or a range for the number of elements. ```clojure (gen/vector g) ``` ```clojure (gen/vector g num-elements) ``` ```clojure (gen/vector g min-elements max-elements) ``` -------------------------------- ### not-empty Combinator for Generators Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Modifies a generator for collections to ensure it never produces empty collections. ```clojure (gen/not-empty g) ``` -------------------------------- ### Large Integer Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates integers within a wide range. A variant allows specifying min and max bounds. ```clojure gen/large-integer ``` ```clojure (gen/large-integer* {:min x, :max y}) ``` -------------------------------- ### Equatable Simple Type Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates any equatable Clojure value, excluding collections. ```clojure gen/simple-type-equatable ``` -------------------------------- ### Equatable Clojure Value Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates any Clojure value that can be compared for equality with another value. ```clojure gen/any-equatable ``` -------------------------------- ### Boolean Generator Source: https://github.com/clojure/test.check/blob/master/doc/cheatsheet.md Generates random boolean values (`true` or `false`). ```clojure gen/boolean ```