View difference between Paste ID: vCn6rydF and 9zWX8baH
SHOW: | | - or go back to the newest paste.
1
// Author: Ronen Schaffer
2
import static org.junit.Assert.*;
3
4
import org.junit.Test;
5
6
public class StringListTests {
7
8
	@Test
9
	public void tests_from_example() {
10
		// Arrange
11
		StringList s = new StringList("abcae");
12
13
		// Act
14
15
		// Assert
16
		assertEquals(5, s.length());
17
		assertEquals('b', s.charAt(1));
18
		assertEquals(0, s.indexOf('a'));
19
		assertEquals(3, s.indexOf('a', 2));
20
		assertEquals(-1, s.indexOf('d'));
21
		assertTrue(s.compareTo(new StringList("abcde")) < 0);
22
	}
23
24
	@Test
25
	public void StringListString_null_ShouldCreateEmptyString() {
26
		// Arrange
27
28
		// Act
29
		StringList sl = new StringList((String) null);
30
31
		// Assert
32
		assertEquals("", sl.toString());
33
	}
34
35
	@Test
36
	public void StringListString_EmptyString_ShouldCreateEmptyString() {
37
		// Arrange
38
39
		// Act
40
		StringList sl = new StringList("");
41
42
		// Assert
43
		assertEquals("", sl.toString());
44
	}
45
46
	@Test
47
	public void StringListString_a() {
48
		// Arrange
49
50
		// Act
51
		StringList sl = new StringList("a");
52
53
		// Assert
54
		assertEquals("a", sl.toString());
55
	}
56
57
	@Test
58
	public void StringListString_aa() {
59
		// Arrange
60
61
		// Act
62
		StringList sl = new StringList("aa");
63
64
		// Assert
65
		assertEquals("aa", sl.toString());
66
	}
67
68
	@Test
69
	public void StringListString_aab() {
70
		// Arrange
71
72
		// Act
73
		StringList sl = new StringList("aab");
74
75
		// Assert
76
		assertEquals("aab", sl.toString());
77
	}
78
79
	@Test
80
	public void StringListString_aabb() {
81
		// Arrange
82
83
		// Act
84
		StringList sl = new StringList("aabb");
85
86
		// Assert
87
		assertEquals("aabb", sl.toString());
88
	}
89
90
	@Test
91
	public void StringListStringList_null_ShouldCreateEmptyString() {
92
		// Arrange
93
94
		// Act
95
		StringList sl = new StringList((StringList) null);
96
97
		// Assert
98
		assertEquals("", sl.toString());
99
	}
100
101
	@Test
102
	public void StringListStringList_EmptyStringList_ShouldCreateEmptyString() {
103
		// Arrange
104
105
		// Act
106
		StringList sl = new StringList(new StringList());
107
108
		// Assert
109
		assertEquals("", sl.toString());
110
	}
111
112
	@Test
113
	public void StringListStringList_a() {
114
		// Arrange
115
116
		// Act
117
		StringList sl = new StringList(new StringList("a"));
118
119
		// Assert
120
		assertEquals("a", sl.toString());
121
	}
122
123
	@Test
124
	public void StringListStringList_aa() {
125
		// Arrange
126
127
		// Act
128
		StringList sl = new StringList(new StringList("aa"));
129
130
		// Assert
131
		assertEquals("aa", sl.toString());
132
	}
133
134
	@Test
135
	public void StringListStringList_aab() {
136
		// Arrange
137
138
		// Act
139
		StringList sl = new StringList(new StringList("aab"));
140
141
		// Assert
142
		assertEquals("aab", sl.toString());
143
	}
144
145
	@Test
146
	public void StringListStringList_aabb() {
147
		// Arrange
148
149
		// Act
150
		StringList sl = new StringList(new StringList("aabb"));
151
152
		// Assert
153
		assertEquals("aabb", sl.toString());
154
	}
155
156
	@Test
157
	public void StringListStringList_aabb_CheckAliasing() {
158
		// Arrange
159
		StringList src = new StringList("aabb");
160
161
		// Act
162
		StringList sl = new StringList(src);
163
		src.concat(new StringList("c"));
164
165
		// Assert
166
		assertEquals("aabb", sl.toString());
167
	}
168
169
	@Test
170
	public void charAt_a_0_ShouldReturn_a() {
171
		// Arrange
172
		StringList sl = new StringList("a");
173
174
		// Act
175
		int res = sl.charAt(0);
176
177
		// Assert
178
		assertEquals('a', res);
179
	}
180
181
	@Test
182
	public void charAt_aa_1_ShouldReturn_a() {
183
		// Arrange
184
		StringList sl = new StringList("aa");
185
186
		// Act
187
		int res = sl.charAt(1);
188
189
		// Assert
190
		assertEquals('a', res);
191
	}
192
193
	@Test
194
	public void charAt_aab_2_ShouldReturn_b() {
195
		// Arrange
196
		StringList sl = new StringList("aab");
197
198
		// Act
199
		int res = sl.charAt(2);
200
201
		// Assert
202
		assertEquals('b', res);
203
	}
204
205
	@Test
206
	public void charAt_aabb_3_ShouldReturn_b() {
207
		// Arrange
208
		StringList sl = new StringList("aabb");
209
210
		// Act
211
		int res = sl.charAt(3);
212
213
		// Assert
214
		assertEquals('b', res);
215
	}
216
217
	@Test
218
	public void concat_a_null_ShouldReturn_a() {
219
		// Arrange
220
		StringList sl1 = new StringList("a");
221
		StringList sl2 = null;
222
223
		// Act
224
		StringList res = sl1.concat(sl2);
225
226
		// Assert
227
		assertEquals("a", res.toString());
228
	}
229
230
	@Test
231
	public void concat_a_EmptyStringList_ShouldReturn_a() {
232
		// Arrange
233
		StringList sl1 = new StringList("a");
234
		StringList sl2 = new StringList();
235
236
		// Act
237
		StringList res = sl1.concat(sl2);
238
239
		// Assert
240
		assertEquals("a", res.toString());
241
	}
242
243
	@Test
244
	public void concat_EmptyStringList_a_ShouldReturn_a() {
245
		// Arrange
246
		StringList sl1 = new StringList();
247
		StringList sl2 = new StringList("a");
248
249
		// Act
250
		StringList res = sl1.concat(sl2);
251
252
		// Assert
253
		assertEquals("a", res.toString());
254
	}
255
256
	@Test
257
	public void concat_a_a_ShouldReturn_aa() {
258
		// Arrange
259
		StringList sl1 = new StringList("a");
260
		StringList sl2 = new StringList("a");
261
262
		// Act
263
		StringList res = sl1.concat(sl2);
264
265
		// Assert
266
		assertEquals("aa", res.toString());
267
	}
268
269
	@Test
270
	public void concat_abc_def_ShouldReturn_abcdef() {
271
		// Arrange
272
		StringList sl1 = new StringList("abc");
273
		StringList sl2 = new StringList("def");
274
275
		// Act
276
		StringList res = sl1.concat(sl2);
277
278
		// Assert
279
		assertEquals("abcdef", res.toString());
280
	}
281
282
	@Test
283
	public void concat_abc_def_CheckAliasing_ShouldReturn_abcdef() {
284
		// Arrange
285
		StringList sl1 = new StringList("abc");
286
		StringList sl2 = new StringList("def");
287
288
		// Act
289
		StringList res1 = sl1.concat(sl2);
290
		StringList res2 = sl1.concat(new StringList("ghi"));
291
		StringList res3 = sl2.concat(new StringList("jkl"));
292
		StringList res4 = sl2.concat(sl1);
293
294
		// Assert
295
		assertEquals("abcdef", res1.toString());
296
		assertEquals("abcghi", res2.toString());
297
		assertEquals("defjkl", res3.toString());
298
		assertEquals("defabc", res4.toString());
299
	}
300
301
	@Test
302
	public void indexOf_a_a_ShouldReturn0() {
303
		// Arrange
304
		StringList sl = new StringList("a");
305
306
		// Act
307
		int res = sl.indexOf('a');
308
309
		// Assert
310
		assertEquals(0, res);
311
	}
312
313
	@Test
314
	public void indexOf_abc_a_ShouldReturn0() {
315
		// Arrange
316
		StringList sl = new StringList("abc");
317
318
		// Act
319
		int res = sl.indexOf('a');
320
321
		// Assert
322
		assertEquals(0, res);
323
	}
324
325
	@Test
326
	public void indexOf_aaa_a_ShouldReturn0() {
327
		// Arrange
328
		StringList sl = new StringList("aaa");
329
330
		// Act
331
		int res = sl.indexOf('a');
332
333
		// Assert
334
		assertEquals(0, res);
335
	}
336
337
	@Test
338
	public void indexOf_aaabccd_e_ShouldReturnMinus1() {
339
		// Arrange
340
		StringList sl = new StringList("aaabccd");
341
342
		// Act
343
		int res = sl.indexOf('e');
344
345
		// Assert
346
		assertEquals(-1, res);
347
	}
348
349
	@Test
350
	public void indexOf_aaa_a_1_ShouldReturn1() {
351
		// Arrange
352
		StringList sl = new StringList("aaa");
353
354
		// Act
355
		int res = sl.indexOf('a', 1);
356
357
		// Assert
358
		assertEquals(1, res);
359
	}
360
361
	@Test
362
	public void indexOf_aabbccddd_b_ShouldReturn2() {
363
		// Arrange
364
		StringList sl = new StringList("aabbccddd");
365
366
		// Act
367
		int res = sl.indexOf('b');
368
369
		// Assert
370
		assertEquals(2, res);
371
	}
372
373
	@Test
374
	public void indexOf_aabbccddd_b_3_ShouldReturn3() {
375
		// Arrange
376
		StringList sl = new StringList("aabbccddd");
377
378
		// Act
379
		int res = sl.indexOf('b', 3);
380
381
		// Assert
382
		assertEquals(3, res);
383
	}
384
385
	@Test
386
	// Test ch doesn't exist from fromIndex
387
	public void indexOf_aabbccddd_b_4_ShouldReturnMinus1() {
388
		// Arrange
389
		StringList sl = new StringList("aabbccddd");
390
391
		// Act
392
		int res = sl.indexOf('b', 4);
393
394
		// Assert
395
		assertEquals(-1, res);
396
	}
397
398
	@Test
399
	// Test fromIndex OutOfRange
400
	public void indexOf_aa_a_4_ShouldReturnMinus1() {
401
		// Arrange
402
		StringList sl = new StringList("aa");
403
404
		// Act
405
		int res = sl.indexOf('a', 4);
406
407
		// Assert
408
		assertEquals(-1, res);
409
	}
410
411
	@Test
412
	// Test fromIndex OutOfRange
413
	public void indexOf_aa_a_minus2_ShouldReturnMinus1() {
414
		// Arrange
415
		StringList sl = new StringList("aabbccddd");
416
417
		// Act
418
		int res = sl.indexOf('a', -2);
419
420
		// Assert
421
		assertEquals(-1, res);
422
	}
423
424
	@Test
425
	public void indexOf_EmptyStringList_a_ShouldReturnMinus1() {
426
		// Arrange
427
		StringList sl = new StringList();
428
429
		// Act
430
		int res = sl.indexOf('a');
431
432
		// Assert
433
		assertEquals(-1, res);
434
	}
435
436
	@Test
437
	public void indexOf_EmptyStringList_a_1_ShouldReturnMinus1() {
438
		// Arrange
439
		StringList sl = new StringList();
440
441
		// Act
442
		int res = sl.indexOf('a', 1);
443
444
		// Assert
445
		assertEquals(-1, res);
446
	}
447
448
	@Test
449
	public void equals_EmptyStringList_null_ShouldReturnFalse() {
450
		// Arrange
451
		StringList sl = new StringList();
452
453
		// Act
454
		boolean res = sl.equals((StringList) null);
455
456
		// Assert
457
		assertEquals(false, res);
458
	}
459
460
	@Test
461
	public void equals_EmptyStringList_EmptyStringList_ShouldReturnTrue() {
462
		// Arrange
463
		StringList sl = new StringList();
464
465
		// Act
466
		boolean res = sl.equals(new StringList());
467
468
		// Assert
469
		assertEquals(true, res);
470
	}
471
472
	@Test
473
	public void equals_abc_null_ShouldReturnFalse() {
474
		// Arrange
475
		StringList sl = new StringList("abc");
476
477
		// Act
478
		boolean res = sl.equals((StringList) null);
479
480
		// Assert
481
		assertEquals(false, res);
482
	}
483
484
	@Test
485
	public void equals_abc_EmptyStringList_ShouldReturnTrue() {
486
		// Arrange
487
		StringList sl = new StringList("abc");
488
489
		// Act
490
		boolean res = sl.equals(new StringList());
491
492
		// Assert
493
		assertEquals(false, res);
494
	}
495
496
	@Test
497
	public void equals_abc_abc_ShouldReturnTrue() {
498
		// Arrange
499
		StringList sl1 = new StringList("abc");
500
		StringList sl2 = new StringList("abc");
501
502
		// Act
503
		boolean res = sl1.equals(sl2);
504
505
		// Assert
506
		assertEquals(true, res);
507
	}
508
509
	@Test
510
	public void equals_abc_def_ShouldReturnFalse() {
511
		// Arrange
512
		StringList sl1 = new StringList("abc");
513
		StringList sl2 = new StringList("def");
514
515
		// Act
516
		boolean res = sl1.equals(sl2);
517
518
		// Assert
519
		assertEquals(false, res);
520
	}
521
522
	@Test
523
	public void equals_abc_ab_ShouldReturnFalse() {
524
		// Arrange
525
		StringList sl1 = new StringList("abc");
526
		StringList sl2 = new StringList("ab");
527
528
		// Act
529
		boolean res = sl1.equals(sl2);
530
531
		// Assert
532
		assertEquals(false, res);
533
	}
534
535
	@Test
536
	public void equals_abc_abcc_ShouldReturnFalse() {
537
		// Arrange
538
		StringList sl1 = new StringList("abc");
539
		StringList sl2 = new StringList("abcc");
540
541
		// Act
542
		boolean res = sl1.equals(sl2);
543
544
		// Assert
545
		assertEquals(false, res);
546
	}
547
548
	@Test
549
	public void equals_aabbccc_aabbccc_ShouldReturnTrue() {
550
		// Arrange
551
		StringList sl1 = new StringList("aabbccc");
552
		StringList sl2 = new StringList("aabbccc");
553
554
		// Act
555
		boolean res = sl1.equals(sl2);
556
557
		// Assert
558
		assertEquals(true, res);
559
	}
560
561
	@Test
562
	public void equals_aabbccc_aabbddd_ShouldReturnTrue() {
563
		// Arrange
564
		StringList sl1 = new StringList("aabbccc");
565
		StringList sl2 = new StringList("aabbddd");
566
567
		// Act
568
		boolean res = sl1.equals(sl2);
569
570
		// Assert
571
		assertEquals(false, res);
572
	}
573
574
	@Test
575
	public void equals_abb_abb_Concat_ShouldReturnTrue() {
576
		// Arrange
577
		StringList sl1 = new StringList("abb");
578
		StringList sl2 = new StringList("ab").concat(new StringList("b"));
579
580
		// Act
581
		boolean res = sl1.equals(sl2);
582
583
		// Assert
584
		assertEquals(true, res);
585
	}
586
587
	@Test
588
	public void compareTo_EmptyString_EmptyString() {
589
		compareToTest("", "");
590
	}
591
592
	@Test
593
	public void compareTo_EmptyString_a_ShouldReturnNegative() {
594
		compareToTest("", "a");
595
	}
596
597
	@Test
598
	public void compareTo_a_a() {
599
		compareToTest("a", "a");
600
	}
601
602
	@Test
603
	public void compareTo_aa_aa() {
604
		compareToTest("aa", "aa");
605
	}
606
607
	@Test
608
	public void compareTo_a_aa() {
609
		compareToTest("a", "aa");
610
	}
611
612
	@Test
613
	public void compareTo_aac_aab() {
614
		compareToTest("aac", "aab");
615
	}
616
617
	@Test
618
	public void compareTo_aabb_aa() {
619
		compareToTest("aabb", "aa");
620
	}
621
622
	@Test
623
	public void compareTo_aabb_aab() {
624
		compareToTest("aabb", "aab");
625
	}
626
627
	@Test
628
	public void compareTo_aabbc_aabbbc() {
629
		compareToTest("aabbc", "aabbbc");
630
	}
631
632
	@Test
633
	public void compareTo_A_a() {
634
		compareToTest("A", "a");
635
	}
636
637
	@Test
638
	public void compareTo_a_B() {
639
		compareToTest("a", "B");
640
	}
641
642
	@Test
643
	public void compareTo_Aaa_aaa() {
644
		compareToTest("Aaa", "aaa");
645
	}
646
647
	@Test
648
	public void compareTo_StrangeChars_StrangeChars() {
649
		compareToTest(")*(&$^%@#!", "!#@$%#^$&*()&");
650
	}
651
652
	@Test
653
	public void compareTo_Itself() {
654
		// Arrange
655
		String s = "abc";
656
		StringList sl = new StringList(s);
657
658
		// Act
659
		int exp1 = s.compareTo(s);
660
		int res1 = sl.compareTo(sl);
661
662
		// Assert
663
		assertCompareTo(exp1, res1);
664
	}
665
666
	// The idea is to run the compareTo on "real" Java Strings and make sure our
667
	// version of compareTo behaves the same.
668
	private void compareToTest(String s1, String s2) {
669
		// Arrange
670
		StringList sl1 = new StringList(s1);
671
		StringList sl2 = new StringList(s2);
672
673
		// Act
674
		int exp1 = s1.compareTo(s2);
675
		int exp2 = s2.compareTo(s1);
676
		int res1 = sl1.compareTo(sl2);
677
		int res2 = sl2.compareTo(sl1);
678
679
		// Assert
680
		assertCompareTo(exp1, res1);
681
		assertCompareTo(exp2, res2);
682
	}
683
684
	private void assertCompareTo(int expected, int actual) {
685
		if (expected == 0) {
686
			assertEquals(0, actual);
687
		} else if (expected > 0) {
688
			assertTrue(actual > 0);
689
		} else // if (expected < 0)
690
		{
691
			assertTrue(actual < 0);
692
		}
693
	}
694
695
	@Test
696
	public void substring_EmptyString_0_ShouldReturnEmptyString() {
697
		// Arrange
698
		StringList sl = new StringList();
699
700
		// Act
701
		StringList res = sl.substring(0);
702
703
		// Assert
704
		assertEquals("", res.toString());
705
	}
706
707
	@Test
708
	public void substring_a_0_ShouldReturn_a() {
709
		// Arrange
710
		StringList sl = new StringList("a");
711
712
		// Act
713
		StringList res = sl.substring(0);
714
715
		// Assert
716
		assertEquals("a", res.toString());
717
	}
718
719
	@Test
720
	public void substring_aaa_0_ShouldReturn_aaa() {
721
		// Arrange
722
		StringList sl = new StringList("aaa");
723
724
		// Act
725
		StringList res = sl.substring(0);
726
727
		// Assert
728
		assertEquals("aaa", res.toString());
729
	}
730
731
	@Test
732
	public void substring_aaa_1_ShouldReturn_aa() {
733
		// Arrange
734
		StringList sl = new StringList("aaa");
735
736
		// Act
737
		StringList res = sl.substring(1);
738
739
		// Assert
740
		assertEquals("aa", res.toString());
741
	}
742
743
	@Test
744
	public void substring_aaabbc_1_ShouldReturn_aabbc() {
745
		// Arrange
746
		StringList sl = new StringList("aaabbc");
747
748
		// Act
749
		StringList res = sl.substring(1);
750
751
		// Assert
752
		assertEquals("aabbc", res.toString());
753
	}
754
755
	@Test
756
	public void substring_aaabbc_4_ShouldReturn_bc() {
757
		// Arrange
758
		StringList sl = new StringList("aaabbc");
759
760
		// Act
761
		StringList res = sl.substring(4);
762
763
		// Assert
764
		assertEquals("bc", res.toString());
765
	}
766
767
	@Test
768
	public void substring_abcdefg_2_5_ShouldReturn_cde() {
769
		// Arrange
770
		StringList sl = new StringList("abcdefg");
771
772
		// Act
773
		StringList res = sl.substring(2, 5);
774
775
		// Assert
776
		assertEquals("cde", res.toString());
777
	}
778
779
	@Test
780
	// Check the case where i = j. According to Anita Cassapu it should return
781
	// an empty StringList
782
	public void substring_abcdefg_2_2_ShouldReturn_EmptyString() {
783
		// Arrange
784
		StringList sl = new StringList("abcdefg");
785
786
		// Act
787
		StringList res = sl.substring(2, 2);
788
789
		// Assert
790
		assertEquals("", res.toString());
791
	}
792
793
	@Test
794
	public void substring_abbcdeeefg_1_6_ShouldReturn_bbcde() {
795
		// Arrange
796
		StringList sl = new StringList("abbcdeeefg");
797
798
		// Act
799
		StringList res = sl.substring(1, 6);
800
801
		// Assert
802
		assertEquals("bbcde", res.toString());
803
	}
804
805
	@Test
806
	public void substring_aaaaaa_1_4_ShouldReturn_aaa() {
807
		// Arrange
808
		StringList sl = new StringList("aaaaaa");
809
810
		// Act
811
		StringList res = sl.substring(1, 4);
812
813
		// Assert
814
		assertEquals("aaa", res.toString());
815
	}
816
817
	@Test
818
	public void substring_bbaaaaaa_2_4_ShouldReturn_aaa() {
819
		// Arrange
820
		StringList sl = new StringList("bbaaaaaa");
821
822
		// Act
823
		StringList res = sl.substring(2, 5);
824
825
		// Assert
826
		assertEquals("aaa", res.toString());
827
	}
828
829
	@Test
830
	public void substring_aab_0_1_ShouldReturn_a() {
831
		// Arrange
832
		StringList sl = new StringList("aab");
833
834
		// Act
835
		StringList res = sl.substring(0, 1);
836
837
		// Assert
838
		assertEquals("a", res.toString());
839
	}
840
841
	@Test
842
	public void substring_abbc_1_2_ShouldReturn_b() {
843
		// Arrange
844
		StringList sl = new StringList("abbc");
845
846
		// Act
847
		StringList res = sl.substring(1, 2);
848
849
		// Assert
850
		assertEquals("b", res.toString());
851
	}
852
853
	@Test
854
	public void substring_abbc_2_3_ShouldReturn_b() {
855
		// Arrange
856
		StringList sl = new StringList("abbc");
857
858
		// Act
859
		StringList res = sl.substring(2, 3);
860
861
		// Assert
862
		assertEquals("b", res.toString());
863
	}
864
865
	@Test
866
	public void substring_abb_2_3_ShouldReturn_b() {
867
		// Arrange
868
		StringList sl = new StringList("abb");
869
870
		// Act
871
		StringList res = sl.substring(2, 3);
872
873
		// Assert
874
		assertEquals("b", res.toString());
875
	}
876
877
	@Test
878
	public void length_EmptyString_ShouldReturn0() {
879
		// Arrange
880
		StringList sl = new StringList();
881
882
		// Act
883
		int res = sl.length();
884
885
		// Assert
886
		assertEquals(0, res);
887
	}
888
889
	@Test
890
	public void length_a_ShouldReturn1() {
891
		// Arrange
892
		StringList sl = new StringList("a");
893
894
		// Act
895
		int res = sl.length();
896
897
		// Assert
898
		assertEquals(1, res);
899
	}
900
901
	@Test
902
	public void length_aaa_ShouldReturn3() {
903
		// Arrange
904
		StringList sl = new StringList("aaa");
905
906
		// Act
907
		int res = sl.length();
908
909
		// Assert
910
		assertEquals(3, res);
911
	}
912
913
	@Test
914
	public void length_aaabbc_ShouldReturn6() {
915
		// Arrange
916
		StringList sl = new StringList("aaabbc");
917
918
		// Act
919
		int res = sl.length();
920
921
		// Assert
922
		assertEquals(6, res);
923
	}
924
}