BAEL-7862: Float conversion tests (#16506)
* BAEL-7862: Float conversion tests * BAEL-7862: Float Collector
This commit is contained in:
parent
c4b34c1793
commit
edf65df452
@ -13,6 +13,14 @@
|
|||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>${apache.commons.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<finalName>core-java-collections-conversions-3</finalName>
|
<finalName>core-java-collections-conversions-3</finalName>
|
||||||
<resources>
|
<resources>
|
||||||
@ -23,4 +31,8 @@
|
|||||||
</resources>
|
</resources>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<apache.commons.version>3.14.0</apache.commons.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -0,0 +1,104 @@
|
|||||||
|
package com.baeldung.listtoprimitivearray;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
import org.apache.commons.lang3.ArrayUtils;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
|
class ConvertingListToPrimitiveArrayUnitTest {
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("floatListProvider")
|
||||||
|
void givenListOfWrapperFloat_whenConvertToWrapperArray_thenGetCorrectResult(List<Float> floats) {
|
||||||
|
Float[] actual = floats.toArray(new Float[0]);
|
||||||
|
assertSequences(floats, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("floatListProvider")
|
||||||
|
void givenListOfWrapperFloat_whenConvertToWrapperArrayWithPreSizedArray_thenGetCorrectResult(List<Float> floats) {
|
||||||
|
Float[] actual = floats.toArray(new Float[floats.size()]);
|
||||||
|
assertSequences(floats, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("floatListProvider")
|
||||||
|
void givenListOfWrapperFloat_whenConvertToPrimitiveArray_thenGetCorrectResult(List<Float> floats) {
|
||||||
|
float[] actual = new float[floats.size()];
|
||||||
|
for (int i = 0; i < floats.size(); i++) {
|
||||||
|
actual[i] = floats.get(i);
|
||||||
|
}
|
||||||
|
assertSequences(floats, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("floatListProvider")
|
||||||
|
void givenListOfWrapperFloat_whenUnboxToPrimitiveArray_thenGetCorrectResult(List<Float> floats) {
|
||||||
|
float[] actual = new float[floats.size()];
|
||||||
|
Float[] floatArray = floats.toArray(new Float[0]);
|
||||||
|
for (int i = 0; i < floats.size(); i++) {
|
||||||
|
actual[i] = floatArray[i];
|
||||||
|
}
|
||||||
|
assertSequences(floats, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("floatListProvider")
|
||||||
|
void givenListOfWrapperFloat_whenConvertToPrimitiveArrayWithArrayUtils_thenGetCorrectResult(List<Float> floats) {
|
||||||
|
float[] actual = ArrayUtils.toPrimitive(floats.toArray(new Float[]{}));
|
||||||
|
assertSequences(floats, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("floatListProvider")
|
||||||
|
void givenListOfWrapperFloat_whenConvertingToPrimitiveArrayUsingStreams_thenGetCorrectResult(List<Float> floats) {
|
||||||
|
double[] actual = floats.stream().mapToDouble(Float::doubleValue).toArray();
|
||||||
|
assertSequences(floats, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("floatListProvider")
|
||||||
|
void givenListOfWrapperFloat_whenConvertingWithCollector_thenGetCorrectResult(List<Float> floats) {
|
||||||
|
float[] actual = floats.stream().collect(new FloatCollector(floats.size()));
|
||||||
|
assertSequences(floats, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void assertSequences(Iterable<Float> floats, Float[] actual) {
|
||||||
|
assertThat(actual).hasSameSizeAs(floats);
|
||||||
|
for (Float value : floats) {
|
||||||
|
assertThat(actual).contains(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void assertSequences(Iterable<Float> floats, float[] actual) {
|
||||||
|
assertThat(actual).hasSameSizeAs(floats);
|
||||||
|
for (Float value : floats) {
|
||||||
|
assertThat(actual).contains(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void assertSequences(Iterable<Float> floats, double[] actual) {
|
||||||
|
assertThat(actual).hasSameSizeAs(floats);
|
||||||
|
for (Float value : floats) {
|
||||||
|
assertThat(actual).contains(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static Stream<List<Float>> floatListProvider() {
|
||||||
|
ArrayList<Float> floats = new ArrayList<>();
|
||||||
|
floats.add(Float.valueOf(131.36f));
|
||||||
|
floats.add(Float.valueOf(66.59f));
|
||||||
|
floats.add(Float.valueOf(58.03f));
|
||||||
|
floats.add(Float.valueOf(951.56f));
|
||||||
|
floats.add(Float.valueOf(192.04f));
|
||||||
|
floats.add(Float.valueOf(899.03f));
|
||||||
|
floats.add(Float.valueOf(147.32f));
|
||||||
|
floats.add(Float.valueOf(573.75f));
|
||||||
|
floats.add(Float.valueOf(94.75f));
|
||||||
|
return Stream.of(floats);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
package com.baeldung.listtoprimitivearray;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.BinaryOperator;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
import java.util.stream.Collector;
|
||||||
|
|
||||||
|
public class FloatCollector implements Collector<Float, float[], float[]> {
|
||||||
|
|
||||||
|
private final int size;
|
||||||
|
private int index = 0;
|
||||||
|
public FloatCollector(int size) {
|
||||||
|
this.size = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Supplier<float[]> supplier() {
|
||||||
|
return () -> new float[size];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BiConsumer<float[], Float> accumulator() {
|
||||||
|
return (array, number) -> {
|
||||||
|
array[index] = number;
|
||||||
|
index++;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BinaryOperator<float[]> combiner() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Function<float[], float[]> finisher() {
|
||||||
|
return Function.identity();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Characteristics> characteristics() {
|
||||||
|
return Collections.emptySet();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user