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 pathTestShellSort.java
232 lines (226 loc) · 5.66 KB
/
TestShellSort.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
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
import java.util.ArrayList;
public class TestShellSort
{
//testing the test for peak memory usage
private static ArrayList<String> TestTestPeakUsage()
{
String n = "";
ArrayList<String> arrOfProblems = new ArrayList<String>();
//setup
ShellSort s = new ShellSort();
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 ShSortMethod
private static ArrayList<String> TestShSortMethod()
{
String n = "";
ArrayList<String> arrOfProblems = new ArrayList<String>();
//setup
ShellSort s = new ShellSort();
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);
sP.setArrSize(arrIN.length);
//experiment step 1
sP.setGapSeqType(gapSeqType.SHELL);
s.ShSort(arrIN, 0, arrIN.length, GapSeqGenerator.generateGapSeq(sP), Direction.ASCENDING);
//testing case 1
for(int i=0; i<arrIN.length; i++)
{
if(arrIN[i] != arrOUT[i])
{
n = "Shell Sort Ascending with SHELL gap sequence";
arrOfProblems.add(n);
}
}
//experiment step 2
sP.setGapSeqType(gapSeqType.KNUTH);
s.ShSort(arrIN, 0, arrIN.length, GapSeqGenerator.generateGapSeq(sP), Direction.ASCENDING);
//testing case 2
for(int i=0; i<arrIN.length; i++)
{
if(arrIN[i] != arrOUT[i])
{
n = "Shell Sort Ascending with KNUTH gap sequence";
arrOfProblems.add(n);
}
}
//experiment step 3
sP.setGapSeqType(gapSeqType.TOKUDA);
s.ShSort(arrIN, 0, arrIN.length, GapSeqGenerator.generateGapSeq(sP), Direction.ASCENDING);
//testing case 3
for(int i=0; i<arrIN.length; i++)
{
if(arrIN[i] != arrOUT[i])
{
n = "Shell Sort Ascending with TOKUDA gap sequence";
arrOfProblems.add(n);
}
}
//experiment step 4
sP.setGapSeqType(gapSeqType.SHELL);
s.ShSort(arrIN2, 0, arrIN2.length, GapSeqGenerator.generateGapSeq(sP), Direction.DESCENDING);
//testing case 4
for(int i=0; i<arrIN2.length; i++)
{
if(arrIN2[i] != arrOUT2[i])
{
n = "Shell Sort Descending with SHELL gap sequence";
arrOfProblems.add(n);
}
}
//experiment step 2
sP.setGapSeqType(gapSeqType.KNUTH);
s.ShSort(arrIN2, 0, arrIN2.length, GapSeqGenerator.generateGapSeq(sP), Direction.DESCENDING);
//testing case 2
for(int i=0; i<arrIN2.length; i++)
{
if(arrIN2[i] != arrOUT2[i])
{
n = "Shell Sort Descending with KNUTH gap sequence";
arrOfProblems.add(n);
}
}
//experiment step 3
sP.setGapSeqType(gapSeqType.TOKUDA);
s.ShSort(arrIN2, 0, arrIN2.length, GapSeqGenerator.generateGapSeq(sP), Direction.DESCENDING);
//testing case 3
for(int i=0; i<arrIN2.length; i++)
{
if(arrIN2[i] != arrOUT2[i])
{
n = "Shell Sort Descending with TOKUDA gap sequence";
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 ShellSort();
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);
sP.setArrSize(arrIN.length);
s.initialize(sP);
//because we already covered shell sort ascending and descending
//with all gap sequences, it doesn't matter what gap sequence we use
sP.setGapSeqType(gapSeqType.TOKUDA);
sP.setGapSeq(GapSeqGenerator.generateGapSeq(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 = "Shell 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 = "Shell Sort's Sort wrapper (Descending)";
arrOfProblems.add(n);
}
}
return arrOfProblems;
}
/*
* Runs all of the unit tests in this class
*/
/*
* Runs all of the unit tests in this class
*/
public static void TestShSort()
{
ArrayList<String> whatTests = new ArrayList<String>();
String Testname = "Shell Sort";
ArrayList<String> g = TestTestPeakUsage();
ArrayList<String> g2 = TestShSortMethod();
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;
}
}