diff options
Diffstat (limited to 'vendor/proptest/examples/dateparser_v1.rs')
-rw-r--r-- | vendor/proptest/examples/dateparser_v1.rs | 48 |
1 files changed, 48 insertions, 0 deletions
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(); +} |