Untitled

testGetLastItemComprehensive and testGetLastItemBasic
mail@pastecode.io avatar
unknown
java
3 years ago
12 kB
1
Indexable
Never
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Arrays;

public class ListOfListService {
    ArrayList<ArrayList<Integer>> emptyList;
	ArrayList<ArrayList<Integer>> oneRow;
	ArrayList<ArrayList<Integer>> listSameSize;
	ArrayList<ArrayList<Integer>> list1;
	ArrayList<ArrayList<Integer>> list2;
	ArrayList<ArrayList<Integer>> list3_emptySubLists;
	ArrayList<ArrayList<Integer>> list4_nullItems;

	public static double score = 0.0;
	public static String result = "";
	public static String currentMethodName = null;
	public static ArrayList<String> methodsPassed = new ArrayList<String>();
	
	
	public void createLists() {
		emptyList = new ArrayList<ArrayList<Integer>>();
		oneRow = new ArrayList<ArrayList<Integer>>();
		oneRow.add(new ArrayList<Integer>(Arrays.asList(10, 70, 20, 90)));

		listSameSize = new ArrayList<ArrayList<Integer>>();
		listSameSize.add(new ArrayList<Integer>(Arrays.asList(10, 70, 20, 90)));
		listSameSize.add(new ArrayList<Integer>(Arrays.asList(100, 700, 200, 900)));
		listSameSize.add(new ArrayList<Integer>(Arrays.asList(9, 8, 7, 6)));
		listSameSize.add(new ArrayList<Integer>(Arrays.asList(20, 22, 24, 26)));

		list1 = new ArrayList<ArrayList<Integer>>();
		list1.add(new ArrayList<Integer>(Arrays.asList(9, 8, 7, 6, 5, 4, -1, -2)));
		list1.add(new ArrayList<Integer>(Arrays.asList(100, 200, 700, 900)));
		list1.add(new ArrayList<Integer>(Arrays.asList(10, 70, 20, 90, 30, 80)));
		list1.add(new ArrayList<Integer>(Arrays.asList(20, 22)));

		list2 = new ArrayList<ArrayList<Integer>>();
		list2.add(new ArrayList<Integer>(Arrays.asList(20, 22)));
		list2.add(new ArrayList<Integer>(Arrays.asList(9, 8)));
		list2.add(new ArrayList<Integer>(Arrays.asList(100, 700, 200)));
		list2.add(new ArrayList<Integer>(Arrays.asList(10, 70)));
		list2.add(new ArrayList<Integer>(Arrays.asList(20, 90)));
		list2.add(new ArrayList<Integer>(Arrays.asList(24, 26)));

		list3_emptySubLists = new ArrayList<ArrayList<Integer>>();
		list3_emptySubLists.add(new ArrayList<Integer>());
		list3_emptySubLists.add(new ArrayList<Integer>(Arrays.asList(9, 8, 7, 6, 5, 4, -1, -2)));
		list3_emptySubLists.add(new ArrayList<Integer>(Arrays.asList(10, 70, 20, 90, 30, 80)));
		list3_emptySubLists.add(new ArrayList<Integer>());
		list3_emptySubLists.add(new ArrayList<Integer>(Arrays.asList(20, 22)));
		list3_emptySubLists.add(new ArrayList<Integer>());

		list4_nullItems = new ArrayList<ArrayList<Integer>>();
		list4_nullItems.add(null);
		list4_nullItems.add(new ArrayList<Integer>(Arrays.asList(-1, -2, -1, 4, 5, 6, 7, 8, 9)));
		list4_nullItems.add(new ArrayList<Integer>(Arrays.asList(100, 200, 700, 900)));
		list4_nullItems.add(new ArrayList<Integer>(Arrays.asList(10, null, 20, 90)));
		list4_nullItems.add(null);
		list4_nullItems.add(new ArrayList<Integer>(Arrays.asList(20, 22, null)));
		list4_nullItems.add(null);
		list4_nullItems.add(null);
		list4_nullItems.add(null);
		
		currentMethodName = null;
	}

    public void testGetFirstItemBasic() {
		assertEquals(null, ListOfListService.getFirstItem(emptyList));
		assertEquals(Integer.valueOf(10), ListOfListService.getFirstItem(oneRow));
		assertEquals(Integer.valueOf(10), ListOfListService.getFirstItem(listSameSize));
		assertEquals(Integer.valueOf(9), ListOfListService.getFirstItem(list1));
		assertEquals(Integer.valueOf(20), ListOfListService.getFirstItem(list2));
		assertEquals(null, ListOfListService.getFirstItem(list3_emptySubLists));
		String currentMethodName = new Throwable().getStackTrace()[0].getMethodName();
	}
        
    public void testGetFirstItemComprehensive() {
		testGetFirstItemBasic();
		assertEquals(null, ListOfListService.getFirstItem(null));
		assertEquals(null, ListOfListService.getFirstItem(list4_nullItems));

		list4_nullItems.set(0, new ArrayList<Integer>(Arrays.asList(5)));
		assertEquals(Integer.valueOf(5), ListOfListService.getFirstItem(list4_nullItems));
		currentMethodName = new Throwable().getStackTrace()[0].getMethodName();
	}

    public void testContainsBasic() {
		assertFalse(ListOfListService.contains(emptyList, Integer.valueOf(10)));

		assertTrue(ListOfListService.contains(oneRow, Integer.valueOf(10)));
		assertTrue(ListOfListService.contains(oneRow, Integer.valueOf(70)));
		assertTrue(ListOfListService.contains(oneRow, Integer.valueOf(20)));
		assertTrue(ListOfListService.contains(oneRow, Integer.valueOf(90)));
		assertFalse(ListOfListService.contains(oneRow, Integer.valueOf(100)));

		assertTrue(ListOfListService.contains(list1, Integer.valueOf(-2)));
		assertTrue(ListOfListService.contains(list1, Integer.valueOf(30)));
		assertTrue(ListOfListService.contains(list1, Integer.valueOf(22)));
		assertTrue(ListOfListService.contains(list1, Integer.valueOf(900)));
		assertFalse(ListOfListService.contains(list1, Integer.valueOf(-3)));
		assertFalse(ListOfListService.contains(list1, Integer.valueOf(11)));

		assertTrue(ListOfListService.contains(list3_emptySubLists, Integer.valueOf(10)));
		assertFalse(ListOfListService.contains(list3_emptySubLists, Integer.valueOf(-3)));
		currentMethodName = new Throwable().getStackTrace()[0].getMethodName();
	}

    public void testContainsComprehensive() {
		testContainsBasic();
		assertFalse(ListOfListService.contains(null, Integer.valueOf(10)));

		assertTrue(ListOfListService.contains(list4_nullItems, Integer.valueOf(22)));
		assertFalse(ListOfListService.contains(list4_nullItems, Integer.valueOf(-3)));
		currentMethodName = new Throwable().getStackTrace()[0].getMethodName();
	}
    public void testGetLastItemBasic() {
		assertEquals(null, ListOfListService.getLastItem(emptyList));
		assertEquals(Integer.valueOf(26), ListOfListService.getLastItem(listSameSize));
		assertEquals(Integer.valueOf(90), ListOfListService.getLastItem(oneRow));
		assertEquals(Integer.valueOf(22), ListOfListService.getLastItem(list1));
		assertEquals(Integer.valueOf(26), ListOfListService.getLastItem(list2));
		assertEquals(Integer.valueOf(22), ListOfListService.getLastItem(list3_emptySubLists));
		currentMethodName = new Throwable().getStackTrace()[0].getMethodName();
	}

	
	public void testGetLastItemComprehensive() {
		testGetLastItemBasic();
		assertEquals(null, ListOfListService.getLastItem(null));
		assertEquals(null, ListOfListService.getLastItem(list4_nullItems));
		list4_nullItems.get(5).set(2, 24);
		assertEquals(Integer.valueOf(24), ListOfListService.getLastItem(list4_nullItems));

		ArrayList<ArrayList<Integer>> allEmptyOrNull = new ArrayList<ArrayList<Integer>>();
		allEmptyOrNull.add(null);
		allEmptyOrNull.add(new ArrayList<Integer>());
		allEmptyOrNull.add(null);
		allEmptyOrNull.add(new ArrayList<Integer>());
		assertEquals(null, ListOfListService.getLastItem(allEmptyOrNull));
		currentMethodName = new Throwable().getStackTrace()[0].getMethodName();
	}
    private static Integer getLastItem(ArrayList<ArrayList<Integer>> list) {
        if(list == null || list.size() == 0){
            return null;
        }
      
        for(int i = list.size(); i >0 ; i-- ){
            if(list.get(i-1) != null && list.get(i-1).size() != 0){
            
                int innerSize = list.get(i-1).size();
				
				if(innerSize != 0)
                return list.get(i-1).get(innerSize-1); 
            }
        }
        
        return null;
    }

    public static void main(String[] args) {
        ListOfListService listOfListService = new ListOfListService();

        listOfListService.createLists();
        listOfListService.testGetFirstItemComprehensive();
        listOfListService.testContainsComprehensive();
        listOfListService.testGetItemComprehensive();
        listOfListService.testGetLastItemComprehensive();
    }
    public static boolean contains(ArrayList<ArrayList<Integer>> list, Integer target) {
        if (list == null || list.size() == 0)
            return false;
     
        // iterate through every sublist of list
        for (int i = 0; i < list.size(); i++) {
            // checking wheter sublist contain a target or not if yes return true
            if (list.get(i) != null  && list.get(i).contains(target))
                return true;

        }
        return false;

    }

    public static Integer getItem(ArrayList<ArrayList<Integer>> list, int i, int k) {
        if (list == null || list.size() == 0 || i < 0 || k < 0 )
            return null;
        if (i < list.size() && list.get(i) != null  && k <list.get(i).size()) {
            return list.get(i).get(k);
        }
        return null;
    }

    public static boolean isSubListNotNull(ArrayList<ArrayList<Integer>> list, int i) {
        if (list == null || list.size() == 0 || list.get(0) == null )
        return false;
        // checking boundary
        if (i >= 0 && i < list.size()) {
            // taking sublist from list and testing its size
            ArrayList<Integer> arrayList = list.get(i);
            if (arrayList != null) {
                return true;
            }
        }
        return false;

    }

    public static Integer getFirstItem(ArrayList<ArrayList<Integer>> list) {
        if (list == null || list.size() == 0 || list.get(0) == null )
            return null;
       
        // checking boundary
        if (list.size() == 0 || list.get(0).size() == 0) {
            return null;
        }
        // returning first element
        return list.get(0).get(0);

    }
    public void testGetItemBasic() {
		assertEquals(null, ListOfListService.getItem(emptyList, 0, 2));

		assertEquals(Integer.valueOf(10), ListOfListService.getItem(listSameSize, 0, 0));
		assertEquals(Integer.valueOf(90), ListOfListService.getItem(listSameSize, 0, 3));
		assertEquals(Integer.valueOf(22), ListOfListService.getItem(listSameSize, 3, 1));

		assertEquals(Integer.valueOf(10), ListOfListService.getItem(oneRow, 0, 0));
		assertEquals(Integer.valueOf(90), ListOfListService.getItem(oneRow, 0, 3));
		assertEquals(null, ListOfListService.getItem(oneRow, 0, 4));
		assertEquals(null, ListOfListService.getItem(oneRow, 0, -1));
		assertEquals(null, ListOfListService.getItem(oneRow, -1, 0));
		assertEquals(null, ListOfListService.getItem(oneRow, 1, 0));

		assertEquals(Integer.valueOf(20), ListOfListService.getItem(list1, 3, 0));
		assertEquals(Integer.valueOf(70), ListOfListService.getItem(list1, 2, 1));
		assertEquals(Integer.valueOf(-2), ListOfListService.getItem(list1, 0, 7));

		assertEquals(Integer.valueOf(26), ListOfListService.getItem(list2, 5, 1));
		assertEquals(null, ListOfListService.getItem(list2, 6, 1));

		assertEquals(null, ListOfListService.getItem(list3_emptySubLists, 3, 0));
		assertEquals(Integer.valueOf(20), ListOfListService.getItem(list3_emptySubLists, 2, 2));
		currentMethodName = new Throwable().getStackTrace()[0].getMethodName();
	}

    public void testGetItemComprehensive() {
		testGetItemBasic();
		assertEquals(null, ListOfListService.getItem(null, 0, 0));

		assertEquals(null, ListOfListService.getItem(list4_nullItems, 4, 4));
		assertEquals(null, ListOfListService.getItem(list4_nullItems, 3, 1));
		assertEquals(Integer.valueOf(20), ListOfListService.getItem(list4_nullItems, 3, 2));
		currentMethodName = new Throwable().getStackTrace()[0].getMethodName();
	}
 
}