Skip to content

Conversation

Harsh-dubey-git
Copy link

Description

This PR fixes a bug in MiniMaxAlgorithm.java related to incorrect validation of power-of-2 array lengths in the setScores() method and improves edge case handling in the log2() method. Additionally, it adds a comprehensive JUnit 5 test suite to verify correctness and prevent future regressions.

Changes Made

  1. setScores()

    • Added validation for null and empty arrays.
    • Correctly checks that the array length is a power of 2 using (length & (length - 1)) == 0.
    • Throws IllegalArgumentException for invalid inputs.
  2. log2()

    • Handles invalid inputs (0 or negative numbers) safely.
    • Recursively calculates log2 for valid power-of-2 integers.
    • Optionally throws exceptions for non-power-of-2 numbers (ensures robustness).
  3. Unit Tests

    • MiniMaxAlgorithmTest.java includes tests for:
      • Valid and invalid setScores() inputs.
      • Edge cases and invalid inputs for log2().
      • Correct miniMax() computation for deterministic score arrays.
    • Uses reflection to test private log2() method.
    • Ensures proper array lengths, heights, and exception handling.
  4. Other Improvements

    • Tests verify the algorithm behaves correctly for maximizer and minimizer scenarios.
    • Helps prevent regressions for future contributors.

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.

1 participant