This repository has been 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
/
Copy pathTestQuickSort.java
197 lines (191 loc) · 4.28 KB
/
TestQuickSort.java
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
import java.util.ArrayList;
public class TestQuickSort
{
//testing the test for peak memory usage
private static ArrayList<String> TestTestPeakUsage()
{
String n = "";
ArrayList<String> arrOfProblems = new ArrayList<String>();
//setup
QuickSort s = new QuickSort();
sortParams sP = new sortParams();
sP.setMemUsage(64);
s.initialize(sP);
int memUsage1 = 96;
int memUsage2 = 32;
//experiment step 1
s.TestPeakUsage(memUsage1);
//testing case 1
if(s.getSortParams().getMemUsage() != memUsage1)
{
n = "TestPeakUsage when greater";
arrOfProblems.add(n);
}
//experiment step 2
s.initialize(sP);
s.TestPeakUsage(memUsage2);
//testing case 2
if(s.getSortParams().getMemUsage() != sP.getMemUsage())
{
n = "TestPeakUsage when less than";
arrOfProblems.add(n);
}
return arrOfProblems;
}
//testing the pivot value chooser
private static String TestChoosePVal()
{
String n = "";
//setup'
QuickSort qSort = new QuickSort();
int[] arr = {1, 1, 1, 2, 2, 2, 3, 3, 3};
int start = 0;
int end = 8;
int correctOutput = 2;
//experiment step
int PVal = qSort.choosePVal(arr, start, end);
//testing
if(PVal != correctOutput)
{
n = "Choose Pivot Value";
}
return n;
}
//testing QuickSort
private static ArrayList<String> TestQSortMethod()
{
String n = "";
ArrayList<String> arrOfProblems = new ArrayList<String>();
//setup
QuickSort s = new QuickSort();
int[] arrIN = {4, 7, 2, 0, 15, 200, 1, 37};
int[] arrOUT = {0, 1, 2, 4, 7, 15, 37, 200};
int[] arrIN2 = {4, 7, 9, 0, 12, 98, 1, 24};
int[] arrOUT2 = {98, 24, 12, 9, 7, 4, 1, 0};
sortParams sP = new sortParams();
s.initialize(sP);
//experiment step 1
s.QSort(arrIN, 0, arrIN.length-1, Direction.ASCENDING, 0);
//testing case 1
for(int i=0; i<arrIN.length; i++)
{
if(arrIN[i] != arrOUT[i])
{
n = "Quick Sort Ascending";
arrOfProblems.add(n);
}
}
//experiment step 2
s.QSort(arrIN2, 0, arrIN2.length-1, Direction.DESCENDING, 0);
//testing case 2
for(int i=0; i<arrIN2.length; i++)
{
if(arrIN2[i] != arrOUT2[i])
{
n = "Quick Sort Descending";
arrOfProblems.add(n);
}
}
return arrOfProblems;
}
//testing the sort wrapper method
private static ArrayList<String> TestSort()
{
String n = "";
ArrayList<String> arrOfProblems = new ArrayList<String>();
//setup
Sort s = new QuickSort();
sortParams sP = new sortParams();
int[] arrIN = {7, 400, 207, 152, 671};
int[] arrOUTas = {7, 152, 207, 400, 671};
int[] arrOUTds = {671, 400, 207, 152, 7};
sP.setArray(arrIN);
sP.setStartIndex(0);
sP.setEndIndex(arrIN.length);
s.initialize(sP);
//experiment step 1
sP.setDirection(Direction.ASCENDING);
s.sort(sP);
//testing case 1
for(int i=0; i<arrIN.length; i++)
{
if(s.getSortParams().getArray()[i] != arrOUTas[i])
{
n = "Quick Sort's Sort wrapper (Ascending)";
arrOfProblems.add(n);
}
}
//experiment step 2
sP.setDirection(Direction.DESCENDING);
s.sort(sP);
//testing case 2
for(int i=0; i<arrIN.length; i++)
{
if(s.getSortParams().getArray()[i] != arrOUTds[i])
{
n = "Quick Sort's Sort wrapper (Descending)";
arrOfProblems.add(n);
}
}
return arrOfProblems;
}
/*
* Runs all of the unit tests in this class
*/
public static void TestQSort()
{
ArrayList<String> whatTests = new ArrayList<String>();
String Testname = "Quick Sort";
String c = TestChoosePVal();
if(!c.equals(""))
{
whatTests.add(c);
}
ArrayList<String> g = TestTestPeakUsage();
ArrayList<String> g2 = TestQSortMethod();
ArrayList<String> g3 = TestSort();
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));
}
}
}
if(!g3.isEmpty())
{
for(int i=0; i<g3.size(); i++)
{
if(!g3.get(i).equals(""))
{
whatTests.add(g3.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;
}
}