-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIntroductionToStreamsTest.java
135 lines (119 loc) · 4.47 KB
/
IntroductionToStreamsTest.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
package dev.boiarshinov.stepik;
import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
* These tests are related to topic 1.4 - Introduction to streams.
* Part of these tests are solutions of exercises 2.15-2.19.
* There is no solution for exercises 2.15-2.16 because they too simple.
*/
public class IntroductionToStreamsTest {
//todo: add missing exercises
/**
* Usage examples of {@link IntStream#summaryStatistics()}.
*/
@Test
public void summaryStatistics() {
final IntSummaryStatistics statistics = IntStream.rangeClosed(1, 55_555).summaryStatistics();
Assert.assertEquals(statistics.getCount(), 55_555);
Assert.assertEquals(statistics.getMax(), 55_555);
Assert.assertEquals(statistics.getMin(), 1);
Assert.assertEquals(statistics.getAverage(), 27778);
Assert.assertEquals(statistics.getSum(), 1_543_206_790);
}
/**
* Exercise 2.17 "Checking if a number is prime".
*/
@Test(dataProvider = "primeNumbers")
public void primeNumbers(final long input, final boolean expected) {
boolean actual = isPrime(input);
Assert.assertEquals(actual, expected);
}
/**
* Checking if a number is prime
* @param number to test >= 2
* @return true if number is prime else false
*/
private static boolean isPrime(final long number) {
return LongStream.rangeClosed(2L, (long) Math.sqrt(number)).allMatch(value -> number % value != 0);
}
/**
* Data provider for exercise 2.17.
* @return integer number and expected boolean value.
*/
@DataProvider(name = "primeNumbers")
public static Object[][] primeNumbers() {
return new Object[][] {
{2, true},
{3, true},
{4, false},
{5, true},
{29, true}
};
}
/**
* Exercise 2.18 "Bad words detecting".
*/
@Test
public void badWords() {
final String text = "Вован пидр лох негодник лох";
final List<String> badWords = Arrays.asList("петух", "пидр", "лох");
final List<String> expected = Arrays.asList("лох", "пидр");
final List<String> actual = createBadWordsDetectingStream(text, badWords).collect(Collectors.toList());
Assert.assertEquals(actual, expected);
}
/**
* @param text some text.
* @param badWords list of bad words.
* @return stream of bad words found in text sorted and without duplicates.
*/
private static Stream<String> createBadWordsDetectingStream(final String text, final List<String> badWords) {
return Arrays.stream(text.split(" "))
.filter(badWords::contains)
.distinct()
.sorted();
}
/**
* Exercise 2.19 "Numbers filtering".
*/
@Test(dataProvider = "mixTwoStreams")
public void mixTwoStreams(int[] evens, int[] odds, int[] expectedInts) {
final IntStream evenStream = Arrays.stream(evens);
final IntStream oddStream = Arrays.stream(odds);
final List<Integer> actualNumbers = createFilteringStream(evenStream, oddStream)
.boxed()
.collect(Collectors.toList());
Assert.assertEquals(expectedInts.length, actualNumbers.size());
Arrays.stream(expectedInts).forEach(x -> Assert.assertTrue(actualNumbers.contains(x)));
}
/**
* @param evenStream stream with even numbers.
* @param oddStream stream with odd numbers.
* @return stream with elements from both streams which is divisible by 3 and 5.
*/
public static IntStream createFilteringStream(final IntStream evenStream, final IntStream oddStream) {
return IntStream.concat(evenStream, oddStream)
.filter(x -> x % 3 == 0)
.filter(x -> x % 5 == 0)
.sorted()
.skip(2);
}
/**
* Data provider for exercise 2.19.
* @return two input IntStreams and expected result IntStream.
*/
@DataProvider(name = "mixTwoStreams")
public Object[][] provideIntStreams() {
return new Object[][] {
{new int[]{2, 4}, new int[]{1, 3}, new int[]{}},
{new int[]{30, 60, 90}, new int[]{75}, new int[]{75, 90}}
};
}
}