Conversation
Add focused tests for key utils components and fix WindowResultQueue empty-state averaging to avoid division by zero. Signed-off-by: Xiangyu Wang <wxy407827@antgroup.com>
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the robustness of the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request adds a comprehensive suite of unit tests for the utils module, significantly improving code coverage and confidence in these components. It also includes a crucial fix for a potential division-by-zero error in WindowResultQueue. The new tests are well-structured and cover a good range of scenarios. I have one suggestion for improving the API design of a function that is now covered by these new tests.
| REQUIRE(v >= 0); | ||
| REQUIRE(v < 100); | ||
| } | ||
| REQUIRE_THROWS(select_k_numbers(10, 0)); |
There was a problem hiding this comment.
This test asserts that select_k_numbers(10, 0) throws an exception. While the test correctly reflects the current implementation's behavior, it highlights a potential area for API improvement. In many standard libraries and conventions, requesting 0 elements is a valid operation that should return an empty collection rather than throwing an error. Consider updating select_k_numbers to handle k=0 gracefully by returning an empty vector. The test could then be changed to REQUIRE(select_k_numbers(10, 0).empty());, leading to a more intuitive and robust API.
There was a problem hiding this comment.
Pull request overview
Adds unit tests for several src/utils components and fixes WindowResultQueue::GetAvgResult() to safely handle the empty-queue case (avoiding division-by-zero).
Changes:
- Add Catch2 unit tests for multiple utils modules (timer, util_functions, sparse vector transform, lock strategy, LCG, window result queue).
- Fix
WindowResultQueue::GetAvgResult()to return0.0Fwhen there are no samples.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/utils/window_result_queue.cpp | Adds a zero-sample guard to prevent divide-by-zero in averaging. |
| src/utils/window_result_queue_test.cpp | Adds tests for empty, mean, and fixed-window behavior of WindowResultQueue. |
| src/utils/util_functions_test.cpp | Adds unit tests for several helpers (split/format/align/base64/etc.). |
| src/utils/timer_test.cpp | Adds basic timing/threshold/destructor write-back tests for Timer. |
| src/utils/sparse_vector_transform_test.cpp | Adds tests for sparse vector sorting and subset checks. |
| src/utils/slow_task_timer_test.cpp | Adds constructor/basic-field tests for SlowTaskTimer. |
| src/utils/lock_strategy_test.cpp | Adds basic locking and concurrency tests for PointsMutex/guards and EmptyMutex. |
| src/utils/linear_congruential_generator_test.cpp | Adds range and “sequence varies” tests for the LCG. |
| return 0.0F; | ||
| } | ||
| float result = 0; | ||
| for (int i = 0; i < statistic_num; i++) { |
| std::this_thread::sleep_for(std::chrono::milliseconds(2)); | ||
| auto elapsed_ms = timer.Record(); | ||
| REQUIRE(elapsed_ms >= 1.0); |
| for (int i = 1; i <= 20; ++i) { | ||
| queue.Push(static_cast<float>(i)); | ||
| } | ||
| REQUIRE(std::abs(queue.GetAvgResult() - 10.5F) < 1e-6F); | ||
|
|
||
| queue.Push(21.0F); | ||
| REQUIRE(std::abs(queue.GetAvgResult() - 11.5F) < 1e-6F); |
| REQUIRE(after > before); | ||
| mutex_array.Resize(2); | ||
| REQUIRE(mutex_array.GetMemoryUsage() < after); |
| SECTION("next multiple of power of two") { | ||
| REQUIRE(next_multiple_of_power_of_two(5, 2) == 8); | ||
| REQUIRE(next_multiple_of_power_of_two(16, 4) == 16); | ||
| REQUIRE_THROWS(next_multiple_of_power_of_two(1, 64)); |
Add focused tests for key utils components and fix WindowResultQueue empty-state averaging to avoid division by zero.