Fix MiniMaxAlgorithm setScores bug and add comprehensive tests (#6566)

* Fix MiniMaxAlgorithm setScores bug and add comprehensive tests

- Fix bug in setScores method where scores.length % 1 == 0 always returned true
- Add isPowerOfTwo helper method to properly validate array length
- Add comprehensive unit tests covering edge cases and algorithm correctness
- Tests include validation for power of 2 check, minimax logic, and error handling

Fixes issue with incorrect validation logic in MiniMaxAlgorithm.setScores()

* Fix Checkstyle violations and improve setScores validation

- Replace star imports with explicit imports in test file
- Remove trailing whitespaces from all lines
- Add proper file ending newline
- Improve setScores method to handle edge cases properly
- Use direct bit manipulation for power-of-2 check as suggested
- Ensure error message is printed and scores remain unchanged for invalid input

All Checkstyle violations resolved and tests pass successfully.

* Add missing newline at end of MiniMaxAlgorithmTest.java

- Fix Checkstyle violation: NewlineAtEndOfFile
- Ensure file ends with proper newline character
- All tests continue to pass successfully

* Fix PMD violation by utilizing isPowerOfTwo method

- Replace inline bit manipulation with isPowerOfTwo method call
- Resolves PMD UnusedPrivateMethod violation
- Maintains same validation logic and error handling
- All tests continue to pass successfully

* test: improve MiniMaxAlgorithm test coverage for CodeCov

- Add comprehensive test cases for isPowerOfTwo function edge cases
- Test setScores method with various invalid array lengths (0, 3, 5, 6, 7, 9, 10, 15)
- Add tests for large valid powers of 2 (up to 64 elements)
- Ensure complete coverage of error handling branches
- Increase test count from 14 to 19 tests
- Fix partial coverage issues identified in CodeCov report

Resolves CodeCov coverage gaps in MiniMaxAlgorithm.java

* Fix checkstyle errors

* Fix checkstyle errors

---------

Co-authored-by: ssaha <ssaha@wiley.com>
Co-authored-by: Deniz Altunkapan <93663085+DenizAltunkapan@users.noreply.github.com>
This commit is contained in:
Sourav Saha
2025-10-02 12:54:25 +05:30
committed by GitHub
parent f8f315eaa8
commit 15695c6e54
2 changed files with 286 additions and 4 deletions

View File

@@ -109,13 +109,18 @@ public class MiniMaxAlgorithm {
return (n == 1) ? 0 : log2(n / 2) + 1;
}
// A utility function to check if a number is a power of 2
private boolean isPowerOfTwo(int n) {
return n > 0 && (n & (n - 1)) == 0;
}
public void setScores(int[] scores) {
if (scores.length % 1 == 0) {
this.scores = scores;
height = log2(this.scores.length);
} else {
if (!isPowerOfTwo(scores.length)) {
System.out.println("The number of scores must be a power of 2.");
return;
}
this.scores = scores;
height = log2(this.scores.length);
}
public int[] getScores() {