import java.util.Arrays; /* PLC_Utils_Tester.java ** Java application having as its purpose to test the methods of the ** PL_With_C_Utils class (and, indirectly, those in the underlying classes ** that it uses). ** ** Author: R. McCloskey ** Date: November 2023 (updated from Nov. 2018) */ public class PLC_Utils_Tester { private static final String TERMINATOR = " "; // printed after each list item public static void main(String[] args) { String[] strAry = new String[] { "PIG", "DOG", "DOG", "DOG", "CAT", "PIG", "ELK", "MOOSE", "T-REX", "ELK", "ELK", "COW", "DOG", "BEAR" }; System.out.println("PLC_Utils_Tester Program starting execution...\n"); System.out.print("The array to be listified:\n "); System.out.println(Arrays.toString(strAry)); PL_With_C list = PL_With_C_Utils.listify(strAry); System.out.print("\nAfter listifying, the list is\n "); PL_With_C_Utils.printList(list, TERMINATOR); System.out.println(); // tests the lengthOf() method. lengthOfTest(list); // test the find() method by looking for various animals findTest(list, "PIG"); findTest(list, "CAT"); findTest(list, "COW"); findTest(list, "LION"); findTest(list, "BEAR"); /* // test the removeAdjacentDuplicates() method removeAdjDupsTest(list); */ // test the insertNonDuplicate() method insertNonDupTest(list, "GORN"); // should insert insertNonDupTest(list, "CAT"); // should not, as CAT is a duplicate // test the removeDuplicates() method removeDupTest(list); // test the reverse() method reverseTest(list); System.out.println("\nGoodbye from PLC_Utils_Tester"); } /* Reports the result of calling the lengthOf() method on the given list. */ public static void lengthOfTest(PL_With_C list) { System.out.println("The length of the list is " + PL_With_C_Utils.lengthOf(list)); } /* For testing the find() method in the PL_With_C_Utils class. */ private static void findTest(PL_With_C list, String str) { System.out.println("\nTrying to find " + str + " ..."); try { PLC cursor = PL_With_C_Utils.find(list, str); if (cursor.atRear()) { System.out.println("Not found."); } else if (str.equals(cursor.getItem())) { if (cursor.atFront()) { System.out.println("Found at front of list."); } else { System.out.println("Found at node whose predecessor contains " + cursor.toPrev().getItem()); } } else { // cursor is at node containing item different from str System.out.println("ERROR: found at node containing " + cursor.getItem()); } cursor.dispose(); } catch (Exception e) { e.printStackTrace(System.out); } } /* Invokes the removeAdjacentDuplicates() method upon the given list ** and displays the updated list. */ public static void removeAdjDupsTest(PL_With_C list) { System.out.print("\nRemoving adjacent duplicates from the list\n "); PL_With_C_Utils.printList(list, TERMINATOR); System.out.print("\nAfterwards, the list contains ...\n "); PL_With_C_Utils.removeAdjacentDuplicates(list); PL_With_C_Utils.printList(list, TERMINATOR); } /* Tries to insert the given String into the given list using the ** insertNotDuplicate() method, reporting upon the result thereafter. */ public static void insertNonDupTest(PL_With_C list, String elem) { System.out.printf("\nInserting (if not a duplicate) %s ...\n", elem); try { PL_With_C_Utils.insertNonDuplicate(list, elem); } catch (Exception e) { e.printStackTrace(System.out); } System.out.print("Afterwards, the list contains\n "); PL_With_C_Utils.printList(list, TERMINATOR); } /* Invokes the removeDuplicates() method upon the given list ** and displays the updated list. */ public static void removeDupTest(PL_With_C list) { System.out.println("\nRemoving all duplicates ..."); try { PL_With_C_Utils.removeDuplicates(list); } catch (Exception e) { e.printStackTrace(System.out); } System.out.print("Afterwards, the list contains\n "); PL_With_C_Utils.printList(list, TERMINATOR); } /* Invokes the reverse() method upon the given list ** and displays the updated list. */ public static void reverseTest(PL_With_C list) { System.out.println("\nReversing the list ..."); try { PL_With_C_Utils.reverse(list); } catch (Exception e) { e.printStackTrace(System.out); } System.out.print("Afterwards, the list contains\n "); PL_With_C_Utils.printList(list, TERMINATOR); } }