-
Notifications
You must be signed in to change notification settings - Fork 19.4k
/
WineProblemTest.java
72 lines (63 loc) · 2.4 KB
/
WineProblemTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.thealgorithms.dynamicprogramming;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
/**
* Unit tests for the WineProblem class.
* This test class verifies the correctness of the wine selling problem solutions.
*/
class WineProblemTest {
/**
* Test for wpRecursion method.
*/
@Test
void testWpRecursion() {
int[] wines = {2, 3, 5, 1, 4}; // Prices of wines
int expectedProfit = 50; // The expected maximum profit
assertEquals(expectedProfit, WineProblem.wpRecursion(wines, 0, wines.length - 1), "The maximum profit using recursion should be 50.");
}
/**
* Test for wptd method (Top-Down DP with Memoization).
*/
@Test
void testWptd() {
int[] wines = {2, 3, 5, 1, 4}; // Prices of wines
int expectedProfit = 50; // The expected maximum profit
assertEquals(expectedProfit, WineProblem.wptd(wines, 0, wines.length - 1, new int[wines.length][wines.length]), "The maximum profit using top-down DP should be 50.");
}
/**
* Test for wpbu method (Bottom-Up DP with Tabulation).
*/
@Test
void testWpbu() {
int[] wines = {2, 3, 5, 1, 4}; // Prices of wines
int expectedProfit = 50; // The expected maximum profit
assertEquals(expectedProfit, WineProblem.wpbu(wines), "The maximum profit using bottom-up DP should be 50.");
}
/**
* Test with a single wine.
*/
@Test
void testSingleWine() {
int[] wines = {10}; // Only one wine
int expectedProfit = 10; // Selling the only wine at year 1
assertEquals(expectedProfit, WineProblem.wpbu(wines), "The maximum profit for a single wine should be 10.");
}
/**
* Test with multiple wines of the same price.
*/
@Test
void testSamePriceWines() {
int[] wines = {5, 5, 5}; // All wines have the same price
int expectedProfit = 30; // Profit is 5 * (1 + 2 + 3)
assertEquals(expectedProfit, WineProblem.wpbu(wines), "The maximum profit with same price wines should be 30.");
}
/**
* Test with no wines.
*/
@Test
void testNoWines() {
int[] wines = {};
assertThrows(IllegalArgumentException.class, () -> WineProblem.wpbu(wines), "The maximum profit for no wines should throw an IllegalArgumentException.");
}
}