|
| 1 | +unit TestCases; |
| 2 | + |
| 3 | +{$mode ObjFPC}{$H+} |
| 4 | + |
| 5 | +interface |
| 6 | + |
| 7 | +uses Classes, SysUtils, FPCUnit, TestRegistry, FPCUnitTestUtils; |
| 8 | + |
| 9 | +type |
| 10 | + ChangeTest = class(TTestCase) |
| 11 | + published |
| 12 | + procedure change_for_1_cent; |
| 13 | + procedure single_coin_change; |
| 14 | + procedure multiple_coin_change; |
| 15 | + procedure change_with_lilliputian_coins; |
| 16 | + procedure change_with_lower_elbonia_coins; |
| 17 | + procedure large_target_values; |
| 18 | + procedure possible_change_without_unit_coins_available; |
| 19 | + procedure another_possible_change_without_unit_coins_available; |
| 20 | + procedure a_greedy_approach_is_not_optimal; |
| 21 | + procedure no_coins_make_0_change; |
| 22 | + procedure error_testing_for_change_smaller_than_the_smallest_of_coins; |
| 23 | + procedure error_if_no_combination_can_add_up_to_target; |
| 24 | + procedure cannot_find_negative_change_values; |
| 25 | + end; |
| 26 | + |
| 27 | +implementation |
| 28 | + |
| 29 | +uses Change; |
| 30 | + |
| 31 | +procedure TapAssertExpectionMessage( |
| 32 | + ACase : TTestCase ; |
| 33 | + const AMessage : string ; |
| 34 | + const Expected : string ; |
| 35 | + const coins : TIntArray ; |
| 36 | + const target : Integer |
| 37 | +); |
| 38 | +var |
| 39 | + JsonMsg : string; |
| 40 | + actual : string; |
| 41 | +begin |
| 42 | + actual := ''; |
| 43 | + try |
| 44 | + findFewestCoins(coins, target); |
| 45 | + except |
| 46 | + on E: Exception do actual := E.Message; |
| 47 | + end; |
| 48 | + JsonMsg := EncodeJsonMessage(AMessage, expected, actual); |
| 49 | + ACase.AssertTrue(JsonMsg, expected = actual); |
| 50 | +end; |
| 51 | + |
| 52 | +// d0ebd0e1-9d27-4609-a654-df5c0ba1d83a |
| 53 | +procedure ChangeTest.change_for_1_cent; |
| 54 | +begin |
| 55 | + TapAssertTrue(Self, 'change for 1 cent', [1], Change.findFewestCoins([1,5,10,25], 1)); |
| 56 | +end; |
| 57 | + |
| 58 | +// 36887bea-7f92-4a9c-b0cc-c0e886b3ecc8 |
| 59 | +procedure ChangeTest.single_coin_change; |
| 60 | +begin |
| 61 | + TapAssertTrue(Self, 'single coin change', [25], Change.findFewestCoins([1,5,10,25,100], 25)); |
| 62 | +end; |
| 63 | + |
| 64 | +// cef21ccc-0811-4e6e-af44-f011e7eab6c6 |
| 65 | +procedure ChangeTest.multiple_coin_change; |
| 66 | +begin |
| 67 | + TapAssertTrue(Self, 'multiple coin change', [5,10], Change.findFewestCoins([1,5,10,25,100], 15)); |
| 68 | +end; |
| 69 | + |
| 70 | +// d60952bc-0c1a-4571-bf0c-41be72690cb3 |
| 71 | +procedure ChangeTest.change_with_lilliputian_coins; |
| 72 | +begin |
| 73 | + TapAssertTrue(Self, 'change with Lilliputian Coins', [4,4,15], Change.findFewestCoins([1,4,15,20,50], 23)); |
| 74 | +end; |
| 75 | + |
| 76 | +// 408390b9-fafa-4bb9-b608-ffe6036edb6c |
| 77 | +procedure ChangeTest.change_with_lower_elbonia_coins; |
| 78 | +begin |
| 79 | + TapAssertTrue(Self, 'change with Lower Elbonia Coins', [21,21,21], Change.findFewestCoins([1,5,10,21,25], 63)); |
| 80 | +end; |
| 81 | + |
| 82 | +// 7421a4cb-1c48-4bf9-99c7-7f049689132f |
| 83 | +procedure ChangeTest.large_target_values; |
| 84 | +begin |
| 85 | + TapAssertTrue(Self, 'large target values', [2,2,5,20,20,50,100,100,100,100,100,100,100,100,100], Change.findFewestCoins([1,2,5,10,20,50,100], 999)); |
| 86 | +end; |
| 87 | + |
| 88 | +// f79d2e9b-0ae3-4d6a-bb58-dc978b0dba28 |
| 89 | +procedure ChangeTest.possible_change_without_unit_coins_available; |
| 90 | +begin |
| 91 | + TapAssertTrue(Self, 'possible change without unit coins available', [2,2,2,5,10], Change.findFewestCoins([2,5,10,20,50], 21)); |
| 92 | +end; |
| 93 | + |
| 94 | +// 9a166411-d35d-4f7f-a007-6724ac266178 |
| 95 | +procedure ChangeTest.another_possible_change_without_unit_coins_available; |
| 96 | +begin |
| 97 | + TapAssertTrue(Self, 'another possible change without unit coins available', [4,4,4,5,5,5], Change.findFewestCoins([4,5], 27)); |
| 98 | +end; |
| 99 | + |
| 100 | +// ce0f80d5-51c3-469d-818c-3e69dbd25f75 |
| 101 | +procedure ChangeTest.a_greedy_approach_is_not_optimal; |
| 102 | +begin |
| 103 | + TapAssertTrue(Self, 'a greedy approach is not optimal', [10,10], Change.findFewestCoins([1,10,11], 20)); |
| 104 | +end; |
| 105 | + |
| 106 | +// bbbcc154-e9e9-4209-a4db-dd6d81ec26bb |
| 107 | +procedure ChangeTest.no_coins_make_0_change; |
| 108 | +begin |
| 109 | + TapAssertTrue(Self, 'no coins make 0 change', [], Change.findFewestCoins([1,5,10,21,25], 0)); |
| 110 | +end; |
| 111 | + |
| 112 | +// c8b81d5a-49bd-4b61-af73-8ee5383a2ce1 |
| 113 | +procedure ChangeTest.error_testing_for_change_smaller_than_the_smallest_of_coins; |
| 114 | +begin |
| 115 | + TapAssertExpectionMessage(Self, 'error testing for change smaller than the smallest of coins', 'cannot make target with given coins', [5,10], 3); |
| 116 | +end; |
| 117 | + |
| 118 | +// 3c43e3e4-63f9-46ac-9476-a67516e98f68 |
| 119 | +procedure ChangeTest.error_if_no_combination_can_add_up_to_target; |
| 120 | +begin |
| 121 | + TapAssertExpectionMessage(Self, 'error if no combination can add up to target', 'cannot make target with given coins', [5,10], 94); |
| 122 | +end; |
| 123 | + |
| 124 | +// 8fe1f076-9b2d-4f44-89fe-8a6ccd63c8f3 |
| 125 | +procedure ChangeTest.cannot_find_negative_change_values; |
| 126 | +begin |
| 127 | + TapAssertExpectionMessage(Self, 'cannot find negative change values', 'target cannot be negative', [1,2,5], -5); |
| 128 | +end; |
| 129 | + |
| 130 | +initialization |
| 131 | +RegisterTest(ChangeTest); |
| 132 | + |
| 133 | +end. |
0 commit comments