|
| 1 | +package com.baeldung.array.flatarray; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.List; |
| 8 | +import java.util.stream.Collectors; |
| 9 | +import java.util.stream.Stream; |
| 10 | +import org.junit.jupiter.params.ParameterizedTest; |
| 11 | +import org.junit.jupiter.params.provider.Arguments; |
| 12 | +import org.junit.jupiter.params.provider.MethodSource; |
| 13 | + |
| 14 | +class FlatArrayUnitTest { |
| 15 | + |
| 16 | + @ParameterizedTest |
| 17 | + @MethodSource("arrayProvider") |
| 18 | + void giveTwoDimensionalArray_whenFlatWithStream_thenGetCorrectResult(int[][] initialArray, |
| 19 | + int[] expected) { |
| 20 | + int[] actual = Arrays.stream(initialArray).flatMapToInt(Arrays::stream).toArray(); |
| 21 | + assertThat(actual).containsExactly(expected); |
| 22 | + } |
| 23 | + |
| 24 | + @ParameterizedTest |
| 25 | + @MethodSource("arrayProvider") |
| 26 | + void giveTwoDimensionalArray_whenFlatWithForLoopAndAdditionalList_thenGetCorrectResult(int[][] initialArray, |
| 27 | + int[] intArray) { |
| 28 | + List<Integer> expected = Arrays.stream(intArray).boxed().collect(Collectors.toList()); |
| 29 | + List<Integer> actual = new ArrayList<>(); |
| 30 | + for (int[] numbers : initialArray) { |
| 31 | + for (int number : numbers) { |
| 32 | + actual.add(number); |
| 33 | + } |
| 34 | + } |
| 35 | + assertThat(actual).isEqualTo(expected); |
| 36 | + } |
| 37 | + |
| 38 | + @ParameterizedTest |
| 39 | + @MethodSource("arrayProvider") |
| 40 | + void giveTwoDimensionalArray_whenFlatWithForLoopAndLists_thenGetCorrectResult(int[][] initialArray, |
| 41 | + int[] intArray) { |
| 42 | + List<Integer> expected = Arrays.stream(intArray).boxed().collect(Collectors.toList()); |
| 43 | + List<Integer> actual = new ArrayList<>(); |
| 44 | + for (int[] numbers : initialArray) { |
| 45 | + List<Integer> listOfNumbers = Arrays.stream(numbers).boxed().collect(Collectors.toList()); |
| 46 | + actual.addAll(listOfNumbers); |
| 47 | + } |
| 48 | + assertThat(actual).isEqualTo(expected); |
| 49 | + } |
| 50 | + |
| 51 | + @ParameterizedTest |
| 52 | + @MethodSource("arrayProvider") |
| 53 | + void giveTwoDimensionalArray_whenFlatWithArrayCopy_thenGetCorrectResult(int[][] initialArray, |
| 54 | + int[] expected) { |
| 55 | + int[] actual = new int[]{}; |
| 56 | + int position = 0; |
| 57 | + for (int[] numbers : initialArray) { |
| 58 | + if (actual.length < position + numbers.length) { |
| 59 | + int[] newArray = new int[actual.length + numbers.length]; |
| 60 | + System.arraycopy(actual, 0, newArray, 0, actual.length); |
| 61 | + actual = newArray; |
| 62 | + } |
| 63 | + System.arraycopy(numbers, 0, actual, position, numbers.length); |
| 64 | + position += numbers.length; |
| 65 | + } |
| 66 | + assertThat(actual).isEqualTo(expected); |
| 67 | + } |
| 68 | + |
| 69 | + @ParameterizedTest |
| 70 | + @MethodSource("arrayProvider") |
| 71 | + void giveTwoDimensionalArray_whenFlatWithArrayCopyAndTotalNumberOfElements_thenGetCorrectResult(int[][] initialArray, |
| 72 | + int[] expected) { |
| 73 | + int totalNumberOfElements = 0; |
| 74 | + for (int[] numbers : initialArray) { |
| 75 | + totalNumberOfElements += numbers.length; |
| 76 | + } |
| 77 | + int[] actual = new int[totalNumberOfElements]; |
| 78 | + int position = 0; |
| 79 | + for (int[] numbers : initialArray) { |
| 80 | + System.arraycopy(numbers, 0, actual, position, numbers.length); |
| 81 | + position += numbers.length; |
| 82 | + } |
| 83 | + assertThat(actual).isEqualTo(expected); |
| 84 | + } |
| 85 | + |
| 86 | + @ParameterizedTest |
| 87 | + @MethodSource("arrayProvider") |
| 88 | + void giveTwoDimensionalArray_whenFlatWithForLoopAndTotalNumberOfElements_thenGetCorrectResult(int[][] initialArray, |
| 89 | + int[] expected) { |
| 90 | + int totalNumberOfElements = 0; |
| 91 | + for (int[] numbers : initialArray) { |
| 92 | + totalNumberOfElements += numbers.length; |
| 93 | + } |
| 94 | + int[] actual = new int[totalNumberOfElements]; |
| 95 | + int position = 0; |
| 96 | + for (int[] numbers : initialArray) { |
| 97 | + for (int number : numbers) { |
| 98 | + actual[position] = number; |
| 99 | + ++position; |
| 100 | + } |
| 101 | + } |
| 102 | + assertThat(actual).isEqualTo(expected); |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + static Stream<Arguments> arrayProvider() { |
| 107 | + return Stream.of( |
| 108 | + Arguments.of( |
| 109 | + new int[][]{ |
| 110 | + {805, 902, 259, 162, 775}, |
| 111 | + {278, 216, 0, 72, 663}, |
| 112 | + {185, 390, 537, 909, 918}, |
| 113 | + {150, 782, 282, 482, 401}, |
| 114 | + {244, 685, 643, 364, 307}, |
| 115 | + {483, 939, 750, 190, 424}, |
| 116 | + {44, 160, 290, 963, 881} |
| 117 | + }, |
| 118 | + new int[]{ |
| 119 | + 805, 902, 259, 162, 775, |
| 120 | + 278, 216, 0, 72, 663, |
| 121 | + 185, 390, 537, 909, 918, |
| 122 | + 150, 782, 282, 482, 401, |
| 123 | + 244, 685, 643, 364, 307, |
| 124 | + 483, 939, 750, 190, 424, |
| 125 | + 44, 160, 290, 963, 881 |
| 126 | + } |
| 127 | + ) |
| 128 | + ); |
| 129 | + } |
| 130 | +} |
0 commit comments