-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromeCheckerTests.java
More file actions
114 lines (103 loc) · 4.08 KB
/
PalindromeCheckerTests.java
File metadata and controls
114 lines (103 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package module4.recursion.palindromes;
import java.util.HashMap;
import java.util.Map;
/**
* An emoji-rich console-based app which iterates over a map of palindromes, and determines
* whether the isPalindrome() method returns the expected result.
* @author Mae Morella
*/
public class PalindromeCheckerTests {
private final static HashMap<String, Boolean> WORD_TESTS = new HashMap<>(Map.of(
// test string –– expected value of isPalindrome()
"", true,
"a", true,
"abca", false,
"abba", true,
"aloha ahola", true,
"atlanta", false,
"toy boat", false,
"bananab", true
));
private final static HashMap<String, Boolean> SENTENCE_TESTS = new HashMap<>(Map.of(
// test string –– expected value
"Able was I, ere I saw Elba", true,
"Hello world", false,
"Madam, I'm Adam.", true,
"This isn't a palindrome", false,
"No 'x' in Nixon?", true,
"This sentence is not a palindrome.", false,
"Was it a car or a cat I saw?", true,
"Woh! OwO OwO. How?", true,
"NOTICE: This test is expected to fail", true
));
public static boolean palindromeTest(String palindrome, boolean expectedValue) {
boolean isPalindrome, testPassed;
try {
isPalindrome = PalindromeChecker.isPalindrome(palindrome);
testPassed = (isPalindrome == expectedValue);
} catch (Exception e) {
e.printStackTrace();
return false;
}
if (!testPassed) {
System.out.print("❌ ");
} else {
System.out.print(isPalindrome ? "✅ " : "❎ ");
}
System.out.print("\"" + palindrome + "\" -> " + (isPalindrome ? "TRUE" : "FALSE") + "; ");
System.out.println("expected " + (expectedValue ? "TRUE" : "FALSE"));
return testPassed;
}
public static void runTests(HashMap<String, Boolean> tests) {
int testCount = 0;
int passCount = 0;
for (Map.Entry<String, Boolean> t : tests.entrySet()) {
testCount++;
String str = t.getKey();
boolean expectedValue = t.getValue();
boolean pass = palindromeTest(str, expectedValue);
if (pass) {
passCount++;
}
}
System.out.println(passCount + " out of " + testCount + " tests passed\n");
}
public static void main(String[] args) {
System.out.println("=== LEGEND ===\n"
+ "✅ — \"test passed, palindrome detected as expected\"\n"
+ "❎ — \"test passed, no palindrome detected, as expected\"\n"
+ "❌ — \"test failed! isPalindrome did not return expected value\"\n");
System.out.println();
System.out.println("=== PALINDROME TEST #1 (SIMPLE WORDS) ===");
runTests(WORD_TESTS);
System.out.println("=== PALINDROME TEST #2 (COMPLEX SENTENCES) ===");
runTests(SENTENCE_TESTS);
}
}
/* CONSOLE OUTPUT:
=== LEGEND ===
✅ — "test passed, palindrome detected as expected"
❎ — "test passed, no palindrome detected, as expected"
❌ — "test failed! isPalindrome did not return expected value"
=== PALINDROME TEST #1 (SIMPLE WORDS) ===
✅ "" -> TRUE; expected TRUE
✅ "a" -> TRUE; expected TRUE
❎ "abca" -> FALSE; expected FALSE
✅ "aloha ahola" -> TRUE; expected TRUE
❎ "toy boat" -> FALSE; expected FALSE
✅ "bananab" -> TRUE; expected TRUE
❎ "atlanta" -> FALSE; expected FALSE
✅ "abba" -> TRUE; expected TRUE
8 out of 8 tests passed
=== PALINDROME TEST #2 (COMPLEX SENTENCES) ===
✅ "Able was I, ere I saw Elba" -> TRUE; expected TRUE
❌ "NOTICE: This test is expected to fail" -> FALSE; expected TRUE
✅ "No 'x' in Nixon?" -> TRUE; expected TRUE
✅ "Was it a car or a cat I saw?" -> TRUE; expected TRUE
❎ "This sentence is not a palindrome." -> FALSE; expected FALSE
✅ "Madam, I'm Adam." -> TRUE; expected TRUE
❎ "This isn't a palindrome" -> FALSE; expected FALSE
✅ "Woh! OwO OwO. How?" -> TRUE; expected TRUE
❎ "Hello world" -> FALSE; expected FALSE
8 out of 9 tests passed
*/