|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
4 | 4 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
5 | 5 |
|
6 |
| -import org.junit.jupiter.api.Test; |
| 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; |
7 | 10 |
|
8 | 11 | /**
|
9 | 12 | * Test case for IsPowerTwo class
|
10 | 13 | * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
|
11 | 14 | */
|
12 | 15 |
|
13 | 16 | public class IsPowerTwoTest {
|
14 |
| - @Test |
15 |
| - public void testIsPowerTwo() { |
16 |
| - // test some positive powers of 2 |
17 |
| - assertTrue(IsPowerTwo.isPowerTwo(1)); |
18 |
| - assertTrue(IsPowerTwo.isPowerTwo(2)); |
19 |
| - assertTrue(IsPowerTwo.isPowerTwo(4)); |
20 |
| - assertTrue(IsPowerTwo.isPowerTwo(16)); |
21 |
| - assertTrue(IsPowerTwo.isPowerTwo(1024)); |
22 | 17 |
|
23 |
| - // test some negative numbers |
24 |
| - assertFalse(IsPowerTwo.isPowerTwo(-1)); |
25 |
| - assertFalse(IsPowerTwo.isPowerTwo(-2)); |
26 |
| - assertFalse(IsPowerTwo.isPowerTwo(-4)); |
| 18 | + @ParameterizedTest |
| 19 | + @MethodSource("provideNumbersForPowerTwo") |
| 20 | + public void testIsPowerTwo(int number, boolean expected) { |
| 21 | + if (expected) { |
| 22 | + assertTrue(IsPowerTwo.isPowerTwo(number)); |
| 23 | + } else { |
| 24 | + assertFalse(IsPowerTwo.isPowerTwo(number)); |
| 25 | + } |
| 26 | + } |
27 | 27 |
|
28 |
| - // test some numbers that are not powers of 2 |
29 |
| - assertFalse(IsPowerTwo.isPowerTwo(0)); |
30 |
| - assertFalse(IsPowerTwo.isPowerTwo(3)); |
31 |
| - assertFalse(IsPowerTwo.isPowerTwo(5)); |
32 |
| - assertFalse(IsPowerTwo.isPowerTwo(15)); |
33 |
| - assertFalse(IsPowerTwo.isPowerTwo(1000)); |
| 28 | + private static Stream<Arguments> provideNumbersForPowerTwo() { |
| 29 | + return Stream.of(Arguments.of(1, Boolean.TRUE), // 2^0 |
| 30 | + Arguments.of(2, Boolean.TRUE), // 2^1 |
| 31 | + Arguments.of(4, Boolean.TRUE), // 2^2 |
| 32 | + Arguments.of(8, Boolean.TRUE), // 2^3 |
| 33 | + Arguments.of(16, Boolean.TRUE), // 2^4 |
| 34 | + Arguments.of(32, Boolean.TRUE), // 2^5 |
| 35 | + Arguments.of(64, Boolean.TRUE), // 2^6 |
| 36 | + Arguments.of(128, Boolean.TRUE), // 2^7 |
| 37 | + Arguments.of(256, Boolean.TRUE), // 2^8 |
| 38 | + Arguments.of(1024, Boolean.TRUE), // 2^10 |
| 39 | + Arguments.of(0, Boolean.FALSE), // 0 is not a power of two |
| 40 | + Arguments.of(-1, Boolean.FALSE), // Negative number |
| 41 | + Arguments.of(-2, Boolean.FALSE), // Negative number |
| 42 | + Arguments.of(-4, Boolean.FALSE), // Negative number |
| 43 | + Arguments.of(3, Boolean.FALSE), // 3 is not a power of two |
| 44 | + Arguments.of(5, Boolean.FALSE), // 5 is not a power of two |
| 45 | + Arguments.of(6, Boolean.FALSE), // 6 is not a power of two |
| 46 | + Arguments.of(15, Boolean.FALSE), // 15 is not a power of two |
| 47 | + Arguments.of(1000, Boolean.FALSE), // 1000 is not a power of two |
| 48 | + Arguments.of(1023, Boolean.FALSE) // 1023 is not a power of two |
| 49 | + ); |
34 | 50 | }
|
35 | 51 | }
|
0 commit comments