mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add test
This commit is contained in:
parent
a64155e0cc
commit
34dc797d23
@ -42,6 +42,10 @@ public class Partition<T> extends AbstractList<List<T>> {
|
||||
public int size() {
|
||||
// 此处采用动态计算,以应对list变
|
||||
final int size = this.size;
|
||||
if(0 == size){
|
||||
return 0;
|
||||
}
|
||||
|
||||
final int total = list.size();
|
||||
// 类似于判断余数,当总数非整份size时,多余的数>=1,则相当于被除数多一个size,做到+1目的
|
||||
// 类似于:if(total % size > 0){length += 1;}
|
||||
|
@ -0,0 +1,6 @@
|
||||
/**
|
||||
* 列表分区或分段
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
package cn.hutool.core.collection.partition;
|
@ -0,0 +1,56 @@
|
||||
package cn.hutool.core.collection.partition;
|
||||
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PartitionTest {
|
||||
@Test
|
||||
public void sizeTest() {
|
||||
final ArrayList<Integer> list = ListUtil.of(1, 2, 3, 4, 5);
|
||||
final Partition<Integer> partition = new Partition<>(list, 4);
|
||||
Assert.assertEquals(2, partition.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSizeTest() {
|
||||
List<Integer> mockedList = makingList(19);
|
||||
Partition<Integer> partition = new Partition<>(mockedList, 10);
|
||||
Assert.assertEquals(2, partition.size());
|
||||
|
||||
mockedList = makingList(11);
|
||||
partition = new Partition<>(mockedList, 10);
|
||||
Assert.assertEquals(2, partition.size());
|
||||
|
||||
mockedList = makingList(10);
|
||||
partition = new Partition<>(mockedList, 10);
|
||||
Assert.assertEquals(1, partition.size());
|
||||
|
||||
mockedList = makingList(9);
|
||||
partition = new Partition<>(mockedList, 10);
|
||||
Assert.assertEquals(1, partition.size());
|
||||
|
||||
mockedList = makingList(5);
|
||||
partition = new Partition<>(mockedList, 10);
|
||||
Assert.assertEquals(1, partition.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getZeroSizeTest() {
|
||||
final List<Integer> mockedList = makingList(0);
|
||||
final Partition<Integer> partition = new Partition<>(mockedList, 10);
|
||||
Assert.assertEquals(0, partition.size());
|
||||
}
|
||||
|
||||
private List<Integer> makingList(final int length) {
|
||||
final List<Integer> list = ListUtil.of();
|
||||
for (int i = 0; i < length; i++) {
|
||||
list.add(i);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user