Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import junit.framework.TestCase;
- import java.util.Arrays;
- public class SolutionTest extends TestCase {
- public void testMultipleValues() {
- assertIsBeautiful("");
- isNotBeautifiable("a");
- isNotBeautifiable("aa");
- assertIsBeautiful("ab");
- isNotBeautifiable("abc");
- assertIsBeautiful("aabb");
- isBeautifiable("abba");
- isBeautifiable("abbc");
- isNotBeautifiable("abaa");
- isNotBeautifiable("abcde");
- assertIsBeautiful("aaabbb");
- isNotBeautifiable("abaaab");
- isBeautifiable("abccba");
- isBeautifiable("aabbba");
- isNotBeautifiable("abcdefghijklmnopqrstuvwxyza");
- isBeautifiable(copies(80, "abcdefghijklmnopqrstuvwxyz") + "aa");
- // isBeautifiable(copies(3846, "abcdefghijklmnopqrstuvwxyz") + "aa");
- // isBeautifiable(copies(4000, "abcdefghijklmnopqrstuvwxyz") + "aa");
- isNotBeautifiable("abbb");
- isNotBeautifiable("aabbbb");
- }
- private String copies(final int numberOfCopies, final String value) {
- final StringBuilder stringBuilder = new StringBuilder();
- for (int counter = 0; counter < numberOfCopies; ++counter) {
- stringBuilder.append(value);
- }
- return stringBuilder.toString();
- }
- private static void assertIsBeautiful(final String beautifulStringValue) {
- assertEquals("The String value <" + beautifulStringValue + "> is beautiful;", true, isBeautiful(beautifulStringValue));
- assertEquals("The String value <" + beautifulStringValue + "> is beautiful;", reverse(beautifulStringValue), Solution.makeBeautiful(beautifulStringValue));
- }
- private static void isNotBeautifiable(final String original) {
- assertEquals("The String value <" + original + "> is NOT beautiful;", false, isBeautiful(original));
- assertEquals("The String value <" + original + "> is NOT beautiful;", null, Solution.makeBeautiful(original));
- }
- private static void isBeautifiable(final String original) {
- System.out.println("isBeautifiable " + original.length() + " : " + original);
- assertEquals("The String value <" + original + "> is NOT beautiful;", false, isBeautiful(original));
- final String butified = Solution.makeBeautiful(original);
- if (butified == null) {
- fail("The String value <" + original + "> is should be beautifyable.");
- } else {
- assertEquals("The String value <" + original + "> is should be beautifyable, as value <" + butified + ">;",
- true, isBeautiful(butified));
- assertEquals("The String value <" + original + "> and butified value <" + butified + "> should be the same length;",
- original.length(), butified.length());
- assertEquals("The String value <" + original + "> and butified value <" + butified + "> should be the same length;",
- sortChars(original), sortChars(butified));
- }
- }
- private static boolean isBeautiful(final String value) {
- if (value.length() == 0) {
- return true;
- }
- final int centerIndex = value.length() / 2;
- for (int charIdx = 0; charIdx <= centerIndex; ++charIdx) {
- if (value.charAt(charIdx) == value.charAt(value.length() - charIdx - 1)) {
- return false; // Found duplicate character.
- }
- }
- return true; // No duplicates found.
- }
- private static String reverse(final CharSequence value) {
- return new StringBuilder(value).reverse().toString();
- }
- private static String sortChars(final String original) {
- final char[] characters = original.toCharArray();
- Arrays.sort(characters);
- return new String(characters);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement