Add ModuloPowerOfTwo algorithm (#5863)

This commit is contained in:
Hardik Pawar
2024-10-26 14:52:29 +05:30
committed by GitHub
parent fb85a4884f
commit 8551addbf2
3 changed files with 68 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
package com.thealgorithms.bitmanipulation;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
class ModuloPowerOfTwoTest {
@ParameterizedTest
@CsvSource({
"10, 3, 2",
"15, 2, 3",
"20, 4, 4",
"7, 1, 1",
"5, 1, 1",
"36, 5, 4",
})
void
testModuloPowerOfTwo(int x, int n, int expected) {
assertEquals(expected, ModuloPowerOfTwo.moduloPowerOfTwo(x, n));
}
@ParameterizedTest
@CsvSource({
"10, 0",
"15, -2",
"20, -4",
"7, -1",
"5, -1",
})
void
testNegativeExponent(int x, int n) {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> ModuloPowerOfTwo.moduloPowerOfTwo(x, n));
assertEquals("The exponent must be positive", exception.getMessage());
}
}