Debian Codegraph @

src/tester.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/;
2 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/;
3 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#;
4 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/;
5
6 use craterust-analyzer cargo quickcheck 1.1.0 crate/::{
7 testerrust-analyzer cargo quickcheck 1.1.0 tester/::Statusrust-analyzer cargo quickcheck 1.1.0 tester/Status#::{Discardrust-analyzer cargo quickcheck 1.1.0 tester/Status#Discard#, Failrust-analyzer cargo quickcheck 1.1.0 tester/Status#Fail#, Passrust-analyzer cargo quickcheck 1.1.0 tester/Status#Pass#},
8 Arbitraryrust-analyzer cargo quickcheck 1.1.0 arbitrary/Arbitrary#, Genrust-analyzer cargo quickcheck 1.1.0 arbitrary/Gen#,
9 };
10
11 /// The main `QuickCheck` type for setting configuration and running
12 /// `QuickCheck`.
13 pub struct QuickCheckrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck# {
14 testsrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck#tests.: u64,
15 max_testsrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck#max_tests.: u64,
16 min_tests_passedrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck#min_tests_passed.: u64,
17 rngrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck#rng.: Genrust-analyzer cargo quickcheck 1.1.0 arbitrary/Gen#,
18 }
19
20 fn qc_testsrust-analyzer cargo quickcheck 1.1.0 tester/qc_tests().() -> u64 {
21 let default = 100;
22 match envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("QUICKCHECK_TESTS") {
23 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(val) => val.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or().(default),
24 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => default,
25 }
26 }
27
28 fn qc_max_testsrust-analyzer cargo quickcheck 1.1.0 tester/qc_max_tests().() -> u64 {
29 let default = 10_000;
30 match envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("QUICKCHECK_MAX_TESTS") {
31 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(val) => val.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or().(default),
32 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => default,
33 }
34 }
35
36 fn qc_gen_sizerust-analyzer cargo quickcheck 1.1.0 tester/qc_gen_size().() -> usize {
37 let default = 100;
38 match envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("QUICKCHECK_GENERATOR_SIZE") {
39 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(val) => val.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or().(default),
40 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => default,
41 }
42 }
43
44 fn qc_min_tests_passedrust-analyzer cargo quickcheck 1.1.0 tester/qc_min_tests_passed().() -> u64 {
45 let default = 0;
46 match envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("QUICKCHECK_MIN_TESTS_PASSED") {
47 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(val) => val.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or().(default),
48 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => default,
49 }
50 }
51
52 impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for QuickCheckrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck# {
53 fn defaultrust-analyzer cargo quickcheck 1.1.0 tester/impl#[QuickCheck][Default]default().() -> Selfrust-analyzer cargo quickcheck 1.1.0 tester/impl#[QuickCheck][Default] {
54 Selfrust-analyzer cargo quickcheck 1.1.0 tester/impl#[QuickCheck][Default]::newrust-analyzer cargo quickcheck 1.1.0 tester/impl#[QuickCheck]new().()
55 }
56 }
57
58 impl QuickCheckrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck# {
59 /// Creates a new `QuickCheck` value.
60 ///
61 /// This can be used to run `QuickCheck` on things that implement
62 /// `Testable`. You may also adjust the configuration, such as the
63 /// number of tests to run.
64 ///
65 /// By default, the maximum number of passed tests is set to `100`, the max
66 /// number of overall tests is set to `10000` and the generator is created
67 /// with a size of `100`.
68 pub fn newrust-analyzer cargo quickcheck 1.1.0 tester/impl#[QuickCheck]new().() -> QuickCheckrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck# {
69 let rng = Genrust-analyzer cargo quickcheck 1.1.0 arbitrary/Gen#::newrust-analyzer cargo quickcheck 1.1.0 arbitrary/impl#[Gen]new().(qc_gen_sizerust-analyzer cargo quickcheck 1.1.0 tester/qc_gen_size().());
70 let tests = qc_testsrust-analyzer cargo quickcheck 1.1.0 tester/qc_tests().();
71 let max_tests = cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/max().(tests, qc_max_testsrust-analyzer cargo quickcheck 1.1.0 tester/qc_max_tests().());
72 let min_tests_passed = qc_min_tests_passedrust-analyzer cargo quickcheck 1.1.0 tester/qc_min_tests_passed().();
73
74 QuickCheckrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck# { testsrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck#tests., max_testsrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck#max_tests., min_tests_passedrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck#min_tests_passed., rngrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck#rng. }
75 }
76
77 /// Set the random number generator to be used by `QuickCheck`.
78 pub fn rngrust-analyzer cargo quickcheck 1.1.0 tester/impl#[QuickCheck]rng().(self, rng: Genrust-analyzer cargo quickcheck 1.1.0 arbitrary/Gen#) -> QuickCheckrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck# {
79 QuickCheckrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck# { rngrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck#rng., ..self }
80 }
81
82 /// Set the random number generator to be used by `QuickCheck`.
83 ///
84 /// This is **DEPRECATED**. Using this method on Rust 2024 or newer
85 /// requires the raw identifier syntax because `gen` is now a keyword.
86 /// Instead, prefer the [`QuickCheck::rng`] method.
87 #[deprecated(since = "1.1.0", note = "use `set_rng` instead")]
88 pub fn r#genrust-analyzer cargo quickcheck 1.1.0 tester/impl#[QuickCheck]gen().(self, rng: Genrust-analyzer cargo quickcheck 1.1.0 arbitrary/Gen#) -> QuickCheckrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck# {
89 self.rngrust-analyzer cargo quickcheck 1.1.0 tester/impl#[QuickCheck]rng().(rng)
90 }
91
92 /// Set the number of tests to run.
93 ///
94 /// This actually refers to the maximum number of *passed* tests that
95 /// can occur. Namely, if a test causes a failure, future testing on that
96 /// property stops. Additionally, if tests are discarded, there may be
97 /// fewer than `tests` passed.
98 pub fn testsrust-analyzer cargo quickcheck 1.1.0 tester/impl#[QuickCheck]tests().(mut self, tests: u64) -> QuickCheckrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck# {
99 self.testsrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck#tests. = tests;
100 self
101 }
102
103 /// Set the maximum number of tests to run.
104 ///
105 /// The number of invocations of a property will never exceed this number.
106 /// This is necessary to cap the number of tests because `QuickCheck`
107 /// properties can discard tests.
108 pub fn max_testsrust-analyzer cargo quickcheck 1.1.0 tester/impl#[QuickCheck]max_tests().(mut self, max_tests: u64) -> QuickCheckrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck# {
109 self.max_testsrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck#max_tests. = max_tests;
110 self
111 }
112
113 /// Set the minimum number of tests that needs to pass.
114 ///
115 /// This actually refers to the minimum number of *valid* *passed* tests
116 /// that needs to pass for the property to be considered successful.
117 pub fn min_tests_passedrust-analyzer cargo quickcheck 1.1.0 tester/impl#[QuickCheck]min_tests_passed().(mut self, min_tests_passed: u64) -> QuickCheckrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck# {
118 self.min_tests_passedrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck#min_tests_passed. = min_tests_passed;
119 self
120 }
121
122 /// Tests a property and returns the result.
123 ///
124 /// The result returned is either the number of tests passed or a witness
125 /// of failure.
126 ///
127 /// (If you're using Rust's unit testing infrastructure, then you'll
128 /// want to use the `quickcheck` method, which will `panic!` on failure.)
129 pub fn quicktestrust-analyzer cargo quickcheck 1.1.0 tester/impl#[QuickCheck]quicktest().<A>(&mut self, f: A) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<u64, TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#>
130 where
131 A: Testablerust-analyzer cargo quickcheck 1.1.0 tester/Testable#,
132 {
133 let mut n_tests_passed = 0;
134 for _ in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#self.max_testsrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck#max_tests. {
135 if n_tests_passed rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]ge().self.testsrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck#tests. {
136 break;
137 }
138 match f.resultrust-analyzer cargo quickcheck 1.1.0 tester/Testable#result().(&mut self.rngrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck#rng.) {
139 TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult# { statusrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#status.: Passrust-analyzer cargo quickcheck 1.1.0 tester/Status#Pass#, .. } => n_tests_passed rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().1,
140 TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult# { statusrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#status.: Discardrust-analyzer cargo quickcheck 1.1.0 tester/Status#Discard#, .. } => continue,
141 r @ TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult# { statusrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#status.: Failrust-analyzer cargo quickcheck 1.1.0 tester/Status#Fail#, .. } => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(r),
142 }
143 }
144 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(n_tests_passed)
145 }
146
147 /// Tests a property and calls `panic!` on failure.
148 ///
149 /// The `panic!` message will include a (hopefully) minimal witness of
150 /// failure.
151 ///
152 /// It is appropriate to use this method with Rust's unit testing
153 /// infrastructure.
154 ///
155 /// Note that if the environment variable `RUST_LOG` is set to enable
156 /// `info` level log messages for the `quickcheck` crate, then this will
157 /// include output on how many `QuickCheck` tests were passed.
158 ///
159 /// # Example
160 ///
161 /// ```rust
162 /// use quickcheck::QuickCheck;
163 ///
164 /// fn prop_reverse_reverse() {
165 /// fn revrev(xs: Vec<usize>) -> bool {
166 /// let rev: Vec<_> = xs.clone().into_iter().rev().collect();
167 /// let revrev: Vec<_> = rev.into_iter().rev().collect();
168 /// xs == revrev
169 /// }
170 /// QuickCheck::new().quickcheck(revrev as fn(Vec<usize>) -> bool);
171 /// }
172 /// ```
173 pub fn quickcheckrust-analyzer cargo quickcheck 1.1.0 tester/impl#[QuickCheck]quickcheck().<A>(&mut self, f: A)
174 where
175 A: Testablerust-analyzer cargo quickcheck 1.1.0 tester/Testable#,
176 {
177 // Ignore log init failures, implying it has already been done.
178 let _ = craterust-analyzer cargo quickcheck 1.1.0 crate/::env_logger_initrust-analyzer cargo quickcheck 1.1.0 env_logger_init().();
179
180 let n_tests_passed = match self.quicktestrust-analyzer cargo quickcheck 1.1.0 tester/impl#[QuickCheck]quicktest().(f) {
181 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(n_tests_passed) => n_tests_passed,
182 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(result) => panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("{}", result.failed_msgrust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult]failed_msg().()),
183 };
184
185 if n_tests_passed rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]ge().self.min_tests_passedrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck#min_tests_passed. {
186 inforust-analyzer cargo quickcheck 1.1.0 info!!("(Passed {} QuickCheck tests.)", n_tests_passed);
187 } else {
188 panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!(
189 "(Unable to generate enough tests, {} not discarded.)",
190 n_tests_passed
191 );
192 }
193 }
194 }
195
196 /// Convenience function for running `QuickCheck`.
197 ///
198 /// This is an alias for `QuickCheck::new().quickcheck(f)`.
199 pub fn quickcheckrust-analyzer cargo quickcheck 1.1.0 tester/quickcheck().<A: Testablerust-analyzer cargo quickcheck 1.1.0 tester/Testable#>(f: A) {
200 QuickCheckrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck#::newrust-analyzer cargo quickcheck 1.1.0 tester/impl#[QuickCheck]new().().quickcheckrust-analyzer cargo quickcheck 1.1.0 tester/impl#[QuickCheck]quickcheck().(f)
201 }
202
203 /// Describes the status of a single instance of a test.
204 ///
205 /// All testable things must be capable of producing a `TestResult`.
206 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
207 pub struct TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult# {
208 statusrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#status.: Statusrust-analyzer cargo quickcheck 1.1.0 tester/Status#,
209 argumentsrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#arguments.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>>,
210 errrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#err.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
211 }
212
213 /// Whether a test has passed, failed or been discarded.
214 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
215 enum Statusrust-analyzer cargo quickcheck 1.1.0 tester/Status# {
216 Passrust-analyzer cargo quickcheck 1.1.0 tester/Status#Pass#,
217 Failrust-analyzer cargo quickcheck 1.1.0 tester/Status#Fail#,
218 Discardrust-analyzer cargo quickcheck 1.1.0 tester/Status#Discard#,
219 }
220
221 impl TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult# {
222 /// Produces a test result that indicates the current test has passed.
223 pub fn passedrust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult]passed().() -> TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult# {
224 TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#::from_boolrust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult]from_bool().(true)
225 }
226
227 /// Produces a test result that indicates the current test has failed.
228 pub fn failedrust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult]failed().() -> TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult# {
229 TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#::from_boolrust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult]from_bool().(false)
230 }
231
232 /// Produces a test result that indicates failure from a runtime error.
233 pub fn errorrust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult]error().<S: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>>(msg: S) -> TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult# {
234 let mut r = TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#::from_boolrust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult]from_bool().(false);
235 r.errrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#err. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(msg.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().());
236 r
237 }
238
239 /// Produces a test result that instructs `quickcheck` to ignore it.
240 /// This is useful for restricting the domain of your properties.
241 /// When a test is discarded, `quickcheck` will replace it with a
242 /// fresh one (up to a certain limit).
243 pub fn discardrust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult]discard().() -> TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult# {
244 TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult# { statusrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#status.: Discardrust-analyzer cargo quickcheck 1.1.0 tester/Status#Discard#, argumentsrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#arguments.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, errrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#err.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# }
245 }
246
247 /// Converts a `bool` to a `TestResult`. A `true` value indicates that
248 /// the test has passed and a `false` value indicates that the test
249 /// has failed.
250 pub fn from_boolrust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult]from_bool().(b: bool) -> TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult# {
251 TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult# {
252 statusrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#status.: if b { Passrust-analyzer cargo quickcheck 1.1.0 tester/Status#Pass# } else { Failrust-analyzer cargo quickcheck 1.1.0 tester/Status#Fail# },
253 argumentsrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#arguments.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
254 errrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#err.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
255 }
256 }
257
258 /// Tests if a "procedure" fails when executed. The test passes only if
259 /// `f` generates a task failure during its execution.
260 pub fn must_failrust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult]must_fail().<T, F>(f: F) -> TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#
261 where
262 F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> T,
263 F: 'static,
264 T: 'static,
265 {
266 let f = panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/::AssertUnwindSaferust-analyzer cargo core https://github.com/rust-lang/rust/library/core panic/unwind_safe/AssertUnwindSafe#(f);
267 TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#::from_boolrust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult]from_bool().(panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/::catch_unwindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/catch_unwind().(f).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().())
268 }
269
270 /// Returns `true` if and only if this test result describes a failing
271 /// test.
272 pub fn is_failurerust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult]is_failure().(&self) -> bool {
273 match self.statusrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#status. {
274 Failrust-analyzer cargo quickcheck 1.1.0 tester/Status#Fail# => true,
275 Passrust-analyzer cargo quickcheck 1.1.0 tester/Status#Pass# | Discardrust-analyzer cargo quickcheck 1.1.0 tester/Status#Discard# => false,
276 }
277 }
278
279 /// Returns `true` if and only if this test result describes a failing
280 /// test as a result of a run time error.
281 pub fn is_errorrust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult]is_error().(&self) -> bool {
282 self.is_failurerust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult]is_failure().() && self.errrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#err..is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().()
283 }
284
285 fn failed_msgrust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult]failed_msg().(&self) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
286 let arguments_msg = match self.argumentsrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#arguments. {
287 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => "No Arguments Provided".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(),
288 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ref args) => formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("Arguments: ({})", args.joinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]join().(", ")),
289 };
290 match self.errrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#err. {
291 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("[quickcheck] TEST FAILED. {arguments_msg}"),
292 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ref err) => formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
293 "[quickcheck] TEST FAILED (runtime error). {arguments_msg}\nError: {err}"
294 ),
295 }
296 }
297 }
298
299 /// A shorter way of producing a `TestResult` from a `bool`.
300 ///
301 /// # Example
302 ///
303 /// ```rust
304 /// use quickcheck::TestResult;
305 ///
306 /// let result: TestResult = (2 > 1).into();
307 /// assert!(!result.is_failure());
308 /// ```
309 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<bool> for TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult# {
310 fn fromrust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult][`From<bool>`]from().(b: bool) -> TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult# {
311 TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#::from_boolrust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult]from_bool().(b)
312 }
313 }
314
315 /// `Testable` describes types (e.g., a function) whose values can be
316 /// tested.
317 ///
318 /// Anything that can be tested must be capable of producing a `TestResult`
319 /// given a random number generator. This is trivial for types like `bool`,
320 /// which are just converted to either a passing or failing test result.
321 ///
322 /// For functions, an implementation must generate random arguments
323 /// and potentially shrink those arguments if they produce a failure.
324 ///
325 /// It's unlikely that you'll have to implement this trait yourself.
326 pub trait Testablerust-analyzer cargo quickcheck 1.1.0 tester/Testable#: 'static {
327 fn resultrust-analyzer cargo quickcheck 1.1.0 tester/Testable#result().(&self, _: &mut Genrust-analyzer cargo quickcheck 1.1.0 arbitrary/Gen#) -> TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#;
328 }
329
330 impl Testablerust-analyzer cargo quickcheck 1.1.0 tester/Testable# for bool {
331 fn resultrust-analyzer cargo quickcheck 1.1.0 tester/impl#[bool][Testable]result().(&self, _: &mut Genrust-analyzer cargo quickcheck 1.1.0 arbitrary/Gen#) -> TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult# {
332 TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#::from_boolrust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult]from_bool().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self)
333 }
334 }
335
336 impl Testablerust-analyzer cargo quickcheck 1.1.0 tester/Testable# for () {
337 fn resultrust-analyzer cargo quickcheck 1.1.0 tester/impl#[`()`][Testable]result().(&self, _: &mut Genrust-analyzer cargo quickcheck 1.1.0 arbitrary/Gen#) -> TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult# {
338 TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#::passedrust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult]passed().()
339 }
340 }
341
342 impl Testablerust-analyzer cargo quickcheck 1.1.0 tester/Testable# for TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult# {
343 fn resultrust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult][Testable]result().(&self, _: &mut Genrust-analyzer cargo quickcheck 1.1.0 arbitrary/Gen#) -> TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult# {
344 self.clonerust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult][Clone]clone().()
345 }
346 }
347
348 impl<A, E> Testablerust-analyzer cargo quickcheck 1.1.0 tester/Testable# for Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<A, E>
349 where
350 A: Testablerust-analyzer cargo quickcheck 1.1.0 tester/Testable#,
351 E: Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# + 'static,
352 {
353 fn resultrust-analyzer cargo quickcheck 1.1.0 tester/impl#[`Result<A, E>`][Testable]result().(&self, g: &mut Genrust-analyzer cargo quickcheck 1.1.0 arbitrary/Gen#) -> TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult# {
354 match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self {
355 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ref r) => r.resultrust-analyzer cargo quickcheck 1.1.0 tester/Testable#result().(g),
356 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ref err) => TestResultrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#::errorrust-analyzer cargo quickcheck 1.1.0 tester/impl#[TestResult]error().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{err:?}")),
357 }
358 }
359 }
360
361 /// Return a vector of the debug formatting of each item in `args`
362 fn debug_reprsrust-analyzer cargo quickcheck 1.1.0 tester/debug_reprs().(args: &[&dyn Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#]) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> {
363 args.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|x| formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{x:?}")).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()
364 }
365
366 macro_rules! testable_fnrust-analyzer cargo quickcheck 1.1.0 tester/testable_fn! {
367 ($($name: ident),*) => {
368
369 impl<T: Testable,
370 $($name: Arbitrary + Debug),*> Testable for fn($($name),*) -> T {
371 #[allow(non_snake_case)]
372 fn result(&self, g: &mut Gen) -> TestResult {
373 let self_ = *self;
374 let a: ($($name,)*) = Arbitrary::arbitrary(g);
375 let ( $($name,)* ) = a.clone();
376 let mut r = safe(move || {self_($($name),*)}).result(g);
377
378 if r.is_failure() {
379 let mut a = a.shrink();
380 while let Some(t) = a.next() {
381 let ($($name,)*) = t.clone();
382 let mut r_new = safe(move || {self_($($name),*)}).result(g);
383 if r_new.is_failure() {
384 {
385 let ($(ref $name,)*) : ($($name,)*) = t;
386 r_new.arguments = Some(debug_reprs(&[$($name),*]));
387 }
388
389 // The shrunk value *does* witness a failure, so remember
390 // it for now
391 r = r_new;
392
393 // ... and switch over to that value, i.e. try to shrink
394 // it further.
395 a = t.shrink()
396 }
397 }
398 }
399
400 r
401 }
402 }}}
403
404 testable_fnrust-analyzer cargo quickcheck 1.1.0 tester/testable_fn!!();
405 testable_fnrust-analyzer cargo quickcheck 1.1.0 tester/testable_fn!!(A);
406 testable_fnrust-analyzer cargo quickcheck 1.1.0 tester/testable_fn!!(A, B);
407 testable_fnrust-analyzer cargo quickcheck 1.1.0 tester/testable_fn!!(A, B, C);
408 testable_fnrust-analyzer cargo quickcheck 1.1.0 tester/testable_fn!!(A, B, C, D);
409 testable_fnrust-analyzer cargo quickcheck 1.1.0 tester/testable_fn!!(A, B, C, D, E);
410 testable_fnrust-analyzer cargo quickcheck 1.1.0 tester/testable_fn!!(A, B, C, D, E, F);
411 testable_fnrust-analyzer cargo quickcheck 1.1.0 tester/testable_fn!!(A, B, C, D, E, F, G);
412 testable_fnrust-analyzer cargo quickcheck 1.1.0 tester/testable_fn!!(A, B, C, D, E, F, G, H);
413
414 fn saferust-analyzer cargo quickcheck 1.1.0 tester/safe().<T, F>(fun: F) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>
415 where
416 F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> T,
417 F: 'static,
418 T: 'static,
419 {
420 panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/::catch_unwindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/catch_unwind().(panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/::AssertUnwindSaferust-analyzer cargo core https://github.com/rust-lang/rust/library/core panic/unwind_safe/AssertUnwindSafe#(fun)).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|any_err| {
421 // Extract common types of panic payload:
422 // panic and assert produce &str or String
423 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&s) = any_err.downcast_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/impl#[`dyn Any + Send + 'static`]downcast_ref().::<&str>() {
424 s.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()
425 } else if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(s) = any_err.downcast_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/impl#[`dyn Any + Send + 'static`]downcast_ref().::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>() {
426 s.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[T][ToOwned]to_owned().()
427 } else {
428 "UNABLE TO SHOW RESULT OF PANIC.".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()
429 }
430 })
431 }
432
433 #[cfg(test)]
434 mod testrust-analyzer cargo quickcheck 1.1.0 tester/test/ {
435 use craterust-analyzer cargo quickcheck 1.1.0 crate/::{Genrust-analyzer cargo quickcheck 1.1.0 arbitrary/Gen#, QuickCheckrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck#};
436
437 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
438 fn shrinking_regression_issue_126rust-analyzer cargo quickcheck 1.1.0 tester/test/shrinking_regression_issue_126().() {
439 fn thetestrust-analyzer cargo quickcheck 1.1.0 tester/test/thetest().(vals: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<bool>) -> bool {
440 vals.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|&v| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().v).countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/filter/impl#[`Filter<I, P>`][Iterator]count().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().2
441 }
442 let failing_case = QuickCheckrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck#::newrust-analyzer cargo quickcheck 1.1.0 tester/impl#[QuickCheck]new().()
443 .quicktestrust-analyzer cargo quickcheck 1.1.0 tester/impl#[QuickCheck]quicktest().(thetestrust-analyzer cargo quickcheck 1.1.0 tester/test/thetest(). as fn(vals: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<bool>) -> bool)
444 .unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().();
445 let expected_argument = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{:?}", [true, true]);
446 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(failing_case.argumentsrust-analyzer cargo quickcheck 1.1.0 tester/TestResult#arguments., Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![expected_argument]));
447 }
448
449 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
450 fn size_for_small_types_issue_143rust-analyzer cargo quickcheck 1.1.0 tester/test/size_for_small_types_issue_143().() {
451 fn trust-analyzer cargo quickcheck 1.1.0 tester/test/t().(_: i8) -> bool {
452 true
453 }
454 QuickCheckrust-analyzer cargo quickcheck 1.1.0 tester/QuickCheck#::newrust-analyzer cargo quickcheck 1.1.0 tester/impl#[QuickCheck]new().().rngrust-analyzer cargo quickcheck 1.1.0 tester/impl#[QuickCheck]rng().(Genrust-analyzer cargo quickcheck 1.1.0 arbitrary/Gen#::newrust-analyzer cargo quickcheck 1.1.0 arbitrary/impl#[Gen]new().(129)).quickcheckrust-analyzer cargo quickcheck 1.1.0 tester/impl#[QuickCheck]quickcheck().(trust-analyzer cargo quickcheck 1.1.0 tester/test/t(). as fn(i8) -> bool);
455 }
456
457 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
458 fn regression_signed_shrinker_panicrust-analyzer cargo quickcheck 1.1.0 tester/test/regression_signed_shrinker_panic().() {
459 fn foo_can_shrinkrust-analyzer cargo quickcheck 1.1.0 tester/test/foo_can_shrink().(v: i8) -> bool {
460 let _ = craterust-analyzer cargo quickcheck 1.1.0 crate/::Arbitraryrust-analyzer cargo quickcheck 1.1.0 arbitrary/Arbitrary#::shrinkrust-analyzer cargo quickcheck 1.1.0 arbitrary/impl#[i8][Arbitrary]shrink().(&v).takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(100).countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#count().();
461 true
462 }
463 craterust-analyzer cargo quickcheck 1.1.0 crate/::quickcheckrust-analyzer cargo quickcheck 1.1.0 tester/quickcheck().(foo_can_shrinkrust-analyzer cargo quickcheck 1.1.0 tester/test/foo_can_shrink(). as fn(i8) -> bool);
464 }
465 }