forked from amaembo/streamex
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master' into issue/amaembo#185-…
…without # Conflicts: # wiki/CHANGES.md
- Loading branch information
Showing
25 changed files
with
4,691 additions
and
4,496 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,109 @@ | ||
/* | ||
* Copyright 2015, 2019 StreamEx contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package one.util.streamex; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import java.math.MathContext; | ||
import java.util.Arrays; | ||
import java.util.OptionalDouble; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Collector; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
import java.util.stream.LongStream; | ||
|
||
import org.junit.Test; | ||
|
||
import static one.util.streamex.Internals.AverageLong; | ||
import static one.util.streamex.TestHelpers.repeat; | ||
import static one.util.streamex.TestHelpers.withRandom; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
|
||
/** | ||
* @author Tagir Valeev | ||
*/ | ||
public class AverageLongTest { | ||
|
||
@Test | ||
public void testAverageLongNoOverflow() { | ||
AverageLong avg = new AverageLong(); | ||
assertFalse(avg.result().isPresent()); | ||
avg.accept(1); | ||
avg.accept(2); | ||
avg.accept(3); | ||
assertEquals(2.0, avg.result().getAsDouble(), 0.0); | ||
|
||
avg.accept(2); | ||
avg.accept(-4); | ||
avg.accept(8); | ||
assertEquals(2.0, avg.result().getAsDouble(), 0.0); | ||
|
||
AverageLong avg1 = new AverageLong(); | ||
avg1.accept(-2); | ||
AverageLong avg2 = new AverageLong(); | ||
avg2.accept(-2); | ||
assertEquals(-2.0, avg1.combine(avg2).result().getAsDouble(), 0.0); | ||
|
||
withRandom(r -> { | ||
int[] input = r.ints(1000).toArray(); | ||
OptionalDouble expected = IntStream.of(input).average(); | ||
assertEquals(expected, Arrays.stream(input) | ||
.collect(AverageLong::new, AverageLong::accept, AverageLong::combine).result()); | ||
|
||
assertEquals(expected, Arrays.stream(input).parallel().collect(AverageLong::new, AverageLong::accept, | ||
AverageLong::combine).result()); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testCombine() { | ||
withRandom(r -> repeat(100, i -> { | ||
AverageLong avg1 = new AverageLong(); | ||
AverageLong avg2 = new AverageLong(); | ||
long[] set1 = r.longs(100).toArray(); | ||
long[] set2 = r.longs(100).toArray(); | ||
double expected = LongStreamEx.of(set1).append(set2).boxed().collect(getBigIntegerAverager()).getAsDouble(); | ||
LongStream.of(set1).forEach(avg1::accept); | ||
LongStream.of(set2).forEach(avg2::accept); | ||
assertEquals(expected, avg1.combine(avg2).result().getAsDouble(), Math.abs(expected / 1e14)); | ||
})); | ||
} | ||
|
||
@Test | ||
public void testCompareToBigInteger() { | ||
withRandom(r -> { | ||
long[] input = LongStreamEx.of(r, 1000).toArray(); | ||
Supplier<LongStream> supplier = () -> Arrays.stream(input); | ||
double expected = supplier.get().boxed().collect(getBigIntegerAverager()).getAsDouble(); | ||
assertEquals(expected, supplier.get().collect(AverageLong::new, AverageLong::accept, AverageLong::combine) | ||
.result().getAsDouble(), Math.abs(expected) / 1e14); | ||
assertEquals(expected, supplier.get().parallel().collect(AverageLong::new, AverageLong::accept, | ||
AverageLong::combine).result().getAsDouble(), Math.abs(expected) / 1e14); | ||
}); | ||
} | ||
|
||
private static Collector<Long, ?, OptionalDouble> getBigIntegerAverager() { | ||
BiFunction<BigInteger, Long, OptionalDouble> finisher = (BigInteger sum, Long cnt) -> cnt == 0L ? OptionalDouble | ||
.empty() | ||
: OptionalDouble.of(new BigDecimal(sum).divide(BigDecimal.valueOf(cnt), MathContext.DECIMAL64) | ||
.doubleValue()); | ||
return MoreCollectors.pairing(Collectors.reducing(BigInteger.ZERO, | ||
BigInteger::valueOf, BigInteger::add), Collectors.counting(), finisher); | ||
} | ||
} | ||
/* | ||
* Copyright 2015, 2019 StreamEx contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package one.util.streamex; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import java.math.MathContext; | ||
import java.util.Arrays; | ||
import java.util.OptionalDouble; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Collector; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
import java.util.stream.LongStream; | ||
|
||
import org.junit.Test; | ||
|
||
import static one.util.streamex.Internals.AverageLong; | ||
import static one.util.streamex.TestHelpers.repeat; | ||
import static one.util.streamex.TestHelpers.withRandom; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
|
||
/** | ||
* @author Tagir Valeev | ||
*/ | ||
public class AverageLongTest { | ||
|
||
@Test | ||
public void testAverageLongNoOverflow() { | ||
AverageLong avg = new AverageLong(); | ||
assertFalse(avg.result().isPresent()); | ||
avg.accept(1); | ||
avg.accept(2); | ||
avg.accept(3); | ||
assertEquals(2.0, avg.result().getAsDouble(), 0.0); | ||
|
||
avg.accept(2); | ||
avg.accept(-4); | ||
avg.accept(8); | ||
assertEquals(2.0, avg.result().getAsDouble(), 0.0); | ||
|
||
AverageLong avg1 = new AverageLong(); | ||
avg1.accept(-2); | ||
AverageLong avg2 = new AverageLong(); | ||
avg2.accept(-2); | ||
assertEquals(-2.0, avg1.combine(avg2).result().getAsDouble(), 0.0); | ||
|
||
withRandom(r -> { | ||
int[] input = r.ints(1000).toArray(); | ||
OptionalDouble expected = IntStream.of(input).average(); | ||
assertEquals(expected, Arrays.stream(input) | ||
.collect(AverageLong::new, AverageLong::accept, AverageLong::combine).result()); | ||
|
||
assertEquals(expected, Arrays.stream(input).parallel().collect(AverageLong::new, AverageLong::accept, | ||
AverageLong::combine).result()); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testCombine() { | ||
withRandom(r -> repeat(100, i -> { | ||
AverageLong avg1 = new AverageLong(); | ||
AverageLong avg2 = new AverageLong(); | ||
long[] set1 = r.longs(100).toArray(); | ||
long[] set2 = r.longs(100).toArray(); | ||
double expected = LongStreamEx.of(set1).append(set2).boxed().collect(getBigIntegerAverager()).getAsDouble(); | ||
LongStream.of(set1).forEach(avg1::accept); | ||
LongStream.of(set2).forEach(avg2::accept); | ||
assertEquals(expected, avg1.combine(avg2).result().getAsDouble(), Math.abs(expected / 1e14)); | ||
})); | ||
} | ||
|
||
@Test | ||
public void testCompareToBigInteger() { | ||
withRandom(r -> { | ||
long[] input = LongStreamEx.of(r, 1000).toArray(); | ||
Supplier<LongStream> supplier = () -> Arrays.stream(input); | ||
double expected = supplier.get().boxed().collect(getBigIntegerAverager()).getAsDouble(); | ||
assertEquals(expected, supplier.get().collect(AverageLong::new, AverageLong::accept, AverageLong::combine) | ||
.result().getAsDouble(), Math.abs(expected) / 1e14); | ||
assertEquals(expected, supplier.get().parallel().collect(AverageLong::new, AverageLong::accept, | ||
AverageLong::combine).result().getAsDouble(), Math.abs(expected) / 1e14); | ||
}); | ||
} | ||
|
||
private static Collector<Long, ?, OptionalDouble> getBigIntegerAverager() { | ||
BiFunction<BigInteger, Long, OptionalDouble> finisher = (BigInteger sum, Long cnt) -> cnt == 0L ? OptionalDouble | ||
.empty() | ||
: OptionalDouble.of(new BigDecimal(sum).divide(BigDecimal.valueOf(cnt), MathContext.DECIMAL64) | ||
.doubleValue()); | ||
return MoreCollectors.pairing(Collectors.reducing(BigInteger.ZERO, | ||
BigInteger::valueOf, BigInteger::add), Collectors.counting(), finisher); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/test/java/one/util/streamex/EntryStreamInternalTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2015, 2020 StreamEx contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package one.util.streamex; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
import org.junit.FixMethodOrder; | ||
import org.junit.Test; | ||
import org.junit.runners.MethodSorters; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertSame; | ||
|
||
/** | ||
* @author Tagir Valeev | ||
*/ | ||
@FixMethodOrder(MethodSorters.NAME_ASCENDING) | ||
public class EntryStreamInternalTest { | ||
|
||
@Test | ||
public void testCreate() { | ||
EntryStream<String, Integer> stream = EntryStream.of(createMap()); | ||
assertSame(stream.stream(), EntryStream.of(stream).stream()); | ||
assertSame(stream.stream(), EntryStream.of(StreamEx.of(EntryStream.of(stream))).stream()); | ||
} | ||
|
||
private static Map<String, Integer> createMap() { | ||
Map<String, Integer> data = new LinkedHashMap<>(); | ||
data.put("a", 1); | ||
data.put("bb", 22); | ||
data.put("ccc", 33); | ||
return data; | ||
} | ||
|
||
} |
Oops, something went wrong.