-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElectionManagerTester.java
More file actions
753 lines (619 loc) · 31.9 KB
/
ElectionManagerTester.java
File metadata and controls
753 lines (619 loc) · 31.9 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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
//////////////// FILE HEADER (INCLUDE IN EVERY FILE) //////////////////////////
//
// Title: ElectionManager
// Course: CS 300 Fall 2024
//
// Author: Matthew Suri
// Email: mssuri@wisc.edu
// Lecturer: Blerina Gkotse
//
//////////////////// PAIR PROGRAMMERS COMPLETE THIS SECTION ///////////////////
//
// Partner Name: (name of your pair programming partner)
// Partner Email: (email address of your programming partner)
// Partner Lecturer's Name: (name of your partner's lecturer)
//
// VERIFY THE FOLLOWING BY PLACING AN X NEXT TO EACH TRUE STATEMENT:
// ___ Write-up states that pair programming is allowed for this assignment.
// ___ We have both read and understand the course Pair Programming Policy.
// ___ We have registered our team prior to the team registration deadline.
//
//////////////////////// ASSISTANCE/HELP CITATIONS ////////////////////////////
//
// Persons: N/A
// Online Sources: https://docs.oracle.com/en/java/
//
///////////////////////////////////////////////////////////////////////////////
import java.util.Arrays;
/**
* This is a class that tests the methods in ElectionManager
*/
public class ElectionManagerTester {
// CONTAINS TESTS:
/**
* Tests the ElectionManager.containsCandidate method to ensure it correctly handles
* an empty candidate list and returns false when no candidates are present.
*
* This test method sets up an empty candidate list and verifies that the containsCandidate
* method returns false when attempting to find a candidate in the empty list. It also
* ensures that the candidate list is not modified during the process.
*
* @return true if the method behaves as expected (returns false and does not modify
* the array), otherwise false.
*/
public static boolean testContainsEmpty() {
// (1a) Set up the test variables
String[][] candidateList = {null, null, null, null, null, null};
String[][] candidateCopy = Arrays.copyOf(candidateList, candidateList.length);
int size = 0;
String targetName = "Wooper";
String targetParty = "Water";
boolean expected = false;
// (1b) call the method we are testing
boolean actual = ElectionManager.containsCandidate(candidateList, size, targetName, targetParty);
// (2) verify that the expected method return value and the actual return value match
if (expected != actual) return false;
// (3) since THIS method should not modify the array, check it against a copy we made
if (!Arrays.deepEquals(candidateList, candidateCopy)) return false;
// (4) if we have not yet returned false, we can now return true as all tests have passed!
return true;
}
/**
* Tests the ElectionManager.containsCandidate method to ensure it correctly returns false
* when the candidate is not present in the provided candidate list.
*
* This test method sets up a candidate list with valid entries and verifies that the
* containsCandidate method does not mistakenly find a candidate that isn't in the list.
* The method also ensures that the candidate list is not modified during the process.
*
* @return true if the method behaves as expected (returns false and does not modify
* the array), otherwise false.
*/
public static boolean testDoesNotContain() {
// (1a) set up the test variables
String[][] candidateList = {
{"Slowpoke", "Water", "3"},
{"Squirtle", "Water", "127"},
{"Wooper", "Water", "300"},
null, null, null};
String[][] candidateCopy = Arrays.copyOf(candidateList, candidateList.length);
int size = 3;
String targetName = "Goldeen";
String targetParty = "Water";
boolean expected = false;
// (1b) call the method we are testing
boolean actual = ElectionManager.containsCandidate(candidateList, size, targetName, targetParty);
// (2) verify that the expected method return value and the actual return value match
if (expected != actual) return false;
// (3) since THIS method should not modify the array, check it against a copy we made
if (!Arrays.deepEquals(candidateList, candidateCopy)) return false;
// (4) if we have not yet returned false, we can now return true as all tests have passed!
return true;
}
/**
* PROVIDED TESTER METHOD: example test method for verifying whether a candidate has
* already been added to the race.
*
* NOTE: This method ONLY tests scenarios where the candidate IS PRESENT in the list;
* situations where the candidate is not present or the list is empty should be
* tested in the other contains tester methods.
*
* @return false if any of the scenarios we test have results other than what we expect;
* true ONLY if all of our expectations are met by the method we are testing
*/
public static boolean testDoesContain() {
// (1a) set up the test variables
String[][] candidateList = {
{"Slowpoke", "Water", "3"},
{"Squirtle", "Water", "127"},
{"Wooper", "Water", "300"},
null, null, null};
String[][] candidateCopy = Arrays.copyOf(candidateList, candidateList.length);
int size = 3;
String targetName = "Wooper";
String targetParty = "Water";
boolean expected = true;
// (1b) call the method we are testing
boolean actual = ElectionManager.containsCandidate(candidateList, size, targetName, targetParty);
// (2) verify that the expected method return value and the actual return value match
if (expected != actual) return false;
// (3) since THIS method should not modify the array, check it against a copy we made
if (!Arrays.deepEquals(candidateList, candidateCopy)) return false;
// (4) if we have not yet returned false, we can now return true as all tests have passed!
return true;
}
// ADD CANDIDATE TESTS:
/**
* Tests the ElectionManager.addCandidate method to ensure that it correctly adds a new candidate
* to an empty candidate list.
*
* This test method sets up an empty candidate list and verifies that the addCandidate method
* properly adds a new candidate to the list, updates the list size, and ensures the new candidate
* is placed in the correct position. It also checks that the candidate list is updated correctly
* to reflect the added candidate.
*
* @return true if the method behaves as expected (adds the candidate, updates the size, and
* modifies the array correctly), otherwise false.
*/
public static boolean testAddToEmpty() {
// (1a) set up the test variables
String[][] candidateList = {null, null, null, null, null, null};
String newName = "Goldeen";
String newParty = "Water";
int newVotes = 5;
String[][] expectedList = {{"Goldeen", "Water", "5"}, null, null, null, null, null};
int size = 0;
int expected = 1;
// (1b) call the method we are testing
int actual = ElectionManager.addCandidate(candidateList, size, newName, newParty, newVotes);
// (2) verify that the expected method return value and the actual return value match
if (expected != actual) return false;
// (3) since THIS method should not modify the array, check it against a copy we made
if (!Arrays.deepEquals(candidateList, expectedList)) return false;
// (4) if we have not yet returned false, we can now return true as all tests have passed!
return true;
}
/**
* PROVIDED TESTER METHOD: example test method for verifying whether a new candidate has
* been added correctly to the race.
*
* @return false if any of the scenarios we test have results other than what we expect;
* true ONLY if all of our expectations are met by the method we are testing
*/
public static boolean testAddToNonEmpty() {
// (1a) set up the test variables
String[][] candidateList = {
{"Slowpoke", "Water", "3"},
{"Squirtle", "Water", "127"},
{"Wooper", "Water", "300"},
null, null, null};
String newName = "Goldeen";
String newParty = "Water";
int newVotes = 5;
String[][] expectedList = {
{"Goldeen", "Water", "5"}, // alphabetically first, new candidate will be added here
{"Slowpoke", "Water", "3"},
{"Squirtle", "Water", "127"},
{"Wooper", "Water", "300"},
null, null}; // now only TWO null values in this length-6 array!
int size = 3;
int expected = 4;
// (1b) call the method we are testing
int actual = ElectionManager.addCandidate(candidateList, size, newName, newParty, newVotes);
// (2) verify that the expected method return value and the actual return value match
if (expected != actual) return false;
// (3) this method modifies the input array; verify that it was modified correctly
if (!Arrays.deepEquals(candidateList, expectedList)) return false;
// (4) if we have not yet returned false, we can now return true as all tests have passed!
return true;
}
/**
* Tests the ElectionManager.addCandidate method to ensure that it handles invalid inputs,
* specifically negative vote counts, without modifying the candidate list.
*
* This test method sets up a valid candidate list and attempts to add a new candidate with
* a negative vote count. The test verifies that the addCandidate method correctly rejects
* the candidate, does not modify the candidate list, and returns the appropriate list size.
*
* @return true if the method behaves as expected (rejects the invalid candidate, does not modify
* the array, and returns the correct list size), otherwise false.
*/
public static boolean testAddCandidateErrors() {
// (1a) set up the test variables
String[][] candidateList = {
{"Slowpoke", "Water", "3"},
{"Squirtle", "Water", "127"},
{"Wooper", "Water", "300"},
null, null, null};
String newName = "Goldeen";
String newParty = "Water";
int newVotes = -5;
String[][] expectedList = {
{"Slowpoke", "Water", "3"},
{"Squirtle", "Water", "127"},
{"Wooper", "Water", "300"},
null, null, null};
int size = 3;
int expected = 3;
// (1b) call the method we are testing
int actual = ElectionManager.addCandidate(candidateList, size, newName, newParty, newVotes);
// (2) verify that the expected method return value and the actual return value match
if (expected != actual) return false;
// (3) this method modifies the input array; verify that it was modified correctly
if (!Arrays.deepEquals(candidateList, expectedList)) return false;
// (4) if we have not yet returned false, we can now return true as all tests have passed!
return true;
}
/**
* Tests the ElectionManager.addCandidate method to ensure that it correctly handles the
* scenario where the candidate list is already full and does not add a new candidate.
*
* This test method sets up a full candidate list and attempts to add a new candidate.
* The test verifies that the addCandidate method does not add the new candidate, does not
* modify the candidate list, and returns the appropriate list size.
*
* @return true if the method behaves as expected (does not add the new candidate, does not
* modify the array, and returns the correct list size), otherwise false.
*/
public static boolean testAddToFull() {
// (1a) set up the test variables
String[][] candidateList = {
{"Slowpoke", "Water", "3"},
{"Squirtle", "Water", "127"},
{"Wooper", "Water", "300"} };
String newName = "Goldeen";
String newParty = "Water";
int newVotes = 5;
String[][] expectedList = {
{"Slowpoke", "Water", "3"},
{"Squirtle", "Water", "127"},
{"Wooper", "Water", "300"} };
int size = 3;
int expected = 3;
// (1b) call the method we are testing
int actual = ElectionManager.addCandidate(candidateList, size, newName, newParty, newVotes);
// (2) verify that the expected method return value and the actual return value match
if (expected != actual) return false;
// (3) this method modifies the input array; verify that it was modified correctly
if (!Arrays.deepEquals(candidateList, expectedList)) return false;
// (4) if we have not yet returned false, we can now return true as all tests have passed!
return true;
}
// DROP CANDIDATE TESTS:
/**
* Tests the ElectionManager.dropCandidate method to ensure that it correctly removes the only
* candidate from the list and updates the list size to zero.
*
* This test method sets up a candidate list with only one candidate and verifies that the
* dropCandidate method removes that candidate, updates the candidate list to be empty,
* and returns the correct list size.
*
* @return true if the method behaves as expected (removes the only candidate, modifies the
* array correctly, and returns the updated list size), otherwise false.
*/
public static boolean testDropOnlyCandidate() {
// (1a) set up the test variables
String[][] candidateList = {
{"Slowpoke", "Water", "3"},
null, null, null};
String[][] expectedList = {null, null, null, null};
String name = "Slowpoke";
String party = "Water";
int size = 1;
int expected = 0;
// (1b) call the method we are testing
int actual = ElectionManager.dropCandidate(candidateList, size, name, party);
// (2) verify that the expected method return value and the actual return value match
if (expected != actual) return false;
// (3) this method modifies the input array; verify that it was modified correctly
if (!Arrays.deepEquals(candidateList, expectedList)) return false;
// (4) if we have not yet returned false, we can now return true as all tests have passed!
return true;
}
/**
* Tests the ElectionManager.dropCandidate method to ensure that it correctly removes
* the first candidate from the list and shifts the remaining candidates accordingly.
*
* This test method sets up a candidate list with three candidates and verifies that
* the dropCandidate method correctly removes the first candidate, shifts the remaining
* candidates up in the list, and returns the updated list size. The method also checks
* that the candidate list is modified correctly.
*
* @return true if the method behaves as expected (removes the first candidate, shifts
* the remaining candidates, and updates the list size), otherwise false.
*/
public static boolean testDropFirstCandidate() {
// (1a) set up the test variables
String[][] candidateList = {
{"Slowpoke", "Water", "3"},
{"Squirtle", "Water", "127"},
{"Wooper", "Water", "300"},
null, null, null};
String[][] expectedList = {
{"Squirtle", "Water", "127"},
{"Wooper", "Water", "300"},
null, null, null, null};
String name = "Slowpoke";
String party = "Water";
int size = 3;
int expected = 2;
// (1b) call the method we are testing
int actual = ElectionManager.dropCandidate(candidateList, size, name, party);
// (2) verify that the expected method return value and the actual return value match
if (expected != actual) return false;
// (2a) sometimes you may want to REPEAT the process with slightly different variables:
if (expected != actual) return false;
// (3) this scenario should NOT modify the input array; check it against a copy we made
if (!Arrays.deepEquals(candidateList, expectedList)) return false;
// (4) if we have not yet returned false, we can now return true as all tests have passed!
return true;
}
/**
* PROVIDED TESTER METHOD: example test method for verifying whether trying to drop a
* candidate who is not running in the race correctly has NO effect on the candidate list.
*
* @return false if any of the scenarios we test have results other than what we expect;
* true ONLY if all of our expectations are met by the method we are testing
*/
public static boolean testDropCandidateNotRunning() {
// (1a) set up the test variables
String[][] candidateList = {
{"Slowpoke", "Water", "3"},
{"Squirtle", "Water", "127"},
{"Wooper", "Water", "300"},
null, null, null};
String[][] candidateCopy = Arrays.copyOf(candidateList, candidateList.length);
String name = "Goldeen";
String party = "Water";
int size = 3;
int expected = 3;
// (1b) call the method we are testing
int actual =
ElectionManager.dropCandidate(candidateList, size, name, party);
// (2) verify that the expected method return value and the actual return value match
if (expected != actual) return false;
// (2a) sometimes you may want to REPEAT the process with slightly different variables:
name = "Slowpoke";
party = "Fire"; // try with a name that's present but a different PARTY; should still not drop
actual = ElectionManager.dropCandidate(candidateList, size, name, party);
if (expected != actual) return false;
// (3) this scenario should NOT modify the input array; check it against a copy we made
if (!Arrays.deepEquals(candidateList, candidateCopy)) return false;
// (4) if we have not yet returned false, we can now return true as all tests have passed!
return true;
}
// FIND WINNER TESTS:
/**
* Tests the ElectionManager.findWinner method to ensure that it correctly identifies
* the winner in an uncontested election where there is only one candidate.
*
* This test method sets up a candidate list with only one candidate and verifies that
* the findWinner method correctly returns the winner's name, party, and vote percentage.
* It also checks that the format of the result is correct and that the percentage
* is calculated accurately. The method ensures that the candidate list is not modified
* during the process.
*
* @return true if the method behaves as expected (returns the correct winner, with proper
* formatting and percentage calculation, and does not modify the array), otherwise false.
*/
public static boolean testUncontestedWinner() {
// (1a) set up the test variables
String[][] candidateList = {
{"Slowpoke", "Water", "3"},
null, null, null};
String[][] candidateCopy = Arrays.copyOf(candidateList, candidateList.length);
String expectedName = "Slowpoke";
String expectedParty = "(Water)";
double expectedVotePct = 3/3.0*100;
int size = 1;
// (1b) call the method we are testing
String result = ElectionManager.findWinner(candidateList, size);
// (2) verify that the expected method return value and the actual return value match
// NOTE: for a String, this takes a little more processing to do sensitively.
// We expect this result to be "Wooper (Water) - 75.0%" but there may be some weirdness
// especially with that percentage. See how we do it here:
String[] resultPieces = result.split(" "); // get the space-separated pieces of the string
if (resultPieces.length != 4) return false; // incorrect formatting
if (!resultPieces[3].endsWith("%")) return false; // no % at the end
if (!resultPieces[0].equals(expectedName) || !resultPieces[1].equals(expectedParty))
return false; // wrong name or wrong party
if (!resultPieces[2].equals("-")) return false; // forgot the "-" between party and %
// do a range check on the calculated vote percentage, since it's not always going to come out
// exactly the same:
double actualVotePct = Double.valueOf(resultPieces[3].substring(0,resultPieces[3].length()-1));
if (Math.abs(actualVotePct-expectedVotePct) > 0.01) return false;
// (3) this scenario should NOT modify the input array; check it against a copy we made
if (!Arrays.deepEquals(candidateList, candidateCopy)) return false;
// (4) if we have not yet returned false, we can now return true as all tests have passed!
return true;
}
/**
* PROVIDED TESTER METHOD: example test method for verifying the results of an election
* where one candidate has received a clear majority of the votes cast.
*
* @return false if any of the scenarios we test have results other than what we expect;
* true ONLY if all of our expectations are met by the method we are testing
*/
public static boolean testClearWinner() {
// (1a) set up the test variables
String[][] candidateList = {
{"Slowpoke", "Water", "3"},
{"Squirtle", "Water", "97"},
{"Wooper", "Water", "300"},
null, null, null};
String[][] candidateCopy = Arrays.copyOf(candidateList, candidateList.length);
String expectedName = "Wooper";
String expectedParty = "(Water)";
double expectedVotePct = 300.0/(300+97+3)*100;
int size = 3;
// (1b) call the method we are testing
String result = ElectionManager.findWinner(candidateList, size);
// (2) verify that the expected method return value and the actual return value match
// NOTE: for a String, this takes a little more processing to do sensitively.
// We expect this result to be "Wooper (Water) - 75.0%" but there may be some weirdness
// especially with that percentage. See how we do it here:
String[] resultPieces = result.split(" "); // get the space-separated pieces of the string
if (resultPieces.length != 4) return false; // incorrect formatting
if (!resultPieces[3].endsWith("%")) return false; // no % at the end
if (!resultPieces[0].equals(expectedName) || !resultPieces[1].equals(expectedParty))
return false; // wrong name or wrong party
if (!resultPieces[2].equals("-")) return false; // forgot the "-" between party and %
// do a range check on the calculated vote percentage, since it's not always going to come out
// exactly the same:
double actualVotePct = Double.valueOf(resultPieces[3].substring(0,resultPieces[3].length()-1));
if (Math.abs(actualVotePct-expectedVotePct) > 0.01) return false;
// (3) this scenario should NOT modify the input array; check it against a copy we made
if (!Arrays.deepEquals(candidateList, candidateCopy)) return false;
// (4) if we have not yet returned false, we can now return true as all tests have passed!
return true;
}
/**
* Tests the ElectionManager.findWinner method to ensure that it correctly identifies
* a contingent election, where no candidate receives more than 50% of the votes.
*
* This test method sets up a candidate list where no candidate has a majority of votes
* and verifies that the findWinner method returns "CONTINGENT" to indicate that no clear
* winner was found. The test also checks that the candidate list is not modified during
* the process.
*
* @return true if the method behaves as expected (returns "CONTINGENT" and does not
* modify the array), otherwise false.
*/
public static boolean testContingentElection() {
// (1a) set up the test variables
String[][] candidateList = {
{"Slowpoke", "Water", "150"},
{"Squirtle", "Water", "75"},
{"Wooper", "Water", "175"},
null, null, null};
int size = 3;
String[][] candidateCopy = Arrays.copyOf(candidateList, candidateList.length);
String expected = "CONTINGENT";
// (1b) call the method we are testing
String result = ElectionManager.findWinner(candidateList, size);
// (2) verify that the expected method return value and the actual return value match
if (result != expected) return false;
// (3) this scenario should NOT modify the input array; check it against a copy we made
if (!Arrays.deepEquals(candidateList, candidateCopy)) return false;
// (4) if we have not yet returned false, we can now return true as all tests have passed!
return true;
}
// FIND LOWEST-POLLING TESTS:
/**
* Tests the ElectionManager.findLowestPollingCandidate method to ensure that it correctly
* identifies an uncontested election, where there are no candidates, and returns "UNCONTESTED".
*
* This test method sets up an empty candidate list and verifies that the findLowestPollingCandidate
* method returns "UNCONTESTED" when there are no candidates in the election. The test also
* checks that the candidate list is not modified during the process.
*
* @return true if the method behaves as expected (returns "UNCONTESTED" and does not modify
* the array), otherwise false.
*/
public static boolean testUncontestedLowestPolling() {
// (1a) set up the test variables
String[][] candidateList = {null, null, null, null, null, null};
int size = 0;
String[][] candidateCopy = Arrays.copyOf(candidateList, candidateList.length);
String expected = "UNCONTESTED";
// (1b) call the method we are testing
String result = ElectionManager.findLowestPollingCandidate(candidateList, size);
// (2) verify that the expected method return value and the actual return value match
if (result != expected) return false;
// (3) this scenario should NOT modify the input array; check it against a copy we made
if (!Arrays.deepEquals(candidateList, candidateCopy)) return false;
// (4) if we have not yet returned false, we can now return true as all tests have passed!
return true;
}
/**
* Tests the ElectionManager.findLowestPollingCandidate method to ensure that it correctly
* identifies the candidate with the lowest unique vote count.
*
* This test method sets up a candidate list where one candidate has a distinct lowest vote count
* and verifies that the findLowestPollingCandidate method correctly returns that candidate's name,
* party, and vote count. The method also checks that the format of the result is correct and
* that the vote count, name, and party are as expected.
*
* @return true if the method behaves as expected (returns the correct candidate with the lowest
* unique vote count, formatted correctly), otherwise false.
*/
public static boolean testLowestUniqueVoteCount() {
// (1a) set up the test variables
String[][] candidateList = {
{"Slowpoke", "Water", "150"},
{"Squirtle", "Water", "75"},
{"Wooper", "Water", "175"},
null, null, null};
int size = 3;
String[][] candidateCopy = Arrays.copyOf(candidateList, candidateList.length);
String expectedName = "Squirtle";
String expectedParty = "(Water)";
int expectedVotes = 75;
// (1b) call the method we are testing
String result = ElectionManager.findLowestPollingCandidate(candidateList, size);
String[] resultPieces = result.split(" "); // (2a) get the space-separated pieces of the string
if (resultPieces.length != 4) return false; // (2b) incorrect formatting
if (!resultPieces[3].endsWith(Integer.toString(expectedVotes))) return false; // (2c) wrong votes
if (!resultPieces[0].equals(expectedName) || !resultPieces[1].equals(expectedParty)) return false; // (2d) wrong name or wrong party
if (!resultPieces[2].equals("-")) return false; // (2f) forgot the "-"
// (4) if we have not yet returned false, we can now return true as all tests have passed!
return true;
}
/**
* Tests the ElectionManager.findLowestPollingCandidate method to ensure that it correctly
* identifies the lowest polling candidate when multiple candidates have the same vote count.
*
* This test method sets up a candidate list where all candidates have the same number of votes
* and verifies that the findLowestPollingCandidate method correctly returns the first candidate
* with the lowest vote count. It also checks that the format of the result is correct and that
* the vote count, name, and party are as expected.
*
* @return true if the method behaves as expected (returns the correct candidate with the
* lowest vote count, formatted correctly), otherwise false.
*/
public static boolean testLowestVoteCountTied() {
// (1a) set up the test variables
String[][] candidateList = {
{"Slowpoke", "Water", "75"},
{"Squirtle", "Water", "75"},
{"Wooper", "Water", "75"},
null, null, null};
int size = 3;
String expectedName = "Slowpoke";
String expectedParty = "(Water)";
int expectedVotes = 75;
// (1b) call the method we are testing
String result = ElectionManager.findLowestPollingCandidate(candidateList, size);
String[] resultPieces = result.split(" "); // (2a) get the space-separated pieces of the string
if (resultPieces.length != 4) return false; // (2b) incorrect formatting
if (!resultPieces[3].endsWith(Integer.toString(expectedVotes))) return false; // (2c) wrong votes
if (!resultPieces[0].equals(expectedName) || !resultPieces[1].equals(expectedParty)) return false; // (2d) wrong name or wrong party
if (!resultPieces[2].equals("-")) return false; // (2e) forgot the "-"
// if we have not yet returned false, we can now return true as all tests have passed!
return true;
}
/**
* PROVIDED MAIN METHOD to manage the tester methods above.
*
* We're getting a little esoteric here to take advantage of loops to keep the code short;
* each pass through the loop could also be written as follows:
*
* boolean singleTest = testMethodCall();
* allPass &= singleTest;
* System.out.println("testMethodCall : " + singleTest);
*
* @throws NoSuchMethodException if you spell a method name incorrectly
*
* And a couple of other "checked" exceptions that should never happen with our usage here:
* @throws IllegalAccessException
* @throws java.lang.reflect.InvocationTargetException
*/
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException,
java.lang.reflect.InvocationTargetException {
boolean allPass = true, singlePass = true;
String printFormat = "%-29s: %s\n";
// NOTE TO STUDENTS: If you create any additional tests for any of the methods in
// ElectionManager, add their names to the appropriate array below!
String[] containsTests = {"testContainsEmpty", "testDoesNotContain", "testDoesContain"};
String[] addTests = {"testAddToEmpty", "testAddToNonEmpty", "testAddCandidateErrors",
"testAddToFull"};
String[] dropTests = {"testDropOnlyCandidate", "testDropFirstCandidate",
"testDropCandidateNotRunning"};
String[] winTests = {"testUncontestedWinner", "testClearWinner", "testContingentElection"};
String[] lowTests = {"testUncontestedLowestPolling", "testLowestUniqueVoteCount",
"testLowestVoteCountTied"};
String[][] testNames = {containsTests, addTests, dropTests, winTests, lowTests};
// NOTE TO STUDENTS: this for-loop is moving through the method names we've added to the 2D
// array testNames and attempting to call methods with those names from this tester
// (specifically line 286 here). See Java's reflection framework for more details!
for (String[] testSet : testNames) {
for (String name : testSet) {
singlePass = (boolean) ElectionManagerTester.class.getDeclaredMethod(name).invoke(null);
allPass &= singlePass;
System.out.printf(printFormat, name, singlePass);
}
System.out.println();
}
System.out.println("ALL TESTS: "+allPass);
}
}