|
| 1 | +package com.baeldung.streamreduce.tests; |
| 2 | + |
| 3 | +import com.baeldung.streamreduce.entities.User; |
| 4 | +import com.baeldung.streamreduce.utilities.NumberUtils; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.List; |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +public class StreamReduceUnitTest { |
| 12 | + |
| 13 | + @Test |
| 14 | + public void givenIntegerList_whenReduceWithSumAccumulatorLambda_thenCorrect() { |
| 15 | + List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6); |
| 16 | + |
| 17 | + int result = numbers.stream().reduce(0, (a, b) -> a + b); |
| 18 | + |
| 19 | + assertThat(result).isEqualTo(21); |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + public void givenIntegerList_whenReduceWithSumAccumulatorMethodReference_thenCorrect() { |
| 24 | + List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6); |
| 25 | + |
| 26 | + int result = numbers.stream().reduce(0, Integer::sum); |
| 27 | + |
| 28 | + assertThat(result).isEqualTo(21); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void givenStringList_whenReduceWithConcatenatorAccumulatorLambda_thenCorrect() { |
| 33 | + List<String> letters = Arrays.asList("a", "b", "c", "d", "e"); |
| 34 | + |
| 35 | + String result = letters.stream().reduce("", (a, b) -> a + b); |
| 36 | + |
| 37 | + assertThat(result).isEqualTo("abcde"); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void givenStringList_whenReduceWithConcatenatorAccumulatorMethodReference_thenCorrect() { |
| 42 | + List<String> letters = Arrays.asList("a", "b", "c", "d", "e"); |
| 43 | + |
| 44 | + String result = letters.stream().reduce("", String::concat); |
| 45 | + |
| 46 | + assertThat(result).isEqualTo("abcde"); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void givenStringList_whenReduceWithUppercaseConcatenatorAccumulator_thenCorrect() { |
| 51 | + List<String> letters = Arrays.asList("a", "b", "c", "d", "e"); |
| 52 | + |
| 53 | + String result = letters.stream().reduce("", (a, b) -> a.toUpperCase() + b.toUpperCase()); |
| 54 | + |
| 55 | + assertThat(result).isEqualTo("ABCDE"); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void givenUserList_whenReduceWithAgeAccumulatorAndSumCombiner_thenCorrect() { |
| 60 | + List<User> users = Arrays.asList(new User("John", 30), new User("Julie", 35)); |
| 61 | + |
| 62 | + int result = users.stream().reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum); |
| 63 | + |
| 64 | + assertThat(result).isEqualTo(65); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void givenStringList_whenReduceWithParallelStream_thenCorrect() { |
| 69 | + List<String> letters = Arrays.asList("a", "b", "c", "d", "e"); |
| 70 | + |
| 71 | + String result = letters.parallelStream().reduce("", String::concat); |
| 72 | + |
| 73 | + assertThat(result).isEqualTo("abcde"); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void givenNumberUtilsClass_whenCalledDivideListElements_thenCorrect() { |
| 78 | + List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6); |
| 79 | + |
| 80 | + assertThat(NumberUtils.divideListElements(numbers, 1)).isEqualTo(21); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void givenNumberUtilsClass_whenCalledDivideListElementsWithExtractedTryCatchBlock_thenCorrect() { |
| 85 | + List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6); |
| 86 | + |
| 87 | + assertThat(NumberUtils.divideListElementsWithExtractedTryCatchBlock(numbers, 1)).isEqualTo(21); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void givenNumberUtilsClass_whenCalledDivideListElementsWithExtractedTryCatchBlockAndListContainsZero_thenCorrect() { |
| 92 | + List<Integer> numbers = Arrays.asList(0, 1, 2, 3, 4, 5, 6); |
| 93 | + |
| 94 | + assertThat(NumberUtils.divideListElementsWithExtractedTryCatchBlock(numbers, 1)).isEqualTo(21); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void givenNumberUtilsClass_whenCalledDivideListElementsWithExtractedTryCatchBlockAndDividerIsZero_thenCorrect() { |
| 99 | + List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6); |
| 100 | + |
| 101 | + assertThat(NumberUtils.divideListElementsWithExtractedTryCatchBlock(numbers, 0)).isEqualTo(0); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void givenStream_whneCalleddivideListElementsWithApplyFunctionMethod_thenCorrect() { |
| 106 | + List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6); |
| 107 | + |
| 108 | + assertThat(NumberUtils.divideListElementsWithApplyFunctionMethod(numbers, 1)).isEqualTo(21); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void givenTwoStreams_whenCalledReduceOnParallelizedStream_thenFasterExecutionTime() { |
| 113 | + List<User> userList = new ArrayList<>(); |
| 114 | + for (int i = 0; i <= 1000000; i++) { |
| 115 | + userList.add(new User("John" + i, i)); |
| 116 | + } |
| 117 | + long currentTime1 = System.currentTimeMillis(); |
| 118 | + userList.stream().reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum); |
| 119 | + long sequentialExecutionTime = System.currentTimeMillis() -currentTime1; |
| 120 | + long currentTime2 = System.currentTimeMillis(); |
| 121 | + userList.parallelStream().reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum); |
| 122 | + long parallelizedExecutionTime = System.currentTimeMillis() - currentTime2; |
| 123 | + |
| 124 | + assertThat(parallelizedExecutionTime).isLessThan(sequentialExecutionTime); |
| 125 | + } |
| 126 | +} |
0 commit comments