import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import java.util.Arrays; import java.util.List; import java.util.Map; public class SimpleTest { private static ChangeProblem change; @BeforeAll public static void init() { change = new DynamicChangeProblem(); } @Test public void test1() { List coins = Arrays.asList(1, 2, 5, 10, 20, 50); int total = 120; Map sol = change.change(coins, total); checkCoin(sol, 50, 2); checkCoin(sol, 20, 1); checkCoin(sol, 10, 0); checkCoin(sol, 5, 0); checkCoin(sol, 2, 0); checkCoin(sol, 1, 0); Assertions.assertEquals(120, berekenTotaal(sol), "Bedrag incorrect."); } @Test public void test2() { List coins = Arrays.asList(1, 2, 5, 10, 20, 50); int total = 79; Map sol = change.change(coins, total); checkCoin(sol, 50, 1); checkCoin(sol, 20, 1); checkCoin(sol, 10, 0); checkCoin(sol, 5, 1); checkCoin(sol, 2, 2); checkCoin(sol, 1, 0); Assertions.assertEquals(79, berekenTotaal(sol), "Bedrag incorrect."); } @Test public void test3() { List coins = Arrays.asList(1, 2, 5, 10, 20, 50); int total = 208; Map sol = change.change(coins, total); checkCoin(sol, 50, 4); checkCoin(sol, 20, 0); checkCoin(sol, 10, 0); checkCoin(sol, 5, 1); checkCoin(sol, 2, 1); checkCoin(sol, 1, 1); Assertions.assertEquals(208, berekenTotaal(sol), "Bedrag incorrect."); } @Test public void test4() { List coins = Arrays.asList(1, 2, 3, 10, 12, 37, 38, 76); int total = 379; Map sol = change.change(coins, total); checkCoin(sol, 76, 4); checkCoin(sol, 38, 1); checkCoin(sol, 37, 1); checkCoin(sol, 12, 0); checkCoin(sol, 10, 0); checkCoin(sol, 3, 0); checkCoin(sol, 2, 0); checkCoin(sol, 1, 0); Assertions.assertEquals(379, berekenTotaal(sol), "Bedrag incorrect."); } @Test public void test5() { List coins = Arrays.asList(1, 2, 3, 10, 12, 37, 38, 76); int total = 112; Map sol = change.change(coins, total); checkCoin(sol, 76, 0); checkCoin(sol, 38, 1); checkCoin(sol, 37, 2); checkCoin(sol, 12, 0); checkCoin(sol, 10, 0); checkCoin(sol, 3, 0); checkCoin(sol, 2, 0); checkCoin(sol, 1, 0); Assertions.assertEquals(112, berekenTotaal(sol)); } private void checkCoin(Map antw, int coin, int expectedCount) { Assertions.assertEquals(expectedCount, antw.getOrDefault(coin, 0).intValue(), "Fout aantal munten van waarde " + coin + "."); } private int berekenTotaal(Map map) { int total = 0; for(int coin: map.keySet()) { total += coin*map.get(coin); } return total; } }