Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import junit.framework.TestCase;
- public class Lucifere20171208Test extends TestCase {
- public void test() {
- assertFirstEncodesToSecondAndBack("", "");
- assertFirstEncodesToSecondAndBack("a", "0");
- assertFirstEncodesToSecondAndBack("b", "01");
- assertFirstEncodesToSecondAndBack("c", "11");
- assertFirstEncodesToSecondAndBack("aa", "00");
- assertFirstEncodesToSecondAndBack("bb", "0101");
- assertFirstEncodesToSecondAndBack("cc", "1111");
- assertFirstEncodesToSecondAndBack("ab", "001");
- assertFirstEncodesToSecondAndBack("ac", "011");
- assertFirstEncodesToSecondAndBack("ba", "010");
- assertFirstEncodesToSecondAndBack("bc", "0111");
- assertFirstEncodesToSecondAndBack("ca", "110");
- assertFirstEncodesToSecondAndBack("cb", "1101");
- final StringBuilder inputValue = new StringBuilder();
- while (inputValue.length() <= 10) {
- boolean carry = true;
- for (int idx = inputValue.length() - 1; idx >= 0 && carry; --idx) {
- final char nextChar = (char)(inputValue.charAt(idx) + 1);
- if (nextChar <= 'c') {
- inputValue.setCharAt(idx, nextChar);
- carry = false;
- } else {
- inputValue.setCharAt(idx, 'a');
- carry = true;
- }
- }
- if (carry) {
- inputValue.insert(0, "a");
- }
- final String plainText = inputValue.toString();
- //System.out.println(plainText);
- final String encodedValue = encode(plainText);
- assertEquals("Plain text <" + plainText + ">, encoded as <" + encodedValue + ">;", plainText, decode(encodedValue));
- }
- }
- private static void assertFirstEncodesToSecondAndBack(final String plainText, final String encodedString) {
- assertEquals("Plain text string <" + plainText + "> encoding;", encodedString, encode(plainText));
- assertEquals("Encoded text string <\" + encodedString + \"> decoding;", plainText, decode(encodedString));
- }
- private static String encode(final String plainText) {
- final StringBuilder encodedValue = new StringBuilder();
- for (final char chr : plainText.toCharArray()) {
- switch (chr) {
- case 'a': encodedValue.append("0"); break;
- case 'b': encodedValue.append("01"); break;
- case 'c': encodedValue.append("11"); break;
- }
- }
- return encodedValue.toString();
- }
- private static String decode(String encodedString) {
- final StringBuilder decodedValue = new StringBuilder();
- int currentLength = encodedString.length();
- while (encodedString.length() > 0) {
- if (encodedString.endsWith("0")) {
- decodedValue.append('a');
- encodedString = encodedString.substring(0, encodedString.length() - 1);
- } else if (encodedString.endsWith("01")) {
- decodedValue.append('b');
- encodedString = encodedString.substring(0, encodedString.length() - 2);
- } else if (encodedString.endsWith("11")) {
- decodedValue.append('c');
- encodedString = encodedString.substring(0, encodedString.length() - 2);
- } else {
- throw new IllegalStateException("Don't know how to process string ending like this <" + encodedString + ">");
- }
- if (encodedString.length() == currentLength) {
- throw new IllegalStateException("Infinate loop on <" + encodedString + ">");
- }
- currentLength = encodedString.length();
- }
- return decodedValue.reverse().toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement