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 pathTestSelectionSort.java
More file actions
176 lines (170 loc) · 3.89 KB
/
TestSelectionSort.java
File metadata and controls
176 lines (170 loc) · 3.89 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
import java.util.ArrayList;
/*
* Unit Tests for SelectionSort
*/
public class TestSelectionSort
{
//testing the test for peak memory usage
private static ArrayList<String> TestTestPeakUsage()
{
String n = "";
ArrayList<String> arrOfProblems = new ArrayList<String>();
//setup
SelectionSort s = new SelectionSort();
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 SelSort
private static ArrayList<String> TestSelSortMethod()
{
String n = "";
ArrayList<String> arrOfProblems = new ArrayList<String>();
//setup
SelectionSort s = new SelectionSort();
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.SelSort(arrIN, 0, arrIN.length, Direction.ASCENDING);
//testing case 1
for(int i=0; i<arrIN.length; i++)
{
if(arrIN[i] != arrOUT[i])
{
n = "Selection Sort Ascending";
arrOfProblems.add(n);
}
}
//experiment step 2
s.SelSort(arrIN2, 0, arrIN2.length, Direction.DESCENDING);
//testing case 2
for(int i=0; i<arrIN2.length; i++)
{
if(arrIN2[i] != arrOUT2[i])
{
n = "Selection 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 SelectionSort();
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 = "Selection 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 = "Selection Sort's Sort wrapper (Descending)";
arrOfProblems.add(n);
}
}
return arrOfProblems;
}
/*
* Runs all of the unit tests in this class
*/
public static void TestSelSort()
{
ArrayList<String> whatTests = new ArrayList<String>();
String Testname = "Selection Sort";
ArrayList<String> g = TestTestPeakUsage();
ArrayList<String> g2 = TestSelSortMethod();
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;
}
}