diff options
Diffstat (limited to 'vendor/proptest/examples')
-rw-r--r-- | vendor/proptest/examples/config-defaults.rs | 14 | ||||
-rw-r--r-- | vendor/proptest/examples/dateparser_v1.rs | 48 | ||||
-rw-r--r-- | vendor/proptest/examples/dateparser_v2.rs | 66 | ||||
-rw-r--r-- | vendor/proptest/examples/fib.rs | 51 | ||||
-rw-r--r-- | vendor/proptest/examples/tutorial-simplify-play.rs | 27 | ||||
-rw-r--r-- | vendor/proptest/examples/tutorial-strategy-play.rs | 29 |
6 files changed, 235 insertions, 0 deletions
diff --git a/vendor/proptest/examples/config-defaults.rs b/vendor/proptest/examples/config-defaults.rs new file mode 100644 index 000000000..8c9817af6 --- /dev/null +++ b/vendor/proptest/examples/config-defaults.rs @@ -0,0 +1,14 @@ +//- +// Copyright 2017 Jason Lingle +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use proptest::test_runner::Config; + +fn main() { + println!("Default config: {:?}", Config::default()); +} diff --git a/vendor/proptest/examples/dateparser_v1.rs b/vendor/proptest/examples/dateparser_v1.rs new file mode 100644 index 000000000..9b9c78c7c --- /dev/null +++ b/vendor/proptest/examples/dateparser_v1.rs @@ -0,0 +1,48 @@ +//- +// Copyright 2017 Jason Lingle +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use proptest::prelude::*; + +fn parse_date(s: &str) -> Option<(u32, u32, u32)> { + if 10 != s.len() { + return None; + } + // ! + if "-" != &s[4..5] || "-" != &s[7..8] { + return None; + } + + let year = &s[0..4]; + let month = &s[6..7]; // ! + let day = &s[8..10]; + + year.parse::<u32>().ok().and_then(|y| { + month + .parse::<u32>() + .ok() + .and_then(|m| day.parse::<u32>().ok().map(|d| (y, m, d))) + }) +} + +// NB We omit #[test] on these functions so that main() can call them. +proptest! { + fn doesnt_crash(s in "\\PC*") { + parse_date(&s); + } +} + +fn main() { + assert_eq!(None, parse_date("2017-06-1")); + assert_eq!(None, parse_date("2017-06-170")); + assert_eq!(None, parse_date("2017006-17")); + assert_eq!(None, parse_date("2017-06017")); + assert_eq!(Some((2017, 6, 17)), parse_date("2017-06-17")); + + doesnt_crash(); +} diff --git a/vendor/proptest/examples/dateparser_v2.rs b/vendor/proptest/examples/dateparser_v2.rs new file mode 100644 index 000000000..45ec4e8f9 --- /dev/null +++ b/vendor/proptest/examples/dateparser_v2.rs @@ -0,0 +1,66 @@ +//- +// Copyright 2017 Jason Lingle +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use proptest::prelude::*; + +fn parse_date(s: &str) -> Option<(u32, u32, u32)> { + if 10 != s.len() { + return None; + } + + // NEW: Ignore non-ASCII strings so we don't need to deal with Unicode. + if !s.is_ascii() { + return None; + } + + if "-" != &s[4..5] || "-" != &s[7..8] { + return None; + } + + let year = &s[0..4]; + let month = &s[6..7]; // ! + let day = &s[8..10]; + + year.parse::<u32>().ok().and_then(|y| { + month + .parse::<u32>() + .ok() + .and_then(|m| day.parse::<u32>().ok().map(|d| (y, m, d))) + }) +} + +// NB We omit #[test] on these functions so that main() can call them. +proptest! { + fn doesnt_crash(s in "\\PC*") { + parse_date(&s); + } + + fn parses_all_valid_dates(s in "[0-9]{4}-[0-9]{2}-[0-9]{2}") { + parse_date(&s).unwrap(); + } + + fn parses_date_back_to_original(y in 0u32..10_000, + m in 1u32..13, d in 1u32..32) { + let (y2, m2, d2) = parse_date( + &format!("{:04}-{:02}-{:02}", y, m, d)).unwrap(); + prop_assert_eq!((y, m, d), (y2, m2, d2)); + } +} + +fn main() { + assert_eq!(None, parse_date("2017-06-1")); + assert_eq!(None, parse_date("2017-06-170")); + assert_eq!(None, parse_date("2017006-17")); + assert_eq!(None, parse_date("2017-06017")); + assert_eq!(Some((2017, 6, 17)), parse_date("2017-06-17")); + + doesnt_crash(); + parses_all_valid_dates(); + parses_date_back_to_original(); +} diff --git a/vendor/proptest/examples/fib.rs b/vendor/proptest/examples/fib.rs new file mode 100644 index 000000000..f2d440853 --- /dev/null +++ b/vendor/proptest/examples/fib.rs @@ -0,0 +1,51 @@ +//- +// Copyright 2018 Jason Lingle +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// This #[cfg] is only here so that CI can test building proptest with the +// timeout feature disabled. You do not need it in your code. +#[cfg(feature = "timeout")] +mod fib { + use proptest::prelude::*; + + // The worst possible way to calculate Fibonacci numbers + fn fib(n: u64) -> u64 { + if n <= 1 { + n + } else { + fib(n - 1) + fib(n - 2) + } + } + + proptest! { + #![proptest_config(ProptestConfig { + // Setting both fork and timeout is redundant since timeout implies + // fork, but both are shown for clarity. + fork: true, + timeout: 1000, + .. ProptestConfig::default() + })] + + // NB We omit #[test] on the test function so that main() can call it. + fn test_fib(n in prop::num::u64::ANY) { + // For large n, this will variously run for an extremely long time, + // overflow the stack, or panic due to integer overflow. + assert!(fib(n) >= n); + } + } + + // This is just here so that main can call it + pub fn do_test_fib() { + test_fib(); + } +} + +fn main() { + #[cfg(feature = "timeout")] + fib::do_test_fib(); +} diff --git a/vendor/proptest/examples/tutorial-simplify-play.rs b/vendor/proptest/examples/tutorial-simplify-play.rs new file mode 100644 index 000000000..fed4c1def --- /dev/null +++ b/vendor/proptest/examples/tutorial-simplify-play.rs @@ -0,0 +1,27 @@ +//- +// Copyright 2017 Jason Lingle +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Shows how to pick values from a strategy and simplify them. +// +// This is *not* how proptest is normally used; it is simply used to play +// around with value generation. + +use proptest::strategy::{Strategy, ValueTree}; +use proptest::test_runner::TestRunner; + +fn main() { + let mut runner = TestRunner::default(); + let mut str_val = "[a-z]{1,4}\\p{Cyrillic}{1,4}\\p{Greek}{1,4}" + .new_tree(&mut runner) + .unwrap(); + println!("str_val = {}", str_val.current()); + while str_val.simplify() { + println!(" = {}", str_val.current()); + } +} diff --git a/vendor/proptest/examples/tutorial-strategy-play.rs b/vendor/proptest/examples/tutorial-strategy-play.rs new file mode 100644 index 000000000..52b293093 --- /dev/null +++ b/vendor/proptest/examples/tutorial-strategy-play.rs @@ -0,0 +1,29 @@ +//- +// Copyright 2017 Jason Lingle +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Shows how to pick values from a strategy. +// +// This is *not* how proptest is normally used; it is simply used to play +// around with value generation. + +use proptest::strategy::{Strategy, ValueTree}; +use proptest::test_runner::TestRunner; + +fn main() { + let mut runner = TestRunner::default(); + let int_val = (0..100i32).new_tree(&mut runner).unwrap(); + let str_val = "[a-z]{1,4}\\p{Cyrillic}{1,4}\\p{Greek}{1,4}" + .new_tree(&mut runner) + .unwrap(); + println!( + "int_val = {}, str_val = {}", + int_val.current(), + str_val.current() + ); +} |