/* ITI 1121. Introduction to Computing II; Laboratory 3 * ITI 1521. Introduction a l'informatique II; Laboratoire 3 */ /** A series of tests for the method findAndReplace. * * @author Marcel Turcotte (turcotte@site.uottawa.ca) */ public class TestFindAndReplace { public static void main(String[] args){ boolean testPassed = (testInIsNull() && testQueryIsNull() && testReplacementIsNull() && testInAndQueryAreNull() && testInAndReplacementreNull() && testQueryAndReplacementreNull() && testAllNull() && testNotSameLength() && testNullInIn() && testNullInQuery() && testNullInReplacement() && testNoChange1() && testNoChange2() && testChangeFirst1() && testChangeFirst2() && testChangeLast() && testChangeLeft() && testMultipleChanges1() && testMultipleChanges2()); if (testPassed){ System.out.println("All Test passed: Success!"); } else{ System.out.println("Failure"); } } public static boolean testInIsNull() { String[] query = { "I" }; String[] replacement = { "You" }; if(Utils.findAndReplace( null, query, replacement ) == null){ return true; } else{ System.out.println("test 'testInIsNull' Failed"); return false; } } public static boolean testQueryIsNull() { String[] text = { "I", "understand" }; String[] replacement = { "You" }; if(Utils.findAndReplace( text, null, replacement ) == null){ return true; } else{ System.out.println("test 'testQueryIsNull' Failed"); return false; } } public static boolean testReplacementIsNull() { String[] text = { "I", "understand" }; String[] query = { "I" }; if(Utils.findAndReplace( text, query, null ) == null){ return true; } else{ System.out.println("test 'testReplacementIsNull' Failed"); return false; } } public static boolean testInAndQueryAreNull() { String[] replacement = { "You" }; if(Utils.findAndReplace( null, null, replacement ) == null){ return true; } else{ System.out.println("test 'testInAndQueryAreNull' Failed"); return false; } } public static boolean testInAndReplacementreNull() { String[] query = { "I" }; if(Utils.findAndReplace( null, query, null ) == null){ return true; } else{ System.out.println("test 'testInAndReplacementreNull' Failed"); return false; } } public static boolean testQueryAndReplacementreNull() { String[] text = { "I", "understand" }; if(Utils.findAndReplace( text, null, null ) == null){ return true; } else{ System.out.println("test 'testQueryAndReplacementreNull' Failed"); return false; } } public static boolean testAllNull() { if(Utils.findAndReplace( null, null, null ) == null){ return true; } else{ System.out.println("test 'testAllNull' Failed"); return false; } } public static boolean testNotSameLength() { String[] text = { "I", "understand" }; String[] query = { "I" }; String[] replacement = { "You", "They" }; if(Utils.findAndReplace( text, query, replacement ) == null){ return true; } else{ System.out.println("test 'testNotSameLength' Failed"); return false; } } public static boolean testNullInIn() { String[] text = { "I", null }; String[] query = { "I" }; String[] replacement = { "You" }; if(Utils.findAndReplace( text, query, replacement ) == null){ return true; } else{ System.out.println("test 'testNullInIn' Failed"); return false; } } public static boolean testNullInQuery() { String[] text = { "I", "understand" }; String[] query = { "I", null }; String[] replacement = { "You", "They" }; if(Utils.findAndReplace( text, query, replacement ) == null){ return true; } else{ System.out.println("test 'testNullInQuery' Failed"); return false; } } public static boolean testNullInReplacement() { String[] text = { "I", "understand" }; String[] query = { "I", "We" }; String[] replacement = { null, "They" }; if(Utils.findAndReplace( text, query, replacement ) == null){ return true; } else{ System.out.println("test 'testNullInReplacement' Failed"); return false; } } public static boolean testNoChange1() { String[] text = { "I", "understand" }; String[] query = { }; String[] replacement = { }; String[] result = Utils.findAndReplace( text, query, replacement ); boolean flag = true; if (result == null){ flag = false; } if (result == text){ flag = false; } if (text.length != result.length){ flag = false; } for ( int i=0; i