1
1
package com .baeldung .stream ;
2
2
3
3
import com .codepoetics .protonpack .Indexed ;
4
+
4
5
import org .junit .Test ;
5
6
6
7
import java .util .Arrays ;
7
8
import java .util .List ;
8
9
9
10
import static org .junit .Assert .assertEquals ;
11
+ import static org .junit .jupiter .api .Assertions .assertNotEquals ;
10
12
11
13
public class StreamIndicesUnitTest {
12
14
13
15
@ Test
14
16
public void whenCalled_thenReturnListOfEvenIndexedStrings () {
15
- String [] names = {"Afrim" , "Bashkim" , "Besim" , "Lulzim" , "Durim" , "Shpetim" };
17
+ String [] names = { "Afrim" , "Bashkim" , "Besim" , "Lulzim" , "Durim" , "Shpetim" };
16
18
List <String > expectedResult = Arrays .asList ("Afrim" , "Besim" , "Durim" );
17
19
List <String > actualResult = StreamIndices .getEvenIndexedStrings (names );
18
20
@@ -21,7 +23,7 @@ public void whenCalled_thenReturnListOfEvenIndexedStrings() {
21
23
22
24
@ Test
23
25
public void whenCalled_thenReturnListOfEvenIndexedStringsVersionTwo () {
24
- String [] names = {"Afrim" , "Bashkim" , "Besim" , "Lulzim" , "Durim" , "Shpetim" };
26
+ String [] names = { "Afrim" , "Bashkim" , "Besim" , "Lulzim" , "Durim" , "Shpetim" };
25
27
List <String > expectedResult = Arrays .asList ("Afrim" , "Besim" , "Durim" );
26
28
List <String > actualResult = StreamIndices .getEvenIndexedStrings (names );
27
29
@@ -30,7 +32,7 @@ public void whenCalled_thenReturnListOfEvenIndexedStringsVersionTwo() {
30
32
31
33
@ Test
32
34
public void whenCalled_thenReturnListOfOddStrings () {
33
- String [] names = {"Afrim" , "Bashkim" , "Besim" , "Lulzim" , "Durim" , "Shpetim" };
35
+ String [] names = { "Afrim" , "Bashkim" , "Besim" , "Lulzim" , "Durim" , "Shpetim" };
34
36
List <String > expectedResult = Arrays .asList ("Bashkim" , "Lulzim" , "Shpetim" );
35
37
List <String > actualResult = StreamIndices .getOddIndexedStrings (names );
36
38
@@ -40,9 +42,7 @@ public void whenCalled_thenReturnListOfOddStrings() {
40
42
@ Test
41
43
public void givenList_whenCalled_thenReturnListOfEvenIndexedStrings () {
42
44
List <String > names = Arrays .asList ("Afrim" , "Bashkim" , "Besim" , "Lulzim" , "Durim" , "Shpetim" );
43
- List <Indexed <String >> expectedResult = Arrays
44
- .asList (Indexed .index (0 , "Afrim" ), Indexed .index (2 , "Besim" ), Indexed
45
- .index (4 , "Durim" ));
45
+ List <Indexed <String >> expectedResult = Arrays .asList (Indexed .index (0 , "Afrim" ), Indexed .index (2 , "Besim" ), Indexed .index (4 , "Durim" ));
46
46
List <Indexed <String >> actualResult = StreamIndices .getEvenIndexedStrings (names );
47
47
48
48
assertEquals (expectedResult , actualResult );
@@ -51,20 +51,34 @@ public void givenList_whenCalled_thenReturnListOfEvenIndexedStrings() {
51
51
@ Test
52
52
public void givenList_whenCalled_thenReturnListOfOddIndexedStrings () {
53
53
List <String > names = Arrays .asList ("Afrim" , "Bashkim" , "Besim" , "Lulzim" , "Durim" , "Shpetim" );
54
- List <Indexed <String >> expectedResult = Arrays
55
- .asList (Indexed .index (1 , "Bashkim" ), Indexed .index (3 , "Lulzim" ), Indexed
56
- .index (5 , "Shpetim" ));
54
+ List <Indexed <String >> expectedResult = Arrays .asList (Indexed .index (1 , "Bashkim" ), Indexed .index (3 , "Lulzim" ), Indexed .index (5 , "Shpetim" ));
57
55
List <Indexed <String >> actualResult = StreamIndices .getOddIndexedStrings (names );
58
56
59
57
assertEquals (expectedResult , actualResult );
60
58
}
61
59
62
60
@ Test
63
61
public void whenCalled_thenReturnListOfOddStringsVersionTwo () {
64
- String [] names = {"Afrim" , "Bashkim" , "Besim" , "Lulzim" , "Durim" , "Shpetim" };
62
+ String [] names = { "Afrim" , "Bashkim" , "Besim" , "Lulzim" , "Durim" , "Shpetim" };
65
63
List <String > expectedResult = Arrays .asList ("Bashkim" , "Lulzim" , "Shpetim" );
66
64
List <String > actualResult = StreamIndices .getOddIndexedStringsVersionTwo (names );
67
65
68
66
assertEquals (expectedResult , actualResult );
69
67
}
68
+
69
+ @ Test
70
+ public void whenCalledSequentially_thenReturnListOfEvenIndexedStrings () {
71
+ String [] names = { "Afrim" , "Bashkim" , "Besim" , "Lulzim" , "Durim" , "Shpetim" };
72
+ List <String > expectedResult = Arrays .asList ("Afrim" , "Besim" , "Durim" );
73
+ List <String > actualResult = StreamIndices .getEvenIndexedStringsUsingAtomicInteger (names );
74
+ assertEquals (expectedResult , actualResult );
75
+ }
76
+
77
+ @ Test
78
+ public void whenCalledInParallel_thenResultInconsistent () {
79
+ String [] names = { "Afrim" , "Bashkim" , "Besim" , "Lulzim" , "Durim" , "Shpetim" };
80
+ List <String > result = StreamIndices .getEvenIndexedStringsAtomicIntegerParallel (names );
81
+ // The result can be inconsistent because of race conditions.
82
+ //assertNotEquals(Arrays.asList("Afrim", "Besim", "Durim"), result);
83
+ }
70
84
}
0 commit comments