Untitled

testIsAscendingComprehensive & testIsAscendingBasic both working
mail@pastecode.io avatar
unknown
java
3 years ago
5.5 kB
7
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;
import java.util.ListIterator;


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 static void main(String[] args) {
        ListOfListService listOfListService = new ListOfListService();

        listOfListService.createLists();
       
		listOfListService.testIsAscendingComprehensive();
    }

	@Before
	public void testIsAscendingBasic() {
		assertFalse(ListOfListService.isAscending(emptyList, 0));

		assertFalse(ListOfListService.isAscending(oneRow, 0));
		assertFalse(ListOfListService.isAscending(oneRow, 1));
		assertFalse(ListOfListService.isAscending(oneRow, -1));

		assertTrue(ListOfListService.isAscending(list2, 0));
		assertFalse(ListOfListService.isAscending(list2, 1));
		assertFalse(ListOfListService.isAscending(list2, 2));
		assertTrue(ListOfListService.isAscending(list2, 3));
		assertTrue(ListOfListService.isAscending(list2, 4));
		assertTrue(ListOfListService.isAscending(list2, 5));

		list2.get(4).set(1, 20); // 20, 20
		assertTrue(ListOfListService.isAscending(list2, 4));

		assertTrue(ListOfListService.isAscending(list1, 1));

		assertTrue(ListOfListService.isAscending(list3_emptySubLists, 0)); // empty sub-list is ascending
		assertFalse(ListOfListService.isAscending(list3_emptySubLists, 1));
		currentMethodName = new Throwable().getStackTrace()[0].getMethodName();
	}

	
	public void testIsAscendingComprehensive() {
		testIsAscendingBasic();
		assertFalse(ListOfListService.isAscending(null, 0));

		assertFalse(ListOfListService.isAscending(list4_nullItems, 0));
		assertTrue(ListOfListService.isAscending(list4_nullItems, 2));
		assertFalse(ListOfListService.isAscending(list4_nullItems, 3));
		assertFalse(ListOfListService.isAscending(list4_nullItems, 5));
		currentMethodName = new Throwable().getStackTrace()[0].getMethodName();
	}


   	public static boolean isAscending(ArrayList<ArrayList<Integer>> list, int i) {

		if(i<0 || list==null || i>=list.size() || list.get(i) == null || list.get(i).contains(null)){
			return false;
		}
		
	    else if( list.get(i) == null){
            return false;
		}
		else if (list.get(i).size() < 2) {
		        return true;
		   
		}else {
			for (int j = 1; j < list.get(i).size(); j++) {

				if(list.get(i).get(j-1)>list.get(i).get(j)){
					return false;
				}
					
			}
			if(list.get(i).get(list.get(i).size()-1) == null){
				return false;
			}
			return true;
		}

	  }
 
}