|
| 1 | +package javasabr.rlib.collections.array; |
| 2 | + |
| 3 | +import java.util.Objects; |
| 4 | +import java.util.stream.Stream; |
| 5 | +import org.junit.jupiter.api.Assertions; |
| 6 | +import org.junit.jupiter.params.ParameterizedTest; |
| 7 | +import org.junit.jupiter.params.provider.Arguments; |
| 8 | +import org.junit.jupiter.params.provider.MethodSource; |
| 9 | + |
| 10 | +class ArrayIterationsTest { |
| 11 | + |
| 12 | + @ParameterizedTest |
| 13 | + @MethodSource("generateArrays") |
| 14 | + void shouldFindAnyCorrectly(Array<String> array) { |
| 15 | + // when/then: |
| 16 | + Assertions.assertNull(array |
| 17 | + .iterations() |
| 18 | + .findAny("notexist", Objects::equals)); |
| 19 | + Assertions.assertNotNull(array |
| 20 | + .iterations() |
| 21 | + .findAny("Second", Objects::equals)); |
| 22 | + } |
| 23 | + |
| 24 | + @ParameterizedTest |
| 25 | + @MethodSource("generateArrays") |
| 26 | + void shouldFindAny2Correctly(Array<String> array) { |
| 27 | + // when/then: |
| 28 | + Assertions.assertNull(array |
| 29 | + .iterations() |
| 30 | + .findAny(10, (element, intArg) -> String.valueOf(intArg).equals(element))); |
| 31 | + Assertions.assertNotNull(array |
| 32 | + .iterations() |
| 33 | + .findAny(5, (element, intArg) -> String.valueOf(intArg).equals(element))); |
| 34 | + } |
| 35 | + |
| 36 | + @ParameterizedTest |
| 37 | + @MethodSource("generateArrays") |
| 38 | + void shouldDoForEachCorrectly(Array<String> array) { |
| 39 | + // given: |
| 40 | + var result = MutableArray.ofType(String.class); |
| 41 | + var expected = Array.typed(String.class, |
| 42 | + "First_postfix", |
| 43 | + "Second_postfix", |
| 44 | + "Third_postfix", |
| 45 | + " _postfix", |
| 46 | + "5_postfix"); |
| 47 | + |
| 48 | + // when: |
| 49 | + array.iterations() |
| 50 | + .forEach("_postfix", (element, arg1) -> result.add(element + arg1)); |
| 51 | + |
| 52 | + // then: |
| 53 | + Assertions.assertEquals(expected, result); |
| 54 | + } |
| 55 | + |
| 56 | + @ParameterizedTest |
| 57 | + @MethodSource("generateArrays") |
| 58 | + void shouldDoForEach2Correctly(Array<String> array) { |
| 59 | + // given: |
| 60 | + var result = MutableArray.ofType(String.class); |
| 61 | + var expected = Array.typed(String.class, |
| 62 | + "prefix_First_postfix", |
| 63 | + "prefix_Second_postfix", |
| 64 | + "prefix_Third_postfix", |
| 65 | + "prefix_ _postfix", |
| 66 | + "prefix_5_postfix"); |
| 67 | + |
| 68 | + // when: |
| 69 | + array.iterations() |
| 70 | + .forEach("prefix_", "_postfix", (element, arg1, arg2) -> result.add(arg1 + element + arg2)); |
| 71 | + |
| 72 | + // then: |
| 73 | + Assertions.assertEquals(expected, result); |
| 74 | + } |
| 75 | + |
| 76 | + @ParameterizedTest |
| 77 | + @MethodSource("generateArrays") |
| 78 | + void shouldDoForEach3Correctly(Array<String> array) { |
| 79 | + // given: |
| 80 | + var result = MutableArray.ofType(String.class); |
| 81 | + var expected = Array.typed(String.class, |
| 82 | + "prefix_First55", |
| 83 | + "prefix_Second55", |
| 84 | + "prefix_Third55", |
| 85 | + "prefix_ 55", |
| 86 | + "prefix_555"); |
| 87 | + |
| 88 | + // when: |
| 89 | + array.iterations() |
| 90 | + .forEach("prefix_", 55L, (element, arg1, arg2) -> result.add(arg1 + element + arg2)); |
| 91 | + |
| 92 | + // then: |
| 93 | + Assertions.assertEquals(expected, result); |
| 94 | + } |
| 95 | + |
| 96 | + @ParameterizedTest |
| 97 | + @MethodSource("generateArrays") |
| 98 | + void shouldAnyMatchCorrectly(Array<String> array) { |
| 99 | + // when/then: |
| 100 | + Assertions.assertFalse(array |
| 101 | + .iterations() |
| 102 | + .anyMatch(10, (element, intArg) -> String.valueOf(intArg).equals(element))); |
| 103 | + Assertions.assertTrue(array |
| 104 | + .iterations() |
| 105 | + .anyMatch(5, (element, intArg) -> String.valueOf(intArg).equals(element))); |
| 106 | + } |
| 107 | + |
| 108 | + private static Stream<Arguments> generateArrays() { |
| 109 | + Array<String> array = Array.typed(String.class, "First", "Second", "Third", " ", "5"); |
| 110 | + MutableArray<String> mutableArray = ArrayFactory.mutableArray(String.class); |
| 111 | + mutableArray.addAll(array); |
| 112 | + MutableArray<String> copyOnModifyArray = ArrayFactory.copyOnModifyArray(String.class); |
| 113 | + copyOnModifyArray.addAll(array); |
| 114 | + LockableArray<String> stampedLockBasedArray = ArrayFactory.stampedLockBasedArray(String.class); |
| 115 | + stampedLockBasedArray.addAll(array); |
| 116 | + return Stream.of( |
| 117 | + Arguments.of(array), |
| 118 | + Arguments.of(mutableArray), |
| 119 | + Arguments.of(copyOnModifyArray), |
| 120 | + Arguments.of(stampedLockBasedArray)); |
| 121 | + } |
| 122 | +} |
0 commit comments