-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGreedyTester.java
More file actions
326 lines (267 loc) · 11.6 KB
/
GreedyTester.java
File metadata and controls
326 lines (267 loc) · 11.6 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
//package assignment2;
import java.util.Arrays;
public class GreedyTester {
public static boolean typicalInput() {//CodePost test 1-2
//This is the typical kind of input you will be tested with. The format will always be the same
//Each index represents a single homework. For example, homework zero has weight 23 and deadline t=3.
int[] weights = new int[] {23, 60, 14, 25, 7};
int[] deadlines = new int[] {3, 1, 2, 1, 3};
int m = weights.length;
//This is the declaration of a schedule of the appropriate size
HW_Sched schedule = new HW_Sched(weights, deadlines, m);
//This call organizes the assignments and outputs homeworkPlan
int[] student = schedule.SelectAssignments();
int[] expected = new int[] {1,2,0};
//System.out.println(Arrays.toString(student));
//System.out.println(Arrays.toString(student));
for (int i=0; i<expected.length; i++) {
if (student[i]!=expected[i]) {
return false;
}
}
return true;
}
public static boolean noConflicts() { //CodePost test #3
int[] weights = new int[] {3, 2, 1};
int[] deadlines = new int[] {3, 2, 1};
int m = weights.length;
//This is the declaration of a schedule of the appropriate size
HW_Sched schedule = new HW_Sched(weights, deadlines, m);
//This call organizes the assignments and outputs homeworkPlan
int[] student = schedule.SelectAssignments();
int[] expected = new int[] {2,1,0};
//System.out.println(Arrays.toString(student));
for (int i=0; i<expected.length; i++) {
if (student[i]!=expected[i]) {
return false;
}
}
return true;
}
public static boolean weightAdvantage() { //CodePost test #4
int[] weights = new int[] {3, 2, 9};
int[] deadlines = new int[] {2, 1, 1};
int m = weights.length;
//This is the declaration of a schedule of the appropriate size
HW_Sched schedule = new HW_Sched(weights, deadlines, m);
//This call organizes the assignments and outputs homeworkPlan
int[] student = schedule.SelectAssignments();
int[] expected = new int[] {2,0};
//System.out.println(Arrays.toString(student));
for (int i=0; i<expected.length; i++) {
if (student[i]!=expected[i]) {
return false;
}
}
return true;
}
public static boolean notUrgent() { //CodePost test #5
int[] weights = new int[] {1, 1, 100};
int[] deadlines = new int[] {1, 2, 3};
int m = weights.length;
//This is the declaration of a schedule of the appropriate size
HW_Sched schedule = new HW_Sched(weights, deadlines, m);
//This call organizes the assignments and outputs homeworkPlan
int[] student = schedule.SelectAssignments();
int[] expected = new int[] {0,1,2};
//System.out.println(Arrays.toString(student));
for (int i=0; i<expected.length; i++) {
if (student[i]!=expected[i]) {
return false;
}
}
return true;
}
public static boolean codepostTests() {
System.out.println("-------TESTING CODEPOST'S TESTS-----\n");
int trigger = 0;
if (typicalInput()==false) { System.out.println("Error found in typicalInput() /codepost test 1&2/."); trigger = 1; }
if (noConflicts()==false) { System.out.println("Error found in noConflicts() /codepost test 3/."); trigger = 1; }
if (weightAdvantage()==false) { System.out.println("Error found in weightAdvantage() /codepost test 4/."); trigger = 1; }
if (notUrgent()==false) { System.out.println("Error found in notUrgent() /codepost test 5/."); trigger = 1; }
if (trigger == 1) { return false; }
else { return true; }
}
public static boolean freeTimeSlot() { //empty time slot @HW_Sched[3]
int[] weights = new int[] {3, 2, 1, 4};
int[] deadlines = new int[] {4, 2, 1, 5};
int m = weights.length;
//This is the declaration of a schedule of the appropriate size
HW_Sched schedule = new HW_Sched(weights, deadlines, m);
//This call organizes the assignments and outputs homeworkPlan
int[] student = schedule.SelectAssignments();
int[] expected = new int[] {2,1,-1, 0, 3};
//System.out.println(Arrays.toString(student));
for (int i=0; i<expected.length; i++) {
if (student[i]!=expected[i]) {
return false;
}
}
return true;
}
public static boolean oneAssignment() { //checks for errors with a single assignment
int[] weights = new int[] {1};
int[] deadlines = new int[] {1};
int m = weights.length;
//This is the declaration of a schedule of the appropriate size
HW_Sched schedule = new HW_Sched(weights, deadlines, m);
//This call organizes the assignments and outputs homeworkPlan
int[] student = schedule.SelectAssignments();
int[] expected = new int[] {0};
//System.out.println(Arrays.toString(student));
for (int i=0; i<expected.length; i++) {
if (student[i]!=expected[i]) {
return false;
}
}
return true;
}
public static boolean dueInAWhile() { //checks for errors with a single assignment due at a "big" deadline
int[] weights = new int[] {1};
int[] deadlines = new int[] {10};
int m = weights.length;
//This is the declaration of a schedule of the appropriate size
HW_Sched schedule = new HW_Sched(weights, deadlines, m);
//This call organizes the assignments and outputs homeworkPlan
int[] student = schedule.SelectAssignments();
int[] expected = new int[] {-1,-1,-1,-1,-1,-1,-1,-1,-1, 0};
//System.out.println(Arrays.toString(student));
for (int i=0; i<expected.length; i++) {
if (student[i]!=expected[i]) {
return false;
}
}
return true;
}
public static boolean noAssignment() { //checks for errors (null pointers) when no assignments are inputed
int[] weights = new int[] {};
int[] deadlines = new int[] {};
int m = weights.length;
//This is the declaration of a schedule of the appropriate size
HW_Sched schedule = new HW_Sched(weights, deadlines, m);
//This call organizes the assignments and outputs homeworkPlan
int[] student = schedule.SelectAssignments();
int[] expected = new int[] {};
//System.out.println(Arrays.toString(student));
for (int i=0; i<expected.length; i++) {
if (student[i]!=expected[i]) {
return false;
}
}
return true;
}
public static boolean zeroDeadLine() { //checks if the code handles an assignment due now (deadline=0)
int[] weights = new int[] {3, 2, 1, 4};
int[] deadlines = new int[] {4, 2, 1, 0};
int m = weights.length;
//This is the declaration of a schedule of the appropriate size
HW_Sched schedule = new HW_Sched(weights, deadlines, m);
//This call organizes the assignments and outputs homeworkPlan
int[] student = schedule.SelectAssignments();
int[] expected = new int[] {2, 1,-1, 0};
//System.out.println(Arrays.toString(student));
for (int i=0; i<expected.length; i++) {
if (student[i]!=expected[i]) {
return false;
}
}
return true;
}
public static boolean allZeroDeadLine() { //checks if the code handles all assignments due now (deadline=0)
int[] weights = new int[] {420, 69, 666, 58008};
int[] deadlines = new int[] {0, 0, 0, 0};
int m = weights.length;
//This is the declaration of a schedule of the appropriate size
HW_Sched schedule = new HW_Sched(weights, deadlines, m);
//This call organizes the assignments and outputs homeworkPlan
int[] student = schedule.SelectAssignments();
int[] expected = new int[] {};
//System.out.println(Arrays.toString(student));
for (int i=0; i<expected.length; i++) {
if (student[i]!=expected[i]) {
return false;
}
}
return true;
}
public static boolean sameWeight() { //testing what happens for situations where more than 1 assignment have the same weight
int[] weights = new int[] {5, 5, 3, 5, 6};
int[] deadlines = new int[] {4, 3, 4, 1, 2};
int m = weights.length;
//This is the declaration of a schedule of the appropriate size
HW_Sched schedule = new HW_Sched(weights, deadlines, m);
//This call organizes the assignments and outputs homeworkPlan
int[] student = schedule.SelectAssignments();
int[] expected = new int[] {3,4,1,0};
//System.out.println(Arrays.toString(student));
for (int i=0; i<expected.length; i++) {
if (student[i]!=expected[i]) {
return false;
}
}
return true;
}
public static boolean additionalTests() {
System.out.println("-------TESTING ADDITIONAL TESTS-----\n");
int trigger = 0;
if (freeTimeSlot()==false) { System.out.println("Error found in freeTimeSlot()."); trigger = 1; }
if (oneAssignment()==false) { System.out.println("Error found in oneAssignment()."); trigger = 1; }
if (dueInAWhile()==false) { System.out.println("Error found in dueInAWhile()."); trigger = 1; }
if (noAssignment()==false) { System.out.println("Error found in noAssignment()."); trigger = 1; }
if (zeroDeadLine()==false) { System.out.println("Error found in zeroDeadLine()."); trigger = 1; }
if (allZeroDeadLine()==false) { System.out.println("Error found in allZeroDeadLine()."); trigger = 1; }
if (sameWeight()==false) { System.out.println("Error found in sameWeight()."); trigger = 1; }
if (trigger == 1) { return false; }
else { return true; }
}
public static void GreedyTester2() {
int tally = 0;
// Add new test cases here:
int[][] weights = new int[][] { { 23, 60, 14, 25, 7 }, { 1, 1, 1, 3 }, { 3, 3, 3, 1 },
{ 43, 43, 1, 22, 22, 69, 25, 98 }, { 8, 8, 8, 9, 9, 9, 10, 10 }, { 11, 12 }, {1, 2, 4, 15, 81 },
{ 10, 15 } };
int[][] deadlines = new int[][] { { 3, 1, 2, 1, 3 }, { 1, 2, 3, 4 }, { 1, 3, 3, 4 }, { 4, 6, 4, 3, 5, 1, 2, 6 },
{ 6, 1, 3, 1, 4, 2, 7, 7 }, { 1, 1 }, {4, 10, 7, 3, 13}, { 1, 6 } };
int[][] expected = new int[][] { { 1, 2, 0 }, { 0, 1, 2, 3 }, { 0, 2, 1, 3 }, { 5, 6, 3, 0, 1, 7 },
{ 3, 5, 2, 4, 0, 7, 6 }, { 1 }, {-1, -1, 3, 0, -1, -1, 2, -1, -1, 1, -1, -1, 4}, { 0, 1, -1, -1, -1, -1 } };
// Where there are multiple possible solutions, the total weight of the
// completed homework assignments is used to verify the validity of your greedy
// algorithm. Please updated expectedSum if you add more tests:
int[] expectedSum = new int[] { 97, 6, 10, 300, 63, 12, 103, 25 };
for (int i = 0; i < expected.length; i++) {
HW_Sched schedule = new HW_Sched(weights[i], deadlines[i], weights[i].length);
int[] res = schedule.SelectAssignments();
String resString = Arrays.toString(res), ansString = Arrays.toString(expected[i]);
int resSum = 0;
for (int j = 0; j < res.length; j++) {
if (res[j] < 0)
continue;
resSum += Math.max(weights[i][res[j]], 0);
}
String feedback;
if (resSum == expectedSum[i]) {
feedback = "PASS";
tally++;
} else {
feedback = "FAIL";
System.out.printf("Test %d: %s\n------------------------------\n", i + 1, feedback);
System.out.printf("Expected: %s\nYour solution: %s\n\n", ansString, resString);
}
}
System.out.printf("You passed %d/%d tests!\n\n", tally, expected.length);
System.out.println(
"N.B. Some test cases may have multiple solutions, so your solution (while valid) may not match the expected solution.");
}
public static void main(String[] args) {
int trigger = 0;
System.out.println("-------STARTING GREEDY TESTER-------\n");
System.out.println("------------------------------------\n");
if (codepostTests()==false) { trigger = 1; }
if (additionalTests()==false) { trigger = 1; }
System.out.println("------------------------------------\n");
if (trigger == 0) { System.out.println("DIDN'T FIND ANY PROBLEMS W/ SOLUTION.\n"); }
else { System.out.println("FOUND SOME PROBLEMS W/ SOLUTION.\n"); }
System.out.println("-------STARTING GREEDY TESTER 2-----\n");
GreedyTester.GreedyTester2();
}
}