· 2 years ago · Sep 06, 2023, 03:45 PM
1import static org.junit.Assert.assertNotEquals;
2import java.io.ByteArrayOutputStream;
3import java.io.FileNotFoundException;
4import java.io.PrintStream;
5import student.TestCase;
6
7
8/**
9 * @author {Shrihari Maheshwari}
10 * @version {v1}
11 *
12 */
13
14
15public class SemManagerTest extends TestCase {
16
17
18 public void testSizeAfterInsertion() {
19
20 // Test the size after inserting seminars
21 assertEquals(0, hashTable.size());
22
23 // Insert a seminar
24 Seminar seminar = new Seminar(1, "Title", "Date", 60, (short) 10, (short) 20, 100,
25 new String[]{"Keyword1", "Keyword2"}, "Description");
26 hashTable.addSeminar(seminar);
27 assertEquals(1, hashTable.size());
28
29 // Insert more seminars
30 Seminar seminartwo = new Seminar(2, "Title", "Date", 60, (short) 10, (short) 20, 100,
31 new String[]{"Keyword1", "Keyword2"}, "Description");
32 Seminar seminarthree = new Seminar(3, "Title", "Date", 60, (short) 10, (short) 20, 100,
33 new String[]{"Keyword1", "Keyword2"}, "Description");
34 hashTable.addSeminar(seminartwo);
35 hashTable.addSeminar(seminarthree);
36 assertEquals(3, hashTable.size());
37 }
38
39 public void testSizeAfterRemoval() {
40 // Test the size after inserting and removing seminars
41 assertEquals(0, hashTable.size());
42
43 // Insert seminars
44 Seminar seminartwo = new Seminar(1, "Title", "Date", 60, (short) 10, (short) 20, 100,
45 new String[]{"Keyword1", "Keyword2"}, "Description");
46 Seminar seminarthree = new Seminar(2, "Title", "Date", 60, (short) 10, (short) 20, 100,
47 new String[]{"Keyword1", "Keyword2"}, "Description");
48 hashTable.addSeminar(seminartwo);
49 hashTable.addSeminar(seminarthree);
50 assertEquals(2, hashTable.size());
51
52 // Remove a seminar
53 hashTable.removeSeminar(1);
54 assertEquals(1, hashTable.size());
55 }
56
57
58 /*
59
60 public void testContainsInEmptyHashTable() {
61 // Test containing in an empty hash table
62 Seminar seminar = new Seminar(1, "Title", "Date", 60, (short) 10, (short) 20, 100,
63 new String[]{"Keyword1", "Keyword2"}, "Description");
64 assertFalse(hashTable.containsSeminar(seminar));
65 }
66
67
68 public void testContainsNonexistentSeminar() {
69 // Test containing a nonexistent seminar
70 Seminar seminar = new Seminar(1, "Title", "Date", 60, (short) 10, (short) 20, 100,
71 new String[]{"Keyword1", "Keyword2"}, "Description");
72 hashTable.addSeminar(seminar);
73
74 Seminar seminarone = new Seminar(2, "Title", "Date", 60, (short) 10, (short) 20, 100,
75 new String[]{"Keyword1", "Keyword2"}, "Description");
76
77
78 assertFalse(hashTable.containsSeminar(seminarone));
79 }
80
81 public void testContainsSeminarFound() {
82 // Test containing a seminar that exists
83 Seminar seminar = new Seminar(1, "Title", "Date", 60, (short) 10, (short) 20, 100,
84 new String[]{"Keyword1", "Keyword2"}, "Description");
85 hashTable.addSeminar(seminar);
86 assertTrue(hashTable.containsSeminar(seminar));
87 }
88
89 public void testContainsTombstoneHandling() {
90 // Test containing a seminar marked with a tombstone
91 Seminar seminar = new Seminar(1, "Title", "Date", 60, (short) 10, (short) 20, 100,
92 new String[]{"Keyword1", "Keyword2"}, "Description");
93 hashTable.addSeminar(seminar);
94 hashTable.removeSeminar(1); // Mark as deleted with a tombstone
95 assertFalse(hashTable.containsSeminar(seminar));
96 }
97
98 public void testContainsCollisionHandling() {
99 // Test containing seminars with hash collisions
100 // Insert two seminars with the same hash (but different IDs)
101 Seminar seminar = new Seminar(1, "Title", "Date", 60, (short) 10, (short) 20, 100,
102 new String[]{"Keyword1", "Keyword2"}, "Description");
103 Seminar seminarone = new Seminar(13, "Title", "Date", 60, (short) 10, (short) 20, 100,
104 new String[]{"Keyword1", "Keyword2"}, "Description");
105
106 hashTable.addSeminar(seminar);
107 hashTable.addSeminar(seminarone);
108
109 assertTrue(hashTable.containsSeminar(seminar));
110 assertTrue(hashTable.containsSeminar(seminarone));
111 }
112 */
113
114 public void testRemoveFromEmptyHashTable() {
115 // Test removing from an empty hash table
116 assertFalse(hashTable.removeSeminar(1));
117 }
118
119 public void testRemoveNonexistentSeminar() {
120 // Test removing a seminar that doesn't exist in the hash table
121 Seminar seminar = new Seminar(1, "Title", "Date", 60, (short) 10, (short) 20, 100,
122 new String[]{"Keyword1", "Keyword2"}, "Description");
123 hashTable.addSeminar(seminar);
124 assertFalse(hashTable.removeSeminar(2));
125 }
126
127 public void testSuccessfulRemoval() {
128 // Test successful removal of a seminar
129 Seminar seminar = new Seminar(1, "Title", "Date", 60, (short) 10, (short) 20, 100,
130 new String[]{"Keyword1", "Keyword2"}, "Description");
131 hashTable.addSeminar(seminar);
132 assertTrue(hashTable.removeSeminar(1));
133 }
134
135 public void testTombstoneHandlingTwo() {
136 // Test tombstone marker handling
137 Seminar seminar = new Seminar(1, "Title", "Date", 60, (short) 10, (short) 20, 100,
138 new String[]{"Keyword1", "Keyword2"}, "Description");
139 hashTable.addSeminar(seminar);
140 assertTrue(hashTable.removeSeminar(1));
141 assertFalse(hashTable.removeSeminar(1));
142 }
143
144 public void testResizeAfterRemoval() {
145 // Test resizing after removal
146 // Insert seminars to make the table resize
147 Seminar seminar = new Seminar(1, "Title", "Date", 60, (short) 10, (short) 20, 100,
148 new String[]{"Keyword1", "Keyword2"}, "Description");
149 hashTable.addSeminar(seminar);
150
151 Seminar seminarone = new Seminar(1, "Title", "Date", 60, (short) 10, (short) 20, 100,
152 new String[]{"Keyword1", "Keyword2"}, "Description");
153 hashTable.addSeminar(seminarone);
154 // Remove one seminar
155 assertTrue(hashTable.removeSeminar(1));
156 // Check if resizing occurred
157 assertEquals(0, hashTable.size());
158 }
159
160 private HashTable hashTable;
161
162 public void setUp() {
163 // Create a new hash table before each test
164 hashTable = new HashTable(16, 8);
165 }
166
167 public void testEmptyHashTable() {
168 // Test when the hash table is empty
169 int index = hashTable.findIndex(1);
170 assertEquals(-1, index);
171 }
172
173 public void testSeminarNotFound() {
174 // Test when the seminar with the given ID is not in the hash table
175 // Insert some seminars with different IDs
176
177 Seminar seminar = new Seminar(1, "Title", "Date", 60, (short) 10, (short) 20, 100,
178 new String[]{"Keyword1", "Keyword2"}, "Description");
179
180 Seminar seminarone = new Seminar(1, "Title", "Date", 60, (short) 10, (short) 20, 100,
181 new String[]{"Keyword1", "Keyword2"}, "Description");
182
183 hashTable.addSeminar(seminar);
184 hashTable.addSeminar(seminarone);
185
186 int index = hashTable.findIndex(3);
187 assertEquals(-1, index);
188 }
189
190 public void testSeminarFound() {
191 // Test when the seminar with the given ID is found
192 // Insert a seminar with the desired ID
193 Seminar seminarone = new Seminar(1, "Title", "Date", 60, (short) 10, (short) 20, 100,
194 new String[]{"Keyword1", "Keyword2"}, "Description");
195 hashTable.addSeminar(seminarone);
196
197 int index = hashTable.findIndex(1);
198 assertEquals(1, index);
199 }
200
201 public void testHashCollision() {
202 // Test when there's a hash collision (multiple seminars have the same hash)
203 // This will test if the method handles collisions correctly
204 // Insert two seminars with the same hash (but different IDs)
205 Seminar seminarone = new Seminar(1, "Title", "Date", 60, (short) 10, (short) 20, 100,
206 new String[]{"Keyword1", "Keyword2"}, "Description");
207 Seminar seminar = new Seminar(1, "Title", "Date", 60, (short) 10, (short) 20, 100,
208 new String[]{"Keyword1", "Keyword2"}, "Description");
209
210 hashTable.addSeminar(seminarone);
211 hashTable.addSeminar(seminar);
212
213 int index = hashTable.findIndex(17);
214 assertEquals(-1, index);
215 }
216
217 public void testTombstoneHandling() {
218 // Test when tombstone markers are used
219 // Insert a seminar and then "delete" it with a tombstone marker
220 // Ensure that the method skips the tombstone and doesn't return it as a match
221 Seminar seminarone = new Seminar(1, "Title", "Date", 60, (short) 10, (short) 20, 100,
222 new String[]{"Keyword1", "Keyword2"}, "Description");
223
224 hashTable.addSeminar(seminarone);
225
226 // Delete the seminar by marking its slot with a tombstone
227 hashTable.removeSeminar(1);
228
229 int index = hashTable.findIndex(1);
230 assertEquals(-1, index);
231 }
232
233
234 public void testDuplicateID() {
235 // Create the first Seminar object with valid data and ID 1
236 Seminar seminar1 = new Seminar(1, "Title", "Date", 60, (short) 10, (short) 20, 100,
237 new String[]{"Keyword1", "Keyword2"}, "Description");
238
239 HashTable hashTable = new HashTable(16, 8);
240
241 // Insert the first seminar
242 hashTable.addSeminar(seminar1);
243
244 // Create the second Seminar object with the same ID 1
245 Seminar seminar2 = new Seminar(1, "Title", "Date", 60, (short) 10, (short) 20, 100,
246 new String[]{"Keyword1", "Keyword2"}, "Description");
247
248 // Attempt to insert the second seminar with the same ID
249 hashTable.addSeminar(seminar2);
250
251 // Check if only one seminar was added to the hash table
252 assertEquals(1, hashTable.size());
253 }
254
255
256 public void testInsertNullSeminar() {
257 HashTable hashTable = new HashTable(16, 8);
258 Seminar seminar = null;
259
260 // Attempt to insert a null seminar
261 boolean result = hashTable.addSeminar(seminar);
262
263 // Check if the insertion fails
264 assertFalse(result);
265 }
266
267 public void testSuccessfulInsertion() {
268 HashTable hashTable = new HashTable(16, 8);
269 Seminar seminar = new Seminar(1, "Title", "Date", 60, (short) 10, (short) 20, 100,
270 new String[]{"Keyword1", "Keyword2"}, "Description");
271
272 // Attempt to insert a valid seminar
273 boolean result = hashTable.addSeminar(seminar);
274
275 // Check if the insertion is successful
276 assertTrue(result);
277 assertEquals(1, hashTable.size());
278 }
279
280 public void testPrintEmptyHashTable() {
281 HashTable hashTable = new HashTable(16, 8);
282
283 // Redirect standard output to capture printed content
284 ByteArrayOutputStream outContent = new ByteArrayOutputStream();
285 System.setOut(new PrintStream(outContent));
286
287 // Print the empty hash table
288 hashTable.printHashTable();
289
290 // Get the printed content and remove any trailing whitespace
291 String printedContent = outContent.toString().trim();
292
293 // Check if the printed content is empty
294 assertEquals("Hashtable:\ntotal records: 0", printedContent);
295 }
296
297
298 public void testMainWithInvalidIntegerArgsSeven() throws FileNotFoundException {
299 // Redirect standard error to capture output
300 ByteArrayOutputStream outContent = new ByteArrayOutputStream();
301 System.setErr(new PrintStream(outContent));
302
303 // Prepare invalid command-line arguments with non-integer values
304 String[] args = {"2", "4.1", "input.txt"};
305
306 // Test the main method with invalid integer arguments
307 SemManager.main(args);
308
309 // Restore the standard error stream
310 System.setErr(System.err);
311
312 // Verify that an error message was printed to standard error
313 String errorMessage = outContent.toString().trim();
314 assertEquals("Invalid integer input. Please provide valid integer arguments.", errorMessage);
315 }
316
317 public void testMainWithInvalidIntegerArgsSix() throws FileNotFoundException {
318 // Redirect standard error to capture output
319 ByteArrayOutputStream outContent = new ByteArrayOutputStream();
320 System.setErr(new PrintStream(outContent));
321
322 // Prepare invalid command-line arguments with non-integer values
323 String[] args = {"2.5", "4.1"};
324
325 // Test the main method with invalid integer arguments
326 SemManager.main(args);
327
328 // Restore the standard error stream
329 System.setErr(System.err);
330
331 // Verify that an error message was printed to standard error
332 String errorMessage = outContent.toString().trim();
333 assertEquals("Please provide exactly three input arguments in the correct format.", errorMessage);
334 }
335
336 public void testMainWithInvalidIntegerArgsFive() throws FileNotFoundException {
337 // Redirect standard error to capture output
338 ByteArrayOutputStream outContent = new ByteArrayOutputStream();
339 System.setErr(new PrintStream(outContent));
340
341 // Prepare invalid command-line arguments with non-integer values
342 String[] args = {"2.5", "4.1", "input.txt"};
343
344 // Test the main method with invalid integer arguments
345 SemManager.main(args);
346
347 // Restore the standard error stream
348 System.setErr(System.err);
349
350 // Verify that an error message was printed to standard error
351 String errorMessage = outContent.toString().trim();
352 assertEquals("Invalid integer input. Please provide valid integer arguments.", errorMessage);
353 }
354
355 public void testMainWithInvalidIntegerArgsFour() throws FileNotFoundException {
356 // Redirect standard error to capture output
357 ByteArrayOutputStream outContent = new ByteArrayOutputStream();
358 System.setErr(new PrintStream(outContent));
359
360 // Prepare invalid command-line arguments with non-integer values
361 String[] args = {"2.5", "4", "input.txt"};
362
363 // Test the main method with invalid integer arguments
364 SemManager.main(args);
365
366 // Restore the standard error stream
367 System.setErr(System.err);
368
369 // Verify that an error message was printed to standard error
370 String errorMessage = outContent.toString().trim();
371 assertEquals("Invalid integer input. Please provide valid integer arguments.", errorMessage);
372 }
373
374 public void testMainWithInvalidIntegerArgsThree() throws FileNotFoundException {
375 // Redirect standard error to capture output
376 ByteArrayOutputStream outContent = new ByteArrayOutputStream();
377 System.setErr(new PrintStream(outContent));
378
379 // Prepare invalid command-line arguments with non-integer values
380 String[] args = {"2", "4"};
381
382 // Test the main method with invalid integer arguments
383 SemManager.main(args);
384
385 // Restore the standard error stream
386 System.setErr(System.err);
387
388 // Verify that an error message was printed to standard error
389 String errorMessage = outContent.toString().trim();
390 assertEquals("Please provide exactly three input arguments in the correct format.", errorMessage);
391 }
392
393 public void testMainWithInvalidIntegerArgsTwo() throws FileNotFoundException {
394 // Redirect standard error to capture output
395 ByteArrayOutputStream outContent = new ByteArrayOutputStream();
396 System.setErr(new PrintStream(outContent));
397
398 // Prepare invalid command-line arguments with non-integer values
399 String[] args = {"5", "7", "input.txt"};
400
401 // Test the main method with invalid integer arguments
402 SemManager.main(args);
403
404 // Restore the standard error stream
405 System.setErr(System.err);
406
407 // Verify that an error message was printed to standard error
408 String errorMessage = outContent.toString().trim();
409 assertEquals("Both arguments must be powers of two.", errorMessage);
410 }
411
412 public void testMainWithInvalidIntegerArgs() throws FileNotFoundException {
413 // Redirect standard error to capture output
414 ByteArrayOutputStream outContent = new ByteArrayOutputStream();
415 System.setErr(new PrintStream(outContent));
416
417 // Prepare invalid command-line arguments with non-integer values
418 String[] args = {"5", "16", "input.txt"};
419
420 // Test the main method with invalid integer arguments
421 SemManager.main(args);
422
423 // Restore the standard error stream
424 System.setErr(System.err);
425
426 // Verify that an error message was printed to standard error
427 String errorMessage = outContent.toString().trim();
428 assertEquals("Both arguments must be powers of two.", errorMessage);
429 }
430
431
432 public void testMainWithValidArgs() throws FileNotFoundException {
433 // Redirect standard error to capture output
434 ByteArrayOutputStream outContent = new ByteArrayOutputStream();
435 System.setErr(new PrintStream(outContent));
436
437 // Prepare valid command-line arguments
438 String[] args = {"4", "16", "input.txt"};
439
440 // Test the main method with valid arguments
441 SemManager.main(args);
442
443 // Restore the standard error stream
444 System.setErr(System.err);
445
446 // Verify that no error message was printed to standard error
447 String errorMessage = outContent.toString().trim();
448 assertEquals("", errorMessage);
449
450 }
451
452
453
454 public void testMainWithInValidArgs() throws FileNotFoundException {
455 // Redirect standard error to capture output
456 ByteArrayOutputStream outContent = new ByteArrayOutputStream();
457 System.setErr(new PrintStream(outContent));
458
459 // Prepare valid command-line arguments with an incorrect file path
460 String[] args = {"4", "16", "non_existent_file.txt"};
461
462 // Test the main method with valid arguments and an incorrect file path
463 SemManager.main(args);
464
465 // Restore the standard error stream
466 System.setErr(System.err);
467
468 // Verify that an error message indicating the file not found is printed to standard error
469 String errorMessage = outContent.toString().trim();
470 assertTrue(errorMessage.contains("FileNotFoundException"));
471 }
472
473 private static boolean isPowerOfTwo(int num) {
474 return (num > 0) && ((num & (num - 1)) == 0);
475 }
476
477 public void testNegativeNumberAsArgument() {
478 int arg1 = -32; // -2^5
479 int arg2 = 64; // 2^6
480 assertTrue(!isPowerOfTwo(arg1) || !isPowerOfTwo(arg2));
481 }
482
483 public void testNegativeBothNumberAsArgument() {
484 int arg1 = -32; // -2^5
485 int arg2 = -64; // -2^6
486 assertTrue(!isPowerOfTwo(arg1) || !isPowerOfTwo(arg2));
487 }
488
489 /*
490 public void testInvalidNumber() {
491 double arg1 = 3.2; // -2^5
492 double arg2 = 6.4; // -2^6
493 assertTrue(!isPowerOfTwo(arg1) || !isPowerOfTwo(arg2));
494 }
495 */
496
497
498 public void testZeroNeagtiveAsArgument() {
499 int arg1 = 0;
500 int arg2 = -8; // -2^3
501 assertTrue(!isPowerOfTwo(arg1) || !isPowerOfTwo(arg2));
502 }
503
504 public void testZeroAsArgument() {
505 int arg1 = 0;
506 int arg2 = 8; // 2^3
507 assertTrue(!isPowerOfTwo(arg1) || !isPowerOfTwo(arg2));
508 }
509
510 public void testOneArgumentIsPowerOfTwo() {
511 int arg1 = 16; // 2^4
512 int arg2 = 5;
513 assertTrue(!isPowerOfTwo(arg1) || !isPowerOfTwo(arg2));
514 }
515
516 public void testBothArgumentsAreNotPowersOfTwo() {
517 int arg1 = 3;
518 int arg2 = 7;
519 assertTrue(!isPowerOfTwo(arg1) || !isPowerOfTwo(arg2));
520 }
521
522 public void testBothArgumentsArePowersOfTwo() {
523 int arg1 = 4; // 2^2
524 int arg2 = 8; // 2^3
525 assertFalse(!isPowerOfTwo(arg1) || !isPowerOfTwo(arg2));
526 }
527
528 public void testPowerOfTwo() {
529 // Test with power of two numbers
530 assertTrue(SemManager.isPowerOfTwo(1));
531 assertTrue(SemManager.isPowerOfTwo(2));
532 }
533
534 public void testZero() {
535 // Test with zero
536 assertFalse(SemManager.isPowerOfTwo(0));
537 }
538
539 public void testNegativeNumber() {
540 // Test with negative numbers
541 assertFalse(SemManager.isPowerOfTwo(-1));
542 assertFalse(SemManager.isPowerOfTwo(-2));
543 }
544
545 public void testNonPowerOfTwo() {
546 // Test with non-power of two numbers
547 assertFalse(SemManager.isPowerOfTwo(3));
548 assertFalse(SemManager.isPowerOfTwo(5));
549 }
550
551 public void testIsPowerOfTwoWithPowerOfTwoNegative() {
552 // Test with a power of two (64) as a negative number
553 assertFalse(SemManager.isPowerOfTwo(-64));
554 }
555
556 public void testHashTableConstructor() {
557 // Define initial memory size and hash table size
558 int initialMemorySize = 16;
559 int initialHashTableSize = 8;
560
561 // Create a HashTable object using the constructor
562 HashTable hashTable = new HashTable(initialMemorySize, initialHashTableSize);
563
564 assertEquals(0, hashTable.size());
565
566 assertTrue(hashTable.isEmpty());
567 }
568
569
570 public void testHash() {
571 // Create a HashTable with a specific size (e.g., 16)
572 HashTable hashTable = new HashTable(16, 8);
573
574 // Test the hash method with a sample ID
575 int sampleId = 42;
576 int expectedIndex = sampleId % 8; // Calculate the expected index manually
577 int actualIndex = hashTable.hash(sampleId); // Call the hash method
578
579 // Assert that the actual index matches the expected index
580 assertEquals(expectedIndex, actualIndex);
581 }
582
583 public void testDoubleHash() {
584 // Create a HashTable with a specific size (e.g., 16)
585 HashTable hashTable = new HashTable(16, 8);
586
587 // Test the doubleHash method with a sample ID
588 int sampleId = 42;
589 int expectedDoubleHash = (((sampleId / 8) % 4) * 2) + 1; // Calculate the expected doubleHash manually
590 int actualDoubleHash = hashTable.doubleHash(sampleId); // Call the doubleHash method
591
592 // Assert that the actual doubleHash value matches the expected value
593 assertEquals(expectedDoubleHash, actualDoubleHash);
594 }
595
596 public void testResize() {
597 // Create a hash table with an initial capacity of 8
598 HashTable hashTable = new HashTable(8, 4);
599
600 // Populate the hash table with some data
601 // (Ensure that it triggers a resize when adding more data)
602 for (int i = 1; i <= 8; i++) {
603 Seminar seminar = new Seminar(i, "Seminar " + i, "2023-09-10", 60, (short) 10, (short) 20, 100,
604 new String[]{"keyword1", "keyword2"}, "Description " + i);
605 hashTable.addSeminar(seminar);
606 }
607
608 // Ensure that the initial capacity is 8
609 //assertEquals(8, hashTable.getCapacity());
610
611 // Resize the table (this should double the capacity)
612 boolean success = hashTable.resize();
613
614 // Ensure that the capacity has doubled to 16
615 //assertEquals(16, hashTable.getCapacity());
616
617 // Check that all IDs from 1 to 8 still exist in the table
618 for (int i = 1; i <= 8; i++) {
619 assertTrue(hashTable.searchAndPrintById(i));
620 }
621
622 // Ensure that resizing was successful (assuming no failure conditions in the resize function)
623 assertTrue(success);
624 }
625
626 public void testFindIndexWhenSeminarExists() {
627 // Create a hash table and add a seminar with ID 42
628 HashTable hashTable = new HashTable(16, 8);
629 Seminar seminar = new Seminar(42, "Sample Seminar", "2023-09-10", 60, (short) 10, (short) 20, 100,
630 new String[]{"keyword1", "keyword2"}, "Sample description");
631 hashTable.addSeminar(seminar);
632
633 // Attempt to find the index of the seminar with ID 42
634 int index = hashTable.findIndex(42);
635
636 // Assert that the index is not -1 (found) and the seminar matches
637 assertNotEquals(-1, index);
638 //assertEquals(seminar, hashTable.getTable()[index]);
639 }
640
641 public void testFindIndexWhenSeminarDoesNotExist() {
642 // Create a hash table and add a seminar with ID 42
643 HashTable hashTable = new HashTable(16, 8);
644 Seminar seminar = new Seminar(42, "Sample Seminar", "2023-09-10", 60, (short) 10, (short) 20, 100,
645 new String[]{"keyword1", "keyword2"}, "Sample description");
646 hashTable.addSeminar(seminar);
647
648 // Attempt to find the index of a non-existent seminar with ID 99
649 int index = hashTable.findIndex(99);
650
651
652 // Assert that the index is -1 (not found)
653 assertEquals(-1, index);
654 }
655
656
657 public void testAddSeminar() {
658 // Create a hash table
659 HashTable hashTable = new HashTable(16, 8);
660
661 // Create a test seminar object with unique ID
662 Seminar testSeminar1 = new Seminar(1, "Sample Seminar", "0610071600", 60, (short) 10, (short) 10, 10,
663 new String[]{"keyword1", "keyword2"}, "Sample description");
664
665 // Attempt to add the test seminar to the hash table
666 boolean addedSuccessfully1 = hashTable.addSeminar(testSeminar1);
667
668 // Assert that the seminar was added successfully
669 assertTrue(addedSuccessfully1);
670
671 // Create another test seminar object with the same ID as the first one
672 Seminar testSeminar2 = new Seminar(1, "Duplicate Seminar", "0610071601", 70, (short) 11, (short) 11, 20,
673 new String[]{"keyword3", "keyword4"}, "Duplicate description");
674
675 // Attempt to add the duplicate seminar to the hash table
676 boolean addedSuccessfully2 = hashTable.addSeminar(testSeminar2);
677
678 // Assert that the duplicate seminar was not added successfully
679 assertFalse(addedSuccessfully2);
680
681 Seminar seminar = null;
682
683 // Attempt to insert a null seminar
684 boolean result = hashTable.addSeminar(seminar);
685
686 // Check if the insertion fails
687 assertFalse(result);
688 }
689
690 public void testRemoveSeminar() {
691 // Create a hash table
692 HashTable hashTable = new HashTable(16, 8);
693
694 // Insert a seminar with ID 1
695 Seminar seminar = new Seminar(1, "Sample Seminar", "2023-09-10", 60, (short) 10, (short) 20, 100,
696 new String[]{"keyword1", "keyword2"}, "Sample description");
697 hashTable.addSeminar(seminar);
698
699 // Remove the seminar and check if removal was successful
700 boolean removalResult = hashTable.removeSeminar(1);
701 assertTrue(removalResult); // Removal should be successful
702
703 // Attempt to remove the seminar again (should fail)
704 removalResult = hashTable.removeSeminar(1);
705 assertFalse(removalResult); // Removal should fail, as the seminar is already removed
706 }
707
708
709 public void testPrintHashTable() {
710 // Create a hash table
711 HashTable hashTable = new HashTable(16, 8);
712
713 // Redirect standard output to capture printed content
714 ByteArrayOutputStream outContent = new ByteArrayOutputStream();
715 System.setOut(new PrintStream(outContent));
716
717 // Add a sample seminar to the hash table
718 hashTable.addSeminar(new Seminar(1, "Seminar 1", "0610071601", 60, (short) 10, (short) 20, 100,
719 new String[]{"keyword1", "keyword2"}, "Description 1"));
720
721 // Call printHashTable to print the content
722 hashTable.printHashTable();
723
724 // Restore the standard output stream
725 System.setOut(System.out);
726
727 // Get the printed content
728 String printedContent = outContent.toString().trim();
729
730 // Define the expected output
731 String expectedOutput = "Hashtable:\n1: 1\ntotal records: 1";
732
733 // Assert that the printed content matches the expected output
734 assertEquals(expectedOutput, printedContent);
735 }
736 /*
737
738 public void testContainsSeminar() {
739 // Create a hash table
740 HashTable hashTable = new HashTable(16, 8);
741
742 // Create a test seminar object
743 Seminar testSeminar = new Seminar(1, "Sample Seminar", "2023-09-10", 60, (short) 10, (short) 20, 100,
744 new String[]{"keyword1", "keyword2"}, "Sample description");
745
746 // Add the test seminar to the hash table
747 hashTable.addSeminar(testSeminar);
748
749 // Check if the seminar exists in the hash table
750 assertTrue(hashTable.containsSeminar(testSeminar));
751
752 // Create another seminar with a different ID
753 Seminar anotherSeminar = new Seminar(2, "Another Seminar", "2023-09-15", 90, (short) 15, (short) 25, 120,
754 new String[]{"keyword3", "keyword4"}, "Another description");
755
756 // Check if the other seminar exists in the hash table (it shouldn't)
757 assertFalse(hashTable.containsSeminar(anotherSeminar));
758 }
759 */
760
761 public void testHashTableIsEmpty() {
762 // Create an empty HashTable
763 HashTable hashTable = new HashTable(16, 8);
764
765 // Verify that the hash table is initially empty
766 assertTrue(hashTable.isEmpty());
767
768 // Add a seminar to the hash table
769 Seminar seminar = new Seminar(1, "Sample Seminar", "2023-09-10", 60, (short) 10, (short) 20, 100,
770 new String[]{"keyword1", "keyword2"}, "Sample description");
771 hashTable.addSeminar(seminar);
772
773 // Verify that the hash table is not empty after adding a seminar
774 assertFalse(hashTable.isEmpty());
775
776 // Remove the seminar from the hash table
777 hashTable.removeSeminar(1);
778
779 // Verify that the hash table is empty after removing the seminar
780 assertTrue(hashTable.isEmpty());
781 }
782
783
784
785 public void testHashTableSize() {
786 // Create an empty HashTable
787 HashTable hashTable = new HashTable(16, 8);
788
789 // Verify that the hash table is initially empty and has a size of 0
790 assertEquals(0, hashTable.size());
791
792 // Add a seminar to the hash table
793 Seminar seminar = new Seminar(1, "Sample Seminar", "2023-09-10", 60, (short) 10, (short) 20, 100,
794 new String[]{"keyword1", "keyword2"}, "Sample description");
795 hashTable.addSeminar(seminar);
796
797 // Verify that the hash table size increases to 1 after adding a seminar
798 assertEquals(1, hashTable.size());
799
800 // Remove the seminar from the hash table
801 hashTable.removeSeminar(1);
802
803 // Verify that the hash table size decreases back to 0 after removing the seminar
804 assertEquals(0, hashTable.size());
805 }
806
807 /**
808 * Get code coverage of the class declaration.
809 */
810 public void testMInitx()
811 {
812
813 SemManager sem = new SemManager();
814 assertNotNull(sem);
815 }
816
817}
818
819