|
24 | 24 | import java.math.BigDecimal;
|
25 | 25 | import java.util.ArrayList;
|
26 | 26 | import java.util.Arrays;
|
| 27 | +import java.util.Comparator; |
27 | 28 | import java.util.Spliterator;
|
28 | 29 | import java.util.function.Function;
|
29 | 30 | import java.util.function.Predicate;
|
30 | 31 | import java.util.stream.Collector;
|
31 | 32 | import org.junit.jupiter.api.Test;
|
| 33 | +import org.junit.jupiter.api.TestTemplate; |
32 | 34 |
|
| 35 | +import static java.util.Comparator.comparingInt; |
33 | 36 | import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
34 | 37 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
35 | 38 |
|
@@ -2286,6 +2289,57 @@ public void shouldTestIndexedSeqEndsWithNonIndexedSeq() {
|
2286 | 2289 | assertThat(of(1, 2, 3, 4).endsWith(Stream.of(2, 3, 5))).isFalse();
|
2287 | 2290 | }
|
2288 | 2291 |
|
| 2292 | + // -- distinctByKeepLast(Comparator) |
| 2293 | + |
| 2294 | + @TestTemplate |
| 2295 | + public void shouldComputeDistinctByKeepLastOfEmptySeqUsingComparator() { |
| 2296 | + final Comparator<Integer> comparator = comparingInt(i -> i); |
| 2297 | + if (useIsEqualToInsteadOfIsSameAs()) { |
| 2298 | + assertThat(this.<Integer>empty().distinctByKeepLast(comparator)).isEqualTo(empty()); |
| 2299 | + } else { |
| 2300 | + assertThat(this.<Integer>empty().distinctByKeepLast(comparator)).isSameAs(empty()); |
| 2301 | + } |
| 2302 | + } |
| 2303 | + |
| 2304 | + @TestTemplate |
| 2305 | + public void shouldComputeDistinctByKeepLastOfNonEmptySeqUsingComparator() { |
| 2306 | + final Comparator<String> comparator = comparingInt(s -> (s.charAt(1))); |
| 2307 | + final Seq<String> distinct = of("1a", "2a", "3b", "4b", "3a", "5c") |
| 2308 | + .distinctByKeepLast(comparator); |
| 2309 | + assertThat(distinct).isEqualTo(of("4b", "3a", "5c")); |
| 2310 | + } |
| 2311 | + |
| 2312 | + @TestTemplate |
| 2313 | + public void shouldReturnSameInstanceWhenDistinctByKeepLastComparatorEmptySeq() { |
| 2314 | + final Seq<?> empty = empty(); |
| 2315 | + assertThat(empty.distinctByKeepLast(Comparators.naturalComparator())).isSameAs(empty); |
| 2316 | + } |
| 2317 | + |
| 2318 | + // -- distinctByKeepLast(Function) |
| 2319 | + |
| 2320 | + @TestTemplate |
| 2321 | + public void shouldComputeDistinctByKeepLastOfEmptySeqUsingKeyExtractor() { |
| 2322 | + if (useIsEqualToInsteadOfIsSameAs()) { |
| 2323 | + assertThat(empty().distinctByKeepLast(Function.identity())).isEqualTo(empty()); |
| 2324 | + } else { |
| 2325 | + assertThat(empty().distinctByKeepLast(Function.identity())).isSameAs(empty()); |
| 2326 | + } |
| 2327 | + } |
| 2328 | + |
| 2329 | + @TestTemplate |
| 2330 | + public void shouldComputeDistinctByKeepLastOfNonEmptySeqUsingKeyExtractor() { |
| 2331 | + final Function<String, Character> function = c -> c.charAt(1); |
| 2332 | + final Seq<String> distinct = of("1a", "2a", "3b", "4b", "3a", "5c") |
| 2333 | + .distinctByKeepLast(function); |
| 2334 | + assertThat(distinct).isEqualTo(of("4b", "3a", "5c")); |
| 2335 | + } |
| 2336 | + |
| 2337 | + @TestTemplate |
| 2338 | + public void shouldReturnSameInstanceWhenDistinctByKeepLastFunctionEmptySeq() { |
| 2339 | + final Seq<?> empty = empty(); |
| 2340 | + assertThat(empty.distinctByKeepLast(Function.identity())).isSameAs(empty); |
| 2341 | + } |
| 2342 | + |
2289 | 2343 | private interface SomeInterface {
|
2290 | 2344 | }
|
2291 | 2345 |
|
|
0 commit comments