dimanche 26 août 2018

Junit test on an ArrayList of ArrayLists

I am a beginner in Java I'm trying to learn by practice and I found this exercise that has as a purpose to partition a list to sublists of n size the method partition take in argument (ArrayList, size)

For example:
partition([1,2,3,4,],2) will return ([1,2],[3,4])
partition([1,2,3,4,],3) will return ([1,2,3],[4])

package partition;
import java.util.ArrayList;
import java.util.List;

public class Partition {


    public ArrayList<ArrayList> partition(List<Integer> li, int n) {
        ArrayList<ArrayList> al = new ArrayList();
        int start = 0;
        int i=n;
        for(; i<li.size(); i+=n){
            List<Integer> lis = li.subList(start, i);
            ArrayList<Integer> list = new ArrayList<>();
            list.addAll(lis);
            al.add(list);
            start = i;
        }

        if(i >= li.size()){
            List<Integer> lis = li.subList(start, li.size());
            ArrayList<Integer> list = new ArrayList<>();
            list.addAll(lis);
            al.add(list);
        }
        return al;
    }
}

I want to write a Junit test to test all cases. I'm trying to read documentation on how to use Junit but I'm finding some difficulties to do it for this case. Can someone help me please by giving some indication or an example which looks like this one so I can test all the cases.

Aucun commentaire:

Enregistrer un commentaire