1
exercism fetch delphi book-store

uBookStoreTests.pas

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
unit uBookStoreTests;

interface
uses
  DUnitX.TestFramework;

const
   CanonicalVersion = '1.1.0';

type
  [TestFixture]
  hpTests = class(TObject)
  public
    [Test]
//    [Ignore('Comment the "[Ignore]" statement to run the test')]
    procedure A_basket_containing_only_a_single_book;

    [Test]
    [Ignore]
    procedure A_basket_containing_only_two_of_the_same_book;

    [Test]
    [Ignore]
    procedure No_charge_to_carry_around_an_empty_basket;

    [Test]
    [Ignore]
    procedure A_basket_containing_only_two_different_books;

    [Test]
    [Ignore]
    procedure A_basket_with_three_different_books;

    [Test]
    [Ignore]
    procedure A_basket_with_four_different_books;

    [Test]
    [Ignore]
    procedure A_basket_with_five_different_books;

    [Test]
    [Ignore]
    procedure A_basket_containing_eight_books_consisting_of_a_pair_each_of_the_first_three_books_plus_one_copy_each_of_the_last_two_books;

    [Test]
    [Ignore]
    procedure A_basket_containing_nine_books_consisting_of_a_pair_each_of_the_first_four_books_plus_one_of_the_last_book;

    [Test]
    [Ignore]
    procedure A_basket_containing_ten_books_consisting_of_two_copies_of_each_book_in_the_series;

    [Test]
    [Ignore]
    procedure A_basket_containing_eleven_books_consisting_of_three_copies_of_the_first_book_plus_two_each_of_the_remaing_four_bookes_in_the_series;

    [Test]
    [Ignore]
    procedure A_basket_containing_twelve_books_consisting_of_three_copies_of_the_first_two_books_plus_two_each_of_the_remaining_three_books_in_the_series;

    [Test]
    [Ignore]
    procedure Four_groups_of_four_are_cheaper_than_two_groups_each_of_five_and_three;
  end;

implementation
uses System.SysUtils, uBookStore;

const MinDelta = 0.005; //cents

procedure hpTests.A_basket_containing_only_a_single_book;
var Basket: TArray<integer>;
    fBasket: IBasket;
    Expected: double;
begin
  SetLength(Basket, 1);
  Basket[0] := 1;
  Expected := 8.0;

  fBasket := NewBasket(Basket);
  assert.AreEqual(Expected, fBasket.Total, MinDelta, format('Total should be %0.2f',[Expected]));
end;

procedure hpTests.A_basket_containing_only_two_of_the_same_book;
var Basket: TArray<integer>;
    fBasket: IBasket;
    Expected: double;
begin
  SetLength(Basket, 2);
  Basket[0] := 2;
  Basket[1] := 2;
  Expected := 16.0;

  fBasket := NewBasket(Basket);
  assert.AreEqual(Expected, fBasket.Total, MinDelta, format('Total should be %0.2f',[Expected]));
end;

procedure hpTests.No_charge_to_carry_around_an_empty_basket;
var Basket: TArray<integer>;
    fBasket: IBasket;
    Expected: double;
begin
  SetLength(Basket, 0);
  Expected := 0.0;

  fBasket := NewBasket(Basket);
  assert.AreEqual(Expected, fBasket.Total, MinDelta, format('Total should be %0.2f',[Expected]));
end;

procedure hpTests.A_basket_containing_only_two_different_books;
var Basket: TArray<integer>;
    fBasket: IBasket;
    Expected: double;
begin
  SetLength(Basket, 2);
  Basket[0] := 1;
  Basket[1] := 2;
  Expected := 15.2;

  fBasket := NewBasket(Basket);
  assert.AreEqual(Expected, fBasket.Total, MinDelta, format('Total should be %0.2f',[Expected]));
end;

procedure hpTests.A_basket_with_three_different_books;
var Basket: TArray<integer>;
    fBasket: IBasket;
    Expected: double;
begin
  SetLength(Basket, 3);
  Basket[0] := 1;
  Basket[1] := 2;
  Basket[2] := 3;
  Expected := 21.6;

  fBasket := NewBasket(Basket);
  assert.AreEqual(Expected, fBasket.Total, MinDelta, format('Total should be %0.2f',[Expected]));
end;

procedure hpTests.Four_groups_of_four_are_cheaper_than_two_groups_each_of_five_and_three;
var Basket: TArray<integer>;
    fBasket: IBasket;
    Expected: double;
begin
  SetLength(Basket, 16);
  Basket[0] := 1;
  Basket[1] := 1;
  Basket[2] := 2;
  Basket[3] := 2;
  Basket[4] := 3;
  Basket[5] := 3;
  Basket[6] := 4;
  Basket[7] := 5;
  Basket[8] := 1;
  Basket[9] := 1;
  Basket[10] := 2;
  Basket[11] := 2;
  Basket[12] := 3;
  Basket[13] := 3;
  Basket[14] := 4;
  Basket[15] := 5;
  Expected := 102.40;

  fBasket := NewBasket(Basket);
  assert.AreEqual(Expected, fBasket.Total, MinDelta, format('Total should be %0.2f',[Expected]));
end;

procedure hpTests.A_basket_with_four_different_books;
var Basket: TArray<integer>;
    fBasket: IBasket;
    Expected: double;
begin
  SetLength(Basket, 4);
  Basket[0] := 1;
  Basket[1] := 2;
  Basket[2] := 3;
  Basket[3] := 4;
  Expected := 25.6;

  fBasket := NewBasket(Basket);
  assert.AreEqual(Expected, fBasket.Total, MinDelta, format('Total should be %0.2f',[Expected]));
end;

procedure hpTests.A_basket_with_five_different_books;
var Basket: TArray<integer>;
    fBasket: IBasket;
    Expected: double;
begin
  SetLength(Basket, 5);
  Basket[0] := 1;
  Basket[1] := 2;
  Basket[2] := 3;
  Basket[3] := 4;
  Basket[4] := 5;
  Expected := 30.0;

  fBasket := NewBasket(Basket);
  assert.AreEqual(Expected, fBasket.Total, MinDelta, format('Total should be %0.2f',[Expected]));
end;

procedure hpTests.A_basket_containing_eight_books_consisting_of_a_pair_each_of_the_first_three_books_plus_one_copy_each_of_the_last_two_books;
var Basket: TArray<integer>;
    fBasket: IBasket;
    Expected: double;
begin
  SetLength(Basket, 8);
  Basket[0] := 1;
  Basket[1] := 1;
  Basket[2] := 2;
  Basket[3] := 2;
  Basket[4] := 3;
  Basket[5] := 3;
  Basket[6] := 4;
  Basket[7] := 5;
  Expected := 51.20;

  fBasket := NewBasket(Basket);
  assert.AreEqual(Expected, fBasket.Total, MinDelta, format('Total should be %0.2f',[Expected]));
end;

procedure hpTests.A_basket_containing_nine_books_consisting_of_a_pair_each_of_the_first_four_books_plus_one_of_the_last_book;
var Basket: TArray<integer>;
    fBasket: IBasket;
    Expected: double;
begin
  SetLength(Basket, 9);
  Basket[0] := 1;
  Basket[1] := 1;
  Basket[2] := 2;
  Basket[3] := 2;
  Basket[4] := 3;
  Basket[5] := 3;
  Basket[6] := 4;
  Basket[7] := 4;
  Basket[8] := 5;
  Expected := 55.60;

  fBasket := NewBasket(Basket);
  assert.AreEqual(Expected, fBasket.Total, MinDelta, format('Total should be %0.2f',[Expected]));
end;

procedure hpTests.A_basket_containing_ten_books_consisting_of_two_copies_of_each_book_in_the_series;
var Basket: TArray<integer>;
    fBasket: IBasket;
    Expected: double;
begin
  SetLength(Basket, 10);
  Basket[0] := 1;
  Basket[1] := 1;
  Basket[2] := 2;
  Basket[3] := 2;
  Basket[4] := 3;
  Basket[5] := 3;
  Basket[6] := 4;
  Basket[7] := 4;
  Basket[8] := 5;
  Basket[9] := 5;
  Expected := 60.00;

  fBasket := NewBasket(Basket);
  assert.AreEqual(Expected, fBasket.Total, MinDelta, format('Total should be %0.2f',[Expected]));
end;

procedure hpTests.A_basket_containing_eleven_books_consisting_of_three_copies_of_the_first_book_plus_two_each_of_the_remaing_four_bookes_in_the_series;
var Basket: TArray<integer>;
    fBasket: IBasket;
    Expected: double;
begin
  SetLength(Basket, 11);
  Basket[0] := 1;
  Basket[1] := 1;
  Basket[2] := 2;
  Basket[3] := 2;
  Basket[4] := 3;
  Basket[5] := 3;
  Basket[6] := 4;
  Basket[7] := 4;
  Basket[8] := 5;
  Basket[9] := 5;
  Basket[10] := 1;
  Expected := 68.00;

  fBasket := NewBasket(Basket);
  assert.AreEqual(Expected, fBasket.Total, MinDelta, format('Total should be %0.2f',[Expected]));
end;

procedure hpTests.A_basket_containing_twelve_books_consisting_of_three_copies_of_the_first_two_books_plus_two_each_of_the_remaining_three_books_in_the_series;
var Basket: TArray<integer>;
    fBasket: IBasket;
    Expected: double;
begin
  SetLength(Basket, 12);
  Basket[0] := 1;
  Basket[1] := 1;
  Basket[2] := 2;
  Basket[3] := 2;
  Basket[4] := 3;
  Basket[5] := 3;
  Basket[6] := 4;
  Basket[7] := 4;
  Basket[8] := 5;
  Basket[9] := 5;
  Basket[10] := 1;
  Basket[11] := 2;
  Expected := 75.20;

  fBasket := NewBasket(Basket);
  assert.AreEqual(Expected, fBasket.Total, MinDelta, format('Total should be %0.2f',[Expected]));
end;

initialization
  TDUnitX.RegisterTestFixture(hpTests);
end.