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
Optionally implement Arbitrary for Naive{Date,Time,DateTime} #559
Conversation
It seems like |
@quodlibetor I'd love to use it.
This test would be much more smaller and, what is also important, not biased (unlike me, a developer). |
If someone is willing to rebase or resubmit it, I would certainly consider it worthwhile. |
@djc Hi! The PR was rebased (unfortunately I also missed that). |
#[cfg(feature = "arbitrary")] | ||
impl arbitrary::Arbitrary<'_> for NaiveTime { | ||
fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result<NaiveTime> { | ||
let secs = u.int_in_range(0..=86_400)?; |
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.
If I understand correctly these ranges should not be inclusive... a day has up to 86399 seconds, and frac
is supposed to be smaller than 2_000_000_000
.
See
Line 391 in 1281ce0
if hour >= 24 || min >= 60 || sec >= 60 || nano >= 2_000_000_000 { |
impl arbitrary::Arbitrary<'_> for NaiveDate { | ||
fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result<NaiveDate> { | ||
let year = u.int_in_range(MIN_YEAR..=MAX_YEAR)?; | ||
let ord = u.int_in_range(1..=366)?; |
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.
Could we construct the YearFlags and determine the exact ndays in the year so constructing the date wouldn't fail
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.
That doesn't seem like it makes a particularly large difference?
fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result<NaiveTime> { | ||
let secs = u.int_in_range(0..=86_400)?; | ||
let frac = u.int_in_range(0..=2_000_000_000)?; | ||
Ok(NaiveTime { secs, frac }) |
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.
This feels like something that might be better to use from_num_seconds_from_midnight_opt
(and unwrap it with a clear error that it is a chrono
bug if it fails). That way you can't silently make invalid NaiveTime
s.
And yeah, a secs of 86_400 will be rejected.
I'm not sure if you guys are interested in taking a change like this. If you are, I'd be happy to fill in more impls.