This repository was archived by the owner on Mar 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestDoSort.java
More file actions
661 lines (655 loc) · 18.1 KB
/
TestDoSort.java
File metadata and controls
661 lines (655 loc) · 18.1 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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
import java.util.ArrayList;
/*
* Unit Tests for doSort.java
*/
public class TestDoSort
{
private static ArrayList<String> testDoSortMethod()
{
//setup
ArrayList<String> arrOfProblems = new ArrayList<String>();
ExpParams e = new ExpParams();
e.setNumOfIterations(5);
e.setArrSize(500);
e.setGenArrayType(GenArrayType.RANDOM);
ArrayList<String> tmplist = testSelSort(e);
//testing every sort according to doSort.java
for(int i=0; i<tmplist.size(); i++)
{
arrOfProblems.add(tmplist.get(i)); //adds all the problems to the arraylist
}
tmplist = testInsSort(e);
for(int i=0; i<tmplist.size(); i++)
{
arrOfProblems.add(tmplist.get(i));
}
tmplist = testShSort(e);
for(int i=0; i<tmplist.size(); i++)
{
arrOfProblems.add(tmplist.get(i));
}
tmplist = testMSort(e);
for(int i=0; i<tmplist.size(); i++)
{
arrOfProblems.add(tmplist.get(i));
}
tmplist = testQSort(e);
for(int i=0; i<tmplist.size(); i++)
{
arrOfProblems.add(tmplist.get(i));
}
tmplist = testMSortHybrid(e);
for(int i=0; i<tmplist.size(); i++)
{
arrOfProblems.add(tmplist.get(i));
}
tmplist = testQSortHybrid(e);
for(int i=0; i<tmplist.size(); i++)
{
arrOfProblems.add(tmplist.get(i));
}
return arrOfProblems;
}
//testing quicksort hybrid according to doSort
private static ArrayList<String> testQSortHybrid(ExpParams e)
{
String n = "";
ArrayList<String> arrOfProblems = new ArrayList<String>();
sortParams s = new sortParams();
ArrayList<sortResults> results = new ArrayList<sortResults>();
s.setDirection(Direction.ASCENDING);
s.setStartIndex(0);
s.setArray(ArrayGenerator.genArray(e));
Sort sorter = new QuickSortHybrid();
for(int t=50; t<e.getArrSize(); t+=50)
{
s.setMemUsage(0);
sorter.initialize(s);
doSort.runSort(e, s, sorter, e.getArrSize(), results);
for(int i=0; i<results.size(); i++)
{
if(!results.get(i).getSuccessful())
{
n = "Quick Sort Hybrid was not successful (Ascending)";
arrOfProblems.add(n);
}
if(results.get(i).getMemoryUsage() < 0)
{
n = "Incorrect Memory Usage with Quick Sort Hybrid (Ascending)";
arrOfProblems.add(n);
}
if(results.get(i).getTimeRequired() < 0)
{
n = "Incorrect average time of sort with Quick Sort Hybrid (Ascending)";
arrOfProblems.add(n);
}
}
}
s.setDirection(Direction.DESCENDING);
s.setArray(ArrayGenerator.genArray(e));
for(int t=50; t<e.getArrSize(); t+=50)
{
s.setMemUsage(0);
sorter.initialize(s);
doSort.runSort(e, s, sorter, e.getArrSize(), results);
for(int i=0; i<results.size(); i++)
{
if(!results.get(i).getSuccessful())
{
n = "Quick Sort Hybrid was not successful (Descending)";
arrOfProblems.add(n);
}
if(results.get(i).getMemoryUsage() < 0)
{
n = "Incorrect Memory Usage with Quick Sort Hybrid (Descending)";
arrOfProblems.add(n);
}
if(results.get(i).getTimeRequired() < 0)
{
n = "Incorrect average time of sort with Quick Sort Hybrid (Descending)";
arrOfProblems.add(n);
}
}
}
return arrOfProblems;
}
//testing merge sort hybrid according to doSort
private static ArrayList<String> testMSortHybrid(ExpParams e)
{
String n = "";
ArrayList<String> arrOfProblems = new ArrayList<String>();
sortParams s = new sortParams();
ArrayList<sortResults> results = new ArrayList<sortResults>();
s.setDirection(Direction.ASCENDING);
s.setStartIndex(0);
s.setArray(ArrayGenerator.genArray(e));
Sort sorter = new MergeSortHybrid();
for(int t=50; t<e.getArrSize(); t+=50)
{
s.setMemUsage(0);
sorter.initialize(s);
doSort.runSort(e, s, sorter, e.getArrSize(), results);
for(int i=0; i<results.size(); i++)
{
if(!results.get(i).getSuccessful())
{
n = "Merge Sort Hybrid was not successful (Ascending)";
arrOfProblems.add(n);
}
if(results.get(i).getMemoryUsage() < 0)
{
n = "Incorrect Memory Usage with Merge Sort Hybrid (Ascending)";
arrOfProblems.add(n);
}
if(results.get(i).getTimeRequired() < 0)
{
n = "Incorrect average time of sort with Merge Sort Hybrid (Ascending)";
arrOfProblems.add(n);
}
}
}
s.setDirection(Direction.DESCENDING);
s.setArray(ArrayGenerator.genArray(e));
for(int t=50; t<e.getArrSize(); t+=50)
{
s.setMemUsage(0);
sorter.initialize(s);
doSort.runSort(e, s, sorter, e.getArrSize(), results);
for(int i=0; i<results.size(); i++)
{
if(!results.get(i).getSuccessful())
{
n = "Merge Sort Hybrid was not successful (Descending)";
arrOfProblems.add(n);
}
if(results.get(i).getMemoryUsage() < 0)
{
n = "Incorrect Memory Usage with Merge Sort Hybrid (Descending)";
arrOfProblems.add(n);
}
if(results.get(i).getTimeRequired() < 0)
{
n = "Incorrect average time of sort with Merge Sort Hybrid (Descending)";
arrOfProblems.add(n);
}
}
}
return arrOfProblems;
}
//testing quicksort according to doSort
private static ArrayList<String> testQSort(ExpParams e)
{
String n = "";
ArrayList<String> arrOfProblems = new ArrayList<String>();
sortParams s = new sortParams();
ArrayList<sortResults> results = new ArrayList<sortResults>();
s.setDirection(Direction.ASCENDING);
s.setStartIndex(0);
s.setArray(ArrayGenerator.genArray(e));
s.setMemUsage(0);
Sort sorter = new QuickSort();
sorter.initialize(s);
doSort.runSort(e, s, sorter, e.getArrSize(), results);
for(int i=0; i<results.size(); i++)
{
if(!results.get(i).getSuccessful())
{
n = "Quick Sort was not successful (Ascending)";
arrOfProblems.add(n);
}
if(results.get(i).getMemoryUsage() < 0)
{
n = "Incorrect Memory Usage with Quick Sort (Ascending)";
arrOfProblems.add(n);
}
if(results.get(i).getTimeRequired() < 0)
{
n = "Incorrect average time of sort with Quick Sort (Ascending)";
arrOfProblems.add(n);
}
}
s.setDirection(Direction.DESCENDING);
s.setArray(ArrayGenerator.genArray(e));
s.setMemUsage(0);
sorter.initialize(s);
doSort.runSort(e, s, sorter, e.getArrSize(), results);
for(int i=0; i<results.size(); i++)
{
if(!results.get(i).getSuccessful())
{
n = "Quick Sort was not successful (Descending)";
arrOfProblems.add(n);
}
if(results.get(i).getMemoryUsage() < 0)
{
n = "Incorrect Memory Usage with Quick Sort (Descending)";
arrOfProblems.add(n);
}
if(results.get(i).getTimeRequired() < 0)
{
n = "Incorrect average time of sort with Quick Sort (Descending)";
arrOfProblems.add(n);
}
}
return arrOfProblems;
}
//testing merge sort according to doSort
private static ArrayList<String> testMSort(ExpParams e)
{
String n = "";
ArrayList<String> arrOfProblems = new ArrayList<String>();
sortParams s = new sortParams();
ArrayList<sortResults> results = new ArrayList<sortResults>();
s.setDirection(Direction.ASCENDING);
s.setStartIndex(0);
s.setArray(ArrayGenerator.genArray(e));
s.setMemUsage(0);
Sort sorter = new MergeSort();
sorter.initialize(s);
doSort.runSort(e, s, sorter, e.getArrSize(), results);
for(int i=0; i<results.size(); i++)
{
if(!results.get(i).getSuccessful())
{
n = "Merge Sort was not successful (Ascending)";
arrOfProblems.add(n);
}
if(results.get(i).getMemoryUsage() < 0)
{
n = "Incorrect Memory Usage with Merge Sort (Ascending)";
arrOfProblems.add(n);
}
if(results.get(i).getTimeRequired() < 0)
{
n = "Incorrect average time of sort with Merge Sort (Ascending)";
arrOfProblems.add(n);
}
}
s.setDirection(Direction.DESCENDING);
s.setArray(ArrayGenerator.genArray(e));
s.setMemUsage(0);
sorter.initialize(s);
doSort.runSort(e, s, sorter, e.getArrSize(), results);
for(int i=0; i<results.size(); i++)
{
if(!results.get(i).getSuccessful())
{
n = "Merge Sort was not successful (Descending)";
arrOfProblems.add(n);
}
if(results.get(i).getMemoryUsage() < 0)
{
n = "Incorrect Memory Usage with Merge Sort (Descending)";
arrOfProblems.add(n);
}
if(results.get(i).getTimeRequired() < 0)
{
n = "Incorrect average time of sort with Merge Sort (Descending)";
arrOfProblems.add(n);
}
}
return arrOfProblems;
}
//testing shell sort according to doSort
private static ArrayList<String> testShSort(ExpParams e)
{
String n = "";
ArrayList<String> arrOfProblems = new ArrayList<String>();
sortParams s = new sortParams();
ArrayList<sortResults> results = new ArrayList<sortResults>();
s.setDirection(Direction.ASCENDING);
s.setStartIndex(0);
s.setArrSize(e.getArrSize());
s.setArray(ArrayGenerator.genArray(e));
s.setGapSeqType(gapSeqType.SHELL);
s.setGapSeq(GapSeqGenerator.generateGapSeq(s));
s.setMemUsage(0);
Sort sorter = new ShellSort();
sorter.initialize(s);
doSort.runSort(e, s, sorter, e.getArrSize(), results);
for(int i=0; i<results.size(); i++)
{
if(!results.get(i).getSuccessful())
{
n = "Shell Sort was not successful (Ascending, Shell)";
arrOfProblems.add(n);
}
if(results.get(i).getMemoryUsage() < 0)
{
n = "Incorrect Memory Usage with Shell Sort (Ascending, Shell)";
arrOfProblems.add(n);
}
if(results.get(i).getTimeRequired() < 0)
{
n = "Incorrect average time of sort with Shell Sort (Ascending, Shell)";
arrOfProblems.add(n);
}
}
s.setGapSeqType(gapSeqType.KNUTH);
s.setGapSeq(GapSeqGenerator.generateGapSeq(s));
s.setArray(ArrayGenerator.genArray(e));
s.setMemUsage(0);
sorter.initialize(s);
doSort.runSort(e, s, sorter, e.getArrSize(), results);
for(int i=0; i<results.size(); i++)
{
if(!results.get(i).getSuccessful())
{
n = "Shell Sort was not successful (Ascending, Knuth)";
arrOfProblems.add(n);
}
if(results.get(i).getMemoryUsage() < 0)
{
n = "Incorrect Memory Usage with Shell Sort (Ascending, Knuth)";
arrOfProblems.add(n);
}
if(results.get(i).getTimeRequired() < 0)
{
n = "Incorrect average time of sort with Shell Sort (Ascending, Knuth)";
arrOfProblems.add(n);
}
}
s.setGapSeqType(gapSeqType.TOKUDA);
s.setGapSeq(GapSeqGenerator.generateGapSeq(s));
s.setArray(ArrayGenerator.genArray(e));
s.setMemUsage(0);
sorter.initialize(s);
doSort.runSort(e, s, sorter, e.getArrSize(), results);
for(int i=0; i<results.size(); i++)
{
if(!results.get(i).getSuccessful())
{
n = "Shell Sort was not successful (Ascending, Tokuda)";
arrOfProblems.add(n);
}
if(results.get(i).getMemoryUsage() < 0)
{
n = "Incorrect Memory Usage with Shell Sort (Ascending, Tokuda)";
arrOfProblems.add(n);
}
if(results.get(i).getTimeRequired() < 0)
{
n = "Incorrect average time of sort with Shell Sort (Ascending, Tokuda)";
arrOfProblems.add(n);
}
}
s.setDirection(Direction.DESCENDING);
s.setArray(ArrayGenerator.genArray(e));
s.setMemUsage(0);
sorter.initialize(s);
doSort.runSort(e, s, sorter, e.getArrSize(), results);
for(int i=0; i<results.size(); i++)
{
if(!results.get(i).getSuccessful())
{
n = "Shell Sort was not successful (Descending, Tokuda)";
arrOfProblems.add(n);
}
if(results.get(i).getMemoryUsage() < 0)
{
n = "Incorrect Memory Usage with Shell Sort (Descending, Tokuda)";
arrOfProblems.add(n);
}
if(results.get(i).getTimeRequired() < 0)
{
n = "Incorrect average time of sort with Shell Sort (Descending, Tokuda)";
arrOfProblems.add(n);
}
}
s.setGapSeqType(gapSeqType.KNUTH);
s.setGapSeq(GapSeqGenerator.generateGapSeq(s));
s.setArray(ArrayGenerator.genArray(e));
s.setMemUsage(0);
sorter.initialize(s);
doSort.runSort(e, s, sorter, e.getArrSize(), results);
for(int i=0; i<results.size(); i++)
{
if(!results.get(i).getSuccessful())
{
n = "Shell Sort was not successful (Descending, Knuth)";
arrOfProblems.add(n);
}
if(results.get(i).getMemoryUsage() < 0)
{
n = "Incorrect Memory Usage with Shell Sort (Descending, Knuth)";
arrOfProblems.add(n);
}
if(results.get(i).getTimeRequired() < 0)
{
n = "Incorrect average time of sort with Shell Sort (Descending, Knuth)";
arrOfProblems.add(n);
}
}
s.setGapSeqType(gapSeqType.SHELL);
s.setGapSeq(GapSeqGenerator.generateGapSeq(s));
s.setArray(ArrayGenerator.genArray(e));
s.setMemUsage(0);
sorter.initialize(s);
doSort.runSort(e, s, sorter, e.getArrSize(), results);
for(int i=0; i<results.size(); i++)
{
if(!results.get(i).getSuccessful())
{
n = "Shell Sort was not successful (Ascending, Shell)";
arrOfProblems.add(n);
}
if(results.get(i).getMemoryUsage() < 0)
{
n = "Incorrect Memory Usage with Shell Sort (Ascending, Shell)";
arrOfProblems.add(n);
}
if(results.get(i).getTimeRequired() < 0)
{
n = "Incorrect average time of sort with Shell Sort (Ascending, Shell)";
arrOfProblems.add(n);
}
}
return arrOfProblems;
}
//testing insertion sort according to doSort
private static ArrayList<String> testInsSort(ExpParams e)
{
String n = "";
ArrayList<String> arrOfProblems = new ArrayList<String>();
sortParams s = new sortParams();
ArrayList<sortResults> results = new ArrayList<sortResults>();
s.setDirection(Direction.ASCENDING);
s.setStartIndex(0);
s.setArray(ArrayGenerator.genArray(e));
s.setMemUsage(0);
Sort sorter = new InsertionSort();
sorter.initialize(s);
doSort.runSort(e, s, sorter, e.getArrSize(), results);
for(int i=0; i<results.size(); i++)
{
if(!results.get(i).getSuccessful())
{
n = "Insertion Sort was not successful (Ascending)";
arrOfProblems.add(n);
}
if(results.get(i).getMemoryUsage() != 96)
{
n = "Incorrect Memory Usage with Insertion Sort (Ascending)";
arrOfProblems.add(n);
}
if(results.get(i).getTimeRequired() < 0)
{
n = "Incorrect average time of sort with Insertion Sort (Ascending)";
arrOfProblems.add(n);
}
}
s.setDirection(Direction.DESCENDING);
s.setArray(ArrayGenerator.genArray(e));
s.setMemUsage(0);
sorter.initialize(s);
doSort.runSort(e, s, sorter, e.getArrSize(), results);
for(int i=0; i<results.size(); i++)
{
if(!results.get(i).getSuccessful())
{
n = "Insertion Sort was not successful (Descending)";
arrOfProblems.add(n);
}
if(results.get(i).getMemoryUsage() != 96)
{
n = "Incorrect Memory Usage with Insertion Sort (Descending)";
arrOfProblems.add(n);
}
if(results.get(i).getTimeRequired() < 0)
{
n = "Incorrect average time of sort with Insertion Sort (Descending)";
arrOfProblems.add(n);
}
}
return arrOfProblems;
}
//testing selection sort according to doSort
private static ArrayList<String> testSelSort(ExpParams e)
{
String n = "";
ArrayList<String> arrOfProblems = new ArrayList<String>();
sortParams s = new sortParams();
ArrayList<sortResults> results = new ArrayList<sortResults>();
s.setDirection(Direction.ASCENDING);
s.setStartIndex(0);
s.setArray(ArrayGenerator.genArray(e));
s.setMemUsage(0);
Sort sorter = new SelectionSort();
sorter.initialize(s);
doSort.runSort(e, s, sorter, e.getArrSize(), results);
for(int i=0; i<results.size(); i++)
{
if(!results.get(i).getSuccessful())
{
n = "Selection Sort was not successful (Ascending)";
arrOfProblems.add(n);
}
if(results.get(i).getMemoryUsage() != 96)
{
n = "Incorrect Memory Usage with Selection Sort (Ascending)";
arrOfProblems.add(n);
}
if(results.get(i).getTimeRequired() < 0)
{
n = "Incorrect average time of sort with Selection Sort (Ascending)";
arrOfProblems.add(n);
}
}
s.setDirection(Direction.DESCENDING);
s.setArray(ArrayGenerator.genArray(e));
s.setMemUsage(0);
sorter.initialize(s);
doSort.runSort(e, s, sorter, e.getArrSize(), results);
for(int i=0; i<results.size(); i++)
{
if(!results.get(i).getSuccessful())
{
n = "Selection Sort was not successful (Descending)";
arrOfProblems.add(n);
}
if(results.get(i).getMemoryUsage() != 96)
{
n = "Incorrect Memory Usage with Selection Sort (Descending)";
arrOfProblems.add(n);
}
if(results.get(i).getTimeRequired() < 0)
{
n = "Incorrect average time of sort with Selection Sort (Descending)";
arrOfProblems.add(n);
}
}
return arrOfProblems;
}
//array check method test
private static ArrayList<String> TestArrCheck()
{
String n = "";
ArrayList<String> arrOfProblems = new ArrayList<String>();
//setup
sortParams s = new sortParams();
int[] asArr = {1, 5, 8, 72, 204, 507, 943, 2342};
int[] raArr = {32, 21, 3452, 23, 342, 1241, 12, 234};
int[] dsArr = {2622, 1231, 435, 234, 35, 9, 4, 2};
//experiment step 1
s.setArray(asArr);
s.setDirection(Direction.ASCENDING);
//testing case 1
if(!doSort.arrCheck(s))
{
n = "Checking a Sorted Ascending Array";
arrOfProblems.add(n);
}
//experiment step 2
s.setArray(raArr);
s.setDirection(Direction.DESCENDING);
//testing case 2
if(doSort.arrCheck(s))
{
n = "Checking a Random Array (ascending)";
arrOfProblems.add(n);
}
//experiment step 3
s.setArray(dsArr);
s.setDirection(Direction.DESCENDING);
//testing case 3
if(!doSort.arrCheck(s))
{
n = "Checking a Sorted Descending Array";
arrOfProblems.add(n);
}
//experiment step 4
s.setArray(raArr);
s.setDirection(Direction.ASCENDING);
//testing case 4
if(doSort.arrCheck(s))
{
n = "Checking a Random Array (descending)";
arrOfProblems.add(n);
}
//returns true if none of the experiments done were false
return arrOfProblems;
}
/*
* Runs all of the unit tests in this class
*/
public static void TestSorting()
{
ArrayList<String> whatTests = new ArrayList<String>();
String Testname = "Main Sorting Method";
ArrayList<String> g = TestArrCheck();
ArrayList<String> g2 = testDoSortMethod();
if(!g.isEmpty())
{
for(int i=0; i<g.size(); i++)
{
if(!g.get(i).equals(""))
{
whatTests.add(g.get(i));
}
}
}
if(!g2.isEmpty())
{
for(int i=0; i<g2.size(); i++)
{
if(!g2.get(i).equals(""))
{
whatTests.add(g2.get(i));
}
}
}
boolean isCorrect = testIfFalse(whatTests);
FileIOUnitTestMenu.displayUnitTestResults(isCorrect, Testname, whatTests);
}
private static boolean testIfFalse(ArrayList<String> whatTests)
{
for(int i=0; i<whatTests.size(); i++)
{
if(!whatTests.get(i).equals(""))
{
return false;
}
}
return true;
}
}