-
Notifications
You must be signed in to change notification settings - Fork 15
/
Assignment5_tester
409 lines (386 loc) · 15 KB
/
Assignment5_tester
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//version 1.1.2
import java.util.*;
public class Test {
public static void main(String[] args) {
//runAllTests(); //Runs all the tests
//testPartA(); //Runs all tests for part A
//testPartB(); //Runs all tests for part B
//partB works with random, so run it in a loop for great results
//testQ1E0(); //Runs tests for 1.0
//testQ1E1a(); //Runs tests for 1.1 depositMoney
//testQ1E1b(); //Runs tests for 1.1 withdrawMoney
//testQ1E4(); //Runs tests for 1.4
//testQ2E5b(); //Runs tests for 2.5 balance
//testQ2E5c(); //Runs tests for 2.5 ThresholdBankAccountsIterator
//testQ2E6a(); //Runs tests for 2.6a
//testQ2E6b1(); //Runs tests for 2.6b1
//testQ2E6b2(); //Runs tests for 2.6b2
//testQ2E6c(); //Runs tests for 2.6c
//testQ2E6c(); //Runs tests for 2.6c
//testQ2E6d(); //Runs tests for 2.6d
}
private static void runAllTests() {
testPartA();
testPartB();
}
private static void testPartA() {
System.out.println("Testing 1.0: ");
testQ1E0();
System.out.println("Testing 1.1a: ");
testQ1E1a();
System.out.println("Testing 1.1b: ");
testQ1E1b();
System.out.println("Testing 1.4: ");
testQ1E4();
}
private static void testPartB() {
System.out.println("Testing 2.5b: ");
testQ2E5b();
System.out.println("Testing 2.5c: ");
testQ2E5c();
System.out.println("Testing 2.6a: ");
testQ2E6a();
System.out.println("Testing 2.6b1");
testQ2E6b1();
System.out.println("Testing 2.6b2");
testQ2E6b2();
System.out.println("Testing 2.6c");
testQ2E6c();
System.out.println("Testing 2.6d");
testQ2E6d();
}
private static void testQ1E0() {
boolean passed = true;
PrimeIterator inputs = new PrimeIterator();
String output = inputs.next().toString();
String expectedOutput = "2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233";
for (int i = 0; i < 50; i++) {
output = output + ", " + inputs.next();
}
if (!output.equals(expectedOutput)) {
passed = false;
System.out.println("Failed on finding all prime numbers: " + " in Q1E0");
System.out.println("Expected output: " + expectedOutput);
System.out.println("But output was: " + output);
}
if (passed)
System.out.println("Passed all tests");
}
private static void testQ1E1a() {
boolean passed = true;
BankAccount test = new BankAccount("testQ1E1a", 1, 50);
boolean output;
int[] inputs = {0, Integer.MAX_VALUE, -1, Integer.MIN_VALUE};
boolean[] expectedOutputs = {true, true, false, false};
for (int i = 0; i < inputs.length; i++) {
output = test.depositMoney(inputs[i]);
if (output != expectedOutputs[i]) {
passed = false;
System.out.println("Failed on input: " + inputs[i] + " in Q1E1a");
System.out.println("Expected output: " + expectedOutputs[i] + " But output was: " + output);
}
}
if (passed)
System.out.println("Passed all tests");
}
private static void testQ1E1b() {
boolean passed = true;
BankAccount test = new BankAccount("testQ1E1b", 1, 50);
boolean output;
int[] inputs = {0, Integer.MAX_VALUE, -1, Integer.MIN_VALUE};
boolean[] expectedOutputs = {true, false, false, false};
for (int i = 0; i < inputs.length; i++) {
output = test.withdrawMoney(inputs[i]);
if (output != expectedOutputs[i]) {
passed = false;
System.out.println("Failed on input: " + inputs[i] + " in Q1E1b");
System.out.println("Expected output: " + expectedOutputs[i] + " But output was: " + output);
}
}
if (passed)
System.out.println("Passed all tests");
}
private static void testQ1E4() {
boolean passed = true;
Comparator<Integer> c = new IntegerComparator();
BinarySearchTree bst;
String output;
int[][] inputs = {{4, 2, 6, 1, 3, 5, 7, 8},
{5, 2, 6, 1, 4, 7, 3, 8},
{2, 1, 3, 4, 5, 6, 7, 8},
{8, 7, 6, 5, 4, 3, 2, 1}};
String[] expectedOutputs = {" 1\n 2\n 3\n4\n 5\n 6\n 7\n 8\n",
" 1\n 2\n 3\n 4\n5\n 6\n 7\n 8\n",
" 1\n2\n 3\n 4\n 5\n 6\n 7\n 8\n",
" 1\n 2\n 3\n 4\n 5\n 6\n 7\n8\n"};
for (int i = 0; i < inputs.length; i++) {
bst = new BinarySearchTree(c);
for (int j = 0; j < inputs[i].length; j++)
bst.insert(inputs[i][j]);
output = bst.toString();
if (!output.equals(expectedOutputs[i])) {
passed = false;
System.out.println("Failed on input: " + Arrays.toString(inputs[i]) + " in Q1E4");
System.out.println("Expected output: ");
System.out.println(expectedOutputs[i]);
System.out.println("But output was: ");
System.out.println(output);
}
}
if (passed)
System.out.println("Passed all tests");
}
public static boolean isBalanced(BankAccountsBinarySearchTree treeBalanced) {
if (treeBalanced.isEmpty()) {
return true;
} else {
return isBalanced(treeBalanced.root);
}
}
private static BankAccount[][] createNew() {
BankAccount[][] add = new BankAccount[7][50];
for(int i = 0;i < add.length;i++) {
for(int j=0;j<add[0].length;j++) {
if(i%2 == 0)
add[i][j] = new BankAccount("" +(int) (Math.random() * 70), 1, 0);
else
add[i][j] = new BankAccount("a" , (int) (Math.random() * 70) + 1, 0);
}
}
return add;
}
private static void testQ2E5b() {
boolean passed = true;
BankAccountsBinarySearchTree test;
Object[] inputs2 = {new AccountComparatorByName(),new AccountComparatorByNumber(),new AccountComparatorByName(),new AccountComparatorByNumber(),new AccountComparatorByName(),new AccountComparatorByNumber(),new AccountComparatorByName()};
BankAccount[][] inputs = createNew();
for (int i = 0; i < inputs.length; i++) {
test = new BankAccountsBinarySearchTree((Comparator<BankAccount>) inputs2[i]);
for (BankAccount acc : inputs[i])
test.insert(acc);
List<BankAccount> preBalanceScan = new DynamicArray(test.size());
for (BankAccount element : test)
preBalanceScan.add(element);
test.balance();
List<BankAccount> postBalanceScan = new DynamicArray(test.size());
for (BankAccount element : test)
postBalanceScan.add(element);
if (!isBalanced(test)) {
passed = false;
System.out.println("Failed in Q2E5b: ");
System.out.println("Insertion sequence: " + Arrays.toString(inputs[i]));
System.out.println("Comparator: " + inputs2[i].getClass().getCanonicalName());
System.out.println("Tree is not balanced");
}
if (!isEquals(postBalanceScan,preBalanceScan)) {
passed = false;
System.out.println("Failed in Q2E5b: ");
System.out.println("inOrder scan is not the same as the unbalanced tree");
}
}
if (passed)
System.out.println("Passed all tests");
}
private static boolean isBalanced(BinaryNode<BankAccount> treeBalanced) {
if (treeBalanced.left == null & treeBalanced.right == null) {
return true;
} else if (treeBalanced.left != null & treeBalanced.right == null) {
return treeBalanced.left.height() == 0;
} else if (treeBalanced.left == null) {
return treeBalanced.right.height() == 0;
} else {
return Math.abs(treeBalanced.right.height() - treeBalanced.left.height()) < 2 && isBalanced(treeBalanced.left) && isBalanced(treeBalanced.right);
}
}
private static void testQ2E5c() {
int[] treshhold = {(int) (Math.random() * 40) - 20,0,(int) (Math.random() * 40) - 20};
boolean passed = true;
BankAccountsBinarySearchTree test;
Comparator<BankAccount> comparator = new AccountComparatorByNumber();
List<BankAccount> lst1 = new DynamicArray<BankAccount>();
List<BankAccount> lst2 = new DynamicArray<BankAccount>();
List<BankAccount> lst3 = new DynamicArray<BankAccount>();
BankAccount[][] inputs = {createBank(treshhold[0], lst1), createBank(treshhold[1], lst2), createBank(treshhold[2], lst3)};
Object[] expectedOutputs = {lst1, lst2, lst3};
for (int i = 0; i < inputs.length; i++) {
test = new BankAccountsBinarySearchTree(comparator);
for (BankAccount acc : inputs[i])
test.insert(acc);
List<BankAccount> lst = new DynamicArray<BankAccount>();
Iterator<BankAccount> iter = new ThresholdBankAccountsIterator(test, treshhold[i]);
while (iter.hasNext()) {
lst.add(iter.next());
}
if (!isEquals(lst, (List<BankAccount>) expectedOutputs[i])) {
passed = false;
System.out.println("Failed on input: " + Arrays.toString(inputs[i]) + " in Q2E5c");
System.out.println("Expected output: " + expectedOutputs[i]);
System.out.println(" But output was: " + lst);
}
}
if (passed) {
System.out.println("Passed all tests");
}
}
private static BankAccount[] createBank(int treshhold, List<BankAccount> lst) {
BankAccount[] bank = new BankAccount[(int) (Math.random() * 50) + 1];
for (int i = 0; i < bank.length; i++) {
bank[i] = new BankAccount("a", i + 1, (int) (Math.random() * 40));
if (bank[i].getBalance() >= treshhold) {
lst.add(bank[i]);
}
}
return bank;
}
private static boolean isEquals(List<BankAccount> lst1, List<BankAccount> lst2) {
return lst1.toString().equals(lst2.toString());
}
private static void testQ2E6a() {
boolean passed = true;
boolean output;
Bank test = new Bank();
BankAccount[] inputs = {new BankAccount("a", 4, 0), new BankAccount("b", 2, 0), new BankAccount("c", 6, 0), new BankAccount("c", 1, 0), new BankAccount("f", 3, 0), new BankAccount("g", 5, 0), new BankAccount("h", 7, 0), new BankAccount("i", 8, 0), new BankAccount("a", 9, 0), new BankAccount("k", 10, 0), new BankAccount("m", 11, 0), new BankAccount("n", 12, 0), new BankAccount("l", 13, 0), new BankAccount("o", 14, 0), new BankAccount("p", 15, 0), new BankAccount("q", 16, 0), new BankAccount("r", 17, 0), new BankAccount("d", 18, 0), new BankAccount("f", 19, 0), new BankAccount("y", 5, 0), new BankAccount("y", 21, 0), new BankAccount("w", 22, 0), new BankAccount("j", 23, 0), new BankAccount("k", 24, 0), new BankAccount("m", 25, 0), new BankAccount("n", 26, 0), new BankAccount("d", 27, 0), new BankAccount("o", 28, 0), new BankAccount("p", 29, 0), new BankAccount("q", 30, 0), new BankAccount("z", 31, 0)};
boolean[] ExpectedOutputs = {true, true, true, false, true, true, true, true, false, true, true, true, true, true, true, true, true, true, false, false, true, true, true, false, false, false, false, false, false, false, true};
for (int i = 0; i < inputs.length; i++) {
output = test.add(inputs[i]);
if (output != ExpectedOutputs[i]) {
passed = false;
System.out.println("Failed on input: " + inputs[i] + " in Q2E6a");
System.out.println("Expected output: " + ExpectedOutputs[i] + "But output was: " + output);
}
}
if (passed) {
System.out.println("Passed all tests");
}
}
private static void testQ2E6b1() {
boolean passed = true;
boolean output;
Bank test = new Bank();
int ran = (int) (Math.random() * 30);
String[] inputs = new String[ran];
boolean[] ExpectedOutputs = new boolean[ran];
test = newSys1(inputs,ExpectedOutputs,ran);
for (int i = 0; i < inputs.length; i++) {
output = test.delete(inputs[i]);
if (output != ExpectedOutputs[i]) {
passed = false;
System.out.println("Failed on input: " + inputs[i] + " in Q2E6b1");
System.out.println("Expected output: " + ExpectedOutputs[i] + " But output was: " + output);
}
}
if (passed) {
System.out.println("Passed all tests");
}
}
private static Bank newSys1(String[] inputs,boolean[] outputs,int ran) {
Bank test = new Bank();
for(int i = 0;i < ran;i++) {
BankAccount add = new BankAccount("" + i,(int) (Math.random() * 30)+1,0);
outputs[i] = test.add(add);
inputs[i] = add.getName();
}
return test;
}
private static void testQ2E6b2() {
boolean passed = true;
boolean output;
Bank test = new Bank();
int ran = (int) (Math.random() * 30);
int[] inputs = new int[ran];
boolean[] ExpectedOutputs = new boolean[ran];
test = newSys2(inputs,ExpectedOutputs,ran);
for (int i = 0; i < inputs.length; i++) {
output = test.delete(inputs[i]);
if (output != ExpectedOutputs[i]) {
passed = false;
System.out.println("Failed on input: " + inputs[i] + " in Q2E6b2");
System.out.println("Expected output: " + ExpectedOutputs[i] + " But output was: " + output);
}
}
if (passed) {
System.out.println("Passed all tests");
}
}
private static Bank newSys2(int[] inputs,boolean[] outputs,int ran) {
Bank test = new Bank();
for(int i = 0;i < ran;i++) {
BankAccount add = new BankAccount("" + (int) (Math.random() * 30),i+1,0);
if(test.lookUp(add.getAccountNumber()) == null && test.lookUp(add.getName()) == null) {
test.add(add);
outputs[i] = true;
}
else {
test.add(add);
outputs[i] = false;
}
inputs[i] = add.getAccountNumber();
}
return test;
}
private static Bank createBankSystemD(boolean[] outputs, int[] inputs, int deposit) {
Bank test = new Bank();
char c = 'a';
for (int i = 0; i < 5; i++, c++) {
BankAccount add = new BankAccount("" + c, i + 1, (int) (Math.random() * 20));
test.add(add);
inputs[i] = i + 1;
outputs[i] = add.depositMoney(deposit);
if (outputs[i])
add.withdrawMoney(deposit);
}
return test;
}
private static void testQ2E6c() {
boolean passed = true;
boolean output;
int[] deposit = {(int) (Math.random() * 40) - 20,(int) (Math.random() * 40) - 20,0,(int) (Math.random() * 40) - 20,(int) (Math.random() * 40) - 20};
Bank[] test = new Bank[deposit.length];
int[] inputs = new int[5];
boolean[] expectedOutputs = new boolean[5];
for (int i = 0; i < test.length; i++) {
test[i] = createBankSystemD(expectedOutputs, inputs, deposit[i]);
output = test[i].depositMoney(deposit[i], inputs[i]);
if (output != expectedOutputs[i]) {
passed = false;
System.out.println("Failed on input: " + inputs[i] + " in Q2E6c");
System.out.println("Expected output: " + expectedOutputs[i] + " But output was: " + output);
}
}
if (passed)
System.out.println("Passed all tests");
}
private static Bank createBankSystemW(boolean[] outputs, int[] inputs, int withdraw) {
Bank test = new Bank();
char c = 'a';
for (int i = 0; i < 5; i++, c++) {
BankAccount add = new BankAccount("" + c, i + 1, (int) (Math.random() * 20));
test.add(add);
inputs[i] = i + 1;
outputs[i] = add.withdrawMoney(withdraw);
if (outputs[i]) {
add.depositMoney(withdraw);
}
}
return test;
}
private static void testQ2E6d() {
boolean passed = true;
boolean output;
int[] withdraw = {(int) (Math.random() * 40) - 20,(int) (Math.random() * 40) - 20,0,(int) (Math.random() * 40) - 20,(int) (Math.random() * 40) - 20};
Bank[] test = new Bank[withdraw.length];
int[] inputs = new int[5];
boolean[] expectedOutputs = new boolean[5];
for (int i = 0; i < test.length; i++) {
test[i] = createBankSystemW(expectedOutputs, inputs, withdraw[i]);
output = test[i].withdrawMoney(withdraw[i], inputs[i]);
if (output != expectedOutputs[i]) {
passed = false;
System.out.println("Failed on input: " + inputs[i] + " in Q2E6d");
System.out.println("Expected output: " + expectedOutputs[i] + " But output was: " + output);
}
}
if (passed)
System.out.println("Passed all tests");
}
}