Skip to content

STYLE: Replace static_cast<T>(X) with T{ X } for any "all caps" X#5337

Merged
dzenanz merged 1 commit intoInsightSoftwareConsortium:masterfrom
N-Dekker:Replace-static_cast-on-all-caps-macro-constants
May 12, 2025
Merged

STYLE: Replace static_cast<T>(X) with T{ X } for any "all caps" X#5337
dzenanz merged 1 commit intoInsightSoftwareConsortium:masterfrom
N-Dekker:Replace-static_cast-on-all-caps-macro-constants

Conversation

@N-Dekker
Copy link
Contributor

@N-Dekker N-Dekker commented May 1, 2025

Replaced static_cast<(\w+)>\(([A-Z_][A-Z_]+)\) with $1{ $2 }. According to common C++ naming conventions, these all "all caps" identifiers refer to constants, specified by preprocessor macro's, so these conversions are evaluated at compile-time.

@github-actions github-actions bot added type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Core Issues affecting the Core module area:Filtering Issues affecting the Filtering module area:IO Issues affecting the IO module area:Segmentation Issues affecting the Segmentation module type:Style Style changes: no logic impact (indentation, comments, naming) labels May 1, 2025
@N-Dekker N-Dekker marked this pull request as ready for review May 1, 2025 12:10
@N-Dekker
Copy link
Contributor Author

N-Dekker commented May 2, 2025

/azp run ITK.Linux

@dzenanz
Copy link
Member

dzenanz commented May 2, 2025

Modules/Core/ImageAdaptors/test/CMakeFiles/ITKImageAdaptorsTestDriver.dir/itkComplexConjugateImageAdaptorTest.cxx.o -MF Modules/Core/ImageAdaptors/test/CMakeFiles/ITKImageAdaptorsTestDriver.dir/itkComplexConjugateImageAdaptorTest.cxx.o.d -o Modules/Core/ImageAdaptors/test/CMakeFiles/ITKImageAdaptorsTestDriver.dir/itkComplexConjugateImageAdaptorTest.cxx.o -c /home/runner/work/ITK/ITK/Modules/Core/ImageAdaptors/test/itkComplexConjugateImageAdaptorTest.cxx
/home/runner/work/ITK/ITK/Modules/Core/ImageAdaptors/test/itkComplexConjugateImageAdaptorTest.cxx: In function 'int itkComplexConjugateImageAdaptorTest(int, char**)':
/home/runner/work/ITK/ITK/Modules/Core/ImageAdaptors/test/itkComplexConjugateImageAdaptorTest.cxx:42:47: error: narrowing conversion of '2147483647' from 'int' to 'float' [-Wnarrowing]
   42 |     auto            randMax = float{ RAND_MAX };
      |                                               ^

@N-Dekker
Copy link
Contributor Author

N-Dekker commented May 2, 2025

Modules/Core/ImageAdaptors/test/CMakeFiles/ITKImageAdaptorsTestDriver.dir/itkComplexConjugateImageAdaptorTest.cxx.o -MF Modules/Core/ImageAdaptors/test/CMakeFiles/ITKImageAdaptorsTestDriver.dir/itkComplexConjugateImageAdaptorTest.cxx.o.d -o Modules/Core/ImageAdaptors/test/CMakeFiles/ITKImageAdaptorsTestDriver.dir/itkComplexConjugateImageAdaptorTest.cxx.o -c /home/runner/work/ITK/ITK/Modules/Core/ImageAdaptors/test/itkComplexConjugateImageAdaptorTest.cxx
/home/runner/work/ITK/ITK/Modules/Core/ImageAdaptors/test/itkComplexConjugateImageAdaptorTest.cxx: In function 'int itkComplexConjugateImageAdaptorTest(int, char**)':
/home/runner/work/ITK/ITK/Modules/Core/ImageAdaptors/test/itkComplexConjugateImageAdaptorTest.cxx:42:47: error: narrowing conversion of '2147483647' from 'int' to 'float' [-Wnarrowing]
   42 |     auto            randMax = float{ RAND_MAX };
      |                                               ^

Interesting, thanks @dzenanz! This is exactly the purpose of T{ X }! Finding possibly unintended lossy conversions! 🎉

More interestingly, it appears that RAND_MAX fits in a 32-bit float on Windows and macos, but not on some Linux platforms! 🤔 On Windows/MSVC2022, RAND_MAX is only 32767, whereas it is 2147483647 on this Linux build, apparently!

I think it would be better to avoid RAND_MAX, as well as rand(), if we want to have the same result on all platforms 🤷

{
auto randMax = static_cast<float>(RAND_MAX);
auto randMax = float{ RAND_MAX };
const PixelType pixel(static_cast<float>(rand()) / randMax, static_cast<float>(rand()) / randMax);
Copy link
Member

Choose a reason for hiding this comment

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

It does not look like anything is done with the value of the image before it is filled below. Maybe just use double, and then after division it can be reduced to float. I don't think it useful to working about system differences here.

Copy link
Contributor Author

@N-Dekker N-Dekker May 2, 2025

Choose a reason for hiding this comment

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

Thanks @blowekamp but for now I just reverted back to static_cast<float>(RAND_MAX). Eventually I think we just shouldn't use the old rand() and RAND_MAX anymore. But that's for later 😺

Copy link
Member

Choose a reason for hiding this comment

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

I don't think this code block is a productive test. I question the usefulness of making it beautiful portable modern C++. Time may be better spent writing a replacement test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think this code block is a productive test. I question the usefulness of making it beautiful portable modern C++. Time may be better spent writing a replacement test.

Well, I would prefer using GoogleTest, of course. But can you please be more specific on why you think the code block is not productive?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It appears that the initial commit 75fd707 ("ENH: Added complex conjugate adaptor", Oct 26, 2011) already did so: first set a random number value for each pixel in the image, then fill the entire image buffer with PixelType( 2.0, -3.7 ):

itk::ImageRegionIterator< ImageType> iter( image, region );
for ( iter.GoToBegin(); iter != iter.End(); ++iter )
{
float randMax = static_cast< float >( RAND_MAX );
PixelType pixel( static_cast< float >( rand() ) / randMax,
static_cast< float >( rand() ) / randMax );
iter.Set( pixel );
}
image->FillBuffer( PixelType( 2.0, -3.7 ) );

Is that why you don't think it's a productive test?

Copy link
Member

Choose a reason for hiding this comment

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

Yes. The values are set and nothing is done with them. I suspect the initial test may have been with the random data, but it didn't work out.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

While I think the test should work fine with arbitrary random input data, right?

@cquammen Hi Cory! Hope you're fine! We're just discussing your code from almost 14 years ago, I think 😃 I'm wondering, do you still remember why the ComplexConjugateImageAdaptor test first iteratively set the pixels of the image to random values, when it did image->FillBuffer directly afterwards anyway? What could have been the intention?

Copy link
Contributor

Choose a reason for hiding this comment

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

Most likely an incomplete change in my testing methodology :-) Feel free to remove.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replaced `static_cast<(\w+)>\(([A-Z_][A-Z_]+)\)` with `$1{ $2 }`, using regular
expressions. According to common C++ naming conventions, these all "all caps"
identifiers refer to constants, specified by preprocessor macro's, so these
conversions are evaluated at compile-time.

Excluded replacing `static_cast<float>(RAND_MAX)` (as encountered in
"itkComplexConjugateImageAdaptorTest.cxx") because this conversion is narrowing
on some platforms. Specifically, on GCC `RAND_MAX` may be 2147483647, while it
is only 32767 on MSVC2022.

- Follow-up to pull request InsightSoftwareConsortium#5324
commit 31859d3
"STYLE: Replace `static_cast<T>(Dimension)` with `T{ Dimension }`"
@N-Dekker N-Dekker force-pushed the Replace-static_cast-on-all-caps-macro-constants branch from a7a146e to 5730a69 Compare May 2, 2025 14:11
@thewtex
Copy link
Member

thewtex commented May 2, 2025

/azp run ITK.Linux

1 similar comment
@N-Dekker
Copy link
Contributor Author

N-Dekker commented May 5, 2025

/azp run ITK.Linux

N-Dekker added a commit to N-Dekker/ITK that referenced this pull request May 5, 2025
Removed the `image->FillBuffer` call from this test, as it appeared to make the
randomization of input pixel values useless. As noticed by Bradley Lowekamp at
InsightSoftwareConsortium#5337 (comment)

Cory Quammen (who originally wrote the test) also suggested removing this line
of code.
@N-Dekker
Copy link
Contributor Author

N-Dekker commented May 7, 2025

/azp run ITK.Linux

@N-Dekker
Copy link
Contributor Author

N-Dekker commented May 7, 2025

@dzenanz @thewtex Do you have any clue why the CI is broken? It says:

2025-05-07T15:29:45.6443149Z CMake Error at /home/vsts/work/1/s/CMake/ExternalData.cmake:1171 (message):
2025-05-07T15:29:45.6443562Z   
2025-05-07T15:29:45.6444438Z 
2025-05-07T15:29:45.6445059Z   Object CID=bafybeifxqqmljz3yrqhrdv7vzrqrfkvs7aq2mx7mo3g3mjykqq3onlgh6i not
2025-05-07T15:29:45.6445869Z   found at:
2025-05-07T15:29:45.6446439Z 
2025-05-07T15:29:45.6447192Z     https://insightsoftwareconsortium.github.io/ITKTestingData/CID/bafybeifxqqmljz3yrqhrdv7vzrqrfkvs7aq2mx7mo3g3mjykqq3onlgh6i ("HTTP response code said error")
2025-05-07T15:29:45.6448294Z     https://data.kitware.com:443/api/v1/file/hashsum/CID/bafybeifxqqmljz3yrqhrdv7vzrqrfkvs7aq2mx7mo3g3mjykqq3onlgh6i/download ("HTTP response code said error")
2025-05-07T15:29:45.6449205Z     https://itk.org/files/ExternalData/CID/bafybeifxqqmljz3yrqhrdv7vzrqrfkvs7aq2mx7mo3g3mjykqq3onlgh6i ("HTTP response code said error")
2025-05-07T15:29:45.6449981Z     http://127.0.0.1:8080/ipfs/bafybeifxqqmljz3yrqhrdv7vzrqrfkvs7aq2mx7mo3g3mjykqq3onlgh6i ("Could not connect to server")
2025-05-07T15:29:45.6450817Z     https://ipfs.io/ipfs/bafybeifxqqmljz3yrqhrdv7vzrqrfkvs7aq2mx7mo3g3mjykqq3onlgh6i ("HTTP response code said error")
2025-05-07T15:29:45.6451825Z     https://gateway.pinata.cloud/ipfs/bafybeifxqqmljz3yrqhrdv7vzrqrfkvs7aq2mx7mo3g3mjykqq3onlgh6i ("HTTP response code said error")
2025-05-07T15:29:45.6452794Z     https://cloudflare-ipfs.com/ipfs/bafybeifxqqmljz3yrqhrdv7vzrqrfkvs7aq2mx7mo3g3mjykqq3onlgh6i ("Could not resolve hostname")
2025-05-07T15:29:45.6453593Z     https://dweb.link/ipfs/bafybeifxqqmljz3yrqhrdv7vzrqrfkvs7aq2mx7mo3g3mjykqq3onlgh6i ("HTTP response code said error")
2025-05-07T15:29:45.6453830Z 

@dzenanz
Copy link
Member

dzenanz commented May 7, 2025

I don't know either.

@blowekamp
Copy link
Member

$ git grep bafybeifxqqmljz3yrqhrdv7vzrqrfkvs7aq2mx7mo3g3mjykqq3onlgh6i
Modules/IO/GDCM/test/Input/Lily/HTJ2K-YBR_RCT.dcm.cid:bafybeifxqqmljz3yrqhrdv7vzrqrfkvs7aq2mx7mo3g3mjykqq3onlgh6i

That is a file I uploading related to DICOM and HTJ2K encoding. So it should be on the interplanetary filesystem?

Maybe the problematic file can be manually uploaded to Kitware's data repo?

@dzenanz
Copy link
Member

dzenanz commented May 7, 2025

@thewtex https://content-link-upload.itk.org/ seems to have dumbed up again. Alt link takes long to load, and has nearly no content.

@thewtex
Copy link
Member

thewtex commented May 7, 2025

So it should be on the interplanetary filesystem?

I manually uploaded it.

/azp run ITK.Linux

Maybe the problematic file can be manually uploaded to Kitware's data repo?

Kitware has recommended not to use data.kitware.com, which may be taken offline.

@thewtex https://content-link-upload.itk.org/ seems to have dumbed up again. Alt link takes long to load, and has nearly no content.

I'll address this.

@thewtex
Copy link
Member

thewtex commented May 7, 2025

/azp run ITK.Linux

@blowekamp
Copy link
Member

@thewtex I am seeing this issue on the nightly dashboard too: https://open.cdash.org/viewBuildError.php?buildid=10385473

@thewtex
Copy link
Member

thewtex commented May 8, 2025

@blowekamp thanks for the note. I added all your recent additions to the GitHub Pages resource:

CID/bafkreib5e6kni2tdbtaxpz2i77k7hp5gzkztppiuws3ov4yx4dswlna6p4
CID/bafybeifxqqmljz3yrqhrdv7vzrqrfkvs7aq2mx7mo3g3mjykqq3onlgh6i
CID/bafybeig7pgo3x55bfh47ymckelybsug7rv2nqwjic2gzurvng6o3vqoux4

@thewtex
Copy link
Member

thewtex commented May 8, 2025

/azp run ITK.Linux

1 similar comment
@N-Dekker
Copy link
Contributor Author

/azp run ITK.Linux

@dzenanz dzenanz merged commit 84cb6e8 into InsightSoftwareConsortium:master May 12, 2025
16 checks passed
dzenanz pushed a commit that referenced this pull request May 13, 2025
Removed the `image->FillBuffer` call from this test, as it appeared to make the
randomization of input pixel values useless. As noticed by Bradley Lowekamp at
#5337 (comment)

Cory Quammen (who originally wrote the test) also suggested removing this line
of code.
@thewtex
Copy link
Member

thewtex commented May 13, 2025

https://content-link-upload.itk.org/

A number of major updates were made, and it is now available at https://content-link-upload.netlify.app, and https://content-link-upload.itk.org/ will be available soon following a DNS fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Core Issues affecting the Core module area:Filtering Issues affecting the Filtering module area:IO Issues affecting the IO module area:Segmentation Issues affecting the Segmentation module type:Style Style changes: no logic impact (indentation, comments, naming) type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants