|
| 1 | +package ae.hackerrank.interview_preparation_kit.miscellaneous; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.util.List; |
| 7 | +import org.junit.jupiter.api.BeforeAll; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.junit.jupiter.api.TestInstance; |
| 10 | +import org.junit.jupiter.api.TestInstance.Lifecycle; |
| 11 | +import util.JsonLoader; |
| 12 | + |
| 13 | +/** |
| 14 | + * FlippingBitsTflippingBits. |
| 15 | + */ |
| 16 | +@TestInstance(Lifecycle.PER_CLASS) |
| 17 | +class FlippingBitsTestAlternativeTest { |
| 18 | + /** |
| 19 | + * FlippingBitsTestAlternativeTestCaseTest. |
| 20 | + */ |
| 21 | + public static class FlippingBitsTestAlternativeTestCaseTest { |
| 22 | + public long input; |
| 23 | + public long answer; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * FlippingBitsTestAlternativeTestCase. |
| 28 | + */ |
| 29 | + public static class FlippingBitsTestAlternativeTestCase { |
| 30 | + public String title; |
| 31 | + public List<FlippingBitsTestAlternativeTestCaseTest> tests; |
| 32 | + } |
| 33 | + |
| 34 | + private List<FlippingBitsTestAlternativeTestCase> testCases; |
| 35 | + |
| 36 | + @BeforeAll |
| 37 | + void setup() throws IOException { |
| 38 | + String path = String.join("/", |
| 39 | + "hackerrank", |
| 40 | + "interview_preparation_kit", |
| 41 | + "miscellaneous", |
| 42 | + "flipping_bits.testcases.json"); |
| 43 | + |
| 44 | + this.testCases = JsonLoader.loadJson(path, FlippingBitsTestAlternativeTestCase.class); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void testLuckBalance() { |
| 49 | + for (FlippingBitsTestAlternativeTestCase tests : testCases) { |
| 50 | + for (FlippingBitsTestAlternativeTestCaseTest test : tests.tests) { |
| 51 | + Long result = FlippingBitsAlternative.flippingBits(test.input); |
| 52 | + |
| 53 | + assertEquals(test.answer, result, |
| 54 | + "%s(%s) => must be: %d".formatted( |
| 55 | + "FlippingBits.flippingBits", |
| 56 | + test.input, |
| 57 | + test.answer)); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments