|
| 1 | +import org.junit.Ignore; |
| 2 | +import org.junit.Rule; |
| 3 | +import org.junit.Test; |
| 4 | +import org.junit.rules.ExpectedException; |
| 5 | + |
| 6 | +import static java.util.Arrays.asList; |
| 7 | +import static java.util.Collections.emptyList; |
| 8 | +import static java.util.Collections.singletonList; |
| 9 | +import static org.junit.Assert.assertEquals; |
| 10 | + |
| 11 | +public final class ChangeCalculatorTest { |
| 12 | + |
| 13 | + /* |
| 14 | + * See https://github.com/junit-team/junit4/wiki/Rules for information on JUnit Rules in general and |
| 15 | + * ExpectedExceptions in particular. |
| 16 | + */ |
| 17 | + @Rule |
| 18 | + public ExpectedException expectedException = ExpectedException.none(); |
| 19 | + |
| 20 | + @Test |
| 21 | + public void testChangeThatCanBeGivenInASingleCoin() { |
| 22 | + ChangeCalculator changeCalculator = new ChangeCalculator(asList(1, 5, 10, 25, 100)); |
| 23 | + |
| 24 | + assertEquals( |
| 25 | + singletonList(25), |
| 26 | + changeCalculator.computeMostEfficientChange(25)); |
| 27 | + } |
| 28 | + |
| 29 | + @Ignore |
| 30 | + @Test |
| 31 | + public void testChangeThatMustBeGivenInMultipleCoins() { |
| 32 | + ChangeCalculator changeCalculator = new ChangeCalculator(asList(1, 5, 10, 25, 100)); |
| 33 | + |
| 34 | + assertEquals( |
| 35 | + asList(5, 10), |
| 36 | + changeCalculator.computeMostEfficientChange(15)); |
| 37 | + } |
| 38 | + |
| 39 | + @Ignore |
| 40 | + @Test |
| 41 | + // https://en.wikipedia.org/wiki/Change-making_problem#Greedy_method |
| 42 | + public void testLilliputianCurrencyForWhichGreedyAlgorithmFails() { |
| 43 | + ChangeCalculator changeCalculator = new ChangeCalculator(asList(1, 4, 15, 20, 50)); |
| 44 | + |
| 45 | + assertEquals( |
| 46 | + asList(4, 4, 15), |
| 47 | + changeCalculator.computeMostEfficientChange(23)); |
| 48 | + } |
| 49 | + |
| 50 | + @Ignore |
| 51 | + @Test |
| 52 | + // https://en.wikipedia.org/wiki/Change-making_problem#Greedy_method |
| 53 | + public void testLowerElbonianCurrencyForWhichGreedyAlgorithmFails() { |
| 54 | + ChangeCalculator changeCalculator = new ChangeCalculator(asList(1, 5, 10, 21, 25)); |
| 55 | + |
| 56 | + assertEquals( |
| 57 | + asList(21, 21, 21), |
| 58 | + changeCalculator.computeMostEfficientChange(63)); |
| 59 | + } |
| 60 | + |
| 61 | + @Ignore |
| 62 | + @Test |
| 63 | + public void testLargeAmountOfChange() { |
| 64 | + ChangeCalculator changeCalculator = new ChangeCalculator(asList(1, 2, 5, 10, 20, 50, 100)); |
| 65 | + |
| 66 | + assertEquals( |
| 67 | + asList(2, 2, 5, 20, 20, 50, 100, 100, 100, 100, 100, 100, 100, 100, 100), |
| 68 | + changeCalculator.computeMostEfficientChange(999)); |
| 69 | + } |
| 70 | + |
| 71 | + @Ignore |
| 72 | + @Test |
| 73 | + public void testZeroChange() { |
| 74 | + ChangeCalculator changeCalculator = new ChangeCalculator(asList(1, 5, 10, 21, 25)); |
| 75 | + |
| 76 | + assertEquals( |
| 77 | + emptyList(), |
| 78 | + changeCalculator.computeMostEfficientChange(0)); |
| 79 | + } |
| 80 | + |
| 81 | + @Ignore |
| 82 | + @Test |
| 83 | + public void testChangeLessThanSmallestCoinInCurrencyCannotBeRepresented() { |
| 84 | + ChangeCalculator changeCalculator = new ChangeCalculator(asList(5, 10)); |
| 85 | + |
| 86 | + expectedException.expect(IllegalArgumentException.class); |
| 87 | + expectedException.expectMessage("The total 3 cannot be represented in the given currency."); |
| 88 | + |
| 89 | + changeCalculator.computeMostEfficientChange(3); |
| 90 | + } |
| 91 | + |
| 92 | + @Ignore |
| 93 | + @Test |
| 94 | + public void testChangeLargerThanAllCoinsInCurrencyThatCannotBeRepresented() { |
| 95 | + ChangeCalculator changeCalculator = new ChangeCalculator(asList(5, 10)); |
| 96 | + |
| 97 | + expectedException.expect(IllegalArgumentException.class); |
| 98 | + expectedException.expectMessage("The total 94 cannot be represented in the given currency."); |
| 99 | + |
| 100 | + changeCalculator.computeMostEfficientChange(94); |
| 101 | + } |
| 102 | + |
| 103 | + @Ignore |
| 104 | + @Test |
| 105 | + public void testNegativeChangeIsRejected() { |
| 106 | + ChangeCalculator changeCalculator = new ChangeCalculator(asList(1, 2, 5)); |
| 107 | + |
| 108 | + expectedException.expect(IllegalArgumentException.class); |
| 109 | + expectedException.expectMessage("Negative totals are not allowed."); |
| 110 | + |
| 111 | + changeCalculator.computeMostEfficientChange(-5); |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments