From fcc1fb76907331d1a622a3b4d5e0700337af681b Mon Sep 17 00:00:00 2001 From: Ivan Hetman Date: Tue, 8 Sep 2020 19:02:45 +0300 Subject: [PATCH] Fix Stream.min and Stream.max (#520) --- .../util/stream/impl/TSimpleStreamImpl.java | 4 ++-- .../classlib/java/util/stream/StreamTest.java | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/stream/impl/TSimpleStreamImpl.java b/classlib/src/main/java/org/teavm/classlib/java/util/stream/impl/TSimpleStreamImpl.java index 3adc07282..07ed91145 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/stream/impl/TSimpleStreamImpl.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/stream/impl/TSimpleStreamImpl.java @@ -226,12 +226,12 @@ public abstract class TSimpleStreamImpl implements TStream { @Override public Optional min(Comparator comparator) { - return reduce((a, b) -> comparator.compare(a, b) < 0 ? a : b); + return reduce((a, b) -> comparator.compare(a, b) <= 0 ? a : b); } @Override public Optional max(Comparator comparator) { - return reduce((a, b) -> comparator.compare(a, b) > 0 ? a : b); + return reduce((a, b) -> comparator.compare(a, b) >= 0 ? a : b); } @Override diff --git a/tests/src/test/java/org/teavm/classlib/java/util/stream/StreamTest.java b/tests/src/test/java/org/teavm/classlib/java/util/stream/StreamTest.java index 996b5c1d7..16d5dd24e 100644 --- a/tests/src/test/java/org/teavm/classlib/java/util/stream/StreamTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/util/stream/StreamTest.java @@ -18,12 +18,15 @@ package org.teavm.classlib.java.util.stream; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.teavm.classlib.java.util.stream.Helper.appendNumbersTo; import static org.teavm.classlib.java.util.stream.Helper.testDoubleStream; import static org.teavm.classlib.java.util.stream.Helper.testIntStream; import static org.teavm.classlib.java.util.stream.Helper.testIntegerStream; import static org.teavm.classlib.java.util.stream.Helper.testLongStream; +import java.util.Arrays; +import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.Spliterator; @@ -287,6 +290,25 @@ public class StreamTest { assertFalse(Stream.empty().min(Comparator.comparing(x -> x)).isPresent()); assertFalse(Stream.empty().max(Comparator.comparing(x -> x)).isPresent()); + + IntPair first = new IntPair(0, 0); + IntPair second = new IntPair(0, 1); + + Comparator comp = Comparator.comparingInt(p -> p.number); + assertEquals(0, Stream.of(first, second).max(comp).get().id); + assertEquals(0, Stream.of(first, second).min(comp).get().id); + assertSame(Collections.max(Arrays.asList(first, second), comp), Stream.of(first, second).max(comp).get()); + assertSame(Collections.min(Arrays.asList(first, second), comp), Stream.of(first, second).min(comp).get()); + } + + private static class IntPair { + private final int number; + private final int id; + + private IntPair(int number, int id) { + this.number = number; + this.id = id; + } } @Test