-
Notifications
You must be signed in to change notification settings - Fork 2
KF-27 Add tests for TreeSet #55
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
Open
belyshevdenis
wants to merge
4
commits into
main
Choose a base branch
from
KF-27-test-tree-set
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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,313 @@ | ||
| #include "pch.h" | ||
| #include <kf/TreeSet.h> | ||
|
|
||
| using namespace kf; | ||
| using Set = kf::TreeSet<int, PagedPool>; | ||
|
|
||
| namespace | ||
| { | ||
| constexpr int kMinElem = 1; | ||
| constexpr int kMaxElem = 5; | ||
| constexpr int kSetSize = kMaxElem - kMinElem + 1; | ||
| } | ||
|
|
||
| SCENARIO("TreeSet: all methods") | ||
| { | ||
| Set set; | ||
|
|
||
| GIVEN("empty set") | ||
| { | ||
| WHEN("isEmpty is called") | ||
| { | ||
| auto isEmpty = set.isEmpty(); | ||
|
|
||
| THEN("the value is true") | ||
| { | ||
| REQUIRE(isEmpty == true); | ||
| } | ||
| } | ||
|
|
||
| WHEN("size is called") | ||
| { | ||
| auto size = set.size(); | ||
|
|
||
| THEN("the size is 0") | ||
| { | ||
| REQUIRE(size == 0); | ||
| } | ||
| } | ||
|
|
||
| WHEN("contains is called") | ||
| { | ||
| auto contains = set.contains(kMinElem); | ||
|
|
||
| THEN("the value is false") | ||
| { | ||
| REQUIRE(contains == false); | ||
| } | ||
| } | ||
|
|
||
| WHEN("find is called") | ||
| { | ||
| auto found = set.find(kMinElem); | ||
|
|
||
| THEN("the value is nullptr") | ||
| { | ||
| REQUIRE(found == nullptr); | ||
| } | ||
| } | ||
|
|
||
| WHEN("remove is called") | ||
| { | ||
| auto removed = set.remove(kMinElem); | ||
|
|
||
| THEN("the removal should fail") | ||
| { | ||
| REQUIRE(removed == false); | ||
| } | ||
| } | ||
|
|
||
| WHEN("clear is called") | ||
| { | ||
| set.clear(); | ||
|
|
||
| THEN("set is still empty") | ||
| { | ||
| REQUIRE(set.isEmpty() == true); | ||
| REQUIRE(set.size() == 0); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| GIVEN("set with elements") | ||
| { | ||
| for (int elem = kMinElem; elem <= kMaxElem; ++elem) | ||
| { | ||
| REQUIRE_NT_SUCCESS(set.add(elem)); | ||
| } | ||
|
|
||
| WHEN("isEmpty is called") | ||
| { | ||
| auto isEmpty = set.isEmpty(); | ||
|
|
||
| THEN("the value is false") | ||
| { | ||
| REQUIRE(isEmpty == false); | ||
| } | ||
| } | ||
|
|
||
| WHEN("size is called") | ||
| { | ||
| auto size = set.size(); | ||
|
|
||
| THEN("the size is valid") | ||
| { | ||
| REQUIRE(size == kSetSize); | ||
| } | ||
| } | ||
|
|
||
| WHEN("contains is called for existing element") | ||
| { | ||
| auto contains = set.contains(kMinElem + 1); | ||
|
|
||
| THEN("the value should be true") | ||
| { | ||
| REQUIRE(contains == true); | ||
| } | ||
| } | ||
|
|
||
| WHEN("contains is called for a non-existing element") | ||
| { | ||
| auto contains = set.contains(kMaxElem + 1); | ||
|
|
||
| THEN("the value should be false") | ||
| { | ||
| REQUIRE(contains == false); | ||
| } | ||
| } | ||
|
|
||
| WHEN("find is called for existing element") | ||
| { | ||
| auto found = set.find(kMinElem + 1); | ||
|
|
||
| THEN("the value should be valid") | ||
| { | ||
| REQUIRE(found != nullptr); | ||
| REQUIRE(*found == kMinElem + 1); | ||
| } | ||
| } | ||
|
|
||
| WHEN("find is called for a non-existing element") | ||
| { | ||
| auto found = set.find(kMaxElem + 1); | ||
|
|
||
| THEN("the value should be nullptr") | ||
| { | ||
| REQUIRE(found == nullptr); | ||
| } | ||
| } | ||
|
|
||
| WHEN("remove is called for existing element") | ||
| { | ||
| auto removed = set.remove(kMinElem); | ||
|
|
||
| THEN("the removal should succeed") | ||
| { | ||
| REQUIRE(removed == true); | ||
| } | ||
|
|
||
| THEN("the element is no longer in the set") | ||
| { | ||
| auto contains = set.contains(kMinElem); | ||
| REQUIRE(contains == false); | ||
| } | ||
|
|
||
| THEN("the size is reduced") | ||
| { | ||
| REQUIRE(set.size() == kSetSize - 1); | ||
| } | ||
| } | ||
|
|
||
| WHEN("remove is called for a non-existing element") | ||
| { | ||
| auto removed = set.remove(kMaxElem + 1); | ||
|
|
||
| THEN("the removal should fail") | ||
| { | ||
| REQUIRE(removed == false); | ||
| } | ||
| } | ||
|
|
||
| WHEN("add is called for new element") | ||
| { | ||
| int newElem = kMaxElem + 1; | ||
| auto status = set.add(newElem); | ||
|
|
||
| THEN("the add should succeed") | ||
| { | ||
| REQUIRE_NT_SUCCESS(status); | ||
| } | ||
|
|
||
| THEN("the element should be accessible") | ||
| { | ||
| auto contains = set.contains(newElem); | ||
| REQUIRE(contains == true); | ||
| } | ||
|
|
||
| THEN("the size should increase") | ||
| { | ||
| REQUIRE(set.size() == kSetSize + 1); | ||
| } | ||
| } | ||
|
|
||
| WHEN("add is called for existing element") | ||
| { | ||
| auto status = set.add(kMinElem); | ||
|
|
||
| THEN("the add should succeed (idempotent)") | ||
| { | ||
| REQUIRE_NT_SUCCESS(status); | ||
| } | ||
|
|
||
| THEN("the element should still be accessible") | ||
| { | ||
| auto contains = set.contains(kMinElem); | ||
| REQUIRE(contains == true); | ||
| } | ||
| } | ||
|
|
||
| WHEN("clear is called") | ||
| { | ||
| set.clear(); | ||
|
|
||
| THEN("set is empty") | ||
| { | ||
| REQUIRE(set.isEmpty() == true); | ||
| REQUIRE(set.size() == 0); | ||
| } | ||
|
|
||
| THEN("valid element is no longer accessible") | ||
| { | ||
| REQUIRE(set.contains(kMinElem) == false); | ||
| } | ||
| } | ||
|
|
||
| WHEN("set is moved") | ||
| { | ||
| Set movedSet = std::move(set); | ||
|
|
||
| THEN("the moved set is not empty") | ||
| { | ||
| REQUIRE(movedSet.isEmpty() == false); | ||
| REQUIRE(movedSet.size() == kSetSize); | ||
| } | ||
|
|
||
| THEN("original set is empty") | ||
| { | ||
| REQUIRE(set.isEmpty() == true); | ||
| REQUIRE(set.size() == 0); | ||
| } | ||
|
|
||
| THEN("moved elements are accessible") | ||
| { | ||
| for (int elem = kMinElem; elem <= kMaxElem; ++elem) | ||
| { | ||
| auto contains = movedSet.contains(elem); | ||
| REQUIRE(contains == true); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| SCENARIO("TreeSetIterator") | ||
| { | ||
| Set set; | ||
|
|
||
| GIVEN("empty set") | ||
| { | ||
| WHEN("TreeSetIterator is created") | ||
| { | ||
| auto it = set.iterator(); | ||
|
|
||
| THEN("hasNext returns false") | ||
| { | ||
| REQUIRE(it.hasNext() == false); | ||
| } | ||
|
|
||
| THEN("next returns a reference to nullptr") | ||
| { | ||
| auto& next = it.next(); | ||
| REQUIRE(&next == nullptr); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| GIVEN("set with elements") | ||
| { | ||
| for (int elem = kMinElem; elem <= kMaxElem; ++elem) | ||
| { | ||
| REQUIRE_NT_SUCCESS(set.add(elem)); | ||
| } | ||
|
|
||
| WHEN("TreeSetIterator is created") | ||
| { | ||
| auto it = set.iterator(); | ||
|
|
||
| int expectedValue = kMinElem; | ||
| int count = 0; | ||
|
|
||
| THEN("it should enumerate all elements in order") | ||
| { | ||
| while (it.hasNext()) | ||
| { | ||
| int value = it.next(); | ||
| REQUIRE(value == expectedValue); | ||
| ++expectedValue; | ||
| ++count; | ||
| } | ||
| REQUIRE(count == (kMaxElem - kMinElem + 1)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.