Debian Codegraph @

debian/missing-sources/proptest/examples/fib.rs

raw
1 //-
2 // Copyright 2018 Jason Lingle
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 // This #[cfg] is only here so that CI can test building proptest with the
11 // timeout feature disabled. You do not need it in your code.
12 #[cfg(feature = "timeout")]
13 mod fib {
14 use proptest::prelude::*;
15
16 // The worst possible way to calculate Fibonacci numbers
17 fn fib(n: u64) -> u64 {
18 if n <= 1 {
19 n
20 } else {
21 fib(n - 1) + fib(n - 2)
22 }
23 }
24
25 proptest! {
26 #![proptest_config(ProptestConfig {
27 // Setting both fork and timeout is redundant since timeout implies
28 // fork, but both are shown for clarity.
29 fork: true,
30 timeout: 1000,
31 .. ProptestConfig::default()
32 })]
33
34 // NB We omit #[test] on the test function so that main() can call it.
35 fn test_fib(n in prop::num::u64::ANY) {
36 // For large n, this will variously run for an extremely long time,
37 // overflow the stack, or panic due to integer overflow.
38 assert!(fib(n) >= n);
39 }
40 }
41
42 // This is just here so that main can call it
43 pub fn do_test_fib() {
44 test_fib();
45 }
46 }
47
48 fn main() {
49 #[cfg(feature = "timeout")]
50 fib::do_test_fib();
51 }