Skip to content
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

fix(autoware_universe_utils): fix bug in test #9710

Merged
merged 3 commits into from
Dec 23, 2024

Conversation

veqcc
Copy link
Contributor

@veqcc veqcc commented Dec 23, 2024

Description

This PR fixes a bug in random float value generations in autoware_unvierse_utils.

When I was fixing clang compiler warning,

$ colcon build --packages-select autoware_universe_utils
Starting >>> autoware_universe_utils
--- stderr: autoware_universe_utils                             
/home/veqcc/work/autoware/src/universe/autoware.universe/common/autoware_universe_utils/test/src/math/test_trigonometry.cpp:66:49: error: implicit conversion from 'int' to 'float' changes value from 2147483647 to 2147483648 [-Werror,-Wimplicit-const-int-float-conversion]
    float x = static_cast<float>(std::rand()) / RAND_MAX * 20.0 - 10.0;
                                              ~ ^~~~~~~~
/usr/include/stdlib.h:87:18: note: expanded from macro 'RAND_MAX'
#define RAND_MAX        2147483647
                        ^~~~~~~~~~

I found that the way random values are generated was wrong.
I added std::cout << x << ' ' << y << std::end to check std::rand() is working:

TEST(trigonometry, opencv_fast_atan2)
{
  for (int i = 0; i < 100; ++i) {
    // Generate random x and y between -10 and 10
    std::srand(0);
    float x = static_cast<float>(std::rand()) / RAND_MAX * 20.0 - 10.0;
    float y = static_cast<float>(std::rand()) / RAND_MAX * 20.0 - 10.0;
    std::cout << x << ' ' << y << std::end
    ...
  }
}

Then I found the values x and y were constants.

[ RUN      ] trigonometry.opencv_fast_atan2
6.80375 -2.11234
6.80375 -2.11234
6.80375 -2.11234
6.80375 -2.11234
6.80375 -2.11234
6.80375 -2.11234
6.80375 -2.11234
6.80375 -2.11234
6.80375 -2.11234
6.80375 -2.11234
6.80375 -2.11234
...

I replaced it with more reliable random generator

TEST(trigonometry, opencv_fast_atan2)
{
  std::random_device rd;
  std::mt19937 gen(rd());

  // Generate random x and y between -10 and 10
  std::uniform_real_distribution<float> dis(-10.0f, 10.0f);

  for (int i = 0; i < 100; ++i) {
    const float x = dis(gen);
    const float y = dis(gen);
    std::cout << x << ' ' << y << std::endl;
    ...
  }
}

I confirmed the values are random.

[ RUN      ] trigonometry.opencv_fast_atan2
-0.095993 -0.101926
0.197819 -1.55618
-9.6819 -8.77628
-6.12843 -4.07039
-4.36896 6.82011
4.78307 -8.05005
-7.99907 -5.32101
7.78429 0.780358
-2.63452 0.678116
6.84502 1.99049
-0.410819 3.21309
4.39843 9.20199
4.26471 -5.10922
3.18221 -0.660125
3.48914 -9.87651
6.14163 -1.30869
-9.28647 -7.76091
-6.74409 7.62205
0.0639296 -8.88451
6.97951 -1.29875
-3.45232 -6.32112
-5.72278 7.52109
9.13137 -5.10323
-5.15501 -3.18629
7.2942 -4.47396
...

Related links

Parent Issue:

  • Link

How was this PR tested?

Run

$ colcon test --packages-select autoware_universe_utils

Notes for reviewers

None.

Interface changes

None.

Effects on system behavior

None.

@github-actions github-actions bot added the component:common Common packages from the autoware-common repository. (auto-assigned) label Dec 23, 2024
Copy link

github-actions bot commented Dec 23, 2024

Thank you for contributing to the Autoware project!

🚧 If your pull request is in progress, switch it to draft mode.

Please ensure:

@veqcc veqcc self-assigned this Dec 23, 2024
@veqcc veqcc added the run:build-and-test-differential Mark to enable build-and-test-differential workflow. (used-by-ci) label Dec 23, 2024
@veqcc
Copy link
Contributor Author

veqcc commented Dec 23, 2024

Does anyone know why autoware_raw_vehicle_cmd_converter build/test is failing in CI???

Summary: 160 packages finished [12min 0s]
  1 package had stderr output: autoware_raw_vehicle_cmd_converter
  1 package had test failures: autoware_raw_vehicle_cmd_converter
Error: Process completed with exit code 1.

@veqcc
Copy link
Contributor Author

veqcc commented Dec 23, 2024

I found it.
This PR could be merged after #9716 is merged.

Copy link

codecov bot commented Dec 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 29.79%. Comparing base (8949358) to head (c9a567b).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9710      +/-   ##
==========================================
+ Coverage   29.74%   29.79%   +0.04%     
==========================================
  Files        1444     1445       +1     
  Lines      108693   108686       -7     
  Branches    42665    42666       +1     
==========================================
+ Hits        32336    32382      +46     
+ Misses      73175    73116      -59     
- Partials     3182     3188       +6     
Flag Coverage Δ *Carryforward flag
differential 31.94% <ø> (?)
total 29.77% <ø> (+0.02%) ⬆️ Carriedforward from ddfacd3

*This pull request uses carry forward flags. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@veqcc veqcc merged commit 9bc173c into autowarefoundation:main Dec 23, 2024
35 checks passed
@veqcc veqcc deleted the fix/universe_util_test_bug branch December 23, 2024 06:41
kminoda pushed a commit to kminoda/autoware.universe that referenced this pull request Dec 25, 2024
kyoichi-sugahara pushed a commit to kyoichi-sugahara/autoware.universe that referenced this pull request Dec 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:common Common packages from the autoware-common repository. (auto-assigned) run:build-and-test-differential Mark to enable build-and-test-differential workflow. (used-by-ci)
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants