Skip to content

Commit

Permalink
add tests; fix ClassCastException
Browse files Browse the repository at this point in the history
  • Loading branch information
xzel23 committed Dec 12, 2023
1 parent ea3661c commit 9a4a3c7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public BiConsumer<Deque<Pair<K, List<T>>>, T> accumulator() {
K key = keyMapper.apply(item);

List<T> bucket;
if (accu.isEmpty() || (key != null && !Objects.equals(key, accu.peekLast().first()))) {
if (accu.isEmpty() || (!Objects.equals(key, accu.peekLast().first()))) {
bucket = new ArrayList<>();
accu.addLast(Pair.of(key == null ? defaultKey : key, bucket));
} else {
Expand All @@ -87,6 +87,6 @@ public Function<Deque<Pair<K, List<T>>>, List<Pair<K, List<T>>>> finisher() {

@Override
public Set<Characteristics> characteristics() {
return EnumSet.of(Characteristics.IDENTITY_FINISH);
return EnumSet.noneOf(Characteristics.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.dua3.utility.lang;

import com.dua3.utility.data.Pair;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* This is a test class for the BatchCollector class.
* It aims to tests the functionality of the methods within the BatchCollector class.
*/
public class BatchCollectorTest {

/**
* This test is for testing the functionality of BatchCollector when generating grouping.
*/
@Test
public void batchCollector_groupingGenerationTest() {
BatchCollector<String, Integer> bc = new BatchCollector<>(s -> s != null ? s.length() : null);
var result = Stream.of("one", "two", "three", "four", "five", "six").collect(bc);

assertEquals(4, result.size());
assertEquals(3, result.get(0).first().intValue());
assertEquals(List.of("one","two"), result.get(0).second());
assertEquals(5, result.get(1).first().intValue());
assertEquals(List.of("three"), result.get(1).second());
assertEquals(4, result.get(2).first().intValue());
assertEquals(List.of("four","five"), result.get(2).second());
assertEquals(3, result.get(3).first().intValue());
assertEquals(List.of("six"), result.get(3).second());
}

/**
* This test is for testing the functionality of BatchCollector when generating grouping with default key.
*/
@Test
public void batchCollector_defaultKeyGroupingGenerationTest() {
BatchCollector<String, Integer> bc = new BatchCollector<>(s -> s != null ? s.length() : null, 0);
var result = Stream.of("one", "two", "three", "four", "five", "six", null).collect(bc);

assertEquals(5, result.size());
assertEquals(3, result.get(0).first().intValue());
assertEquals(List.of("one","two"), result.get(0).second());
assertEquals(5, result.get(1).first().intValue());
assertEquals(List.of("three"), result.get(1).second());
assertEquals(4, result.get(2).first().intValue());
assertEquals(List.of("four","five"), result.get(2).second());
assertEquals(3, result.get(3).first().intValue());
assertEquals(List.of("six"), result.get(3).second());
assertEquals(0, result.get(4).first().intValue());
assertEquals(Collections.singletonList(null), result.get(4).second());
}

}

0 comments on commit 9a4a3c7

Please sign in to comment.