170 lines
4.9 KiB
Java
170 lines
4.9 KiB
Java
|
import org.junit.Test;
|
||
|
|
||
|
import static junit.framework.TestCase.assertFalse;
|
||
|
import static junit.framework.TestCase.assertTrue;
|
||
|
|
||
|
/**
|
||
|
* Generic test class for implementing and testing palindrome detection
|
||
|
* _ _ _ _
|
||
|
* __ ___ __(_) |_| |_ ___ _ __ | |__ _ _
|
||
|
* \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | |
|
||
|
* \ V V /| | | | |_| || __/ | | | | |_) | |_| |
|
||
|
* \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, |
|
||
|
* |___/
|
||
|
* ____ __ __ _
|
||
|
* / ___|_ _____ _ __ \ \ / /__ __ _ ___| |
|
||
|
* \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ |
|
||
|
* ___) \ V / __/ | | | \ V / (_) | (_| | __/ |
|
||
|
* |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_|
|
||
|
* |___/
|
||
|
* Licensed under the GPLv2 License, Version 2.0 (the "License");
|
||
|
* Copyright (c) Sven Vogel
|
||
|
*/
|
||
|
public class Aufgabe7 {
|
||
|
|
||
|
/**
|
||
|
* Tests whether the supplied string is a palindrome or not using iterations
|
||
|
*
|
||
|
* @implNote returns true if the supplied string is smaller than 2 or blank
|
||
|
* @param text the string to test
|
||
|
* @return true if the supplied string is a palindrome, false otherwise
|
||
|
*/
|
||
|
private static boolean isPalindrome(String text) {
|
||
|
if (text.isBlank() || text.length() < 2)
|
||
|
return true;
|
||
|
|
||
|
for (int i = 0; i < text.length() / 2; i++) {
|
||
|
if (text.charAt(i) != text.charAt(text.length() - i - 1))
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Tests whether the supplied string is a palindrome or not using recursion
|
||
|
*
|
||
|
* @implNote returns true if the supplied string is smaller than 2 or blank
|
||
|
* @param text the string to test
|
||
|
* @return true if the supplied string is a palindrome, false otherwise
|
||
|
*/
|
||
|
private static boolean isPalindromeRecursive(String text) {
|
||
|
if (text.isBlank() || text.length() < 2)
|
||
|
return true;
|
||
|
|
||
|
if (text.charAt(0) != text.charAt(text.length() - 1))
|
||
|
return false;
|
||
|
|
||
|
return isPalindromeRecursive(text.substring(1, text.length() - 1));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Tests whether the supplied string is a palindrome or not by iteration
|
||
|
* also ignores case
|
||
|
*
|
||
|
* @implNote returns true if the supplied string is smaller than 2 or blank
|
||
|
* @param text the string to test
|
||
|
* @return true if the supplied string is a palindrome, false otherwise
|
||
|
*/
|
||
|
private static boolean isPalindromeIgnoreCase(String text) {
|
||
|
if (text.isBlank() || text.length() < 2)
|
||
|
return true;
|
||
|
|
||
|
var lowerCase = text.toLowerCase();
|
||
|
|
||
|
for (int i = 0; i < text.length() / 2; i++) {
|
||
|
if (lowerCase.charAt(i) != lowerCase.charAt(text.length() - i - 1))
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Test iterative palindrome detection
|
||
|
*/
|
||
|
@Test
|
||
|
public void testLowercaseIterative() {
|
||
|
var PALINDROMES = new String[]{
|
||
|
"otto",
|
||
|
"121",
|
||
|
"regallager",
|
||
|
"reliefpfeiler"
|
||
|
};
|
||
|
|
||
|
// palindromes
|
||
|
for (var palindrome : PALINDROMES) {
|
||
|
assertTrue(isPalindrome(palindrome));
|
||
|
}
|
||
|
|
||
|
var NO_PALINDROMES = new String[]{
|
||
|
"abcdefghijklmnopqrstuvwxyz",
|
||
|
"sven vogel",
|
||
|
"koeffizient"
|
||
|
};
|
||
|
|
||
|
// no palindromes
|
||
|
for (var noPalindrome : NO_PALINDROMES) {
|
||
|
assertFalse(isPalindrome(noPalindrome));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Test recursive palindrome detection
|
||
|
*/
|
||
|
@Test
|
||
|
public void testLowercaseRecursive() {
|
||
|
var PALINDROMES = new String[]{
|
||
|
"otto",
|
||
|
"121",
|
||
|
"regallager",
|
||
|
"reliefpfeiler"
|
||
|
};
|
||
|
|
||
|
// palindromes
|
||
|
for (var palindrome : PALINDROMES) {
|
||
|
assertTrue(isPalindromeRecursive(palindrome));
|
||
|
}
|
||
|
|
||
|
var NO_PALINDROMES = new String[]{
|
||
|
"abcdefghijklmnopqrstuvwxyz",
|
||
|
"sven vogel",
|
||
|
"koeffizient"
|
||
|
};
|
||
|
|
||
|
// no palindromes
|
||
|
for (var noPalindrome : NO_PALINDROMES) {
|
||
|
assertFalse(isPalindromeRecursive(noPalindrome));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Test iterative palindrome detection with ignore case
|
||
|
*/
|
||
|
@Test
|
||
|
public void testIgnoreCaseIterative() {
|
||
|
var PALINDROMES = new String[]{
|
||
|
"OtTo",
|
||
|
"121",
|
||
|
"ReGaLlAgeR",
|
||
|
"reLiEfPfeIler"
|
||
|
};
|
||
|
|
||
|
// palindromes
|
||
|
for (var palindrome : PALINDROMES) {
|
||
|
assertTrue(isPalindromeIgnoreCase(palindrome));
|
||
|
}
|
||
|
|
||
|
var NO_PALINDROMES = new String[]{
|
||
|
"AbcdeFghijKlmnOpqRstUvwxyz",
|
||
|
"sVeN vOgeL",
|
||
|
"kOefFiZiEnt"
|
||
|
};
|
||
|
|
||
|
// no palindromes
|
||
|
for (var noPalindrome : NO_PALINDROMES) {
|
||
|
assertFalse(isPalindromeIgnoreCase(noPalindrome));
|
||
|
}
|
||
|
}
|
||
|
}
|