1+ package com .baeldung .array .operations ;
2+
3+ import org .junit .jupiter .api .Test ;
4+
5+ import static com .baeldung .array .operations .ArrayOperations .intersectionMultiSet ;
6+ import static com .baeldung .array .operations .ArrayOperations .intersectionSet ;
7+ import static com .baeldung .array .operations .ArrayOperations .intersectionSimple ;
8+ import static org .assertj .core .api .Assertions .assertThat ;
9+
10+ class IntersectionUnitTest {
11+ private static final Integer [] a = { 1 , 3 , 2 };
12+ private static final Integer [] b = { 4 , 3 , 2 , 4 , 2 , 3 , 4 , 4 , 3 };
13+ private static final Integer [] c = { 1 , 3 , 2 , 3 , 3 , 2 };
14+
15+ @ Test
16+ void whenIntersectionSimpleIsUsed_thenCommonEntriesAreInTheResult () {
17+ assertThat (intersectionSimple (a , b )).isEqualTo (new Integer [] { 3 , 2 });
18+ assertThat (intersectionSimple (b , a )).isEqualTo (new Integer [] { 3 , 2 , 2 , 3 , 3 });
19+ }
20+
21+ @ Test
22+ void whenIntersectionSimpleIsUsedWithAnArrayAndItself_thenTheResultIsTheIdentity () {
23+ assertThat (intersectionSimple (b , b )).isEqualTo (b );
24+ assertThat (intersectionSimple (a , a )).isEqualTo (a );
25+ }
26+
27+ @ Test
28+ void whenIntersectionSetIsUsed_thenCommonEntriesAreInTheResult () {
29+ assertThat (intersectionSet (b , a )).isEqualTo (new Integer [] { 3 , 2 });
30+ }
31+
32+ @ Test
33+ void whenIntersectionSetIsUsed_thenTheNumberOfEntriesDoesNotChangeWithTheParameterOrder () {
34+ assertThat (intersectionSet (a , b )).isEqualTo (new Integer [] { 3 , 2 });
35+ assertThat (intersectionSet (b , a )).isEqualTo (new Integer [] { 3 , 2 });
36+ }
37+
38+ @ Test
39+ void whenIntersectionSetIsUsedWithAnArrayAndWithItself_andTheInputHasNoDuplicateEntries_ThenTheResultIsTheIdentity () {
40+ assertThat (intersectionSet (a , a )).isEqualTo (a );
41+ }
42+
43+ @ Test
44+ void whenIntersectionSetIsUsedWithAnArrayAndWithItself_andTheInputHasDuplicateEntries_ThenTheResultIsNotTheIdentity () {
45+ assertThat (intersectionSet (b , b )).isNotEqualTo (b );
46+ }
47+
48+ @ Test
49+ void whenMultiSetIsUsed_thenCommonEntriesAreInTheResult () {
50+ assertThat (intersectionMultiSet (b , a )).isEqualTo (new Integer [] { 3 , 2 });
51+ }
52+
53+ @ Test
54+ void whenIntersectionMultiSetIsUsed_thenTheNumberOfEntriesDoesNotChangeWithTheParameterOrder () {
55+ assertThat (intersectionMultiSet (a , b )).isEqualTo (new Integer [] { 3 , 2 });
56+ assertThat (intersectionMultiSet (b , a )).isEqualTo (new Integer [] { 3 , 2 });
57+ assertThat (intersectionMultiSet (b , c )).isEqualTo (new Integer [] { 3 , 2 , 2 , 3 , 3 });
58+ assertThat (intersectionMultiSet (c , b )).isEqualTo (new Integer [] { 3 , 2 , 3 , 3 , 2 });
59+ }
60+
61+ @ Test
62+ void whenIntersectionMultiSetIsUsedWithAnArrayAndWithItself_ThenTheResultIsTheIdentity () {
63+ assertThat (intersectionMultiSet (b , b )).isEqualTo (b );
64+ assertThat (intersectionMultiSet (a , a )).isEqualTo (a );
65+ }
66+ }
0 commit comments