// This program tests stage 1 of the LetterInventory class. import java.util.*; import java.io.*; public class Test1 { public static String[] STRINGS = new String[] {"abc", "abcdefghijklmnopqrstuvwxyz", "hello", "Hello", "how are you?", "42", "ZZZ", "Do ALL CAPS bother you--when you read??", "xyzzy", ",,;^&@0123456789 --!!??", "", " ", "foo BARbaz", "The.Rain.In.Spain.falls.MAINLY.in.THE.plain--!"}; public static void main(String[] args) { LetterInventory[] inventories = new LetterInventory[STRINGS.length]; testConstructor(inventories, STRINGS); testSize(inventories, STRINGS); testIsEmpty(inventories, STRINGS); testGet(inventories, STRINGS); testToString(inventories, STRINGS); } // pre : arrays have been initalized with correct size // post: inventories and strings arrays populated if no error encountered public static void testConstructor(LetterInventory[] inventories, String[] strings) { System.out.println("Testing constructor..."); for (int i = 0; i < strings.length; i++) { System.out.print(" \"" + strings[i] + "\""); if (i != strings.length - 1) { System.out.print(","); } if (i % 4 == 0 && i != 0) { System.out.println(); } try { inventories[i] = new LetterInventory(strings[i]); } catch (Exception e) { System.out.println("...failed"); System.out.println(" threw exception: " + e); int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" in LetterInventory line#" + line); System.exit(1); } } System.out.println(); System.out.println(); } // pre : inventories and strings contain a series of test cases; // post: prints result of testing, exiting the program if an error is seen public static void testSize(LetterInventory[] inventories, String[] strings) { System.out.println("Testing size..."); for (int i = 0; i < strings.length; i++) { System.out.print(" testing \"" + strings[i] + "\""); int test = 0; try { test = inventories[i].size(); } catch (Exception e) { System.out.println("...failed"); System.out.println(" threw exception: " + e); int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" in LetterInventory line#" + line); System.exit(1); } System.out.println(" .size() = " + test); } System.out.println(); } // pre : inventories and strings contain a series of test cases; // post: prints result of testing, exiting the program if an error is seen public static void testToString(LetterInventory[] inventories, String[] strings) { System.out.println("Testing toString..."); for (int i = 0; i < strings.length; i++) { System.out.print(" testing \"" + strings[i] + "\""); String test = ""; try { test = inventories[i].toString(); } catch (Exception e) { System.out.println("...failed"); System.out.println(" threw exception: " + e); int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" in LetterInventory line#" + line); System.exit(1); } System.out.println(test); } System.out.println(); } // pre : inventories and strings contain a series of test cases; // post: prints result of testing, exiting the program if an error is seen public static void testGet(LetterInventory[] inventories, String[] strings) { System.out.println("Testing get..."); for (int i = 0; i < strings.length; i++) { System.out.println(" testing \"" + strings[i] + "\""); for (char ch = 'a'; ch <= 'z'; ch++) { // int correct = input.nextInt(); if ((ch - 'a') % 3 == 0 && ch != 'a') { System.out.println(); } testLetter(ch, inventories[i], strings[i]); testLetter(Character.toUpperCase(ch), inventories[i], strings[i]); } System.out.println(""); System.out.println(""); } System.out.println(); } // post: tests whether a call on get for the given character returns the // given count, exiting the program if an error is seen public static void testLetter(char ch, LetterInventory inventory, String s) { int test = 0; try { test = inventory.get(ch); } catch (Exception e) { System.out.println("...failed for get on '" + ch + "'"); System.out.println(" threw exception: " + e); int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" in LetterInventory line#" + line); System.exit(1); } System.out.print(" .get('" + ch + "')=" + test); } // pre : inventories and strings contain a series of test cases; // post: prints result of testing, exiting the program if an error is seen public static void testIsEmpty(LetterInventory[] inventories, String[] strings) { System.out.println("Testing isEmpty..."); for (int i = 0; i < strings.length; i++) { System.out.print(" testing \"" + strings[i] + "\""); boolean test = false; try { test = inventories[i].isEmpty(); } catch (Exception e) { System.out.println("...failed"); System.out.println(" threw exception: " + e); int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" in LetterInventory line#" + line); System.exit(1); } System.out.println(" .isEmpty() = " + test); } System.out.println(); } }