-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMaxHeap_UnitTesting.java
More file actions
266 lines (166 loc) · 7.02 KB
/
MaxHeap_UnitTesting.java
File metadata and controls
266 lines (166 loc) · 7.02 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
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import org.junit.Test;
public class MaxHeap_UnitTesting {
@Test
public void sequentialHeapTest() {
Integer[] heapValues = {20,40,30,10,90,70};
MaxHeap<Integer> sequenHeap = new MaxHeap<>();
sequenHeap.createHeap_Sequential(heapValues);
Integer[] result = new Integer[6];
for (int i = 1; i < sequenHeap.getSize() + 1; i++) {
result[i-1] = sequenHeap.getHeapValue_atIndex(i);
}
Integer[] expected = {90,40,70,10,20,30};
assertArrayEquals(expected, result );
}
@Test
public void smartHeapTest() {
Integer[] heapValues = {20,40,30,10,90,70};
MaxHeap<Integer> smartHeap = new MaxHeap<>();
smartHeap.createHeap_SmartWay(heapValues);
Integer[] result = new Integer[6];
for(int i = 1; i < smartHeap.getSize() + 1; i++) {
result[i-1] = smartHeap.getHeapValue_atIndex(i);
}
Integer[] expected = {90,40,70,10,20,30};
assertArrayEquals(expected, result);
}
@Test
public void getMethodTest() {
Integer[] heapValues = {20,40,30,10,90,70};
// heap created using smart method
MaxHeap<Integer> smartHeap = new MaxHeap<>();
smartHeap.createHeap_SmartWay(heapValues);
// heap created using sequential method
MaxHeap<Integer> sequenHeap = new MaxHeap<>();
sequenHeap.createHeap_Sequential(heapValues);
assertTrue(smartHeap.getMethod());
assertFalse(sequenHeap.getMethod());
}
@Test
public void getSwapsTest() {
Integer[] heapValues = {20,40,30,10,90,70};
// heap created using smart method
MaxHeap<Integer> smartHeap = new MaxHeap<>();
smartHeap.createHeap_SmartWay(heapValues);
// heap created using sequential method
MaxHeap<Integer> sequenHeap = new MaxHeap<>();
sequenHeap.createHeap_Sequential(heapValues);
int expectedSmartSwaps = 4;
int expectedSequentialSwaps = 4;
assertEquals(expectedSmartSwaps, smartHeap.getSwaps());
assertEquals(expectedSequentialSwaps, sequenHeap.getSwaps());
}
@Test
public void readInputFileTest() throws FileNotFoundException {
String fileName = "C:/Users/galax/git/CS 2400 Assignments/Project_4-MaxHeap/data_sorted.txt";
Integer[] result = DriverMH.read100Integers(fileName);
Integer[] expected = new Integer[100];
for (int i = 0; i < 100;i++) {
int tempInt = i + 1;
expected[i] = Integer.valueOf(tempInt);
}
assertEquals(expected[0], result[0]);
}
@Test
public void printAfterCreationTest() throws IOException {
String fileName = "testUnitTest.txt";
Integer[] heapValues = {20,40,30,10,90,70};
DriverMH.MakeAFile(fileName);
MaxHeap<Integer> smartHeap = new MaxHeap<>();
smartHeap.createHeap_SmartWay(heapValues);
MaxHeap<Integer> sequenHeap = new MaxHeap<>();
sequenHeap.createHeap_Sequential(heapValues);
DriverMH.print100Num_Creation(smartHeap, fileName);
DriverMH.print100Num_Creation(sequenHeap, fileName);
File myFile = new File(fileName);
Scanner outputFile = new Scanner(myFile);
String readResult1 = outputFile.nextLine();
String readResult2 = outputFile.nextLine();
String expected1 = "Heap built using optimal method: 90,40,70,10,20,30,";
String expected2 = "Heap built using sequential insertions: 90,40,70,10,20,30,";
outputFile.close();
myFile.delete();
assertTrue(readResult1.equals(expected1));
assertTrue(readResult2.equals(expected2));
}
@Test
public void printSwapsTest() throws IOException {
String fileName = "testUnitTest.txt";
Integer[] heapValues = {20,40,30,10,90,70};
DriverMH.MakeAFile(fileName);
MaxHeap<Integer> smartHeap = new MaxHeap<>();
smartHeap.createHeap_SmartWay(heapValues);
DriverMH.printSwaps(smartHeap, fileName);
File myFile = new File(fileName);
Scanner outputFile = new Scanner(myFile);
String readResult = outputFile.nextLine();
String expected = "Number of swaps in the heap creation: 4";
outputFile.close();
myFile.delete();
assertTrue(readResult.equals(expected));
}
@Test
public void perform10RemovalsTest() throws FileNotFoundException {
Integer[] heapValues = {20,40,30,1,10,90,70,50,60,70,10};
MaxHeap<Integer> heap = new MaxHeap<>();
heap.createHeap_SmartWay(heapValues);
DriverMH.perform10Removals(heap);
Integer[] result = new Integer[heap.getSize()];
for(int i = 1; i < heap.getSize() + 1; i++) {
result[i-1] = heap.getHeapValue_atIndex(i);
}
Integer[] expected = {1};
assertArrayEquals(expected, result);
}
@Test
public void printAfterRemovalTest() throws IOException {
String fileName = "testUnitTest.txt";
Integer[] heapValues = {20,40,30,1,10,90,70,50,60,70,10};
DriverMH.MakeAFile(fileName);
MaxHeap<Integer> smartHeap = new MaxHeap<>();
smartHeap.createHeap_SmartWay(heapValues);
DriverMH.perform10Removals(smartHeap);
DriverMH.print100Num_Removal(smartHeap, fileName);
File myFile = new File(fileName);
Scanner outputFile = new Scanner(myFile);
String readResult1 = outputFile.nextLine();
String expected1 = "Heap after 10 removals: 1,";
outputFile.close();
myFile.delete();
assertTrue(readResult1.equals(expected1));
}
@Test
public void printOutputBorderTest() throws IOException {
String fileName = "testUnitTest.txt";
Integer[] heapValues = {20,40,30,1,10,90,70,50,60,70,10};
DriverMH.MakeAFile(fileName);
DriverMH.printOutputBorder(fileName);
File myFile = new File(fileName);
Scanner outputFile = new Scanner(myFile);
String readResult1 = outputFile.nextLine();
String expected1 = "=====================================================================";
outputFile.close();
myFile.delete();
assertTrue(readResult1.equals(expected1));
}
@Test
public void makeFileTest() throws FileNotFoundException {
String fileName = "testUnitTest.txt";
DriverMH.MakeAFile(fileName);
File myFile = new File(fileName);
boolean fileExists = myFile.exists();
myFile.delete();
assertTrue(fileExists);
}
}