-
Notifications
You must be signed in to change notification settings - Fork 26
Add Pytest and tests for dsu (#5) #8
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f0b74f3
Add pytest and tests for dsu (#5)
KATO-Hiro efcb5c7
Merge remote-tracking branch 'root_branch/master' into features/pytest
KATO-Hiro 3b4be75
Enable to run Pytest in GitHub Actions
KATO-Hiro acf2601
Remove requirements.txt
KATO-Hiro f725100
Reflect review of existing dsu's tests
KATO-Hiro a8451ab
Add tests
KATO-Hiro 83592b6
Catch AssertionError if invalid input is given
KATO-Hiro 581d843
Remove conftest.py
KATO-Hiro 7d251eb
Rename
KATO-Hiro 238bc46
Add test
KATO-Hiro 1bf643f
Merge branch 'master' into features/pytest
not522 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 hidden or 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
Empty file.
This file contains hidden or 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,210 @@ | ||
| import pytest | ||
|
|
||
| from atcoder.dsu import DSU | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def dsu(): | ||
| return DSU(5) | ||
|
|
||
|
|
||
| class TestDsu: | ||
|
|
||
| def test_initial_status(self, dsu): | ||
| ''' | ||
| An initialized dsu object is expected to be independent of all | ||
| vertices. | ||
|
|
||
| GIVEN an initialized dsu object | ||
| WHEN before executing dsu.merge(vertex a, vertex b) | ||
| THEN all the vertices are independent | ||
| ''' | ||
|
|
||
| pair_of_vertices = self._generate_pair_of_vertices() | ||
|
|
||
| for first_vertex, second_vertex in pair_of_vertices: | ||
| is_same = dsu.same(first_vertex, second_vertex) | ||
| assert not is_same | ||
|
|
||
| for index in range(5): | ||
| assert dsu.size(index) == 1 | ||
| assert dsu.leader(index) == index | ||
|
|
||
| assert dsu.groups() == [[0], [1], [2], [3], [4]] | ||
|
|
||
| def _generate_pair_of_vertices(self): | ||
| from itertools import combinations | ||
|
|
||
| return list(combinations(range(5), 2)) | ||
|
|
||
| def test_merge(self, dsu): | ||
| ''' | ||
| dsu.merge(vertex a, vertex b) is expected to be in the same group. | ||
|
|
||
| GIVEN an initialized dsu object | ||
| WHEN vertex 0 and 1 are merged | ||
| THEN vertex 0 and 1 are the same group. | ||
| ''' | ||
|
|
||
| is_same = dsu.same(0, 1) | ||
| assert not is_same | ||
|
|
||
| dsu.merge(0, 1) | ||
| is_same = dsu.same(0, 1) | ||
| assert is_same | ||
|
|
||
| def test_merge_elements_of_same_group(self, dsu): | ||
| ''' | ||
| merge elements of the same group. | ||
|
|
||
| GIVEN an initialized dsu object | ||
| WHEN merge vertex 0 and 1 twice | ||
| THEN vertex 0 and 1 are the same group. Their size is 2. | ||
| ''' | ||
|
|
||
| is_same = dsu.same(0, 1) | ||
| assert not is_same | ||
|
|
||
| for _ in range(2): | ||
| dsu.merge(0, 1) | ||
| is_same = dsu.same(0, 1) | ||
| assert is_same | ||
| assert dsu.size(0) == 2 | ||
| assert dsu.size(1) == 2 | ||
|
|
||
| def test_size(self, dsu): | ||
| ''' | ||
| dsu.size(vertex a) is expected to get size of vertex a. | ||
|
|
||
| GIVEN an initialized dsu object | ||
| WHEN vertex 0, 1 and 2 are merged | ||
| THEN size of vertex 0 is 3. | ||
| ''' | ||
|
|
||
| dsu.merge(0, 1) | ||
| dsu.merge(0, 2) | ||
| assert dsu.size(0) == 3 | ||
|
|
||
| def test_leader(self, dsu): | ||
| ''' | ||
| dsu.leader(vertex a) is expected to return the representative of the | ||
| connected component that contains the vertex a. | ||
|
|
||
| GIVEN an initialized dsu object | ||
| WHEN vertex 0, 1 and 2 are merged | ||
| THEN vertex 1 and 2 belong to any of the vertices 0-2. | ||
| ''' | ||
|
|
||
| dsu.merge(0, 1) | ||
| dsu.merge(0, 2) | ||
|
|
||
| assert dsu.leader(1) in [0, 1, 2] | ||
| assert dsu.leader(2) in [0, 1, 2] | ||
| assert dsu.leader(1) == dsu.leader(2) | ||
| assert dsu.leader(3) not in [0, 1, 2] | ||
| assert dsu.leader(4) not in [0, 1, 2] | ||
|
|
||
| def test_groups(self, dsu): | ||
| ''' | ||
| dsu.groups() is expected to return the list of the graph that divided | ||
| into connected components. | ||
|
|
||
| GIVEN an initialized dsu object | ||
| WHEN vertex 0, 1 and 2 are merged | ||
| THEN returns [[0, 1, 2], [3], [4]] | ||
| ''' | ||
|
|
||
| dsu.merge(0, 1) | ||
| dsu.merge(0, 2) | ||
|
|
||
| assert dsu.groups() == [[0, 1, 2], [3], [4]] | ||
|
|
||
| @pytest.mark.parametrize(( | ||
| 'vertex_a', 'vertex_b' | ||
| ), [ | ||
| (-2, 4), | ||
| (-1, 4), | ||
| (0, 5), | ||
| (0, 6), | ||
| (-1, 5), | ||
| (-1, 6), | ||
| ]) | ||
| def test_merge_failed_if_invalid_input_is_given(self, dsu, | ||
| vertex_a, vertex_b): | ||
| ''' | ||
| dsu.merge(vertex a, vertex b) is expected to be raised an | ||
| AssertionError if an invalid input is given. | ||
|
|
||
| GIVEN an initialized dsu object | ||
| WHEN an out-of-range index is given | ||
| THEN raises an AssertionError | ||
| ''' | ||
|
|
||
| with pytest.raises(AssertionError): | ||
| dsu.merge(vertex_a, vertex_b) | ||
|
|
||
| @pytest.mark.parametrize(( | ||
| 'vertex_a', 'vertex_b' | ||
| ), [ | ||
| (-2, 4), | ||
| (-1, 4), | ||
| (0, 5), | ||
| (0, 6), | ||
| (-1, 5), | ||
| (-1, 6), | ||
| ]) | ||
| def test_same_failed_if_invalid_input_is_given(self, dsu, | ||
| vertex_a, vertex_b): | ||
| ''' | ||
| dsu.same(vertex a, vertex b) is expected to be raised an AssertionError | ||
| if an invalid input is given. | ||
|
|
||
| GIVEN an initialized dsu object | ||
| WHEN an out-of-range index is given | ||
| THEN raises an AssertionError | ||
| ''' | ||
|
|
||
| with pytest.raises(AssertionError): | ||
| dsu.same(vertex_a, vertex_b) | ||
|
|
||
| @pytest.mark.parametrize(( | ||
| 'vertex_a' | ||
| ), [ | ||
| -2, | ||
| -1, | ||
| 5, | ||
| 6, | ||
| ]) | ||
| def test_leader_failed_if_invalid_input_is_given(self, dsu, vertex_a): | ||
| ''' | ||
| dsu.leader(vertex a) is expected to be raised an AssertionError if an | ||
| invalid input is given. | ||
|
|
||
| GIVEN an initialized dsu object | ||
| WHEN an out-of-range index is given | ||
| THEN raises an AssertionError | ||
| ''' | ||
|
|
||
| with pytest.raises(AssertionError): | ||
| dsu.leader(vertex_a) | ||
|
|
||
| @pytest.mark.parametrize(( | ||
| 'vertex_a' | ||
| ), [ | ||
| -2, | ||
| -1, | ||
| 5, | ||
| 6, | ||
| ]) | ||
| def test_size_failed_if_invalid_input_is_given(self, dsu, vertex_a): | ||
| ''' | ||
| dsu.size(vertex a) is expected to be raised an AssertionError if an | ||
| invalid input is given. | ||
|
|
||
| GIVEN an initialized dsu object | ||
| WHEN an out-of-range index is given | ||
| THEN raises an AssertionError | ||
| ''' | ||
|
|
||
| with pytest.raises(AssertionError): | ||
| dsu.size(vertex_a) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.