-
Notifications
You must be signed in to change notification settings - Fork 7
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
Add ranges.any_gaps
#185
Merged
Merged
Add ranges.any_gaps
#185
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
38cd52e
Add test case to ensure that any_overlapping does not modify ranges
Peter554 7dbfc82
Test any_overlapping against reversed ranges list
Peter554 d0671e3
Add ranges.any_gaps
Peter554 54f2d33
Add benchmark for ranges.any_gaps
Peter554 e927d82
Update change log
Peter554 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,27 @@ | ||
import random | ||
from decimal import Decimal as D | ||
|
||
import pytest | ||
|
||
from xocto import ranges | ||
|
||
|
||
def test_any_overlapping(benchmark): | ||
ranges_ = [ranges.Range(D(i), D(i + 1)) for i in range(1000)] | ||
random.seed(42) | ||
def _shuffled(ranges_, *, seed=42): | ||
ranges_ = ranges_.copy() | ||
random.seed(seed) | ||
random.shuffle(ranges_) | ||
return ranges_ | ||
|
||
|
||
@pytest.mark.benchmark(group="ranges.any_overlapping") | ||
def test_any_overlapping(benchmark): | ||
ranges_ = _shuffled([ranges.Range(D(i), D(i + 1)) for i in range(1000)]) | ||
any_overlapping = benchmark(ranges.any_overlapping, ranges_) | ||
assert any_overlapping is False | ||
|
||
|
||
@pytest.mark.benchmark(group="ranges.any_gaps") | ||
def test_any_gaps(benchmark): | ||
ranges_ = _shuffled([ranges.Range(D(i), D(i + 1)) for i in range(1000)]) | ||
any_overlapping = benchmark(ranges.any_gaps, ranges_) | ||
assert any_overlapping is False |
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 |
---|---|---|
|
@@ -589,6 +589,17 @@ def test_timestamps_sorted(self, period): | |
|
||
|
||
class TestAnyOverlapping: | ||
def test_does_not_modify_ranges(self): | ||
# The implementation of `any_overlapping` relies on sorting. | ||
# Let's make sure that the ranges passed in are unchanged. | ||
ranges_ = [ | ||
ranges.Range(1, 2), | ||
ranges.Range(0, 1), | ||
] | ||
ranges_copy = ranges_.copy() | ||
assert not ranges.any_overlapping(ranges_) | ||
assert ranges_ == ranges_copy | ||
|
||
@pytest.mark.parametrize( | ||
"ranges_", | ||
[ | ||
|
@@ -613,6 +624,7 @@ class TestAnyOverlapping: | |
) | ||
def test_returns_true_if_and_ranges_overlap(self, ranges_): | ||
assert ranges.any_overlapping(ranges_) | ||
assert ranges.any_overlapping(reversed(ranges_)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably have been part of #184 |
||
|
||
@pytest.mark.parametrize( | ||
"ranges_", | ||
|
@@ -631,11 +643,93 @@ def test_returns_true_if_and_ranges_overlap(self, ranges_): | |
) | ||
def test_returns_false_if_no_ranges_overlap(self, ranges_): | ||
assert not ranges.any_overlapping(ranges_) | ||
assert not ranges.any_overlapping(reversed(ranges_)) | ||
|
||
def test_returns_false_for_empty_set_of_ranges(self): | ||
assert not ranges.any_overlapping([]) | ||
|
||
|
||
class TestAnyGaps: | ||
def test_does_not_modify_ranges(self): | ||
# The implementation of `any_gaps` relies on sorting. | ||
# Let's make sure that the ranges passed in are unchanged. | ||
ranges_ = [ | ||
ranges.Range(1, 2), | ||
ranges.Range(0, 1), | ||
] | ||
ranges_copy = ranges_.copy() | ||
assert not ranges.any_gaps(ranges_) | ||
assert ranges_ == ranges_copy | ||
|
||
@pytest.mark.parametrize( | ||
"ranges_", | ||
[ | ||
[ | ||
ranges.Range(0, 1), | ||
ranges.Range(2, 3), | ||
], | ||
[ | ||
ranges.Range( | ||
0, 1, boundaries=ranges.RangeBoundaries.INCLUSIVE_EXCLUSIVE | ||
), | ||
ranges.Range( | ||
1, 2, boundaries=ranges.RangeBoundaries.EXCLUSIVE_INCLUSIVE | ||
), | ||
], | ||
[ | ||
ranges.Range(0, 2), | ||
ranges.Range(4, 6), | ||
ranges.Range(1, 3), | ||
], | ||
], | ||
) | ||
def test_returns_true_if_gaps(self, ranges_): | ||
assert ranges.any_gaps(ranges_) | ||
assert ranges.any_gaps(reversed(ranges_)) | ||
|
||
@pytest.mark.parametrize( | ||
"ranges_", | ||
[ | ||
[ | ||
ranges.Range(0, 1), | ||
ranges.Range(1, 2), | ||
], | ||
[ | ||
ranges.Range( | ||
0, 1, boundaries=ranges.RangeBoundaries.EXCLUSIVE_INCLUSIVE | ||
), | ||
ranges.Range( | ||
1, 2, boundaries=ranges.RangeBoundaries.EXCLUSIVE_INCLUSIVE | ||
), | ||
], | ||
[ | ||
ranges.Range(0, 2), | ||
ranges.Range(1, 3), | ||
], | ||
[ | ||
ranges.Range(0, 3), | ||
ranges.Range(1, 2), | ||
], | ||
[ | ||
ranges.Range(0, 5), | ||
ranges.Range(1, 2), | ||
ranges.Range(3, 4), | ||
], | ||
[ | ||
ranges.Range(0, 2), | ||
ranges.Range(4, 6), | ||
ranges.Range(2, 4), | ||
], | ||
], | ||
) | ||
def test_returns_false_if_no_gaps(self, ranges_): | ||
assert not ranges.any_gaps(ranges_) | ||
assert not ranges.any_gaps(reversed(ranges_)) | ||
|
||
def test_returns_false_for_empty_set_of_ranges(self): | ||
assert not ranges.any_gaps([]) | ||
|
||
|
||
class TestFiniteDateRange: | ||
""" | ||
Test class for methods specific to the the FiniteDateRange subclass. | ||
|
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.
This should probably have been part of #184