├── .github └── workflows │ ├── doc-build.yml │ ├── release.yml │ ├── snapshot.yml │ └── test.yml ├── .gitignore ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── bin ├── .gitignore ├── repl └── test.clj ├── data-model.org ├── docs └── test.generative.png ├── epl.html ├── examples └── hello_world.clj ├── pom.xml └── src ├── examples └── clojure │ └── clojure │ └── test │ ├── array_test.clj │ ├── core_test.clj │ └── math_test.clj └── main └── clojure └── clojure └── test ├── generative.clj └── generative └── runner.clj /.github/workflows/doc-build.yml: -------------------------------------------------------------------------------- 1 | name: Build API Docs 2 | 3 | on: 4 | workflow_dispatch: 5 | 6 | jobs: 7 | call-doc-build-workflow: 8 | uses: clojure/build.ci/.github/workflows/doc-build.yml@master 9 | with: 10 | project: clojure/test.generative 11 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release on demand 2 | 3 | on: 4 | workflow_dispatch: 5 | inputs: 6 | releaseVersion: 7 | description: "Version to release" 8 | required: true 9 | snapshotVersion: 10 | description: "Snapshot version after release" 11 | required: true 12 | 13 | jobs: 14 | call-release: 15 | uses: clojure/build.ci/.github/workflows/release.yml@master 16 | with: 17 | releaseVersion: ${{ github.event.inputs.releaseVersion }} 18 | snapshotVersion: ${{ github.event.inputs.snapshotVersion }} 19 | secrets: inherit -------------------------------------------------------------------------------- /.github/workflows/snapshot.yml: -------------------------------------------------------------------------------- 1 | name: Snapshot on demand 2 | 3 | on: [workflow_dispatch] 4 | 5 | jobs: 6 | call-snapshot: 7 | uses: clojure/build.ci/.github/workflows/snapshot.yml@master 8 | secrets: inherit 9 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: [push] 4 | 5 | jobs: 6 | call-test: 7 | uses: clojure/build.ci/.github/workflows/test.yml@master 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | .tg/* 3 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | This is a [Clojure contrib] project. 2 | 3 | Under the Clojure contrib [guidelines], this project cannot accept 4 | pull requests. All patches must be submitted via [JIRA]. 5 | 6 | See [Contributing] on the Clojure website for 7 | more information on how to contribute. 8 | 9 | [Clojure contrib]: https://clojure.org/community/contrib_libs 10 | [Contributing]: https://clojure.org/community/contributing 11 | [JIRA]: https://clojure.atlassian.net/browse/TGEN 12 | [guidelines]: https://clojure.org/community/contrib_howto 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Eclipse Public License - v 1.0 2 | 3 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 4 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 5 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 6 | 7 | 1. DEFINITIONS 8 | 9 | "Contribution" means: 10 | 11 | a) in the case of the initial Contributor, the initial code and documentation 12 | distributed under this Agreement, and 13 | b) in the case of each subsequent Contributor: 14 | i) changes to the Program, and 15 | ii) additions to the Program; 16 | 17 | where such changes and/or additions to the Program originate from and are 18 | distributed by that particular Contributor. A Contribution 'originates' 19 | from a Contributor if it was added to the Program by such Contributor 20 | itself or anyone acting on such Contributor's behalf. Contributions do not 21 | include additions to the Program which: (i) are separate modules of 22 | software distributed in conjunction with the Program under their own 23 | license agreement, and (ii) are not derivative works of the Program. 24 | 25 | "Contributor" means any person or entity that distributes the Program. 26 | 27 | "Licensed Patents" mean patent claims licensable by a Contributor which are 28 | necessarily infringed by the use or sale of its Contribution alone or when 29 | combined with the Program. 30 | 31 | "Program" means the Contributions distributed in accordance with this 32 | Agreement. 33 | 34 | "Recipient" means anyone who receives the Program under this Agreement, 35 | including all Contributors. 36 | 37 | 2. GRANT OF RIGHTS 38 | a) Subject to the terms of this Agreement, each Contributor hereby grants 39 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 40 | reproduce, prepare derivative works of, publicly display, publicly 41 | perform, distribute and sublicense the Contribution of such Contributor, 42 | if any, and such derivative works, in source code and object code form. 43 | b) Subject to the terms of this Agreement, each Contributor hereby grants 44 | Recipient a non-exclusive, worldwide, royalty-free patent license under 45 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 46 | transfer the Contribution of such Contributor, if any, in source code and 47 | object code form. This patent license shall apply to the combination of 48 | the Contribution and the Program if, at the time the Contribution is 49 | added by the Contributor, such addition of the Contribution causes such 50 | combination to be covered by the Licensed Patents. The patent license 51 | shall not apply to any other combinations which include the Contribution. 52 | No hardware per se is licensed hereunder. 53 | c) Recipient understands that although each Contributor grants the licenses 54 | to its Contributions set forth herein, no assurances are provided by any 55 | Contributor that the Program does not infringe the patent or other 56 | intellectual property rights of any other entity. Each Contributor 57 | disclaims any liability to Recipient for claims brought by any other 58 | entity based on infringement of intellectual property rights or 59 | otherwise. As a condition to exercising the rights and licenses granted 60 | hereunder, each Recipient hereby assumes sole responsibility to secure 61 | any other intellectual property rights needed, if any. For example, if a 62 | third party patent license is required to allow Recipient to distribute 63 | the Program, it is Recipient's responsibility to acquire that license 64 | before distributing the Program. 65 | d) Each Contributor represents that to its knowledge it has sufficient 66 | copyright rights in its Contribution, if any, to grant the copyright 67 | license set forth in this Agreement. 68 | 69 | 3. REQUIREMENTS 70 | 71 | A Contributor may choose to distribute the Program in object code form under 72 | its own license agreement, provided that: 73 | 74 | a) it complies with the terms and conditions of this Agreement; and 75 | b) its license agreement: 76 | i) effectively disclaims on behalf of all Contributors all warranties 77 | and conditions, express and implied, including warranties or 78 | conditions of title and non-infringement, and implied warranties or 79 | conditions of merchantability and fitness for a particular purpose; 80 | ii) effectively excludes on behalf of all Contributors all liability for 81 | damages, including direct, indirect, special, incidental and 82 | consequential damages, such as lost profits; 83 | iii) states that any provisions which differ from this Agreement are 84 | offered by that Contributor alone and not by any other party; and 85 | iv) states that source code for the Program is available from such 86 | Contributor, and informs licensees how to obtain it in a reasonable 87 | manner on or through a medium customarily used for software exchange. 88 | 89 | When the Program is made available in source code form: 90 | 91 | a) it must be made available under this Agreement; and 92 | b) a copy of this Agreement must be included with each copy of the Program. 93 | Contributors may not remove or alter any copyright notices contained 94 | within the Program. 95 | 96 | Each Contributor must identify itself as the originator of its Contribution, 97 | if 98 | any, in a manner that reasonably allows subsequent Recipients to identify the 99 | originator of the Contribution. 100 | 101 | 4. COMMERCIAL DISTRIBUTION 102 | 103 | Commercial distributors of software may accept certain responsibilities with 104 | respect to end users, business partners and the like. While this license is 105 | intended to facilitate the commercial use of the Program, the Contributor who 106 | includes the Program in a commercial product offering should do so in a manner 107 | which does not create potential liability for other Contributors. Therefore, 108 | if a Contributor includes the Program in a commercial product offering, such 109 | Contributor ("Commercial Contributor") hereby agrees to defend and indemnify 110 | every other Contributor ("Indemnified Contributor") against any losses, 111 | damages and costs (collectively "Losses") arising from claims, lawsuits and 112 | other legal actions brought by a third party against the Indemnified 113 | Contributor to the extent caused by the acts or omissions of such Commercial 114 | Contributor in connection with its distribution of the Program in a commercial 115 | product offering. The obligations in this section do not apply to any claims 116 | or Losses relating to any actual or alleged intellectual property 117 | infringement. In order to qualify, an Indemnified Contributor must: 118 | a) promptly notify the Commercial Contributor in writing of such claim, and 119 | b) allow the Commercial Contributor to control, and cooperate with the 120 | Commercial Contributor in, the defense and any related settlement 121 | negotiations. The Indemnified Contributor may participate in any such claim at 122 | its own expense. 123 | 124 | For example, a Contributor might include the Program in a commercial product 125 | offering, Product X. That Contributor is then a Commercial Contributor. If 126 | that Commercial Contributor then makes performance claims, or offers 127 | warranties related to Product X, those performance claims and warranties are 128 | such Commercial Contributor's responsibility alone. Under this section, the 129 | Commercial Contributor would have to defend claims against the other 130 | Contributors related to those performance claims and warranties, and if a 131 | court requires any other Contributor to pay any damages as a result, the 132 | Commercial Contributor must pay those damages. 133 | 134 | 5. NO WARRANTY 135 | 136 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN 137 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR 138 | IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, 139 | NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each 140 | Recipient is solely responsible for determining the appropriateness of using 141 | and distributing the Program and assumes all risks associated with its 142 | exercise of rights under this Agreement , including but not limited to the 143 | risks and costs of program errors, compliance with applicable laws, damage to 144 | or loss of data, programs or equipment, and unavailability or interruption of 145 | operations. 146 | 147 | 6. DISCLAIMER OF LIABILITY 148 | 149 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 150 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 151 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 152 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 153 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 154 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 155 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 156 | OF SUCH DAMAGES. 157 | 158 | 7. GENERAL 159 | 160 | If any provision of this Agreement is invalid or unenforceable under 161 | applicable law, it shall not affect the validity or enforceability of the 162 | remainder of the terms of this Agreement, and without further action by the 163 | parties hereto, such provision shall be reformed to the minimum extent 164 | necessary to make such provision valid and enforceable. 165 | 166 | If Recipient institutes patent litigation against any entity (including a 167 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 168 | (excluding combinations of the Program with other software or hardware) 169 | infringes such Recipient's patent(s), then such Recipient's rights granted 170 | under Section 2(b) shall terminate as of the date such litigation is filed. 171 | 172 | All Recipient's rights under this Agreement shall terminate if it fails to 173 | comply with any of the material terms or conditions of this Agreement and does 174 | not cure such failure in a reasonable period of time after becoming aware of 175 | such noncompliance. If all Recipient's rights under this Agreement terminate, 176 | Recipient agrees to cease use and distribution of the Program as soon as 177 | reasonably practicable. However, Recipient's obligations under this Agreement 178 | and any licenses granted by Recipient relating to the Program shall continue 179 | and survive. 180 | 181 | Everyone is permitted to copy and distribute copies of this Agreement, but in 182 | order to avoid inconsistency the Agreement is copyrighted and may only be 183 | modified in the following manner. The Agreement Steward reserves the right to 184 | publish new versions (including revisions) of this Agreement from time to 185 | time. No one other than the Agreement Steward has the right to modify this 186 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 187 | Eclipse Foundation may assign the responsibility to serve as the Agreement 188 | Steward to a suitable separate entity. Each new version of the Agreement will 189 | be given a distinguishing version number. The Program (including 190 | Contributions) may always be distributed subject to the version of the 191 | Agreement under which it was received. In addition, after a new version of the 192 | Agreement is published, Contributor may elect to distribute the Program 193 | (including its Contributions) under the new version. Except as expressly 194 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 195 | licenses to the intellectual property of any Contributor under this Agreement, 196 | whether expressly, by implication, estoppel or otherwise. All rights in the 197 | Program not expressly granted under this Agreement are reserved. 198 | 199 | This Agreement is governed by the laws of the State of New York and the 200 | intellectual property laws of the United States of America. No party to this 201 | Agreement will bring a legal action under this Agreement more than one year 202 | after the cause of action arose. Each party waives its rights to a jury trial in 203 | any resulting litigation. 204 | 205 | 206 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | clojure.test.generative 2 | ======================================== 3 | 4 | Generative test runner. 5 | 6 | Releases and Dependency Information 7 | ======================================== 8 | 9 | Latest stable release: 1.1.0 10 | 11 | * [All Released Versions](https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.clojure%22%20AND%20a%3A%22test.generative%22) 12 | * [Development Snapshot Versions](https://oss.sonatype.org/index.html#nexus-search;gav~org.clojure~test.generative~~~) 13 | 14 | [CLI/`deps.edn`](https://clojure.org/reference/deps_edn) dependency information: 15 | ```clojure 16 | org.clojure/test.generative {:mvn/version "1.1.0"} 17 | ``` 18 | 19 | [Leiningen](https://github.com/technomancy/leiningen) dependency information: 20 | 21 | [org.clojure/test.generative "1.1.0"] 22 | 23 | [Maven](https://maven.apache.org/) dependency information: 24 | 25 | 26 | org.clojure 27 | test.generative 28 | 1.1.0 29 | 30 | 31 | 32 | Example Usages 33 | ======================================== 34 | 35 | A defspec consists of a name, a function to be tested, an input spec, 36 | and a validator: 37 | 38 | (defspec integers-closed-over-addition 39 | (fn [a b] (+' a b)) ;; input fn 40 | [^long a ^long b] ;; input spec 41 | (assert (integer? %))) ;; 0 or more validator forms 42 | 43 | To generate test data, see the fns in the generators namespace. Note 44 | that these functions shadow a bunch of clojure.core names. 45 | 46 | You can also create the underlying test data structures directly, 47 | marking vars with :clojure.test.generative/specs so they are picked up 48 | by the runner. This can be useful e.g. to model relationships between 49 | input parameters, or to specify a finite list of special cases. The 50 | example below specifies five specific one-argument arg lists: 51 | 52 | (def ^::tgen/specs 53 | inc'-doesnt-overflow-specs 54 | [{:test 'clojure.test.math-test/inc'-doesnt-overflow 55 | :input-gen #(map vector [Long/MIN_VALUE -1 0 1 Long/MAX_VALUE])}]) 56 | 57 | 58 | Running Interactively During Development 59 | ======================================== 60 | 61 | Specify the number of threads, the number of msec, and one or more 62 | vars to test: 63 | 64 | (runner/run 2 1000 #'my/test-var) 65 | 66 | 67 | Running in a CI environment 68 | ======================================== 69 | 70 | Wire your runner to call runner/-main with a list of directories where 71 | tests can be found. You can see bin/test.clj for an example. 72 | 73 | You can use the following system properties to control the intensity 74 | of your test 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 |
Java PropertyInterpretation
clojure.test.generative.threadsNumber of concurrent threads
clojure.test.generative.msecDesired test run duration
87 | 88 | Developer Information 89 | ======================================== 90 | 91 | * [GitHub project](https://github.com/clojure/test.generative) 92 | * [Bug Tracker](https://clojure.atlassian.net/browse/TGEN) 93 | * [Continuous Integration](https://github.com/clojure/test.generative/actions/workflows/test.yml) 94 | 95 | Related Projects 96 | ======================================== 97 | 98 | * [ClojureCheck](https://bitbucket.org/kotarak/clojurecheck) adds 99 | property based testing to clojure.test following the lines of 100 | [QuickCheck](https://en.wikipedia.org/wiki/QuickCheck) for Haskell. 101 | * [simple-check](https://github.com/reiddraper/simple-check) is a 102 | Clojure property-based testing tool inspired by QuickCheck. 103 | 104 | Copyright and License 105 | ======================================== 106 | 107 | Copyright (c) Rich Hickey. All rights reserved. The use and distribution terms for this software are covered by the Eclipse Public License 1.0 (https://opensource.org/license/epl-1-0/) which can be found in the file epl-v10.html at the root of this distribution. By using this software in any fashion, you are agreeing to be bound bythe terms of this license. You must not remove this notice, or any other, from this software. 108 | -------------------------------------------------------------------------------- /bin/.gitignore: -------------------------------------------------------------------------------- 1 | maven-classpath 2 | -------------------------------------------------------------------------------- /bin/repl: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Note: First you must run mvn dependency:build-classpath -Dmdep.outputFile=bin/maven-classpath 3 | set -e 4 | 5 | CLASSPATH=src/main/clojure:src/test/clojure:src/examples/clojure:`cat bin/maven-classpath` 6 | 7 | java -server -Xmx2G $CTG_JAVA_OPTS -cp $CLASSPATH clojure.main "$@" 8 | 9 | 10 | -------------------------------------------------------------------------------- /bin/test.clj: -------------------------------------------------------------------------------- 1 | (require '[clojure.test.generative.runner :as runner]) 2 | (runner/-main "src/test/clojure" "src/examples/clojure") 3 | 4 | -------------------------------------------------------------------------------- /data-model.org: -------------------------------------------------------------------------------- 1 | * Common Event Keys 2 | | name | type | notes | 3 | |-------------+--------------------+----------------------------------------| 4 | | tstamp | long | msec since epoch | 5 | | thread | long | thread id | 6 | | pid | long | process id | 7 | | level | keyword enum | severiry level | 8 | | type | namespaced keyword | drive extended interpretation | 9 | | locals | map | local bindings | 10 | | file | string | file path | 11 | | ns | symbol | namespace | 12 | | line | long | line number | 13 | | bindings | map | dynamic bindings | 14 | | thread/name | string | name of a thread | 15 | | msec | long | a duration | 16 | | nsec | long | a duration | 17 | | count | long | a count | 18 | | tag | set of keyword | | 19 | | args | list of any | args to a fn | 20 | | return | any | return value of a fun | 21 | | exception | exception | an exception | 22 | | name | symbol | a name | 23 | | description | string | a non-name characterization of a thing | 24 | | uuid | uuid | a unique name | 25 | | message | string | a message | 26 | * Comment Event Types 27 | | type | notes | 28 | |-------+------------| 29 | | error | runtime error | 30 | * Test Event Keys 31 | | name | type | notes | 32 | |---------------+--------+----------------------| 33 | | test/count | long | number of tests | 34 | | test/threads | long | number of threads | 35 | | test/seed | long | numeric seed | 36 | | text/expected | any | | 37 | | test/actual | any | | 38 | | test/library | symbol | name of test library | 39 | * Test Events in c.t.g 40 | | name | begin/end tags? | count | args | test/threads | msec | context | level | 41 | |--------------+-----------------+-------+------+--------------+------+---------+-------| 42 | | :test/group | x | x | | | | | info | 43 | | :test/iter | | | x | | | | info | 44 | | :test/test | x | x | | | x | | info | 45 | | :test/fail | | | | | | x | warn | 46 | | :test/pass | | | | | | | info | 47 | | :assert/pass | | | | | | | debug | 48 | | :assert/fail | | | | | | | debug | 49 | * Keys particular to c.t. 50 | | name | type | 51 | |--------------+----------------| 52 | | c.t/vars | list of symbol | 53 | | c.t/contexts | list of string | 54 | * Test Events in c.t 55 | | name | becomes | other keys | 56 | |---------------------+-------------------------+--------------------------------------------------| 57 | | :pass | :assert/pass | | 58 | | :fail | :assert/fail | :expected, :actual, :message, *testing-contexts* | 59 | | :error | :error | :expected, :actual, :message, *testing-contexts* | 60 | | :summary | (ignore, do own calc) | | 61 | | :begin/end-test-ns | :test/group :begin/:end | :ns -> name | 62 | | :begin/end-test-var | :test/test :begin/:end | :var -> name | 63 | -------------------------------------------------------------------------------- /docs/test.generative.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clojure/test.generative/8142c4854dd48f24f764f6e875db6bd23eb655ce/docs/test.generative.png -------------------------------------------------------------------------------- /epl.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Eclipse Public License - Version 1.0 8 | 25 | 26 | 27 | 28 | 29 | 30 |

Eclipse Public License - v 1.0

31 | 32 |

THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE 33 | PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR 34 | DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS 35 | AGREEMENT.

36 | 37 |

1. DEFINITIONS

38 | 39 |

"Contribution" means:

40 | 41 |

a) in the case of the initial Contributor, the initial 42 | code and documentation distributed under this Agreement, and

43 |

b) in the case of each subsequent Contributor:

44 |

i) changes to the Program, and

45 |

ii) additions to the Program;

46 |

where such changes and/or additions to the Program 47 | originate from and are distributed by that particular Contributor. A 48 | Contribution 'originates' from a Contributor if it was added to the 49 | Program by such Contributor itself or anyone acting on such 50 | Contributor's behalf. Contributions do not include additions to the 51 | Program which: (i) are separate modules of software distributed in 52 | conjunction with the Program under their own license agreement, and (ii) 53 | are not derivative works of the Program.

54 | 55 |

"Contributor" means any person or entity that distributes 56 | the Program.

57 | 58 |

"Licensed Patents" mean patent claims licensable by a 59 | Contributor which are necessarily infringed by the use or sale of its 60 | Contribution alone or when combined with the Program.

61 | 62 |

"Program" means the Contributions distributed in accordance 63 | with this Agreement.

64 | 65 |

"Recipient" means anyone who receives the Program under 66 | this Agreement, including all Contributors.

67 | 68 |

2. GRANT OF RIGHTS

69 | 70 |

a) Subject to the terms of this Agreement, each 71 | Contributor hereby grants Recipient a non-exclusive, worldwide, 72 | royalty-free copyright license to reproduce, prepare derivative works 73 | of, publicly display, publicly perform, distribute and sublicense the 74 | Contribution of such Contributor, if any, and such derivative works, in 75 | source code and object code form.

76 | 77 |

b) Subject to the terms of this Agreement, each 78 | Contributor hereby grants Recipient a non-exclusive, worldwide, 79 | royalty-free patent license under Licensed Patents to make, use, sell, 80 | offer to sell, import and otherwise transfer the Contribution of such 81 | Contributor, if any, in source code and object code form. This patent 82 | license shall apply to the combination of the Contribution and the 83 | Program if, at the time the Contribution is added by the Contributor, 84 | such addition of the Contribution causes such combination to be covered 85 | by the Licensed Patents. The patent license shall not apply to any other 86 | combinations which include the Contribution. No hardware per se is 87 | licensed hereunder.

88 | 89 |

c) Recipient understands that although each Contributor 90 | grants the licenses to its Contributions set forth herein, no assurances 91 | are provided by any Contributor that the Program does not infringe the 92 | patent or other intellectual property rights of any other entity. Each 93 | Contributor disclaims any liability to Recipient for claims brought by 94 | any other entity based on infringement of intellectual property rights 95 | or otherwise. As a condition to exercising the rights and licenses 96 | granted hereunder, each Recipient hereby assumes sole responsibility to 97 | secure any other intellectual property rights needed, if any. For 98 | example, if a third party patent license is required to allow Recipient 99 | to distribute the Program, it is Recipient's responsibility to acquire 100 | that license before distributing the Program.

101 | 102 |

d) Each Contributor represents that to its knowledge it 103 | has sufficient copyright rights in its Contribution, if any, to grant 104 | the copyright license set forth in this Agreement.

105 | 106 |

3. REQUIREMENTS

107 | 108 |

A Contributor may choose to distribute the Program in object code 109 | form under its own license agreement, provided that:

110 | 111 |

a) it complies with the terms and conditions of this 112 | Agreement; and

113 | 114 |

b) its license agreement:

115 | 116 |

i) effectively disclaims on behalf of all Contributors 117 | all warranties and conditions, express and implied, including warranties 118 | or conditions of title and non-infringement, and implied warranties or 119 | conditions of merchantability and fitness for a particular purpose;

120 | 121 |

ii) effectively excludes on behalf of all Contributors 122 | all liability for damages, including direct, indirect, special, 123 | incidental and consequential damages, such as lost profits;

124 | 125 |

iii) states that any provisions which differ from this 126 | Agreement are offered by that Contributor alone and not by any other 127 | party; and

128 | 129 |

iv) states that source code for the Program is available 130 | from such Contributor, and informs licensees how to obtain it in a 131 | reasonable manner on or through a medium customarily used for software 132 | exchange.

133 | 134 |

When the Program is made available in source code form:

135 | 136 |

a) it must be made available under this Agreement; and

137 | 138 |

b) a copy of this Agreement must be included with each 139 | copy of the Program.

140 | 141 |

Contributors may not remove or alter any copyright notices contained 142 | within the Program.

143 | 144 |

Each Contributor must identify itself as the originator of its 145 | Contribution, if any, in a manner that reasonably allows subsequent 146 | Recipients to identify the originator of the Contribution.

147 | 148 |

4. COMMERCIAL DISTRIBUTION

149 | 150 |

Commercial distributors of software may accept certain 151 | responsibilities with respect to end users, business partners and the 152 | like. While this license is intended to facilitate the commercial use of 153 | the Program, the Contributor who includes the Program in a commercial 154 | product offering should do so in a manner which does not create 155 | potential liability for other Contributors. Therefore, if a Contributor 156 | includes the Program in a commercial product offering, such Contributor 157 | ("Commercial Contributor") hereby agrees to defend and 158 | indemnify every other Contributor ("Indemnified Contributor") 159 | against any losses, damages and costs (collectively "Losses") 160 | arising from claims, lawsuits and other legal actions brought by a third 161 | party against the Indemnified Contributor to the extent caused by the 162 | acts or omissions of such Commercial Contributor in connection with its 163 | distribution of the Program in a commercial product offering. The 164 | obligations in this section do not apply to any claims or Losses 165 | relating to any actual or alleged intellectual property infringement. In 166 | order to qualify, an Indemnified Contributor must: a) promptly notify 167 | the Commercial Contributor in writing of such claim, and b) allow the 168 | Commercial Contributor to control, and cooperate with the Commercial 169 | Contributor in, the defense and any related settlement negotiations. The 170 | Indemnified Contributor may participate in any such claim at its own 171 | expense.

172 | 173 |

For example, a Contributor might include the Program in a commercial 174 | product offering, Product X. That Contributor is then a Commercial 175 | Contributor. If that Commercial Contributor then makes performance 176 | claims, or offers warranties related to Product X, those performance 177 | claims and warranties are such Commercial Contributor's responsibility 178 | alone. Under this section, the Commercial Contributor would have to 179 | defend claims against the other Contributors related to those 180 | performance claims and warranties, and if a court requires any other 181 | Contributor to pay any damages as a result, the Commercial Contributor 182 | must pay those damages.

183 | 184 |

5. NO WARRANTY

185 | 186 |

EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS 187 | PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 188 | OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 189 | ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY 190 | OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely 191 | responsible for determining the appropriateness of using and 192 | distributing the Program and assumes all risks associated with its 193 | exercise of rights under this Agreement , including but not limited to 194 | the risks and costs of program errors, compliance with applicable laws, 195 | damage to or loss of data, programs or equipment, and unavailability or 196 | interruption of operations.

197 | 198 |

6. DISCLAIMER OF LIABILITY

199 | 200 |

EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT 201 | NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, 202 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING 203 | WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF 204 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 205 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR 206 | DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED 207 | HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

208 | 209 |

7. GENERAL

210 | 211 |

If any provision of this Agreement is invalid or unenforceable under 212 | applicable law, it shall not affect the validity or enforceability of 213 | the remainder of the terms of this Agreement, and without further action 214 | by the parties hereto, such provision shall be reformed to the minimum 215 | extent necessary to make such provision valid and enforceable.

216 | 217 |

If Recipient institutes patent litigation against any entity 218 | (including a cross-claim or counterclaim in a lawsuit) alleging that the 219 | Program itself (excluding combinations of the Program with other 220 | software or hardware) infringes such Recipient's patent(s), then such 221 | Recipient's rights granted under Section 2(b) shall terminate as of the 222 | date such litigation is filed.

223 | 224 |

All Recipient's rights under this Agreement shall terminate if it 225 | fails to comply with any of the material terms or conditions of this 226 | Agreement and does not cure such failure in a reasonable period of time 227 | after becoming aware of such noncompliance. If all Recipient's rights 228 | under this Agreement terminate, Recipient agrees to cease use and 229 | distribution of the Program as soon as reasonably practicable. However, 230 | Recipient's obligations under this Agreement and any licenses granted by 231 | Recipient relating to the Program shall continue and survive.

232 | 233 |

Everyone is permitted to copy and distribute copies of this 234 | Agreement, but in order to avoid inconsistency the Agreement is 235 | copyrighted and may only be modified in the following manner. The 236 | Agreement Steward reserves the right to publish new versions (including 237 | revisions) of this Agreement from time to time. No one other than the 238 | Agreement Steward has the right to modify this Agreement. The Eclipse 239 | Foundation is the initial Agreement Steward. The Eclipse Foundation may 240 | assign the responsibility to serve as the Agreement Steward to a 241 | suitable separate entity. Each new version of the Agreement will be 242 | given a distinguishing version number. The Program (including 243 | Contributions) may always be distributed subject to the version of the 244 | Agreement under which it was received. In addition, after a new version 245 | of the Agreement is published, Contributor may elect to distribute the 246 | Program (including its Contributions) under the new version. Except as 247 | expressly stated in Sections 2(a) and 2(b) above, Recipient receives no 248 | rights or licenses to the intellectual property of any Contributor under 249 | this Agreement, whether expressly, by implication, estoppel or 250 | otherwise. All rights in the Program not expressly granted under this 251 | Agreement are reserved.

252 | 253 |

This Agreement is governed by the laws of the State of New York and 254 | the intellectual property laws of the United States of America. No party 255 | to this Agreement will bring a legal action under this Agreement more 256 | than one year after the cause of action arose. Each party waives its 257 | rights to a jury trial in any resulting litigation.

258 | 259 | 260 | 261 | 262 | -------------------------------------------------------------------------------- /examples/hello_world.clj: -------------------------------------------------------------------------------- 1 | (require 2 | '[clojure.data.generators :as gen] 3 | '[clojure.test.generative :as test :refer (defspec)] 4 | '[clojure.test.generative.runner :as runner]) 5 | 6 | ;; generators have names that shadow core names of things generated 7 | (gen/long) 8 | 9 | ;; generation is repeatable 10 | (repeatedly 11 | 2 12 | #(binding [gen/*rnd* (java.util.Random. 42)] 13 | (gen/short))) 14 | 15 | ;; generation is composable 16 | (gen/vec gen/short) 17 | 18 | ;; size is parameterized 19 | (gen/vec gen/short 2) 20 | 21 | ;; size parameter can of course also be a generator 22 | (gen/vec gen/short (gen/uniform 3 5)) 23 | 24 | ;; generators are in scope as "types" in a defspec 25 | (defspec longs-are-closed-under-increment 26 | inc ;; function under test 27 | [^long l] ;; indicates generation via gen/long 28 | (assert (instance? Long %))) 29 | 30 | ;; specs are functions 31 | (longs-are-closed-under-increment 4) 32 | 33 | ;; the next two steps are executed for you by the standard runner... 34 | 35 | ;; tests are extracted from vars 36 | (def tests (runner/get-tests #'longs-are-closed-under-increment)) 37 | 38 | (first tests) 39 | 40 | ;; run test with some generated inputs 41 | (runner/run-one 42 | (first tests) 43 | 1000 44 | [42]) 45 | 46 | (runner/run-n 47 | 2 48 | 1000 49 | tests) 50 | 51 | ;; repl-friendly use 52 | (runner/run-vars 53 | 2 1000 #'longs-are-closed-under-increment) 54 | 55 | ;; peek at what defspec tells us 56 | (meta #'longs-are-closed-under-increment) 57 | 58 | ;; test that will fail 59 | (defspec collections-are-small 60 | count 61 | [^{:tag (gen/vec gen/short (gen/uniform 0 25))} l] 62 | (assert (< % 20))) 63 | 64 | ;; run as from REPL 65 | (runner/run-vars 66 | 2 1000 #'collections-are-small) 67 | (ex-data *e) 68 | 69 | ;; run as from suite 70 | (runner/run-suite {:threads 1 :msec 500} (runner/get-tests #'collections-are-small)) 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | test.generative 4 | 1.1.1-SNAPSHOT 5 | test.generative 6 | 7 | 8 | org.clojure 9 | pom.contrib 10 | 1.3.0 11 | 12 | 13 | 14 | 1.9.0 15 | true 16 | 17 | 18 | 19 | 20 | Stuart Halloway 21 | 22 | 23 | 24 | 25 | scm:git:git@github.com:clojure/test.generative.git 26 | scm:git:git@github.com:clojure/test.generative.git 27 | git@github.com:clojure/test.generative.git 28 | HEAD 29 | 30 | 31 | 32 | 33 | 34 | sonatype-oss-snapshots 35 | https://oss.sonatype.org/content/repositories/snapshots 36 | 37 | 38 | 39 | 40 | 41 | org.clojure 42 | tools.namespace 43 | 1.5.0 44 | 45 | 46 | org.clojure 47 | data.generators 48 | 1.1.0 49 | 50 | 51 | 52 | 53 | 54 | 55 | maven-invoker-plugin 56 | 1.5 57 | 58 | 59 | */pom.xml 60 | 61 | true 62 | clojure:test 63 | -Duser.dir=${basedir} 64 | 65 | 66 | 67 | 68 | install 69 | run 70 | 71 | 72 | 73 | 74 | 75 | com.theoryinpractise 76 | clojure-maven-plugin 77 | 1.7.1 78 | 79 | -Dclojure.test.generative.runner=clojure.test 80 | bin/test.clj 81 | 82 | src/examples/clojure 83 | 84 | 85 | !.* 86 | 87 | true 88 | 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /src/examples/clojure/clojure/test/array_test.clj: -------------------------------------------------------------------------------- 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 clojure.test.array-test 11 | (:use clojure.test.generative) 12 | (:require [clojure.data.generators :as gen])) 13 | 14 | (defn nan-or-= 15 | [a b] 16 | (or (= a b) 17 | (and (Double/isNaN a) 18 | (Double/isNaN b)))) 19 | 20 | (defmacro array-access-specs 21 | [pdescs] 22 | `(do 23 | ~@(map 24 | (fn [[prim comp]] 25 | `(defspec ~(symbol (str prim "-array-access")) 26 | vec 27 | [~(with-meta 'arr {:tag (list (symbol (str prim "-array")) prim)})] 28 | (assert (= (alength ~'arr) (count ~'%))) 29 | (dotimes [~'i (count ~'arr)] 30 | (assert (~comp (aget ~'arr ~'i) (get ~'% ~'i)))))) 31 | pdescs))) 32 | 33 | (defmacro copy-array-fns 34 | [& types] 35 | `(do 36 | ~@(map 37 | (fn [t] 38 | `(defn ~(symbol (str "copy-" t "-array")) 39 | [~'a] 40 | (let [~'copy ~(list (symbol (str t "-array")) 'a)] 41 | (dotimes [~'i (alength ~'a)] 42 | (aset ~'copy ~'i ~(with-meta `(aget ~'a ~'i) 43 | {:tag (symbol (str t "s"))}))) 44 | ~'copy))) 45 | types))) 46 | 47 | (copy-array-fns boolean byte char short int long float double object) 48 | 49 | (defmacro aset-specs 50 | [pdescs] 51 | `(do 52 | ~@(map 53 | (fn [[prim comp]] 54 | `(defspec ~(symbol (str prim "-aset-spec")) 55 | copy-object-array 56 | [~(with-meta 'arr {:tag (list (symbol (str prim "-array")) prim)})] 57 | (assert (= (alength ~'arr) (alength ~'%))) 58 | (dotimes [~'i (count ~'arr)] 59 | (assert (~comp (aget ~'arr ~'i) (aget ~'% ~'i)))))) 60 | pdescs))) 61 | 62 | (array-access-specs {int = 63 | long = 64 | float nan-or-= 65 | double nan-or-= 66 | short = 67 | byte = 68 | char = 69 | boolean =}) 70 | 71 | (aset-specs {int = 72 | long = 73 | float nan-or-= 74 | double nan-or-= 75 | short = 76 | byte = 77 | char = 78 | boolean =}) 79 | 80 | 81 | -------------------------------------------------------------------------------- /src/examples/clojure/clojure/test/core_test.clj: -------------------------------------------------------------------------------- 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 clojure.test.core-test 11 | (:use clojure.test.generative clojure.pprint clojure.data clojure.walk) 12 | (:require [clojure.data.generators :as gen])) 13 | 14 | (def ^:private failed-diff-ref (atom nil)) 15 | 16 | (defmacro 17 | assert= 18 | "Specialization of assert for equality. Uses clojure.data/diff to 19 | report detailed difference in unequal things. You can get the 20 | (last-failed-diff)." 21 | ([a b] `(assert= ~a ~b nil)) 22 | ([a b context] 23 | (when *assert* 24 | `(let [[d1# d2# s#] (clojure.data/diff ~a ~b) 25 | context# (if ~context (str "[" ~context "] ") "")] 26 | (when (or d1# d2#) 27 | (reset! failed-diff-ref [d1# d2# s#]) 28 | (throw (new AssertionError (str context# "Items different: " ~a ", " ~b)))))))) 29 | 30 | (defn last-failed-diff 31 | [] 32 | @failed-diff-ref) 33 | 34 | (defn map->case-src 35 | "Returns source for a case statement that works like map m, 36 | with default for missing elements." 37 | [m default] 38 | `(fn [input#] 39 | (case input# 40 | ~@(mapcat identity m) 41 | ~default))) 42 | 43 | (defn map->case-fn 44 | "Create a case statement that works the same as map m, 45 | with default for missing items." 46 | [m default] 47 | (eval (map->case-src m default))) 48 | 49 | (defn map->cond-src 50 | [m default] 51 | (list 'fn '[input] 52 | (apply list 'cond 53 | (concat (mapcat (fn [[k v]] [(list '= 'input (list 'quote k)) 54 | (list 'quote v)]) m) 55 | [:else default])))) 56 | 57 | (defn map->cond-fn 58 | [m default] 59 | (eval (map->cond-src m default))) 60 | 61 | (defn case-test-constant 62 | "Generate a test constant that might appear in a case expression." 63 | [] 64 | (gen/one-of gen/long 65 | gen/char 66 | gen/scalar 67 | gen/keyword)) 68 | 69 | (defspec case-spec 70 | map->case-fn 71 | [^{:tag (hash-map `case-test-constant long)} case-map 72 | ^keyword default] 73 | (doseq [k (keys case-map)] 74 | (assert= (% k) (case-map k) k)) 75 | (doseq [k (range 100)] 76 | (when-not (contains? case-map k) 77 | (assert= (% k) default k)))) 78 | 79 | (defspec cond-spec 80 | map->cond-fn 81 | [^{:tag (hash-map `case-test-constant long)} cond-map 82 | ^keyword default] 83 | (doseq [k (keys cond-map)] 84 | (assert= (% k) (cond-map k) k)) 85 | (doseq [k (range 100)] 86 | (when-not (contains? cond-map k) 87 | (assert= (% k) default k)))) 88 | 89 | (defn encountered-in-walk 90 | "Returns a frequency map of items encountered while walking 91 | obj, using a walk-fn from e.g. clojure.walk." 92 | [walk-fn obj] 93 | (let [encountered (atom [])] 94 | (walk-fn 95 | (fn [obj] (swap! encountered conj obj) obj) 96 | obj) 97 | (frequencies @encountered))) 98 | 99 | (defspec pre-and-post-walk-hit-same-things 100 | identity 101 | [^anything _] 102 | (assert= (encountered-in-walk prewalk %) 103 | (encountered-in-walk postwalk %))) 104 | -------------------------------------------------------------------------------- /src/examples/clojure/clojure/test/math_test.clj: -------------------------------------------------------------------------------- 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 clojure.test.math-test 11 | (:use clojure.test.generative) 12 | (:require 13 | [clojure.data.generators :as gen] 14 | [clojure.test.generative :as tgen])) 15 | 16 | (defn integer 17 | "Distribution of integers biased towards the small, but 18 | including all longs." 19 | [] 20 | (gen/one-of #(gen/uniform -1 32) gen/byte gen/short gen/int gen/long)) 21 | 22 | (defn longable? 23 | [n] 24 | (try 25 | (long n) 26 | true 27 | (catch Exception _))) 28 | 29 | (defspec integer-commutative-laws 30 | (partial map identity) 31 | [^{:tag `integer} a ^{:tag `integer} b] 32 | (if (longable? (+' a b)) 33 | (assert (= (+ a b) (+ b a) 34 | (+' a b) (+' b a) 35 | (unchecked-add a b) (unchecked-add b a))) 36 | (assert (= (+' a b) (+' b a)))) 37 | (if (longable? (*' a b)) 38 | (assert (= (* a b) (* b a) 39 | (*' a b) (*' b a) 40 | (unchecked-multiply a b) (unchecked-multiply b a))) 41 | (assert (= (*' a b) (*' b a))))) 42 | 43 | (defspec integer-associative-laws 44 | (partial map identity) 45 | [^{:tag `integer} a ^{:tag `integer} b ^{:tag `integer} c] 46 | (if (every? longable? [(+' a b) (+' b c) (+' a b c)]) 47 | (assert (= (+ (+ a b) c) (+ a (+ b c)) 48 | (+' (+' a b) c) (+' a (+' b c)) 49 | (unchecked-add (unchecked-add a b) c) (unchecked-add a (unchecked-add b c)))) 50 | (assert (= (+' (+' a b) c) (+' a (+' b c)) 51 | (+ (+ (bigint a) b) c) (+ a (+ (bigint b) c))))) 52 | (if (every? longable? [(*' a b) (*' b c) (*' a b c)]) 53 | (assert (= (* (* a b) c) (* a (* b c)) 54 | (*' (*' a b) c) (*' a (*' b c)) 55 | (unchecked-multiply (unchecked-multiply a b) c) (unchecked-multiply a (unchecked-multiply b c)))) 56 | (assert (= (*' (*' a b) c) (*' a (*' b c)) 57 | (* (* (bigint a) b) c) (* a (* (bigint b) c)))))) 58 | 59 | (defspec integer-distributive-laws 60 | (partial map identity) 61 | [^{:tag `integer} a ^{:tag `integer} b ^{:tag `integer} c] 62 | (if (every? longable? [(*' a (+' b c)) (+' (*' a b) (*' a c)) 63 | (*' a b) (*' a c) (+' b c)]) 64 | (assert (= (* a (+ b c)) (+ (* a b) (* a c)) 65 | (*' a (+' b c)) (+' (*' a b) (*' a c)) 66 | (unchecked-multiply a (+' b c)) (+' (unchecked-multiply a b) (unchecked-multiply a c)))) 67 | (assert (= (*' a (+' b c)) (+' (*' a b) (*' a c)) 68 | (* a (+ (bigint b) c)) (+ (* (bigint a) b) (* (bigint a) c)))))) 69 | 70 | (defspec addition-undoes-subtraction 71 | (partial map identity) 72 | [^{:tag `integer} a ^{:tag `integer} b] 73 | (if (longable? (-' a b)) 74 | (assert (= a 75 | (-> a (- b) (+ b)) 76 | (-> a (unchecked-subtract b) (unchecked-add b))))) 77 | (assert (= a 78 | (-> a (-' b) (+' b))))) 79 | 80 | (defspec quotient-and-remainder 81 | (fn [a b] (sort [a b])) 82 | [^{:tag `integer} a ^{:tag `integer} b] 83 | (when-not (zero? (second %)) 84 | (let [[a d] % 85 | q (quot a d) 86 | r (rem a d)] 87 | (assert (= a 88 | (+ (* q d) r) 89 | (unchecked-add (unchecked-multiply q d) r)))))) 90 | 91 | (defn inc'-doesnt-overflow 92 | [a] 93 | (assert (< a (inc' a)))) 94 | 95 | ;; You can mark a var containing "raw" specs with ::tgen/specs to 96 | ;; include it in the test suite. 97 | (def ^::tgen/specs 98 | inc'-doesnt-overflow-specs 99 | [{:test 'clojure.test.math-test/inc'-doesnt-overflow 100 | :input-gen #(map vector [Long/MIN_VALUE -1 0 1 Long/MAX_VALUE])}]) 101 | -------------------------------------------------------------------------------- /src/main/clojure/clojure/test/generative.clj: -------------------------------------------------------------------------------- 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 clojure.test.generative 11 | (:require [clojure.walk :as walk])) 12 | 13 | (defn- fully-qualified 14 | "Qualify a name used in :tag metadata. Unqualified names are 15 | interpreted in the 'clojure.data.generators, except 16 | for the fn-building symbols fn and fn*." 17 | [n] 18 | (let [ns (cond 19 | (#{'fn*} n) nil 20 | (#{'fn} n) 'clojure.core 21 | (namespace n) (namespace n) 22 | :else 'clojure.data.generators)] 23 | (if ns 24 | (symbol (str ns) (name n)) 25 | n))) 26 | 27 | (defn- dequote 28 | "Remove the backquotes used to call out user-namespaced forms." 29 | [form] 30 | (walk/prewalk 31 | #(if (and (sequential? %) 32 | (= 2 (count %)) 33 | (= 'quote (first %))) 34 | (second %) 35 | %) 36 | form)) 37 | 38 | (defn- tag->gen 39 | "Convert tag to source code form for a test data generator." 40 | [arg] 41 | (let [form (walk/prewalk (fn [s] (if (symbol? s) (fully-qualified s) s)) (dequote arg))] 42 | (if (seq? form) 43 | (list 'fn '[] form) 44 | form))) 45 | 46 | (defmacro defspec 47 | "Defines a function named name that expects args. The defined 48 | function binds '%' to the result of calling fn-to-test with args, 49 | and runs validator-body forms (if any), which have access to both 50 | args and %. The defined function. 51 | 52 | Args must have type hints (i.e. :tag metadata), which are 53 | interpreted as instructions for generating test input 54 | data. Unquoted names in type hints are resolved in the 55 | c.t.g.generators namespace, which has generator functions for 56 | common Clojure data types. For example, the following argument list 57 | declares that 'seed' is an int, and that 'iters' is an int in the 58 | uniform distribution from 1 to 100: 59 | 60 | [^int seed ^{:tag (uniform 1 100)} iters] 61 | 62 | Backquoted names in an argument list are resolved in the current 63 | namespace, allowing arbitrary generators, e.g. 64 | 65 | [^{:tag `scary-word} word] 66 | 67 | The function c.t.g.runner/run-iter takes a var naming a test, and runs 68 | a single test iteration, generating inputs based on the arg type hints." 69 | [name fn-to-test args & validator-body] 70 | (when-let [missing-tags (->> (map #(list % (-> % meta :tag)) args) 71 | (filter (fn [[_ tag]] (nil? tag))) 72 | seq)] 73 | (throw (IllegalArgumentException. (str "Missing tags for " (seq (map first missing-tags)) " in " name)))) 74 | `(defn ~(with-meta name (assoc (meta name) 75 | ::arg-fns (into [] (map #(-> % meta :tag tag->gen eval) args)))) 76 | ~(into [] (map (fn [a#] (with-meta a# (dissoc (meta a#) :tag))) args)) 77 | (let [~'% (apply ~fn-to-test ~args)] 78 | ~@validator-body 79 | ~'%))) 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /src/main/clojure/clojure/test/generative/runner.clj: -------------------------------------------------------------------------------- 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 clojure.test.generative.runner 11 | (:require 12 | [clojure.tools.namespace.find :as ns] 13 | [clojure.data.generators :as gen] 14 | [clojure.test.generative :as tgen])) 15 | 16 | (set! *warn-on-reflection* true) 17 | 18 | (def ^:private config-mapping 19 | [["clojure.test.generative.threads" 20 | [:nthreads] 21 | read-string 22 | (max 1 (dec (.availableProcessors (Runtime/getRuntime))))] 23 | ["clojure.test.generative.msec" 24 | [:msec] 25 | read-string 26 | 10000]]) 27 | 28 | (defn config 29 | "Returns runner configuration derived from system properties." 30 | [] 31 | (reduce 32 | (fn [m [prop path coerce default]] 33 | (let [val (System/getProperty prop)] 34 | (if (seq val) 35 | (assoc-in m path (coerce val)) 36 | (assoc-in m path default)))) 37 | {} 38 | config-mapping)) 39 | 40 | (def ^:private ^java.util.Random rnd (java.util.Random. (System/currentTimeMillis))) 41 | 42 | (defn- next-seed 43 | [] 44 | (locking rnd 45 | (.nextInt rnd))) 46 | 47 | (defprotocol Testable 48 | (get-tests [_])) 49 | 50 | (extend-protocol Testable 51 | clojure.lang.Var 52 | (get-tests 53 | [v] 54 | (let [m (meta v) 55 | arg-fns (::tgen/arg-fns m) 56 | specs (::tgen/specs m)] 57 | (cond 58 | arg-fns 59 | [{:test (-> (if-let [ns (.ns v)] 60 | (str ns "/" (.sym v)) 61 | (.sym v)) 62 | symbol) 63 | :input-gen (fn [] 64 | (repeatedly 65 | (fn [] 66 | (into [] (map #(%) arg-fns)))))}] 67 | 68 | specs 69 | @v)))) 70 | 71 | 72 | (defn- find-vars-in-namespaces 73 | [& nses] 74 | (when nses 75 | (reduce (fn [v ns] (into v (vals (ns-interns ns)))) [] nses))) 76 | 77 | (defn- find-vars-in-dirs 78 | [& dirs] 79 | (let [nses (mapcat #(ns/find-namespaces-in-dir (java.io.File. ^String %)) dirs)] 80 | (doseq [ns nses] (require ns)) 81 | (apply find-vars-in-namespaces nses))) 82 | 83 | (defn- run-one 84 | "Run f (presumably for side effects) repeatedly on n threads, 85 | until msec has passed or somebody throws an exception. 86 | Returns as many status maps as seeds passed in." 87 | [{:keys [test input-gen]} {:keys [msec seeds]}] 88 | (let [f (eval test) 89 | start (System/currentTimeMillis) 90 | futs (mapv 91 | #(future 92 | (try 93 | (binding [gen/*rnd* (java.util.Random. %)] 94 | (loop [iter 0 95 | [input & more] (input-gen)] 96 | (let [status {:iter iter :seed % :test test :input input}] 97 | (if input 98 | (let [failure (try 99 | (apply f input) 100 | nil 101 | (catch Throwable t 102 | (assoc status :exception t) )) 103 | now (System/currentTimeMillis)] 104 | (cond 105 | failure failure 106 | (< now (+ start msec)) (recur (inc iter) more) 107 | :else (select-keys status [:test :seed :iter]))) 108 | (assoc status :exhausted true))))))) 109 | seeds)] 110 | (map deref futs))) 111 | 112 | (defn- failed? 113 | "Does test result indicate a failure?" 114 | [result] 115 | (contains? result :exception)) 116 | 117 | (defn- run-n 118 | "Run tests in parallel on nthreads, dividing msec equally between the tests. 119 | Returns a list of maps of :iter, :seed" 120 | [{:keys [nthreads msec]} tests] 121 | (mapcat #(run-one % 122 | {:msec (/ msec (count tests)) 123 | :seeds (repeatedly nthreads next-seed)}) 124 | tests)) 125 | 126 | (def ^:private serializer (agent nil)) 127 | 128 | (defn serialized 129 | "Returns a function that calls f for side effects, async, 130 | serialized by an agent" 131 | ([f] (serialized f serializer)) 132 | ([f agt] 133 | (fn [& args] 134 | (send-off agt 135 | (fn [_] 136 | (try 137 | (apply f args) 138 | (catch Throwable t 139 | (.printStackTrace t))) 140 | nil)) 141 | nil))) 142 | 143 | (def prf 144 | "Print and flush." 145 | (serialized (fn [s] 146 | (binding [*out* *err*] 147 | (print s) 148 | (flush))))) 149 | 150 | (def print-stack-trace 151 | (serialized (fn [^Throwable t] (.printStackTrace t)))) 152 | 153 | (def sprn (serialized prn)) 154 | 155 | (defn dir-tests 156 | "Returns all tests in dirs" 157 | [dirs] 158 | (let [load (fn [s] (require s) s)] 159 | (->> (mapcat #(ns/find-namespaces-in-dir (java.io.File. ^String %)) dirs) 160 | (map load) 161 | (apply find-vars-in-namespaces) 162 | (mapcat get-tests)))) 163 | 164 | (defn inputs 165 | "For interactive use. Returns an infinite sequence of inputs for 166 | a test." 167 | [test] 168 | ((:input-gen test))) 169 | 170 | (defn run 171 | "Designed for interactive use. Prints results to *out* and throws 172 | on first failure encountered." 173 | [nthreads msec & test-containers] 174 | (doseq [result (run-n {:nthreads nthreads 175 | :msec msec} 176 | (mapcat get-tests test-containers))] 177 | (if (failed? result) 178 | (throw (ex-info "Generative test failed" result)) 179 | (prn result)))) 180 | 181 | (defn run-suite 182 | "Designed for test suite use." 183 | [{:keys [nthreads msec progress]} tests] 184 | (let [progress (or progress #(prf ".")) 185 | ret (reduce 186 | (fn [{:keys [failures iters nresults]} result] 187 | (when (:exception result) 188 | (print-stack-trace (:exception result))) 189 | (if (:exception result) 190 | (sprn result) 191 | (progress)) 192 | {:failures (+ failures (if (:exception result) 1 0)) 193 | :iters (+ iters (:iter result)) 194 | :nresults (+ nresults 1)}) 195 | {:failures 0 :iters 0 :nresults 0} 196 | (run-n {:nthreads nthreads 197 | :msec msec} 198 | tests))] 199 | (-> ret 200 | (assoc :tests (/ (:nresults ret) nthreads)) 201 | (dissoc :nresults)))) 202 | 203 | (defn -main 204 | "Command line entry point. Calls System.exit!" 205 | [& dirs] 206 | (if (seq dirs) 207 | (try 208 | (let [result (run-suite (config) (dir-tests dirs))] 209 | (println "\n" result) 210 | (System/exit (:failures result))) 211 | (catch Throwable t 212 | (.printStackTrace t) 213 | (System/exit -1)) 214 | (finally 215 | (shutdown-agents))) 216 | (do 217 | (println "Specify at least one directory with tests") 218 | (System/exit -1)))) 219 | 220 | 221 | 222 | --------------------------------------------------------------------------------