-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.java
358 lines (263 loc) · 12.6 KB
/
Test.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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//b6030326 - Callum Simpson
/**
* The Test class is your main testing suite for the coursework
* You can add additional methods if you need to in this class
*
* @author Rouaa Yassin-Kassab & John Colquhoun
* @since 30/10/2017
* extended by @author
*/
import java.util.*; //needed for usage of the List interface
public class Test {
// input: up to you if you populate the arguments array
/**
* This method is the main method to run your algorithms
*
*/
public static void main(String[] args) {
/*
* You must complete the Generator class in order to generate a random test
* values You must complete the Algorithms class in order to call the two
* algorithms Remember: You need to calculate the time and number of coils used
* for each run of each algorithm You can use any additional method you created
* in this class
*/
Test p = new Test();
System.out.println("*********** Correctness testing ************");
System.out.println();
p.correctnessTestForFirstFit();
System.out.println("************** Main testing **************");
System.out.println("*********** Performance analysis **************");
System.out.println();
/*
* Here you will need to do performance testing
*/
int noOfTests = 5; // total number of tests - you need to CHANGE this value
int noOfRep = 10; // number of times to run each test - you need to CHANGE this value
int noOfOrders = 10000; // the number of customer orders needed for the first run - you need to CHANGE
// this value
int increment = 10000; // the increment in the number of orders - you need to CHANGE this value
performanceTesting(noOfTests, noOfRep, noOfOrders, increment);
}
/**
* performanceTesting (comparing the two algorithms in term of time and total
* number of coils used)
*
* @param noOfTests
* - the number of tests
* @param noOfRep
* - the number of repetitions for each test
* @param noOfOrders
* - the number of orders in the first order sequence
* @param increment
* - increment of the number of orders
*
*/
public static void performanceTesting(int noOfTests, int noOfRep, int noOfOrders, int increment) {
/*
* Here you will need to set up and run your Performance analysis You are
* expected to run several tests (e.g. noOfTests=5) of your programs for, 10000,
* 20000, 30000, 40000, 50000 orders (noOfOrders=10000, increment=10000) so that
* one can see how the algorithms perform for large datasets.
*
* You are expected to run the same test a number of times to ensure accurate
* result (noOfRep=4). In this case, you need to calculate the average time and
* coils needed for each run
*/
Generator gen = new Generator();
Algorithms alg = new Algorithms();
System.out.format("\n%25s | %25s | %25s | %25s | %25s |", "Number of orders", "Average FirstFit",
"Average FirstFit Time", "Average NextFit", "Average NextFit Time");
for (int i = 1; i <= noOfTests; i++) {
//for every test set these values to be zero
int averageFirstFit = 0;
int averageNextFit = 0;
long averageTimeNextFit = 0;
long averageTimeFirstFit = 0;
//for every rep
for (int j = 1; j <= noOfRep; j++) {
//create a random list of ropes and orders of size noOfOrders
List<Integer> testlist = gen.generateMultipleOrders(noOfOrders);
List<Rope> testRope = gen.orderRopeFromManufacturer(noOfOrders);
//increase next fit average
averageNextFit = averageNextFit
+ alg.nextFit(dulpicateOrdersList(testlist), dulpicateRopeList(testRope));
//increase next fit time average
averageTimeNextFit = averageTimeNextFit + TimeNextFit(alg, testRope, testlist);
//increase Frist fit average
averageFirstFit = averageFirstFit
+ alg.firstFit(dulpicateOrdersList(testlist), dulpicateRopeList(testRope));
//increase First fit time average
averageTimeFirstFit = averageTimeFirstFit + TimeFirstFit(alg, testRope, testlist);
}
//print out all the average
System.out.format("\n%25d | %25d | %25d | %25d | %25d |", noOfOrders, averageFirstFit / noOfRep,
averageTimeFirstFit / noOfRep, averageNextFit / noOfRep, averageTimeNextFit / noOfRep);
//increase the order size by increment
noOfOrders = noOfOrders + increment;
}
System.out.println("\n");
}
//print off a list of orders and a list of ropes
public void report(List<Rope> testlistRope, List<Integer> testlistInt) {
System.out.println("\n--- Here is the Orders ---");
for (int j = 0; j < testlistInt.size(); j++) {
System.out.print("||" + testlistInt.get(j));
}
System.out.print("||\n");
System.out.println("\n--- Here are the Ropes ---");
for (int i = 0; i < testlistRope.size(); i++) {
System.out.print("||" + testlistRope.get(i).getLength());
}
System.out.print("||\n");
}
//a test for correctness
public void correctnessTestForFirstFit() {
Algorithms alg = new Algorithms();
//create a predermined list of ropes
List<Rope> testlistRope = new ArrayList<Rope>();
testlistRope.add(new Rope(120));
testlistRope.add(new Rope(110));
testlistRope.add(new Rope(140));
testlistRope.add(new Rope(150));
testlistRope.add(new Rope(130));
testlistRope.add(new Rope(160));
//create a predermined list of orders
List<Integer> testlistInt = new ArrayList<Integer>();
testlistInt.add(99);
testlistInt.add(98);
testlistInt.add(20);
testlistInt.add(60);
testlistInt.add(56);
testlistInt.add(4);
System.out.println("\nTo test correctness I will first make a non-random list of orders and a ropes \n");
System.out.println("I am doing so so I can visually compare the results to the results I have manually worked out\n");
System.out.println("\nEach should be six long so I will first test that the generators are creating the amount \n");
System.out.println(6 == testlistRope.size()
? "The correct number of ropes has been generated"
: "The wrong number of ropes has been generated");
System.out.println(6 == testlistInt.size()
? "The correct number of orders has been generated"
: "The wrong number of orders has been generated");
report(testlistRope, testlistInt);
System.out.println("\n ------ Correctness test for fist fit------\n");
System.out.println("I am expecting to get a result of 3 ropes used");
System.out.println("Orders one and three will use rope 1");
System.out.println("Orders two and four will use rope 2");
System.out.println("\nResult retrieved from firstfit ; "
+ alg.firstFit(dulpicateOrdersList(testlistInt), dulpicateRopeList(testlistRope)));
System.out.println(3 == alg.firstFit(dulpicateOrdersList(testlistInt), dulpicateRopeList(testlistRope))
? "The algorthim for first fit got 3 which is the expected value"
: "The algorthim for first fit didn't work as expected");
System.out.println("\n ------ Correctness test for next fit------ \n");
System.out.println("I am expecting to get a result of 4 ropes used\n");
System.out.println("Orders one will use rope 1");
System.out.println("Orders two will use rope 2");
System.out.println("Orders three , four and five will use rope 3\n");
System.out.println("Orders 6 will use rope 4\n");
System.out.println("\nResult retrieved from nextfit : "
+ alg.nextFit(dulpicateOrdersList(testlistInt), dulpicateRopeList(testlistRope)));
System.out.println(4 == alg.nextFit(dulpicateOrdersList(testlistInt), dulpicateRopeList(testlistRope))
? "The algorthim for next fit got 4 which is the expected value"
: "The algorthim for next fit didn't work as expected ");
System.out.println("\n");
//Test Best case
List<Rope> testBestcaselistRope = new ArrayList<Rope>();
testBestcaselistRope.add(new Rope(200));
testBestcaselistRope.add(new Rope(110));
testBestcaselistRope.add(new Rope(140));
testBestcaselistRope.add(new Rope(150));
testBestcaselistRope.add(new Rope(130));
testBestcaselistRope.add(new Rope(160));
List<Integer> testBestCaselistInt = new ArrayList<Integer>();
testBestCaselistInt.add(20);
testBestCaselistInt.add(20);
testBestCaselistInt.add(20);
testBestCaselistInt.add(20);
testBestCaselistInt.add(20);
testBestCaselistInt.add(20);
System.out.println("\nTo test correctness I will also need to test best fit\n");
report(testBestcaselistRope, testBestCaselistInt);
System.out.println("\nIf i have done this correctly then both algothim should return 1 rope used\n");
System.out.println(1 == alg.firstFit(dulpicateOrdersList(testBestCaselistInt), dulpicateRopeList(testBestcaselistRope))
? "The algorthim for first fit got 1 which is the expected value"
: "The algorthim for first fit didn't work as expected");
System.out.println(1 == alg.nextFit(dulpicateOrdersList(testBestCaselistInt), dulpicateRopeList(testBestcaselistRope))
? "The algorthim for next fit got 1 which is the expected value"
: "The algorthim for next fit didn't work as expected");
//test worst case
List<Rope> testworstcaselistRope = new ArrayList<Rope>();
testworstcaselistRope.add(new Rope(101));
testworstcaselistRope.add(new Rope(101));
testworstcaselistRope.add(new Rope(101));
testworstcaselistRope.add(new Rope(101));
testworstcaselistRope.add(new Rope(101));
testworstcaselistRope.add(new Rope(101));
List<Integer> testworstCaselistInt = new ArrayList<Integer>();
testworstCaselistInt.add(70);
testworstCaselistInt.add(70);
testworstCaselistInt.add(70);
testworstCaselistInt.add(70);
testworstCaselistInt.add(70);
testworstCaselistInt.add(70);
System.out.println("\nTo test correctness I will also need to test worst case\n");
report(testworstcaselistRope, testworstCaselistInt);
System.out.println("\nIf i have done this correctly then both algothim should return 6 rope used\n");
System.out.println(6 == alg.firstFit(dulpicateOrdersList(testworstCaselistInt), dulpicateRopeList(testworstcaselistRope))
? "The algorthim for first fit got 6 which is the expected value"
: "The algorthim for first fit didn't work as expected");
System.out.println(6 == alg.nextFit(dulpicateOrdersList(testworstCaselistInt), dulpicateRopeList(testworstcaselistRope))
? "The algorthim for next fit got 6 which is the expected value"
: "The algorthim for next fit didn't work as expected");
}
//duplicates a list of orders
public static List<Integer> dulpicateOrdersList(List<Integer> testlistInt) {
//creates a new order list
List<Integer> dupOrderList = new ArrayList<Integer>();
//for each order we want to copy
for (int i = 0; i < testlistInt.size(); i++) {
//add the current order to the new order list
dupOrderList.add(new Integer(testlistInt.get(i)));
}
//return the duplicated list
return dupOrderList;
}
//duplicates a list of ropes
public static List<Rope> dulpicateRopeList(List<Rope> testlistRope) {
//creates a new list of ropes
List<Rope> dupOrderList = new ArrayList<Rope>();
//for each rope we want to copy
for (int i = 0; i < testlistRope.size(); i++) {
//add the current rope to the new rope list
dupOrderList.add(new Rope(testlistRope.get(i).getLength()));
}
//returns the copied list of ropes
return dupOrderList;
}
//work out how long it takes for first fit to run
public static long TimeFirstFit(Algorithms alg, List<Rope> testlistRope, List<Integer> testlistInt) {
// duplicate a list of ropes
List<Rope> dupTestlistRope = dulpicateRopeList(testlistRope);
// duplicate a list of orders
List<Integer> dupTestlistInt = dulpicateOrdersList(testlistInt);
//start the timer
long startTime = System.nanoTime();
//run the first fit algorthim
alg.firstFit(dupTestlistInt, dupTestlistRope);
//return how long it took to run the algorthim
return System.nanoTime() - startTime;
}
//work out how long it takes for next fit to run
public static long TimeNextFit(Algorithms alg, List<Rope> testlistRope, List<Integer> testlistInt) {
// duplicate the list of ropes
List<Rope> dupTestlistRope = dulpicateRopeList(testlistRope);
// duplicate the list of orders
List<Integer> dupTestlistInt = dulpicateOrdersList(testlistInt);
//start the timer
long startTime = System.nanoTime();
//run next fit
alg.nextFit(dupTestlistInt, dupTestlistRope);
//return how long it took to run next fit
return System.nanoTime() - startTime;
}
}