Skip to content

Conversation

@rory-cd
Copy link

@rory-cd rory-cd commented Nov 17, 2025

Description

Adds unit test for the bitmap_filename function from images.cpp (documentation here). The new test is named "bitmap filename can be retrieved".

This test includes sections covering the following cases:

  • Valid bitmap (loaded from file): Returns filename
  • Null reference: Returns empty string
  • Newly created bitmap: Returns empty string

Note: Although the function uses the term filename, it actually returns the full file path. So in the first section, the test extracts the file name from the full path before making its comparison. It may be worth renaming the function to bitmap_filepath and adding a new bitmap_filename to make this clearer for the end user, but that is beyond the scope of this update.

Type of change

  • [✓] Unit test addition

How Has This Been Tested?

Built the test project as per unit testing instructions, running skunit_tests with all tests passed. The returned strings were also manually verified by outputting each using std::cout.

Testing Checklist

  • [ ✓] Tested with skunit_tests

Checklist

  • [✓] My code follows the style guidelines of this project
  • [✓] I have performed a self-review of my own code
  • [✓] I have commented my code in hard-to-understand areas
  • [ ] I have made corresponding changes to the documentation
  • [✓] My changes generate no new warnings
  • [ ] I have requested a review from ... on the Pull Request

Copy link

@222448082Ashen 222448082Ashen left a comment

Choose a reason for hiding this comment

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

Description

This update adds a new unit test for the bitmap_filename function in images.cpp.
The test is named "bitmap filename can be retrieved" and checks three cases:

  • A loaded bitmap returns the correct filename
  • A null bitmap returns an empty string
  • A newly created bitmap returns an empty string

The function currently returns a full file path.
For the comparison, the test takes only the last part of the path.
A rename to bitmap_filepath could help long-term clarity, but that is outside this change.

Type of change

  • Unit test addition

How Has This Been Tested?

The test suite was built and run using the normal unit test flow.
skunit_tests completed with all tests passing.
Output strings were also printed to confirm each value manually.

Testing Checklist

  • Tested with skunit_tests

Checklist

  • Code follows project style
  • Self-review completed
  • Comments added where helpful
  • Documentation not required for this update
  • No new warnings
  • Reviewer will be added when opening PR

Files Modified

  • Updated: unit_test_bitmap.cpp — added new sections testing bitmap_filename

Additional Notes

No API changes or behaviour changes included.

// Extract filename
std::string filepath = bitmap_filename(bmp);
size_t idx = filepath.size() - expected_filename.size();
std::string filename = filepath.substr(idx, expected_filename.size());

Choose a reason for hiding this comment

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

Unsafe substring extraction

This code has a potential unsigned integer underflow if filepath is shorter than expected_filename:

size_t idx = filepath.size() - expected_filename.size();  // Can underflow!
std::string filename = filepath.substr(idx, expected_filename.size());

I would suggest do it this way:

std::string filepath = bitmap_filename(bmp);
REQUIRE(filepath.size() >= expected_filename.size());
std::string filename = filepath.substr(filepath.size() - expected_filename.size());
REQUIRE(filename == expected_filename);

@ralphweng2023
Copy link

Peer Review Summary

Thanks for this contribution! I've reviewed the PR against the SplashKit peer review checklist. Overall, the unit tests are well-structured and follow the existing conventions, but there are a couple of issues that need to be addressed before approval.


Code Quality

  • Repository: Correct repository (splashkit-core)
  • Readability: Code is clear and follows Catch2 conventions
  • Maintainability: Tests are modular and follow existing test structure

Functionality

  • Correctness: Tests cover the main behaviors of bitmap_filename
  • Impact on Existing Functionality: No breaking changes; only adds tests

Testing

  • Test Coverage: Basic scenarios covered (loaded bitmap, null bitmap, created bitmap)
  • Test Results: Author reports 80 tests, 0 failures (100% pass)

⚠️ Issues Found

1. Safety Issue (inline comment added)

The substring extraction in the first test case has a potential unsigned integer underflow. Please see my inline comment with suggested fix.


Decision: Request Changes ⚠️

Please address the two inline comments before this can be approved. Once fixed, I'll be happy to approve!

Let me know if you have any questions about the suggested changes.

Adds requirement ensuring the filepath being read exceeds or equals
the length of the expected filename. This removes the potential for
integer underflow.
@rory-cd
Copy link
Author

rory-cd commented Dec 1, 2025

Thanks @ralphweng2023, nice catch! I have updated to address the issue.

I've followed your suggestion, but used an idx variable, which hopefully makes it a little easier to read.

@rory-cd rory-cd requested a review from ralphweng2023 December 1, 2025 00:09
@rory-cd rory-cd changed the title Add unit test for function bitmap_name Add unit test for function bitmap_filename Dec 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants