// CSE 143, Winter 2011, Marty Stepp // This JUnit test program checks the functionality of our isPalindrome method. import static org.junit.Assert.*; import org.junit.*; public class PalindromesTest { // Returns true if the given string reads the same forwards as backwards. public static boolean isPalindrome(String s) { if (s.length() <= 1) { return true; } else { // > 1 letter s = s.toLowerCase(); char first = s.charAt(0); char last = s.charAt(s.length() - 1); if (first == last) { String middle = s.substring(1, s.length() - 1); return isPalindrome(middle); } else { return false; } } // return s.length() <= 1 || (s.charAt(0) == s.charAt(s.length() - 1) // && isPalindrome(s.substring(1, s.length() - 1))); } @Test public void test1() { helper("Madam", true); } @Test public void test2() { helper("racecar", true); } @Test public void test3() { helper("bob lol bob", true); } @Test public void test4() { helper("able was I ere I saw elba", true); } @Test public void test5() { helper("Java", false); } @Test public void test6() { helper("rotater", false); } @Test public void test7() { helper("byebye", false); } @Test public void test8() { helper("notion", false); } private void helper(String s, boolean expect) { assertEquals("isPalindrome(\"" + s + "\")", expect, isPalindrome(s)); } }