New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Impl Arbitrary for Naive{Date,Time,DateTime} #848
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#![cfg_attr(docsrs, doc(cfg(feature = "arbitrary")))] | ||
|
||
use super::NaiveTime; | ||
use arbitrary::{Arbitrary, Unstructured}; | ||
|
||
impl Arbitrary<'_> for NaiveTime { | ||
fn arbitrary(u: &mut Unstructured) -> arbitrary::Result<NaiveTime> { | ||
let secs = u.int_in_range(0..=86_399)?; | ||
let nano = u.int_in_range(0..=1_999_999_999)?; | ||
let time = NaiveTime::from_num_seconds_from_midnight_opt(secs, nano) | ||
.expect("Could not generate a valid chrono::NaiveTime. It looks like implementation of Arbitrary for NaiveTime is erroneous."); | ||
Ok(time) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
const UNSTRCUTURED_BINARY1: [u8; 8] = [0x8f, 0xc2, 0x95, 0xdc, 0x3e, 0x45, 0xb2, 0x3e]; | ||
const UNSTRCUTURED_BINARY2: [u8; 8] = [0x8b, 0xad, 0x2c, 0xc9, 0xf0, 0x05, 0x75, 0x84]; | ||
|
||
#[test] | ||
fn test_different_unstructured() { | ||
let mut unstrctured1 = Unstructured::new(&UNSTRCUTURED_BINARY1); | ||
let time1 = NaiveTime::arbitrary(&mut unstrctured1).unwrap(); | ||
|
||
let mut unstrctured2 = Unstructured::new(&UNSTRCUTURED_BINARY2); | ||
let time2 = NaiveTime::arbitrary(&mut unstrctured2).unwrap(); | ||
|
||
assert_ne!(time1, time2); | ||
} | ||
|
||
#[test] | ||
fn test_same_unstructured() { | ||
let mut unstrctured1 = Unstructured::new(&UNSTRCUTURED_BINARY1); | ||
let time1 = NaiveTime::arbitrary(&mut unstrctured1).unwrap(); | ||
|
||
let mut unstrctured2 = Unstructured::new(&UNSTRCUTURED_BINARY1); | ||
let time2 = NaiveTime::arbitrary(&mut unstrctured2).unwrap(); | ||
|
||
assert_eq!(time1, time2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the rationale for panicking here vs returning an error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My primary motivation was this comment #559 (comment), which you thumbed up.
I am OK with both approaches, both have their pros and cons. With this one (unwrap/expect) we'll detect the problem faster if somethings gets broken. (Arbitrary is implement that way, that if something returns an error, it retries to generate data until it succeeds).