-
Notifications
You must be signed in to change notification settings - Fork 10
/
MainTester.java
1293 lines (1163 loc) · 40.5 KB
/
MainTester.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
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.NoSuchElementException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;
/**
* A test class for all classes and methods in this HOMEWORK 4
*
* @author David Nguyen
* @since 04/02/2023
* @version 1.0
*/
public class MainTester {
// TESTING THE CONSTRUCTORS AND METHODS OF THE TOKENIZER CLASS:
// Represents a text array
private String[] testArray;
// Represents a text file
private File testFile;
// Represents an expected output
private ArrayList<String> expectedOutput;
/**
* Sets up the Tokenizer for testing
* @throws IOException when the given file is invalid.
*/
@Before
public void setUp() throws IOException {
testArray = new String[] {
"I'm going to eat twenty-five pancakes.",
"I've got 2 cats and 1 dog!",
"This sentence has %^& special characters.",
"This_isn't_separated_by_spaces.",
" :) "
};
testFile = File.createTempFile("testfile", ".txt");
FileWriter fileWriter = new FileWriter(testFile);
for (String line : testArray) {
fileWriter.write(line);
fileWriter.write("\n");
}
fileWriter.close();
expectedOutput = new ArrayList<String>(Arrays.asList(
"im", "going", "to", "eat", "twentyfive", "pancakes",
"ive", "got", "2", "cats", "and", "1", "dog",
"this", "sentence", "has", "special", "characters",
"thisisntseparatedbyspaces"
));
}
/**
* A method that tests the File constructor of the Tokenizer class
* @throws IOException when the given file is invalid.
*/
@Test
public void testTokenizerFile() throws IOException {
Tokenizer tokenizer = new Tokenizer(testFile.getPath());
ArrayList<String> wordList = tokenizer.wordList();
assertEquals(expectedOutput, wordList);
}
/**
* A method that tests the String Array constructor of the Tokenizer class
*/
@Test
public void testTokenizerStringArray() {
Tokenizer tokenizer = new Tokenizer(testArray);
ArrayList<String> wordList = tokenizer.wordList();
assertEquals(expectedOutput, wordList);
}
/**
* A method that tests an edge case: Empty Input array
*/
@Test
public void testEmptyInput() {
Tokenizer tokenizer1 = new Tokenizer(new String[]{});
ArrayList<String> wordList1 = tokenizer1.wordList();
assertEquals(new ArrayList<String>(), wordList1);
try {
File emptyFile = File.createTempFile("emptyfile", ".txt");
Tokenizer tokenizer2 = new Tokenizer(emptyFile.getPath());
ArrayList<String> wordList2 = tokenizer2.wordList();
assertEquals(new ArrayList<String>(), wordList2);
}
catch (IOException e) {
e.printStackTrace();
}
}
/**
* Test the wordList() method
*/
@Test
public void testWordList() {
// Case 1: Testing with an array input of multiple words with special characters
String[] text = {"I'm going to eat twenty-five pancakes."};
Tokenizer tokenizer = new Tokenizer(text);
ArrayList<String> actual = tokenizer.wordList();
assertEquals(6, actual.size());
assertTrue(actual.contains("im"));
assertTrue(actual.contains("going"));
assertTrue(actual.contains("to"));
assertTrue(actual.contains("eat"));
assertTrue(actual.contains("twentyfive"));
assertTrue(actual.contains("pancakes"));
// Case 2: Testing with a file input of multiple words with special characters
String file = "TestTextFile.txt";
Tokenizer tokenizer1 = new Tokenizer(file);
String expected = "[im, going, to, eat, twentyfive, pancakes]";
ArrayList<String> actual1 = tokenizer.wordList();
assertEquals(expected, actual1.toString());
// Case 3: Testing with an input that is empty and has no words in it
String[] text2 = {""};
String expected2 = "[]";
Tokenizer tokenizer2 = new Tokenizer(text2);
ArrayList<String> actual2 = tokenizer2.wordList();
assertEquals(expected2, actual2.toString());
}
/**
* Test the runtime of the wordList() method
*/
@Test
public void testWordListRuntime() {
Tokenizer tokenizer = new Tokenizer(testArray);
long startTime = System.nanoTime();
ArrayList<String> wordList = tokenizer.wordList();
long endTime = System.nanoTime();
long elapsedTime = endTime - startTime;
assertTrue("wordList() runtime should be O(1)", elapsedTime <= 1000000); // 1ms as an example threshold
}
/**
* Performs an extra test on the constructor with file:
*/
@Test
public void extraTestConstructorWithFile() {
/* This file will consist of all kinds of characters, including special ones, as follows:
* "I'm going to eat twenty-five pancakes." */
String file = "TestTextFile.txt";
String expectedOutput = "[im, going, to, eat, twentyfive, pancakes]";
Tokenizer tokenizer = new Tokenizer(file);
ArrayList<String> actualOutput = tokenizer.wordList();
assertEquals(expectedOutput, actualOutput.toString());
}
/**
* Performs an extra test on the constructor with empty file:
*/
@Test
public void testConstructorWithEmptyFile() {
String file = "TestTextFile2.txt";
String expected = "[]";
Tokenizer tokenizer = new Tokenizer(file);
ArrayList<String> actual = tokenizer.wordList();
assertEquals(expected, actual.toString());
}
/**
* Performs an extra test on the constructor with empty array:
*/
@Test
public void testConstructorWithEmptyArray() {
String[] text = {""};
String expected = "[]";
Tokenizer tokenizer = new Tokenizer(text);
ArrayList<String> actual = tokenizer.wordList();
assertEquals(expected, actual.toString());
}
// -------------------------------------------------------------------------------------------------------------//
// TESTING THE CONSTRUCTORS AND METHODS OF THE HASHTABLE<T> CLASS:
// Represents a test table
private HashTable<String> testTable;
/**
* Sets up the table for testing
*/
@Before
public void setUpHashTable() {
testTable = new HashTable<String>();
}
/**
* Tests the constructor with default capacity
*/
@Test
public void testConstructorDefaultCapacity() {
assertTrue(testTable.size() == 0);
}
/**
* Test the constructor with negative capacity
*/
@Test(expected = IllegalArgumentException.class)
public void testConstructorNegativeCapacity() {
new HashTable<String>(-1);
}
/**
* Test the put() and get() methods
*/
@Test
public void testPutAndGet() {
testTable.put("key1", "value1");
assertEquals("value1", testTable.get("key1"));
}
/**
* Test the get() method when empty
*/
@Test
public void testGetWhenEmpty() {
HashTable<Integer> hashTable = new HashTable<>();
boolean thrown = false;
try {
hashTable.get("test");
System.out.println("NoSuchElementException should be thrown");
}
catch (NoSuchElementException e) {
thrown = true;
}
assertTrue(thrown);
}
/**
* Test the get() method with non-existent key
*/
@Test
public void testGetNonExistingKey() {
HashTable<Integer> table = new HashTable<Integer>();
table.put("banana", 2);
table.put("pineapple", 3);
assertThrows(NoSuchElementException.class, () -> table.get("apple"));
}
/**
* Test the put() method with an existing key
*/
@Test
public void testPutExistingKey() {
// Case 1: Specified value is an instance of Integer
HashTable<Integer> table = new HashTable<Integer>();
table.put("apple", 1);
table.put("banana", 2);
table.put("pineapple", 3);
table.put("apple", 4);
assertEquals(Integer.valueOf(2), table.get("banana"));
assertEquals(Integer.valueOf(3), table.get("pineapple"));
assertEquals(Integer.valueOf(5), table.get("apple"));
assertEquals(3, table.size());
// Case 2: Specified value is not an instance of Integer
HashTable<String> table1 = new HashTable<String>();
table1.put("apple", "One");
table1.put("banana", "Two");
table1.put("pineapple", "Three");
table1.put("apple", "four");
assertEquals(3, table1.size());
assertEquals(new String("Two"), table1.get("banana"));
assertEquals(new String("Three"), table1.get("pineapple"));
assertEquals(new String("four"), table1.get("apple"));
}
/**
* Test the put() method with collision
*/
@Test
public void testPutWithCollision() {
HashTable<Integer> table = new HashTable<Integer>(1);
table.put("a", 1);
table.put("b", 2);
assertEquals(1, (int) table.get("a"));
assertEquals(2, (int) table.get("b"));
}
/**
* Test the get() method with non-existent key
*/
@Test(expected = NoSuchElementException.class)
public void testGetNonExistentKey() {
testTable.get("nonExistentKey");
}
/**
* Test the put() and update() methods with integers
*/
@Test
public void testPutAndUpdateWithInteger() {
HashTable<Integer> intTable = new HashTable<>();
intTable.put("key1", 1);
intTable.put("key1", 2);
assertEquals(Integer.valueOf(3), intTable.get("key1"));
}
/**
* Test the put() and update() methods with non-integers
*/
@Test
public void testPutAndUpdateWithNonInteger() {
testTable.put("key1", "value1");
testTable.put("key1", "value2");
assertEquals("value2", testTable.get("key1"));
}
/**
* Test remove() method
*/
@Test
public void testRemove() {
testTable.put("key1", "value1");
assertEquals("value1", testTable.remove("key1"));
assertTrue(testTable.size() == 0);
testTable.put("key2", "value2");
testTable.put("key3", "value3");
assertTrue(testTable.size() == 2);
assertEquals("value3", testTable.remove("key3"));
assertTrue(testTable.size() == 1);
}
/**
* Test remove() non-existent key
*/
@Test(expected = NoSuchElementException.class)
public void testRemoveNonExistentKey() {
testTable.remove("nonExistentKey");
}
/**
* Test remove() when empty
*/
@Test
public void testRemoveEmpty() {
HashTable<Integer> table = new HashTable<Integer>();
boolean thrown = false;
try {
table.remove("test");
}
catch (NoSuchElementException e) {
thrown = true;
}
assertTrue(thrown);
}
/**
* Test the size() method
*/
@Test
public void testSize() {
assertTrue(testTable.size() == 0);
testTable.put("key1", "value1");
testTable.put("key2", "value2");
assertTrue(testTable.size() == 2);
}
/**
* Test the rehash() method
*/
@Test
public void testRehash() {
for (int i = 0; i < 100; i++) {
testTable.put("key" + i, "value" + i);
}
for (int i = 0; i < 100; i++) {
assertEquals("value" + i, testTable.get("key" + i));
}
}
/**
* Test with the collisions and rehashing:
*/
@Test
public void extraTestCollisionsAndRehash() {
int capacity = 10;
HashTable<String> customTable = new HashTable<>(capacity);
for (int i = 0; i < capacity + 1; i++) {
customTable.put("key" + i, "value" + i);
}
assertEquals(capacity + 1, customTable.size());
for (int i = 0; i < capacity + 1; i++) {
assertEquals("value" + i, customTable.get("key" + i));
}
}
// -------------------------------------------------------------------------------------------------------------//
// TESTING CONSTRUCTORS & METHODS OF WORDSTAT CLASS:
// Represents a wordStat instance from file
private WordStat wordStatFromFile;
// Represents a wordStat instance from text
private WordStat wordStatFromText;
// Represents a sample text
private String[] sampleText = {"hello", "world", "hello", "world", "hello", "world", "hello", "david"};
/**
* Prepare the instance for calculations and testings
*/
@Before
public void setUpWordStat() {
wordStatFromText = new WordStat(sampleText);
}
/**
* Test the constructor with file
*/
@Test
public void testFileConstructorWordStat() {
WordStat ws = new WordStat("TestTextFile4.txt");
assertEquals(2, ws.wordCount("this"));
assertEquals(2, ws.wordCount("is"));
assertEquals(2, ws.wordCount("a"));
assertEquals(2, ws.wordCount("test"));
assertEquals(1, ws.wordCount("only"));
}
/**
* Testing the constructor with empty file
*/
@Test
public void testEmptyFileConstructorWordStat() {
WordStat ws = new WordStat("TestTextFile2.txt");
assertEquals(0, ws.wordCount("this"));
assertEquals(0, ws.wordCount("is"));
assertEquals(0, ws.wordCount("a"));
assertEquals(0, ws.wordCount("test"));
assertEquals(0, ws.wordCount("only"));
}
/**
* Test the wordCount() method
*/
@Test
public void testWordCount() {
assertEquals(4, wordStatFromText.wordCount("hello"));
assertEquals(3, wordStatFromText.wordCount("world"));
assertEquals(1, wordStatFromText.wordCount("david"));
assertEquals(0, wordStatFromText.wordCount("notfound"));
}
/**
* Test the wordRank() method
*/
@Test
public void testWordRank() {
assertEquals(1, wordStatFromText.wordRank("hello"));
assertEquals(2, wordStatFromText.wordRank("world"));
assertEquals(3, wordStatFromText.wordRank("david"));
assertEquals(0, wordStatFromText.wordRank("notfound"));
}
/**
* Extra test with the wordCount() method
*/
@Test
public void extraTestWordCount() {
String[] text = new String[]{
"This", "is", "a", "test", "string", "with", "test",
"words", "in", "it", "words", "appear", "multiple", "times"
};
WordStat wordStat = new WordStat(text);
assertEquals(2, wordStat.wordCount("test"));
assertEquals(1, wordStat.wordCount("string"));
assertEquals(0, wordStat.wordCount("not_in_text"));
}
/**
* Extra test with the wordRank() method
*/
@Test
public void extraTestWordRank() {
String[] text = new String[]{"This is a test string", "In this string, test words appear multiple times", "words"};
WordStat wordStat = new WordStat(text);
assertEquals(1, wordStat.wordRank("words"));
assertEquals(1, wordStat.wordRank("test"));
assertEquals(0, wordStat.wordRank("not_in_text"));
assertEquals(1, wordStat.wordRank("string"));
assertEquals(1, wordStat.wordRank("this"));
assertEquals(5, wordStat.wordRank("a"));
assertEquals(5, wordStat.wordRank("multiple"));
assertEquals(0, wordStat.wordRank("xxx"));
}
/**
* Test the mostCommonWords() method
*/
@Test
public void testMostCommonWords() {
String[] expected = {"hello", "world"};
assertArrayEquals(expected, wordStatFromText.mostCommonWords(2));
}
/**
* Test the mostCommonWords() method with negative K
*/
@Test(expected = IllegalArgumentException.class)
public void testMostCommonWordsNegativeK() {
wordStatFromText.mostCommonWords(-1);
}
/**
* Extra test the mostCommonWords() method
*/
@Test
public void extraTestMostCommonWords() {
// Case 1: Test if k is valid and smaller than total number of words in the given array
String[] text = new String[]{"This is a test string of words", "In this string, test words appear multiple times"};
WordStat wordStat = new WordStat(text);
String[] expected = new String[]{"this", "test", "string", "words"};
String[] actual = wordStat.mostCommonWords(4);
assertEquals(expected, actual);
// Case 2: Another test if k is valid and smaller than total number of words in the given array
String[] text1 = new String[]{"This is a test string", "In this string, test words"};
WordStat wordStat1 = new WordStat(text1);
String[] expected1 = new String[]{"this","test","string","is","a","in"};
String[] actual1 = wordStat1.mostCommonWords(6);
assertEquals(expected1, actual1);
// Case 3: If k is equal to the total number of words in the given array
String[] text2 = new String[]{"This is a test string", "In this string, test words"};
WordStat wordStat2 = new WordStat(text2);
String[] expected2 = new String[]{"this","test","string","is","a","in","words"};
String[] actual2 = wordStat2.mostCommonWords(10);
assertEquals(expected2, actual2);
// Case 4: If k is larger than the total number of words in the given array:
String[] text3 = new String[]{"This is a test string", "In this string, test words"};
WordStat wordStat3 = new WordStat(text3);
String[] expected3 = new String[]{"this","test","string","is","a","in","words"};
String[] actual3 = wordStat3.mostCommonWords(12);
assertEquals(expected3, actual3);
// Case 5: If k is equal to 0:
String[] text4 = new String[]{"This is a test string", "In this string, test words"};
WordStat wordStat4 = new WordStat(text4);
String[] expected4 = new String[]{};
String[] actual4 = wordStat4.mostCommonWords(0);
assertEquals(expected4, actual4);
// Case 6: If k is invalid (< 0):
String[] text5 = new String[]{"This is a test string", "In this string, test words"};
WordStat wordStat5 = new WordStat(text5);
boolean thrown = false;
try {
wordStat5.mostCommonWords(-1);
}
catch (IllegalArgumentException e) {
thrown = true;
}
assertTrue(thrown);
}
/**
* Test the leastCommonWords() method:
*/
@Test
public void testLeastCommonWords() {
String[] expected = {"david"};
assertArrayEquals(expected, wordStatFromText.leastCommonWords(1));
}
/**
* Test the leastCommonWords() method with negative K:
*/
@Test(expected = IllegalArgumentException.class)
public void testLeastCommonWordsNegativeK() {
wordStatFromText.leastCommonWords(-1);
}
/**
* Extra test the leastCommonWords() method:
*/
@Test
public void extraTestLeastCommonWords() {
// Case 1: If k is > 0 and less than number of words in the given string
String[] text = new String[]{"This is a test string of words", "In this string, test words appear multiple times"};
WordStat wordStat = new WordStat(text);
String[] expected = new String[]{"times", "multiple", "appear", "in"};
String[] actual = wordStat.leastCommonWords(4);
assertEquals(expected, actual);
// Case 2: If k is > 0 and less than number of words in the given string
String[] text1 = new String[]{"This is a test string", "In this string, test words"};
WordStat wordStat1 = new WordStat(text1);
String[] expected1 = new String[]{"words","in","a","is","string","test"};
String[] actual1 = wordStat1.leastCommonWords(6);
assertEquals(expected1, actual1);
// Case 3: If k is equal to the total number of words in the given string
String[] text2 = new String[]{"This is a test string", "In this string, test words"};
WordStat wordStat2 = new WordStat(text2);
String[] expected2 = new String[]{"words","in","a","is","string","test","this"};
String[] actual2 = wordStat2.leastCommonWords(10);
assertEquals(expected2, actual2);
// Case 4: If k is larger than the total number of words in the given string:
String[] text3 = new String[]{"This is a test string", "In this string, test words"};
WordStat wordStat3 = new WordStat(text3);
String[] expected3 = new String[]{"words","in","a","is","string","test","this"};
String[] actual3 = wordStat3.leastCommonWords(12);
assertEquals(expected3, actual3);
// Case 5: If k is equal to 0:
String[] text4 = new String[]{"This is a test string", "In this string, test words"};
WordStat wordStat4 = new WordStat(text4);
String[] expected4 = new String[]{};
String[] actual4 = wordStat4.leastCommonWords(0);
assertEquals(expected4, actual4);
// Case 6: If k is invalid (< 0):
String[] text5 = new String[]{"This is a test string", "In this string, test words"};
WordStat wordStat5 = new WordStat(text5);
boolean thrown = false;
try {
wordStat5.leastCommonWords(-1);
}
catch (IllegalArgumentException e) {
thrown = true;
}
assertTrue(thrown);
}
/**
* Test the mostCommonCollocations() method:
*/
@Test
public void testMostCommonCollocations() {
String[] expectedPrecede = {"hello"};
assertArrayEquals(expectedPrecede, wordStatFromText.mostCommonCollocations(1, "world", true));
String[] expectedFollow = {"world"};
assertArrayEquals(expectedFollow, wordStatFromText.mostCommonCollocations(1, "hello", false));
}
// Represents another text to perform extra tests on
private String[] text = {"hello", "world", "hello", "hello", "java", "world", "world", "java", "goodbye"};
/**
* Test the most common collocations method with negative K
*/
@Test(expected = IllegalArgumentException.class)
public void testMostCommonCollocationsNegativeK() {
wordStatFromText.mostCommonCollocations(-1, "world", true);
}
/**
* Test the most common collocations method with non-existent base word
*/
@Test(expected = IllegalArgumentException.class)
public void testMostCommonCollocationsNonExistentBaseWord() {
wordStatFromText.mostCommonCollocations(1, "notfound", true);
}
/**
* Exxtra test the most common collocations method
*/
@Test
public void extraTestMostCommonCollocations() {
WordStat wordStat = new WordStat(text);
String[] expectedPrecede = {"world"};
String[] expectedFollow = {"world"};
assertArrayEquals(expectedPrecede, wordStat.mostCommonCollocations(1, "hello", true));
assertArrayEquals(expectedFollow, wordStat.mostCommonCollocations(1, "hello", false));
}
/**
* Extra test the most common collocations method with negative K
*/
@Test(expected = IllegalArgumentException.class)
public void testMostCommonCollocationsWithNegativeK() {
WordStat wordStat = new WordStat(text);
wordStat.mostCommonCollocations(-1, "hello", true);
}
/**
* Extra test the most common collocations method with non-existent base word
*/
@Test(expected = IllegalArgumentException.class)
public void testMostCommonCollocationsWithNonexistentBaseWord() {
WordStat wordStat = new WordStat(text);
wordStat.mostCommonCollocations(1, "nonexistent", true);
}
// ----------------------------------------------------------------------------------------------------------- //
// TESTING THE METHODS & CONSTRUCTORS OF THE PRIORITYQUEUE<K EXTENDS COMPARABLE<? SUPER V>, V> CLASS:
// Represents a test priority queue
private PriorityQueue<Integer, String> priorityQueue;
/**
* Sets up the priority queue
*/
@Before
public void setUpPriorityQueue() {
priorityQueue = new PriorityQueue<>();
}
/**
* Test all the constructors of this class
*/
@Test
public void testConstructors() {
Integer[] keys = new Integer[]{3, 2, 1};
String[] values = new String[]{"Three", "Two", "One"};
PriorityQueue<Integer, String> pq = new PriorityQueue<>(keys, values);
assertEquals(3, pq.size());
}
/**
* Test if the constructors of this class will thrown an exception
*/
@Test(expected = IllegalArgumentException.class)
public void testConstructorThrowsIllegalArgumentException() {
Integer[] keys = new Integer[]{1, 2};
String[] values = new String[]{"One"};
PriorityQueue<Integer, String> pq = new PriorityQueue<>(keys, values);
}
/**
* Test add() method
*/
@Test
public void testAdd() {
priorityQueue.add(3, "Three");
priorityQueue.add(1, "One");
priorityQueue.add(2, "Two");
assertEquals(3, priorityQueue.size());
}
/**
* Test update() method
*/
@Test
public void testUpdate() {
priorityQueue.add(3, "Three");
priorityQueue.add(1, "One");
priorityQueue.add(2, "Two");
priorityQueue.update(4, "Two");
assertEquals("Two", priorityQueue.peek());
}
/**
* Test if update() method will throw a No Such Element Exception:
*/
@Test(expected = NoSuchElementException.class)
public void testUpdateThrowsNoSuchElementException() {
priorityQueue.update(4, "Two");
}
/**
* Test peek() method
*/
@Test
public void testPeek() {
priorityQueue.add(3, "Three");
priorityQueue.add(1, "One");
priorityQueue.add(2, "Two");
assertEquals("Three", priorityQueue.peek());
}
/**
* Test if peek() method will throw a No Such Element Exception:
*/
@Test(expected = NoSuchElementException.class)
public void testPeekThrowsNoSuchElementException() {
priorityQueue.peek();
}
/**
* Test peek(int k) method
*/
@Test
public void testPeekK() {
priorityQueue.add(3, "Three");
priorityQueue.add(1, "One");
priorityQueue.add(2, "Two");
Object[] values = priorityQueue.peek(2);
assertArrayEquals(new String[]{"Three", "Two"}, values);
}
/**
* Test peek(int k) method with k more than queue's size
*/
@Test
public void testPeekMoreThanSize() {
priorityQueue.add(1, "One");
priorityQueue.add(3, "Three");
priorityQueue.add(5, "Five");
priorityQueue.add(4, "Four");
priorityQueue.add(2, "Two");
try {
priorityQueue.peek(6);
fail("Expected NoSuchElementException");
}
catch (IllegalArgumentException e) {
// Expected exception
}
}
/**
* Test peek() with duplicated nodes
*/
@Test
public void testPeekWithDuplicates() {
priorityQueue.add(1, "One");
priorityQueue.add(3, "Three");
priorityQueue.add(5, "Five");
priorityQueue.add(5, "Five-A");
priorityQueue.add(4, "Four");
priorityQueue.add(2, "Two");
String[] expected = {"Five", "Five-A", "Four"};
assertArrayEquals(expected, priorityQueue.peek(3));
}
/**
* Test peek() with zero inputs
*/
@Test
public void testPeekZero() {
priorityQueue.add(1, "One");
priorityQueue.add(3, "Three");
priorityQueue.add(5, "Five");
priorityQueue.add(4, "Four");
priorityQueue.add(2, "Two");
String[] expected = {};
assertArrayEquals(expected, priorityQueue.peek(0));
}
/**
* Test peek() with negative input
*/
@Test(expected = IllegalArgumentException.class)
public void testPeekNegative() {
priorityQueue.add(1, "One");
priorityQueue.add(3, "Three");
priorityQueue.add(5, "Five");
priorityQueue.add(4, "Four");
priorityQueue.peek(-9);
}
/**
* Test peek() with empty queue
*/
@Test(expected = IllegalArgumentException.class)
public void testPeekEmpty() {
priorityQueue.peek(5);
}
/**
* Test poll() method
*/
@Test
public void testPoll() {
priorityQueue.add(3, "Three");
priorityQueue.add(1, "One");
priorityQueue.add(2, "Two");
assertEquals("Three", priorityQueue.poll());
assertEquals(2, priorityQueue.size());
}
/**
* Test poll() method with exception
*/
@Test(expected = NoSuchElementException.class)
public void testPollThrowsNoSuchElementException() {
priorityQueue.poll();
}
/**
* Test poll(V value) method
*/
@Test
public void testPollWithValue() {
priorityQueue.add(3, "Three");
priorityQueue.add(1, "One");
priorityQueue.add(2, "Two");
assertEquals(Integer.valueOf(2), priorityQueue.poll("Two"));
assertEquals(2, priorityQueue.size());
}
/**
* Test poll(V value) method with exception
*/
@Test(expected = NoSuchElementException.class)
public void testPollWithValueThrowsNoSuchElementException() {
priorityQueue.poll("Two");
}
/**
* Test size() method
*/
@Test
public void testSizePriorityQueue() {
assertEquals(0, priorityQueue.size());
priorityQueue.add(3, "Three");
priorityQueue.add(1, "One");
priorityQueue.add(2, "Two");
assertEquals(3, priorityQueue.size());
}
// ------------------------------------------------------------------------------------------------------------- //
// TESTING METHODS & CONSTRUCTORS OF THE CMDBPROFILE CLASS:
// represents a test profile
private CMDbProfile testProfile;
/**
* Sets up the profile for testing
*/
@Before
public void setUpCMDbProfile() {
testProfile = new CMDbProfile("JohnDoe");
}
/**
* Test constructor of the profile
*/
@Test
public void testConstructor() {
assertEquals("JohnDoe", testProfile.getUserName());
}
/**
* Test changeUserName(String userName) method
*/
@Test
public void testChangeUserName() {
testProfile.changeUserName("JaneDoe");
assertEquals("JaneDoe", testProfile.getUserName());
}
/**
* Test rate() method
*/
@Test
public void testRate() {
assertTrue(testProfile.rate("Inception", 9));
assertFalse(testProfile.rate("Interstellar", 11));
assertFalse(testProfile.rate("The Godfather", 0));
}
/**
* Test changeRating() method
*/
@Test
public void testChangeRating() {
testProfile.rate("Inception", 9);
assertTrue(testProfile.changeRating("Inception", 8));
assertFalse(testProfile.changeRating("NonexistentMovie", 6));
assertFalse(testProfile.changeRating("Inception", 0));
assertFalse(testProfile.changeRating("Inception", 11));
}
/**
* Test removeRating() method
*/
@Test
public void testRemoveRating() {
testProfile.rate("Inception", 9);
assertTrue(testProfile.removeRating("Inception"));
assertFalse(testProfile.removeRating("NonexistentMovie"));
}
/**
* Test favorite() method
*/
@Test
public void testFavoriteCMDbProfile() {
CMDbProfile cmDbProfile = new CMDbProfile("Sonw");
assertNull(cmDbProfile.favorite());
cmDbProfile.rate("Inception", 9);
cmDbProfile.rate("The Godfather", 10);
cmDbProfile.rate("Interstellar", 8);
String[] expected1 = {"The Godfather"};
assertArrayEquals(expected1, cmDbProfile.favorite());
cmDbProfile.rate("The Shawshank Redemption", 10);
String[] expected2 = {"The Godfather", "The Shawshank Redemption"};
assertArrayEquals(expected2, cmDbProfile.favorite());
}
/**
* Test favorite(int k) method
*/
@Test
public void testFavoriteK() {