|
1 | 1 | package com.thealgorithms.dynamicprogramming; |
2 | 2 |
|
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
3 | 8 | /** |
4 | 9 | * Dynamic Programming solution for the Egg Dropping Puzzle |
5 | 10 | * The problem is to find the minimum number of attempts needed in the worst case to find the critical |
@@ -60,18 +65,37 @@ public static int minTrials(int eggs, int floors) { |
60 | 65 | return dp[eggs][floors]; |
61 | 66 | } |
62 | 67 |
|
63 | | - /** |
64 | | - * Example usage |
65 | | - */ |
66 | | - public static void main(String[] args) { |
67 | | - try { |
68 | | - // Example: 2 eggs and 4 floors |
69 | | - System.out.println("Minimum number of trials in worst case with 2 eggs and 4 floors: " + minTrials(2, 4)); |
70 | | - |
71 | | - // Additional test case |
72 | | - System.out.println("Minimum number of trials in worst case with 3 eggs and 5 floors: " + minTrials(3, 5)); |
73 | | - } catch (IllegalArgumentException e) { |
74 | | - System.err.println("Error: " + e.getMessage()); |
75 | | - } |
| 68 | + @Test |
| 69 | + void testBasicScenarios() { |
| 70 | + // Test with 2 eggs and 4 floors |
| 71 | + assertEquals(3, minTrials(2, 4)); |
| 72 | + |
| 73 | + // Test with 3 eggs and 5 floors |
| 74 | + assertEquals(3, minTrials(3, 5)); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void testEdgeCases() { |
| 79 | + // Test with single egg |
| 80 | + assertEquals(0, minTrials(1, 0)); |
| 81 | + assertEquals(1, minTrials(1, 1)); |
| 82 | + assertEquals(2, minTrials(1, 2)); |
| 83 | + |
| 84 | + // Test with multiple eggs but minimal floors |
| 85 | + assertEquals(0, minTrials(2, 0)); |
| 86 | + assertEquals(1, minTrials(3, 1)); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void testLargeInputs() { |
| 91 | + assertEquals(4, minTrials(3, 10)); |
| 92 | + assertEquals(7, minTrials(2, 36)); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void testInvalidInputs() { |
| 97 | + assertThrows(IllegalArgumentException.class, () -> minTrials(0, 5)); |
| 98 | + assertThrows(IllegalArgumentException.class, () -> minTrials(-1, 5)); |
| 99 | + assertThrows(IllegalArgumentException.class, () -> minTrials(2, -1)); |
76 | 100 | } |
77 | 101 | } |
0 commit comments