Skip to content

Add MAC Address Tests to Catch2 Test Suite#157

Open
kyriesk wants to merge 5 commits into
thoth-tech:mainfrom
kyriesk:unit-test/mac-address
Open

Add MAC Address Tests to Catch2 Test Suite#157
kyriesk wants to merge 5 commits into
thoth-tech:mainfrom
kyriesk:unit-test/mac-address

Conversation

@kyriesk
Copy link
Copy Markdown

@kyriesk kyriesk commented Mar 24, 2026

Description

This pull request migrates MAC address conversion and validation tests from the legacy test_networking.cpp test functions to the modern Catch2 test framework in unit_test_network.cpp.

The following test cases have been converted from standalone C++ functions to Catch2 TEST_CASE format:

  • mac_to_hex_test()TEST_CASE("can convert mac address string to hex string")
  • hex_to_mac_test()TEST_CASE("can convert hex string to mac address string")
  • is_valid_mac_test()TEST_CASE("check the validation of mac address")

All test logic, assertions, and coverage remain identical to the original tests, ensuring no regression in functionality. This consolidation modernizes the test suite and improves maintainability by using a consistent test framework across the codebase.

Type of Change

  • New feature (non-breaking change which adds functionality)

Note: This is primarily a test refactoring with no changes to core functionality.

Changes Made

  1. Added three new Catch2 test cases to unit_test_network.cpp:

    • TEST_CASE("can convert mac address string to hex string") - Tests MAC to hex conversion with positive, negative, and edge cases
    • TEST_CASE("can convert hex string to mac address string") - Tests hex to MAC conversion with validation
    • TEST_CASE("check the validation of mac address") - Tests MAC address validation for valid and invalid formats
  2. Test Coverage Includes:

    • Valid MAC addresses (standard format with colons)
    • Hex conversion accuracy (both directions)
    • Invalid MAC addresses (wrong length, invalid characters, wrong format)
    • Edge cases and boundary conditions
    • Standard output for verification

How Has This Been Tested?

The converted tests maintain 100% parity with the original test_networking.cpp functions. All assertions from the original standalone tests have been preserved and converted to Catch2 REQUIRE macros. The tests have been executed against the networking library functions:

  • mac_to_hex() - Converts MAC address string to hexadecimal string
  • hex_to_mac() - Converts hexadecimal string to MAC address format
  • is_valid_mac() - Validates MAC address format
cd projects/cmake
cmake --preset Linux
cmake --build build/
cd ../../bin
./skunit_tests "[networking]"

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 (maintained from original tests)
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have requested a review from @[maintainer-handle] on the Pull Request

@kyriesk
Copy link
Copy Markdown
Author

kyriesk commented Mar 29, 2026

To run these tests on WSL/Linux:

cd projects/cmake
cmake --preset Linux
cmake --build build/
cd ../../bin
./skunit_tests "check the validation of mac address"
./skunit_tests "can convert hex string to mac address string"
./skunit_tests "can convert mac address string to hex string"

Copy link
Copy Markdown

@kottochii kottochii left a comment

Choose a reason for hiding this comment

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

Peer review

I manually reviewed the test cases and they are testing the functionality as required. I also ran them on the local machine and ensured that they are passed.

Copy link
Copy Markdown

@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.

General Information

  • Type of Change: New feature (Unit Testing)

Code Quality

  • Repository: Correct. The PR is made to splashkit-core.
  • Readability: Issue with standard practices. The tests use std::cout to print success messages. In unit testing frameworks like Catch2, tests should ideally remain silent on success, as the framework already provides a summary. This output can clutter the console during large test runs.
  • Maintainability: High. The use of macros (TEST_MAC, TEST_MAC_HEX) for repeated values is a good practice.

Functionality

  • Correctness: The tests accurately verify the conversion and validation logic:
    • is_valid_mac: Correctly identifies valid formats and rejects various invalid ones (wrong length, invalid characters, wrong separators).
    • mac_to_hex: Successfully converts colon-separated MAC strings to 0x prefixed hex strings.
    • hex_to_mac: Correctly reverses the conversion.
  • Impact on Existing Functionality: No impact on existing library code.

Testing

  • Test Coverage: Excellent coverage for the new MAC-related networking utilities.
  • Test Results: Logic check confirms the tests are comprehensive and correctly handle edge cases like invalid hex lengths.

Documentation

  • Documentation: The test code is clear and follows the naming conventions of the project.

Pull Request Details

  • Checklist Completion: All relevant items reviewed.

@222448082Ashen
Copy link
Copy Markdown

Recommendations & Observations

  1. Remove std::cout and <iostream>: I recommend removing the std::cout print statements and the #include <iostream> header. Catch2's REQUIRE and CHECK macros are the standard way to report test progress and failures in this project.
  2. Case Sensitivity Test: While the underlying implementation of is_valid_mac uses [0-9A-Fa-f], it would be beneficial to add a test case with mixed-case characters (e.g., 00:ab:CD:12:34:56) to explicitly verify that case-insensitivity is maintained.
  3. Negative Hex Test: The test REQUIRE(hex_to_mac("0xABCDEF123456") != "AB:CD:GF:12:34:56"); is a good catch for potential off-by-one or character replacement errors.

@kyriesk kyriesk requested a review from 222448082Ashen May 15, 2026 03:52
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