Scene/unit Unit test additions and some duplicate items and bug cleanup#19
Scene/unit Unit test additions and some duplicate items and bug cleanup#19CoraBlack merged 5 commits intogkit-org:scene/unitfrom
Conversation
moyichen0219
left a comment
There was a problem hiding this comment.
The implementation of the iterator and the corresponding unit tests look solid. The test covers forward, reverse, and const iterations, which is great.
Nit: In the const iteration part of test_iterator, I'd suggest explicitly using const auto& u to verify that the const_iterator indeed returns a read-only reference.
This is just a minor improvement and shouldn't block the merge. Approved!
There was a problem hiding this comment.
Pull request overview
This PR adds unit tests for the Unit iterator functionality and fixes const-correctness issues in the iterator implementation. The changes address both bug fixes (incorrect const qualifier placement) and code cleanup (removing duplicate type alias declarations).
Changes:
- Fixed const qualifier placement in iterator operators from
-> reference consttoconst -> referencefor proper const member function syntax - Removed duplicate
usingdeclarations for reverse iterators from unit.cpp (they already exist in unit.hpp) - Added comprehensive iterator unit tests covering forward iteration, reverse iteration, increment/decrement operators, and const iterators
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 11 comments.
| File | Description |
|---|---|
| test/core/scene/test_unit_iterator.cpp | New comprehensive test file for Unit iterator functionality including forward/reverse iteration and const correctness |
| src/core/scene/unit.cpp | Fixed const qualifier placement in iterator operators and removed duplicate reverse_iterator type alias declarations |
| src/core/include/scene/unit.hpp | Fixed const qualifier placement in iterator operator declarations and improved code formatting |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| int exit_calls = 0; | ||
| std::string name_storage; | ||
|
|
||
| // 自定义构造函数 |
There was a problem hiding this comment.
Comment is in Chinese ("自定义构造函数" means "Custom constructor"). For consistency and accessibility, consider using English for all code comments, matching the convention in the rest of the codebase (e.g., test/core/scene/test_unit.cpp uses English comments).
| it++; | ||
| TEST(&(*it) == children[2], "post-increment"); | ||
| --it; | ||
| TEST(&(*it) == children[1], "pre-decrement"); | ||
| it--; | ||
| TEST(&(*it) == children[0], "post-decrement"); |
There was a problem hiding this comment.
The test for post-increment and post-decrement operators doesn't verify the return value. Post-increment (it++) should return the iterator value before incrementing. The test should verify both that the returned value points to the correct element AND that the iterator itself was incremented. Consider: "auto old_it = it++; TEST(&(*old_it) == children[1], "post-increment returns old value"); TEST(&(*it) == children[2], "post-increment advances iterator");"
| it++; | |
| TEST(&(*it) == children[2], "post-increment"); | |
| --it; | |
| TEST(&(*it) == children[1], "pre-decrement"); | |
| it--; | |
| TEST(&(*it) == children[0], "post-decrement"); | |
| auto old_it = it++; | |
| TEST(&(*old_it) == children[1], "post-increment returns old value"); | |
| TEST(&(*it) == children[2], "post-increment advances iterator"); | |
| auto old_it2 = it--; | |
| TEST(&(*old_it2) == children[2], "post-decrement returns old value"); | |
| TEST(&(*it) == children[1], "post-decrement moves iterator back"); | |
| --it; | |
| TEST(&(*it) == children[0], "pre-decrement"); |
| bool test_iterator() { | ||
| std::cout << "\n=== test_iterator ===\n"; | ||
| auto parent = TestUnit::create("parent"); | ||
| std::vector<TestUnit*> children; | ||
| for (int i = 0; i < 5; ++i) { | ||
| auto child = TestUnit::create("child" + std::to_string(i)); | ||
| children.push_back(child.get()); | ||
| parent->add_child(std::move(child)); | ||
| } | ||
| parent->process_handler(); | ||
|
|
||
| int idx = 0; | ||
| for (auto& u : *parent) { | ||
| TEST(&u == children[idx], "iterator order matches addition"); | ||
| idx++; | ||
| } | ||
| TEST(idx == 5, "iterated all 5 children"); | ||
|
|
||
| idx = 4; | ||
| for (auto it = parent->rbegin(); it != parent->rend(); ++it) { | ||
| TEST(&(*it) == children[idx], "reverse iterator order"); | ||
| idx--; | ||
| } | ||
| TEST(idx == -1, "reverse iterated all"); | ||
|
|
||
| auto it = parent->begin(); | ||
| ++it; | ||
| TEST(&(*it) == children[1], "pre-increment"); | ||
| it++; | ||
| TEST(&(*it) == children[2], "post-increment"); | ||
| --it; | ||
| TEST(&(*it) == children[1], "pre-decrement"); | ||
| it--; | ||
| TEST(&(*it) == children[0], "post-decrement"); | ||
|
|
||
| const auto& const_parent = *parent; | ||
| idx = 0; | ||
| for (auto& u : const_parent) { | ||
| TEST(&u == children[idx], "const iterator works"); | ||
| idx++; | ||
| } | ||
| return true; | ||
| } |
There was a problem hiding this comment.
The test doesn't verify iterator behavior with an empty parent (zero children). Consider adding a test case that verifies begin() == end() when a Unit has no children, to ensure the iterator handles this edge case correctly.
There was a problem hiding this comment.
So if you can help me, plz tell me what should I do?
There was a problem hiding this comment.
So if you can help me, plz tell me what should I do?
It won't response to you, please read the requirement from copilot carefully
OK, I will try to deal with the suggestions. This program is also maintenance by another group which named Code-landscape, and it also will have it's website called codelandscape.icu and The domain name is now under review. and it also have a QQ group chat and group owner is Cora Black. If you are interested in it, you can try to use 1084782596 to join the QQ group. |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Unit test additions and some duplicate items and bug cleanup