Skip to content

test: add unit coverage for utils module#1696

Closed
wxyucs wants to merge 1 commit intomainfrom
yellow/utils-unit-tests
Closed

test: add unit coverage for utils module#1696
wxyucs wants to merge 1 commit intomainfrom
yellow/utils-unit-tests

Conversation

@wxyucs
Copy link
Collaborator

@wxyucs wxyucs commented Mar 13, 2026

Add focused tests for key utils components and fix WindowResultQueue empty-state averaging to avoid division by zero.

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>
@wxyucs wxyucs self-assigned this Mar 13, 2026
Copilot AI review requested due to automatic review settings March 13, 2026 08:47
@wxyucs wxyucs added kind/improvement Code improvements (variable/function renaming, refactoring, etc. ) version/1.0 labels Mar 13, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 utils module by introducing comprehensive unit tests for various core components. Additionally, it includes a critical bug fix to prevent runtime errors in the WindowResultQueue when calculating averages from an empty state.

Highlights

  • Unit Test Coverage: Expanded unit test coverage for several utility components including LinearCongruentialGenerator, LockStrategy, SlowTaskTimer, SparseVectorTransform, Timer, UtilFunctions, and WindowResultQueue.
  • Bug Fix: Addressed a potential division-by-zero error in WindowResultQueue::GetAvgResult when the queue is empty.

🧠 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
  • src/utils/linear_congruential_generator_test.cpp
    • Added new unit tests for LinearCongruentialGenerator to verify value range and sequence changes.
  • src/utils/lock_strategy_test.cpp
    • Introduced unit tests for LockStrategy, PointsMutex, and EmptyMutex covering locking mechanisms, resizing, and concurrent access.
  • src/utils/slow_task_timer_test.cpp
    • Added unit tests for SlowTaskTimer to confirm proper construction and threshold handling.
  • src/utils/sparse_vector_transform_test.cpp
    • Implemented unit tests for sparse_vector_transform functions, including sorting and subset checks.
  • src/utils/timer_test.cpp
    • Added unit tests for the Timer class, verifying elapsed time recording and overtime detection.
  • src/utils/util_functions_test.cpp
    • Introduced unit tests for various utility functions such as format_map, split_string, align_up, is_approx_zero, next_multiple_of_power_of_two, select_k_numbers, check_equal_on_string_stream, base64_encode/decode, create_fast_dataset, and get_current_time.
  • src/utils/window_result_queue.cpp
    • Fixed a potential division-by-zero error in GetAvgResult by returning 0.0F when statistic_num is zero.
  • src/utils/window_result_queue_test.cpp
    • Added unit tests for WindowResultQueue to ensure correct average calculation and fixed window behavior, including an empty queue scenario.
Activity
  • No human activity (comments, reviews) has been recorded on this pull request yet.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 return 0.0F when 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++) {
Comment on lines +27 to +29
std::this_thread::sleep_for(std::chrono::milliseconds(2));
auto elapsed_ms = timer.Record();
REQUIRE(elapsed_ms >= 1.0);
Comment on lines +39 to +45
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);
Comment on lines +43 to +45
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));
@wxyucs wxyucs closed this Mar 16, 2026
@wxyucs wxyucs deleted the yellow/utils-unit-tests branch March 16, 2026 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/improvement Code improvements (variable/function renaming, refactoring, etc. ) size/L version/1.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants