Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Issue/#91 re-structure tests #224

Merged
merged 13 commits into from
Sep 6, 2020
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions .idea/libraries/Maven__junit_junit_4_12.xml

This file was deleted.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__junit_junit_4_13.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .idea/streamex.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
218 changes: 109 additions & 109 deletions src/test/java/one/util/streamex/AverageLongTest.java
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 src/test/java/one/util/streamex/EntryStreamInternalTest.java
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;
}

}
52 changes: 52 additions & 0 deletions src/test/java/one/util/streamex/StreamExInternalTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.Spliterator;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import static java.util.Arrays.asList;
import static one.util.streamex.TestHelpers.streamEx;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;

/**
* @author Tagir Valeev
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class StreamExInternalTest {
@Test
public void testCreate() {
StreamEx<String> stream = StreamEx.of("foo", "bar");
assertSame(stream.stream(), StreamEx.of(stream).stream());
}

@Test
public void testDropWhile() {
// Test that in JDK9 operation is propagated to JDK dropWhile method.
boolean hasDropWhile = VerSpec.VER_SPEC.getClass().getSimpleName().equals("Java9Specific");
Spliterator<String> spliterator = StreamEx.of("aaa", "b", "cccc").dropWhile(x -> x.length() > 1).spliterator();
assertEquals(hasDropWhile, !spliterator.getClass().getSimpleName().equals("TDOfRef"));
}

@Test
public void testSplit() {
assertEquals(CharSpliterator.class, StreamEx.split("a#a", "\\#").spliterator().getClass());
}
}
Loading