|
| 1 | +package com.thealgorithms.bitmanipulation; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | + |
| 6 | +import java.util.stream.Stream; |
| 7 | +import org.junit.jupiter.params.ParameterizedTest; |
| 8 | +import org.junit.jupiter.params.provider.Arguments; |
| 9 | +import org.junit.jupiter.params.provider.MethodSource; |
| 10 | + |
| 11 | +/** |
| 12 | + * Test case for IsPowerFour class |
| 13 | + */ |
| 14 | + |
| 15 | +public class IsPowerTwoTest { |
| 16 | + |
| 17 | + @ParameterizedTest |
| 18 | + @MethodSource("provideNumbersForPowerFour") |
| 19 | + public void testIsPowerFour(int number, boolean expected) { |
| 20 | + if (expected) { |
| 21 | + assertTrue(IsPowerFour.isPowerFour(number)); |
| 22 | + } else { |
| 23 | + assertFalse(IsPowerFour.isPowerFour(number)); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + private static Stream<Arguments> provideNumbersForPowerFour() { |
| 28 | + return Stream.of( |
| 29 | + Arguments.of(1, Boolean.TRUE), // 4^0 |
| 30 | + Arguments.of(4, Boolean.TRUE), // 4^1 |
| 31 | + Arguments.of(16, Boolean.TRUE), // 4^2 |
| 32 | + Arguments.of(64, Boolean.TRUE), // 4^3 |
| 33 | + Arguments.of(256, Boolean.TRUE), // 4^4 |
| 34 | + Arguments.of(1024, Boolean.FALSE), // 1024 = 2^10, not 4^n |
| 35 | + Arguments.of(0, Boolean.FALSE), // 0 is not a power of four |
| 36 | + Arguments.of(-4, Boolean.FALSE), // Negative number |
| 37 | + Arguments.of(-16, Boolean.FALSE), // Negative number |
| 38 | + Arguments.of(2, Boolean.FALSE), // 2 is not a power of four |
| 39 | + Arguments.of(8, Boolean.FALSE), // 8 = 2^3, not 4^n |
| 40 | + Arguments.of(12, Boolean.FALSE), // 12 is not a power of four |
| 41 | + Arguments.of(20, Boolean.FALSE), // 20 is not a power of four |
| 42 | + Arguments.of(100, Boolean.FALSE), // 100 is not a power of four |
| 43 | + Arguments.of(4096, Boolean.TRUE) // 4^6 = 4096 |
| 44 | + ); |
| 45 | + } |
| 46 | +} |
0 commit comments