Skip to content

STYLE: Replace println functions in Core/Common tests with std::puts - #7063

Closed
N-Dekker wants to merge 1 commit into
InsightSoftwareConsortium:mainfrom
N-Dekker:Replace-println-with-puts
Closed

N-Dekker wants to merge 1 commit into
InsightSoftwareConsortium:mainfrom
N-Dekker:Replace-println-with-puts

Conversation

@N-Dekker

@N-Dekker N-Dekker commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Various tests had their own println(const char *) implementations, which appear very similar to std::puts. (std::puts also adds a newline character at the end.)

Various tests had their own `println(const char *)` implementation, which appear very similar to `std::puts`. (`std::puts` also adds a newline character at the end.)
@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 type:Style Style changes: no logic impact (indentation, comments, naming) labels Oct 31, 2026

@hjmjohnson hjmjohnson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice improvement!

namespace
{
void
println(const char * c)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually we had four different (but very similar) implementations of println(const char *)! This one here was properly placed in an unnamed namespace. But the one in itkConstNeighborhoodIteratorTest.cxx was entirely global, and had external linkage. The one in itkNeighborhoodTest.cxx was declared static, while the one in itkHashTableGTest.cxx was declared inline!

Comment on lines -82 to +77
println("Testing std::unordered_set");
std::puts("Testing std::unordered_set");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Honestly I would rather not have such "unconditional" print statements in unit tests at all. (Not even by std::puts.) It produces distractive noise in the test output, IMO. So I would prefer to only see such informative output in case of a test failure. The GoogleTest macro SCOPED_TRACE(message) does so: it only produces output in case of a failure. Anyway, I think replacing print statements with SCOPED_TRACE calls is beyond the scope of this PR... 🤷

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A nice follow-up PR?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Could be 😃 At least it's good to consider using the GoogleTest SCOPED_TRACE(message) macro, whenever it is useful 😃

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think that SCOPED_TRACE should be used a lot more in GTests.

void
println(const char * c)
{
std::cout << std::endl << c << std::endl;

@N-Dekker N-Dekker Nov 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Please note that this particular println implementation inserts two line breaks (one before and one after the message string), whereas the "global" println (implemented in itkConstNeighborhoodIteratorTest) only inserts a single line break, at the end:

println(const char * s)
{
std::cout << s << std::endl;
}

I think this is an extra reason to remove them. It's too confusing to have multiple println implementations behaving differently.

However, this PR will effectively remove some line breaks from the test output. I hope that's OK!

Looking at https://open.cdash.org/tests/2833926099

[ RUN      ] BoundaryCondition.ConstantBoundaryAtImageEdge

Creating some images

Initializing some images

Initializing smart neighborhood iterators
1327 1328 1329 0 0 

This PR will remove the empty lines. and make it as in https://open.cdash.org/tests/2832303956

[ RUN      ] BoundaryCondition.ConstantBoundaryAtImageEdge
Creating some images
Initializing some images
Initializing smart neighborhood iterators
1327 1328 1329 0 0 

OK? To me it's fine.

@N-Dekker
N-Dekker marked this pull request as ready for review November 2, 2026 12:11
@greptile-apps

greptile-apps Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Low impact] The changes affect printed output from a bounded set of Core/Common tests.

The PR appears safe to merge, with a non-blocking issue that splits two tests' diagnostic output.

Findings

  1. P2 Saved logs lose headings ▶

Summary

Replaces custom println helpers with std::puts across eight Core/Common test files and removes the shared declaration.

  • Core/Common tests print progress messages with std::puts.

Reviews (1) · Last reviewed commit: "STYLE: Replace `println` functions in Co..." · Reviewed by Greptile

itk::ConstNeighborhoodIterator<TestImageType>::RegionType reg = { idx, sz };

println("Creating ConstNeighborhoodIterator");
std::puts("Creating ConstNeighborhoodIterator");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Saved logs lose headings

std::puts bypasses --redirectOutput, which both itkConstNeighborhoodIteratorTest and itkShapedNeighborhoodIteratorTest use. The driver redirects only std::cout.rdbuf(), so progress headings now go to stdout while iterator details go to the attached log file. This makes failure logs harder to follow. Keep these messages on std::cout, or update the driver to capture both output paths.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OK, so it may not be such a good idea to use std::puts in this case. Addressed by:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That's a good catch by greptile. I did not realize the redirect is not supported.

@hjmjohnson hjmjohnson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Given that redirect is not supported by std::puts, I think this should be superceeded by the other PR.

I also like your suggestion of only printing content when a failure occurs. I think inside gTest that the diagnostics should be minimal unless an error occurs.

@N-Dekker

N-Dekker commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

I also like your suggestion of only printing content when a failure occurs. I think inside gTest that the diagnostics should be minimal unless an error occurs.

@hjmjohnson Thanks Hans! I just encountered an extra motivation to minimize output on a successful pass, as https://open.cdash.org/tests/2835201106 says:

[This part of the test output was removed since it exceeds the threshold of 1024 bytes.]

I just wanted to see the output beyond the 1024 bytes boundary, to see the effect of removing println, but it wasn't shown, unfortunately!

@N-Dekker N-Dekker closed this Nov 3, 2026
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 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.

3 participants