Skip to content
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

Fix for infinite Ranges #4127

Merged
merged 7 commits into from
Jun 11, 2022
Merged

Fix for infinite Ranges #4127

merged 7 commits into from
Jun 11, 2022

Conversation

stefandd
Copy link
Contributor

The has_next() function in Range relies entirely on the variable _forward to decide whether or not another iteration exists. However, _forward is too narrowly defined to properly cover some or some _infinite == true cases. Currently some cases of step being 0 or NaN depending on min > or < max were broken (ie. not infinite)

@ponylang-main ponylang-main added the discuss during sync Should be discussed during an upcoming sync label May 27, 2022
@SeanTAllen
Copy link
Member

Can you include a test to prevent regression in the future? I assume that it is possible to write such a test.

@SeanTAllen SeanTAllen added the changelog - fixed Automatically add "Fixed" CHANGELOG entry on merge label May 27, 2022
@ponylang-main
Copy link
Contributor

Hi @stefandd,

The changelog - fixed label was added to this pull request; all PRs with a changelog label need to have release notes included as part of the PR. If you haven't added release notes already, please do.

Release notes are added by creating a uniquely named file in the .release-notes directory. We suggest you call the file 4127.md to match the number of this pull request.

The basic format of the release notes (using markdown) should be:

## Title

End user description of changes, why it's important,
problems it solves etc.

If a breaking change, make sure to include 1 or more
examples what code would look like prior to this change
and how to update it to work after this change.

Thanks.

@SeanTAllen
Copy link
Member

The title for this PR will be in the CHANGELOG and as such should have a human meaningful title that is descriptive unto itself. @stefandd do you have suggestions for such a title/changelog entry?

@stefandd
Copy link
Contributor Author

stefandd commented May 27, 2022 via email

@SeanTAllen SeanTAllen changed the title Fixes #4126 - bug in Range Fix for Range with NaN step parameters May 27, 2022
@stefandd
Copy link
Contributor Author

stefandd commented May 27, 2022

Can you include a test to prevent regression in the future? I assume that it is possible to write such a test.

I extended the _assert_infinite() function in packages/collections/_test.pony to now test for both the first AND the second call to .next() being valid in case of infinite Ranges. So far, tests for .next() calls were badly missing for infinite Ranges (see number of failed tests below)!

I test for the second .next() to be valid since firstly, one could infer from 2 to infinity :-), but secondly and more importantly because a side effect of the _forward = (_min < _max) and (_inc > 0) is that it is always false for step=NaN or =0. When now a Range has a _min > _max such as for example Range[F64](1000, 0, nan), it gets through the first .next() call due to this by "luck", but starts failing on the second .next() call. So the first Range[F64](1000, 0, nan).next() call returns 1000, but the next one causes an error. With the modified _assert_infinite(), this is now caught!

...
**** FAILED: collections/Range
Range(0, 1, -1).next(): first call should not fail but should produce 0
Range(0, 1, -1).next(): subsequent call should not fail
Range(0, 1, 0).next(): first call should not fail but should produce 0
Range(0, 1, 0).next(): subsequent call should not fail
Range(-0.1, 0.2, -0.1).next(): first call should not fail but should produce -0.1
Range(-0.1, 0.2, -0.1).next(): subsequent call should not fail
Range(nan, 0, 1).next(): first call should not fail but should produce nan
Range(nan, 0, 1).next(): subsequent call should not fail
Range(0, nan, 1).next(): first call should not fail but should produce 0
Range(0, nan, 1).next(): subsequent call should not fail
Range(0, 100, nan).next(): first call should not fail but should produce 0
Range(0, 100, nan).next(): subsequent call should not fail
Range(10, inf, 0).next(): first call should not fail but should produce 10
Range(10, inf, 0).next(): subsequent call should not fail
Range(0, 10, inf).next(): subsequent call should not fail
Range(100, 10, -inf).next(): subsequent call should not fail
---- Passed: collections/RingBuffer
---- Passed: collections/Sort
----
---- 25 tests ran.
---- Passed: 24
**** FAILED: 1 test, listed below:
**** FAILED: collections/Range

With the fix, all tests pass!

@stefandd stefandd changed the title Fix for Range with NaN step parameters Fix for infinite Ranges May 27, 2022
@jemc
Copy link
Member

jemc commented May 31, 2022

I'm not sure why the "arm64 Apple Darwin" job failed, so I've rerun the CI job.

@jemc jemc removed the discuss during sync Should be discussed during an upcoming sync label May 31, 2022
The `has_next()` function in `Range` relies entirely on the variable `_forward` to decide whether or not another iteration exists. However, `forward` is too narrowly defined to properly cover some or some `_infinite == true` cases. Currently some cases of `step` being 0 or NaN depending on `min > or < max` were broken (ie. not infinite)
The extens the _assert_infinite() function in packages/collections/_test.pony to detect bugs in infinite Ranges
In addition to test the first .next() to not fail, a value assertion for the expected result this call (min) was added
@stefandd stefandd requested review from ergl and SeanTAllen June 2, 2022 14:41
@stefandd
Copy link
Contributor Author

stefandd commented Jun 3, 2022

@jemc I am not sure about the GitHub and PonyC etiquette: was it proper to request the review by all 3 reviewers after you approved? I saw 3 assigned reviewers and thought that this wass the right thing to do.

@ponylang-main ponylang-main added the discuss during sync Should be discussed during an upcoming sync label Jun 3, 2022
@ergl
Copy link
Member

ergl commented Jun 3, 2022

@stefandd It's fine. I've been a bit busy so I won't probably get the time to properly review this until Sunday.

packages/collections/_test.pony Show resolved Hide resolved
.release-notes/4127.md Outdated Show resolved Hide resolved
@SeanTAllen SeanTAllen merged commit 9c4894b into ponylang:main Jun 11, 2022
@ponylang-main ponylang-main removed the discuss during sync Should be discussed during an upcoming sync label Jun 11, 2022
github-actions bot pushed a commit that referenced this pull request Jun 11, 2022
github-actions bot pushed a commit that referenced this pull request Jun 11, 2022
jemc pushed a commit to jemc/ponyc that referenced this pull request Jul 7, 2022
The extens the _assert_infinite() function in packages/collections/_test.pony to detect bugs in infinite Ranges
jemc pushed a commit that referenced this pull request Jul 7, 2022
The extens the _assert_infinite() function in packages/collections/_test.pony to detect bugs in infinite Ranges
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog - fixed Automatically add "Fixed" CHANGELOG entry on merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants