|
| 1 | +package javasabr.rlib.collections.dictionary; |
| 2 | + |
| 3 | +import static javasabr.rlib.collections.dictionary.IntToRefDictionary.entry; |
| 4 | +import static org.assertj.core.api.Assertions.assertThat; |
| 5 | + |
| 6 | +import java.util.HashSet; |
| 7 | +import java.util.Set; |
| 8 | +import java.util.stream.Stream; |
| 9 | +import javasabr.rlib.collections.array.Array; |
| 10 | +import javasabr.rlib.collections.array.ArrayFactory; |
| 11 | +import javasabr.rlib.collections.array.IntArray; |
| 12 | +import javasabr.rlib.collections.array.MutableArray; |
| 13 | +import javasabr.rlib.common.tuple.IntRefTuple; |
| 14 | +import org.junit.jupiter.params.ParameterizedTest; |
| 15 | +import org.junit.jupiter.params.provider.Arguments; |
| 16 | +import org.junit.jupiter.params.provider.MethodSource; |
| 17 | + |
| 18 | +class IntToRefDictionaryTest { |
| 19 | + |
| 20 | + @ParameterizedTest |
| 21 | + @MethodSource("generateDictionaries") |
| 22 | + void shouldProvideCorrectSize(IntToRefDictionary<String> dictionary) { |
| 23 | + // then: |
| 24 | + assertThat(dictionary.size()).isEqualTo(5); |
| 25 | + assertThat(dictionary.isEmpty()).isFalse(); |
| 26 | + } |
| 27 | + |
| 28 | + @ParameterizedTest |
| 29 | + @MethodSource("generateEmptyDictionaries") |
| 30 | + void shouldConfirmThatDictionaryIsEmpty(IntToRefDictionary<String> dictionary) { |
| 31 | + // then: |
| 32 | + assertThat(dictionary.isEmpty()).isTrue(); |
| 33 | + } |
| 34 | + |
| 35 | + @ParameterizedTest |
| 36 | + @MethodSource("generateDictionaries") |
| 37 | + void shouldFindValuesByKeys(IntToRefDictionary<String> dictionary) { |
| 38 | + // then: |
| 39 | + assertThat(dictionary.get(1)).isEqualTo("val1"); |
| 40 | + assertThat(dictionary.get(3)).isEqualTo("val3"); |
| 41 | + assertThat(dictionary.get(4)).isEqualTo("val4"); |
| 42 | + assertThat(dictionary.get(10)).isNull(); |
| 43 | + } |
| 44 | + |
| 45 | + @ParameterizedTest |
| 46 | + @MethodSource("generateDictionaries") |
| 47 | + void shouldFindValuesByKeysOrReturnDefault(IntToRefDictionary<String> dictionary) { |
| 48 | + // then: |
| 49 | + assertThat(dictionary.get(1)).isEqualTo("val1"); |
| 50 | + assertThat(dictionary.get(10)).isNull(); |
| 51 | + assertThat(dictionary.getOrDefault(10, "def1")).isEqualTo("def1"); |
| 52 | + assertThat(dictionary.getOrDefault(30, "def2")).isEqualTo("def2"); |
| 53 | + } |
| 54 | + |
| 55 | + @ParameterizedTest |
| 56 | + @MethodSource("generateDictionaries") |
| 57 | + void shouldReturnOptionalValuesByKeys(IntToRefDictionary<String> dictionary) { |
| 58 | + // then: |
| 59 | + assertThat(dictionary.getOptional(1)).contains("val1"); |
| 60 | + assertThat(dictionary.getOptional(10)).isEmpty(); |
| 61 | + } |
| 62 | + |
| 63 | + @ParameterizedTest |
| 64 | + @MethodSource("generateDictionaries") |
| 65 | + void shouldCheckContainsKeys(IntToRefDictionary<String> dictionary) { |
| 66 | + // then: |
| 67 | + assertThat(dictionary.containsKey(1)).isTrue(); |
| 68 | + assertThat(dictionary.containsKey(2)).isTrue(); |
| 69 | + assertThat(dictionary.containsKey(5)).isTrue(); |
| 70 | + assertThat(dictionary.containsKey(10)).isFalse(); |
| 71 | + } |
| 72 | + |
| 73 | + @ParameterizedTest |
| 74 | + @MethodSource("generateDictionaries") |
| 75 | + void shouldCheckContainsValues(IntToRefDictionary<String> dictionary) { |
| 76 | + // then: |
| 77 | + assertThat(dictionary.containsValue("val1")).isTrue(); |
| 78 | + assertThat(dictionary.containsValue("val3")).isTrue(); |
| 79 | + assertThat(dictionary.containsValue("val4")).isTrue(); |
| 80 | + assertThat(dictionary.containsValue("val10")).isFalse(); |
| 81 | + } |
| 82 | + |
| 83 | + @ParameterizedTest |
| 84 | + @MethodSource("generateDictionaries") |
| 85 | + void shouldHaveExpectedKeys(IntToRefDictionary<String> dictionary) { |
| 86 | + // given: |
| 87 | + var expectedArray = Array.typed(Integer.class, 1, 2, 3, 4, 5); |
| 88 | + var expectedIntArray = IntArray.of(1, 2, 3, 4, 5); |
| 89 | + var expectedSet = Set.copyOf(expectedArray.toList()); |
| 90 | + |
| 91 | + // when: |
| 92 | + Array<Integer> keys = dictionary.keys(Integer.class); |
| 93 | + |
| 94 | + // then: |
| 95 | + assertThat(keys).isEqualTo(expectedArray); |
| 96 | + |
| 97 | + // when: |
| 98 | + Array<Integer> keys2 = dictionary.keys(MutableArray.ofType(Integer.class)); |
| 99 | + |
| 100 | + // then: |
| 101 | + assertThat(keys2).isEqualTo(expectedArray); |
| 102 | + |
| 103 | + // when: |
| 104 | + Set<Integer> keys3 = dictionary.keys(new HashSet<>()); |
| 105 | + |
| 106 | + // then: |
| 107 | + assertThat(keys3).isEqualTo(expectedSet); |
| 108 | + |
| 109 | + // when: |
| 110 | + IntArray intKeys = dictionary.keys(); |
| 111 | + |
| 112 | + // then: |
| 113 | + assertThat(intKeys).isEqualTo(expectedIntArray); |
| 114 | + |
| 115 | + // when: |
| 116 | + intKeys = dictionary.keys(ArrayFactory.mutableIntArray()); |
| 117 | + |
| 118 | + // then: |
| 119 | + assertThat(intKeys).isEqualTo(expectedIntArray); |
| 120 | + } |
| 121 | + |
| 122 | + @ParameterizedTest |
| 123 | + @MethodSource("generateDictionaries") |
| 124 | + void shouldHaveExpectedValues(IntToRefDictionary<String> dictionary) { |
| 125 | + // given: |
| 126 | + var expectedArray = Array.typed(String.class, "val1", "val2", "val3", "val4", "val5"); |
| 127 | + var expectedSet = Set.copyOf(expectedArray.toList()); |
| 128 | + |
| 129 | + // when: |
| 130 | + Array<String> values = dictionary.values(String.class); |
| 131 | + |
| 132 | + // then: |
| 133 | + assertThat(values).isEqualTo(expectedArray); |
| 134 | + |
| 135 | + // when: |
| 136 | + Array<String> values2 = dictionary.values(MutableArray.ofType(String.class)); |
| 137 | + |
| 138 | + // then: |
| 139 | + assertThat(values2).isEqualTo(expectedArray); |
| 140 | + |
| 141 | + // when: |
| 142 | + Set<String> values3 = dictionary.values(new HashSet<>()); |
| 143 | + |
| 144 | + // then: |
| 145 | + assertThat(values3).isEqualTo(expectedSet); |
| 146 | + } |
| 147 | + |
| 148 | + @ParameterizedTest |
| 149 | + @MethodSource("generateDictionaries") |
| 150 | + void shouldHaveExpectedResultFromForEach(IntToRefDictionary<String> dictionary) { |
| 151 | + // given: |
| 152 | + var expectedArray = Array.typed(String.class, "val1", "val2", "val3", "val4", "val5"); |
| 153 | + var expectedPairs = Array.typed(IntRefTuple.class, |
| 154 | + new IntRefTuple<>(1, "val1"), |
| 155 | + new IntRefTuple<>(2, "val2"), |
| 156 | + new IntRefTuple<>(3, "val3"), |
| 157 | + new IntRefTuple<>(4, "val4"), |
| 158 | + new IntRefTuple<>(5, "val5")); |
| 159 | + |
| 160 | + // when: |
| 161 | + MutableArray<String> values = MutableArray.ofType(String.class); |
| 162 | + dictionary.forEach(values::add); |
| 163 | + |
| 164 | + // then: |
| 165 | + assertThat(values).isEqualTo(expectedArray); |
| 166 | + |
| 167 | + // when: |
| 168 | + MutableArray<IntRefTuple> pairs = MutableArray.ofType(IntRefTuple.class); |
| 169 | + dictionary.forEach((key, value) -> pairs.add(new IntRefTuple<>(key, value))); |
| 170 | + |
| 171 | + // then: |
| 172 | + assertThat(pairs).isEqualTo(expectedPairs); |
| 173 | + } |
| 174 | + |
| 175 | + private static Stream<Arguments> generateDictionaries() { |
| 176 | + |
| 177 | + IntToRefDictionary<String> source = IntToRefDictionary.ofEntries( |
| 178 | + entry(1, "val1"), |
| 179 | + entry(2, "val2"), |
| 180 | + entry(3, "val3"), |
| 181 | + entry(4, "val4"), |
| 182 | + entry(5, "val5")); |
| 183 | + |
| 184 | + return Stream.of( |
| 185 | + Arguments.of(source), |
| 186 | + Arguments.of(DictionaryFactory.mutableIntToRefDictionary().append(source))); |
| 187 | + } |
| 188 | + |
| 189 | + private static Stream<Arguments> generateEmptyDictionaries() { |
| 190 | + return Stream.of( |
| 191 | + Arguments.of(IntToRefDictionary.empty()), |
| 192 | + Arguments.of(DictionaryFactory.mutableIntToRefDictionary())); |
| 193 | + } |
| 194 | +} |
0 commit comments