-
-
Notifications
You must be signed in to change notification settings - Fork 2k
test(unit): add tests for utility, round_robin, and stat_trackers #5308
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
dasepmoch
wants to merge
1
commit into
LizardByte:master
Choose a base branch
from
dasepmoch:test/add-utility-round-robin-stat-trackers-tests
base: master
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.
+752
−0
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| /** | ||
| * @file tests/unit/test_round_robin.cpp | ||
| * @brief Test src/round_robin.h. | ||
| */ | ||
| #include "../tests_common.h" | ||
|
|
||
| #include <src/round_robin.h> | ||
|
|
||
| #include <vector> | ||
|
|
||
| // ========== Basic iteration tests ========== | ||
|
|
||
| TEST(RoundRobinTests, WrapsAroundOnIncrement) { | ||
| std::vector<int> data = {10, 20, 30}; | ||
| auto rr = round_robin_util::make_round_robin<int>(data.begin(), data.end()); | ||
|
|
||
| EXPECT_EQ(*rr, 10); | ||
| ++rr; | ||
| EXPECT_EQ(*rr, 20); | ||
| ++rr; | ||
| EXPECT_EQ(*rr, 30); | ||
| ++rr; | ||
| // Should wrap around to the beginning | ||
| EXPECT_EQ(*rr, 10); | ||
| } | ||
|
|
||
| TEST(RoundRobinTests, WrapsAroundOnDecrement) { | ||
| std::vector<int> data = {10, 20, 30}; | ||
| auto rr = round_robin_util::make_round_robin<int>(data.begin(), data.end()); | ||
|
|
||
| // Decrement from the start should wrap to end | ||
| --rr; | ||
| EXPECT_EQ(*rr, 30); | ||
| --rr; | ||
| EXPECT_EQ(*rr, 20); | ||
| --rr; | ||
| EXPECT_EQ(*rr, 10); | ||
| } | ||
|
|
||
| TEST(RoundRobinTests, PostIncrement) { | ||
| std::vector<int> data = {1, 2, 3}; | ||
| auto rr = round_robin_util::make_round_robin<int>(data.begin(), data.end()); | ||
|
|
||
| auto prev = rr++; | ||
| EXPECT_EQ(*prev, 1); | ||
| EXPECT_EQ(*rr, 2); | ||
| } | ||
|
|
||
| TEST(RoundRobinTests, PostDecrement) { | ||
| std::vector<int> data = {1, 2, 3}; | ||
| auto rr = round_robin_util::make_round_robin<int>(data.begin(), data.end()); | ||
| ++rr; // move to 2 | ||
|
|
||
| auto prev = rr--; | ||
| EXPECT_EQ(*prev, 2); | ||
| EXPECT_EQ(*rr, 1); | ||
| } | ||
|
|
||
| // ========== Arithmetic operator tests ========== | ||
|
|
||
| TEST(RoundRobinTests, PlusEqualsAdvancesMultipleSteps) { | ||
| std::vector<int> data = {10, 20, 30, 40, 50}; | ||
| auto rr = round_robin_util::make_round_robin<int>(data.begin(), data.end()); | ||
|
|
||
| rr += 3; | ||
| EXPECT_EQ(*rr, 40); | ||
| } | ||
|
|
||
| TEST(RoundRobinTests, PlusEqualsWrapsAround) { | ||
| std::vector<int> data = {10, 20, 30}; | ||
| auto rr = round_robin_util::make_round_robin<int>(data.begin(), data.end()); | ||
|
|
||
| rr += 5; // wraps: 10->20->30->10->20->30... position 5 mod 3 = 2 | ||
| EXPECT_EQ(*rr, 30); | ||
| } | ||
|
|
||
| TEST(RoundRobinTests, MinusEqualsRewindsMultipleSteps) { | ||
| std::vector<int> data = {10, 20, 30, 40, 50}; | ||
| auto rr = round_robin_util::make_round_robin<int>(data.begin(), data.end()); | ||
|
|
||
| rr += 4; // at 50 | ||
| rr -= 2; // back to 30 | ||
| EXPECT_EQ(*rr, 30); | ||
| } | ||
|
|
||
| TEST(RoundRobinTests, PlusOperatorDoesNotModifyOriginal) { | ||
| std::vector<int> data = {10, 20, 30}; | ||
| auto rr = round_robin_util::make_round_robin<int>(data.begin(), data.end()); | ||
|
|
||
| auto rr2 = rr + 2; | ||
| EXPECT_EQ(*rr, 10); // original unchanged | ||
| EXPECT_EQ(*rr2, 30); | ||
| } | ||
|
|
||
| TEST(RoundRobinTests, MinusOperatorDoesNotModifyOriginal) { | ||
| std::vector<int> data = {10, 20, 30}; | ||
| auto rr = round_robin_util::make_round_robin<int>(data.begin(), data.end()); | ||
| rr += 2; // at 30 | ||
|
|
||
| auto rr2 = rr - 1; | ||
| EXPECT_EQ(*rr, 30); // original unchanged | ||
| EXPECT_EQ(*rr2, 20); | ||
| } | ||
|
|
||
| // ========== Comparison operator tests ========== | ||
|
|
||
| TEST(RoundRobinTests, EqualityWhenSamePosition) { | ||
| std::vector<int> data = {10, 20, 30}; | ||
| auto rr1 = round_robin_util::make_round_robin<int>(data.begin(), data.end()); | ||
| auto rr2 = round_robin_util::make_round_robin<int>(data.begin(), data.end()); | ||
|
|
||
| EXPECT_TRUE(rr1 == rr2); | ||
| EXPECT_FALSE(rr1 != rr2); | ||
| } | ||
|
|
||
| TEST(RoundRobinTests, InequalityWhenDifferentPosition) { | ||
| std::vector<int> data = {10, 20, 30}; | ||
| auto rr1 = round_robin_util::make_round_robin<int>(data.begin(), data.end()); | ||
| auto rr2 = round_robin_util::make_round_robin<int>(data.begin(), data.end()); | ||
| ++rr2; | ||
|
|
||
| EXPECT_FALSE(rr1 == rr2); | ||
| EXPECT_TRUE(rr1 != rr2); | ||
| } | ||
|
|
||
| // ========== Difference operator tests ========== | ||
|
|
||
| TEST(RoundRobinTests, DifferenceOperator) { | ||
| std::vector<int> data = {10, 20, 30, 40, 50}; | ||
| auto rr1 = round_robin_util::make_round_robin<int>(data.begin(), data.end()); | ||
| auto rr2 = round_robin_util::make_round_robin<int>(data.begin(), data.end()); | ||
| rr2 += 3; | ||
|
|
||
| auto diff = rr2 - rr1; | ||
| EXPECT_EQ(diff, 3); | ||
| } | ||
|
|
||
| // ========== Single element tests ========== | ||
|
|
||
| TEST(RoundRobinTests, SingleElementAlwaysReturnsSame) { | ||
| std::vector<int> data = {42}; | ||
| auto rr = round_robin_util::make_round_robin<int>(data.begin(), data.end()); | ||
|
|
||
| EXPECT_EQ(*rr, 42); | ||
| ++rr; | ||
| EXPECT_EQ(*rr, 42); | ||
| ++rr; | ||
| EXPECT_EQ(*rr, 42); | ||
| } | ||
|
|
||
| // ========== Multiple full cycles ========== | ||
|
|
||
| TEST(RoundRobinTests, MultipleFullCycles) { | ||
| std::vector<int> data = {1, 2, 3}; | ||
| auto rr = round_robin_util::make_round_robin<int>(data.begin(), data.end()); | ||
|
|
||
| // Go around twice | ||
| for (int cycle = 0; cycle < 2; ++cycle) { | ||
| EXPECT_EQ(*rr, 1); | ||
| ++rr; | ||
| EXPECT_EQ(*rr, 2); | ||
| ++rr; | ||
| EXPECT_EQ(*rr, 3); | ||
| ++rr; | ||
| } | ||
| } | ||
|
|
||
| // ========== Pointer dereference test ========== | ||
|
|
||
| TEST(RoundRobinTests, ArrowOperator) { | ||
| struct Item { | ||
| int value; | ||
| std::string name; | ||
| }; | ||
|
|
||
| std::vector<Item> data = {{1, "one"}, {2, "two"}, {3, "three"}}; | ||
| auto rr = round_robin_util::make_round_robin<Item>(data.begin(), data.end()); | ||
|
|
||
| EXPECT_EQ(rr->value, 1); | ||
| EXPECT_EQ(rr->name, "one"); | ||
| ++rr; | ||
| EXPECT_EQ(rr->value, 2); | ||
| EXPECT_EQ(rr->name, "two"); | ||
| } | ||
Oops, something went wrong.
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.
Not really a fan of this style of comment. Can you change them to doxygen style doc blocks?
Or like