/* Java application that is for the purpose of testing the methods in ** the RecListLabUtils class. */ public class RecListLabUtilsTester { private static RecListLabUtils rllu = new RecListLabUtils(); public static void main(String[] args) { testNumOccur(); System.out.println(); testRemoveFirst(); System.out.println(); testRemoveAll(); System.out.println(); testPrefixOf(); System.out.println(); testSuffixOf(); System.out.println(); testIsAscending(); System.out.println(); testOrderedInsert(); } /* Tests the numOccur() method in the RecListLabUtils class. */ public static void testNumOccur() { System.out.println("Testing the numOccur() method..."); Integer[] nums = new Integer[] { 3, 5, 4, 6, 3, 5, 7, 1, 3, 6, 5 }; RecursiveList list = rllu.listify(nums); System.out.printf("With respect to the list %s:\n", omitBrackets(list.toString())); for (int i = 0; i != 9; i++) { System.out.printf(" numOccur(list,%d) yields %d\n", i, rllu.numOccur(list,i)); //System.out.printf(" %d occurs %d times.\n", i, rllu.numOccur(list, i)); } } /* Tests the removeFirst() method in the RecListLabUtils class. */ public static void testRemoveFirst() { System.out.println("Testing the removeFirst() method..."); Integer[] nums = new Integer[] { 3, 5, 4, 6, 3, 5, 7, 1, 3, 6, 5 }; RecursiveList list = rllu.listify(nums); System.out.printf("With respect to the list %s:\n", omitBrackets(list.toString())); for (int i = 1; i != 9; i++) { System.out.printf(" removeFirst(list,%d) yields %s\n", i, omitBrackets(rllu.removeFirst(list,i).toString())); //System.out.printf(" Result of removing first occurrence of %d is %s.\n", // i, omitBrackets(rllu.removeFirst(list,i).toString())); } } /* Tests the removeAll() method in the RecListLabUtils class. */ public static void testRemoveAll() { System.out.println("Testing the removeAll() method..."); Integer[] nums = new Integer[] { 3, 5, 4, 6, 3, 5, 7, 1, 3, 6, 5 }; RecursiveList list = rllu.listify(nums); System.out.printf("With respect to the list %s:\n", omitBrackets(list.toString())); for (int i = 1; i != 9; i++) { System.out.printf(" removeAll(list,%d) yields %s\n", i, omitBrackets(rllu.removeAll(list,i).toString())); //System.out.printf(" Result of removing all occurrences of %d is %s.\n", // i, omitBrackets(rllu.removeAll(list,i).toString())); } } /* Tests the prefixOf() method in the RecListLabUtils class. */ public static void testPrefixOf() { System.out.println("Testing the prefixOf() method..."); Integer[] nums = new Integer[] { 3, 7, 4, 9, 6 }; RecursiveList list = rllu.listify(nums); System.out.printf("With respect to the list %s:\n", omitBrackets(list.toString())); for (int i = 0; i != nums.length + 2; i++) { System.out.printf(" prefixOf(list,%d) is %s.\n", i, omitBrackets(rllu.prefixOf(list,i).toString())); } } /* Tests the suffixOf() method in the RecListLabUtils class. */ public static void testSuffixOf() { System.out.println("Testing the suffixOf() method..."); Integer[] nums = new Integer[] { 3, 7, 4, 9, 6 }; RecursiveList list = rllu.listify(nums); System.out.printf("With respect to the list %s:\n", omitBrackets(list.toString())); for (int i = 0; i != nums.length + 2; i++) { System.out.printf(" suffixOf(list,%d) is %s.\n", i, omitBrackets(rllu.suffixOf(list,i).toString())); } } /* Tests the isAscending() method in the RecListLabUtils class. */ public static void testIsAscending() { System.out.println("Testing the isAscending() method..."); Integer[][] nums = new Integer[3][]; nums[0] = new Integer[] { 3, 5, 4, 6, 3, 5, 7, 1, 3, 6, 5 }; nums[1] = new Integer[] { 1, 3, 4, 6, 7 }; nums[2] = new Integer[] { 1, 3, 3, 3, 6, 7 }; for (int i = 0; i != nums.length; i++) { RecursiveList list = rllu.listify(nums[i]); System.out.printf("isAscending(%s) is %b\n", omitBrackets(list.toString()), rllu.isAscending(list)); } } /* Tests the orderedInsert() method in the RecListLabUtils class. */ public static void testOrderedInsert() { System.out.println("Testing the orderedInsert() method..."); Integer[] nums = new Integer[] { 1, 2, 5, 7, 8 }; RecursiveList list = rllu.listify(nums); System.out.printf("With respect to the list %s:\n", omitBrackets(list.toString())); for (int i = 0; i <= 10; i = i+2) { System.out.printf(" orderedInsert(list,%d) is %s.\n", i, omitBrackets(rllu.orderedInsert(list,i).toString())); } } /* Erases all but the first and last square brackets from the given string. ** In this context, it is for the purpose of displaying recursive lists ** without brackets cluttering up the works. */ private static String omitBrackets(String s) { String middle = s.substring(1,s.length()-1).replace("[", "").replace("]",""); return s.charAt(0) + middle + s.charAt(s.length()-1); } }