-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreditHistoryTest.java
More file actions
345 lines (266 loc) · 8.91 KB
/
CreditHistoryTest.java
File metadata and controls
345 lines (266 loc) · 8.91 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.ArrayList;
import org.junit.Test;
/**
* Group #2
*/
public class CreditHistoryTest {
/**
* Tests method 'addRating' that adds a valid number 0 into array list.
* Should return ArrayList containing 0.
*/
@Test
public void test_add_get_ratings_0() {
CreditHistory ch = new CreditHistory();
ch.addRating(0);
assertEquals("There should be a 0 Rating in a list.",
new ArrayList<Integer>(Arrays.asList(0)),
ch.getRatings());
}
/**
* Tests method 'addRating' that adds a valid number -5 into array list.
* Should return ArrayList containing -5.
*/
@Test
public void test_add_get_ratings_lowerBound_neg5() {
CreditHistory ch = new CreditHistory();
ch.addRating(-5);
assertEquals("There should be a -5 Rating in a list.",
new ArrayList<Integer>(Arrays.asList(Integer.valueOf(-5))),
ch.getRatings());
}
/**
* Tests method 'addRating' that adds invalid number -6 into array list.
* Should return an empty ArrayList.
*/
@Test
public void test_add_get_ratings_lowerBound_neg6() {
CreditHistory ch = new CreditHistory();
ch.addRating(-6);
assertEquals("The ratings should be empty.",
new ArrayList<Integer>(),
ch.getRatings());
}
/**
* Tests method 'addRating' that adds a valid number 5 to array list.
* Should return ArrayList containing 5.
*/
@Test
public void test_add_get_ratings_upperBound_5() {
CreditHistory ch = new CreditHistory();
ch.addRating(5);
assertEquals("There should be a 5 Rating in a list.",
new ArrayList<Integer>(Arrays.asList(Integer.valueOf(5))),
ch.getRatings());
}
/**
* Tests method 'addRating' that adds an invalid number 6 to array list.
* Expectations is to return empty ArrayList.
*/
@Test
public void test_add_get_ratings_upperBound_6() {
CreditHistory ch = new CreditHistory();
ch.addRating(6);
assertEquals("The ratings should be empty.",
new ArrayList<Integer>(),
ch.getRatings());
}
/**
* Tests method 'addRating' that adds valid numbers 0, -2, 5.
* Should return ArrayList containing [0, -2, 5].
*/
@Test
public void test_add_get_ratings_order() {
CreditHistory ch = new CreditHistory();
ch.addRating(0);
ch.addRating(-2);
ch.addRating(5);
assertEquals("There should be 0, -2, 5 in order in a list.",
new ArrayList<Integer>(Arrays.asList(
Integer.valueOf(0), Integer.valueOf(-2), Integer.valueOf(5))),
ch.getRatings());
}
/**
* Tests mehod trimRatings arraylist has less then 10 integers
* Should return the arraylist with no changes
*/
@Test
public void test_trim_get_ratings_lessThan10() {
CreditHistory ch = new CreditHistory();
ArrayList<Integer> iList = new ArrayList<Integer>();
for (int i = 0; i < 7; i++) {
ch.addRating(i - 5);
iList.add(Integer.valueOf(i - 5));
}
assertEquals("There should be integers from -5 to 1 in an ordered list.",
iList, ch.getRatings());
ch.trimRatings();
assertEquals("There should still be integers from -5 to 1 in an ordered list after trimming.",
iList, ch.getRatings());
}
/**
* Tests method trimRatings arraylist inputed has more then 10 integers
* Should return arraylist that trimed the first few arrays inputed
* So that the final arraylist starts from the last integer it trimed
* In this case the array should start from 3
*/
@Test
public void test_trim_get_ratings_moreThan10() {
CreditHistory ch = new CreditHistory();
ArrayList<Integer> iList = new ArrayList<Integer>();
for (int i = 0; i < 13; i++) {
ch.addRating(i % 5);
iList.add(i % 5);
}
assertEquals("There should be integers in an ordered list.",
iList, ch.getRatings());
iList = new ArrayList<Integer>(iList.subList(iList.size() - 10, iList.size()));
ch.trimRatings();
assertEquals("There should be integers in an ordered list after trimming, starting from 3.",
iList, ch.getRatings());
}
/**
* Test method trimRating and method numOfRating
* Inputs an arraylist with less then 10 integers
* Should output a list with the amount of integers inputed
* In this case it would be a list of 7 integers
*/
@Test
public void test_trim_numOf_ratings_lessThan10() {
CreditHistory ch = new CreditHistory();
ArrayList<Integer> iList = new ArrayList<Integer>();
for (int i = 0; i < 7; i++) {
ch.addRating(i - 5);
iList.add(Integer.valueOf(i - 5));
}
assertEquals("There should be 7 integers in the list.",
7, ch.numOfRatings());
ch.trimRatings();
assertEquals("There should still be 7 integers in the list after trimming.",
7, ch.numOfRatings());
}
/**
* Test Method trimRating and method numOfRating
* Inputs an arraylist with more the 10 integers
* Should output a list with 10 integers after the triming
*/
@Test
public void test_trim_numOf_ratings_moreThan10() {
CreditHistory ch = new CreditHistory();
ArrayList<Integer> iList = new ArrayList<Integer>();
for (int i = 0; i < 13; i++) {
ch.addRating(i % 5);
iList.add(i % 5);
}
assertEquals("There should be 13 integers in the list.",
13, ch.numOfRatings());
iList = new ArrayList<Integer>(iList.subList(iList.size() - 10, iList.size()));
ch.trimRatings();
assertEquals("There should be 10 integers in the list after trimming.",
10, ch.numOfRatings());
}
/**
* Test Methods creditRating
* the test inputs an arraylist with odd integers less than 10
* The list outputs the weighted average without trimming
*/
@Test
public void test_creditRating_lessThan10_odd(){
CreditHistory ch = new CreditHistory();
ArrayList<Integer> iList = new ArrayList<Integer>();
for (int i = 0; i < 3; i++) {
ch.addRating(i % 5 - 2);
iList.add(i % 5 - 2);
}
double sum = 0;
for (int i = 1; i <= iList.size(); i++) {
sum += 2d * i / (iList.size() + 1) * iList.get(i - 1);
}
assertEquals("The weighted average without trimming",
sum/iList.size(), ch.getCreditRating(), 0.0001);
}
/**
* Test Methods creditRating
* The test inputs an arraylist with odd integers more than 10
* The list outputs the weighted average without trimming
*/
@Test
public void test_creditRating_moreThan10_odd(){
CreditHistory ch = new CreditHistory();
ArrayList<Integer> iList = new ArrayList<Integer>();
for (int i = 0; i < 13; i++) {
ch.addRating(i % 5 - 2);
iList.add(i % 5 - 2);
}
double sum = 0;
for (int i = 0; i < iList.size(); i++) {
sum += 2d * (i + 1) / (iList.size() + 1) * iList.get(i);
}
assertEquals("The weighted average without trimming",
sum/iList.size(), ch.getCreditRating(), 0.0001);
}
/**
* Test Methods creditRating
* The test inputs an arraylist with even integers more than 10
* The list outputs the weighted average without trimming
*/
@Test
public void test_creditRating_moreThan10_even(){
CreditHistory ch = new CreditHistory();
ArrayList<Integer> iList = new ArrayList<Integer>();
for (int i = 0; i < 14; i++) {
ch.addRating(i % 5 - 2);
iList.add(i % 5 - 2);
}
double sum = 0;
for (int i = 0; i < iList.size(); i++) {
sum += 2d * (i + 1) / (iList.size() + 1) * iList.get(i);
}
assertEquals("The weighted average without trimming",
sum/iList.size(), ch.getCreditRating(), 0.0001);
}
/**
* Test Methods creditRating and trimRatings
* The test inputs an arraylist with odd integers less than 10
* The list outputs the weighted average with trimming
*/
@Test
public void test_trim_creditRating_lessThan10_odd(){
CreditHistory ch = new CreditHistory();
ArrayList<Integer> iList = new ArrayList<Integer>();
for (int i = 0; i < 7; i++) {
ch.addRating(i % 5 - 2);
iList.add(i % 5 - 2);
}
ch.trimRatings();
double sum = 0;
for (int i = 0; i < iList.size(); i++) {
sum += 2d * (i + 1) / (iList.size() + 1) * iList.get(i);
}
assertEquals("The weighted average with trimming",
sum/iList.size(), ch.getCreditRating(), 0.0001);
}
/**
* Test Methods creditRating and trimRatings
* The test inputs an arraylist with odd integers more than 10
* The list outputs the weighted average with trimming
*/
@Test
public void test_trim_creditRating_moreThan10_odd(){
CreditHistory ch = new CreditHistory();
ArrayList<Integer> iList = new ArrayList<Integer>();
for (int i = 0; i < 11; i++) {
ch.addRating(i % 5 - 2);
iList.add(i % 5 - 2);
}
iList = new ArrayList<Integer>(iList.subList(iList.size() - 10, iList.size()));
ch.trimRatings();
double sum = 0;
for (int i = 0; i < iList.size(); i++) {
sum += 2d * (i + 1) / (iList.size() + 1) * iList.get(i);
}
assertEquals("The weighted average with trimming",
sum/iList.size(), ch.getCreditRating(), 0.0001);
}
}