Advertisement
JeffGrigg

Untitled

Dec 8th, 2017
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.80 KB | None | 0 0
  1. import junit.framework.TestCase;
  2.  
  3. public class Lucifere20171208Test extends TestCase {
  4.  
  5.     public void test() {
  6.         assertFirstEncodesToSecondAndBack("", "");
  7.  
  8.         assertFirstEncodesToSecondAndBack("a", "0");
  9.         assertFirstEncodesToSecondAndBack("b", "01");
  10.         assertFirstEncodesToSecondAndBack("c", "11");
  11.  
  12.         assertFirstEncodesToSecondAndBack("aa", "00");
  13.         assertFirstEncodesToSecondAndBack("bb", "0101");
  14.         assertFirstEncodesToSecondAndBack("cc", "1111");
  15.  
  16.         assertFirstEncodesToSecondAndBack("ab", "001");
  17.         assertFirstEncodesToSecondAndBack("ac", "011");
  18.         assertFirstEncodesToSecondAndBack("ba", "010");
  19.         assertFirstEncodesToSecondAndBack("bc", "0111");
  20.         assertFirstEncodesToSecondAndBack("ca", "110");
  21.         assertFirstEncodesToSecondAndBack("cb", "1101");
  22.  
  23.         final StringBuilder inputValue = new StringBuilder();
  24.         while (inputValue.length() <= 10) {
  25.             boolean carry = true;
  26.             for (int idx = inputValue.length() - 1; idx >= 0 && carry; --idx) {
  27.                 final char nextChar = (char)(inputValue.charAt(idx) + 1);
  28.                 if (nextChar <= 'c') {
  29.                     inputValue.setCharAt(idx, nextChar);
  30.                     carry = false;
  31.                 } else {
  32.                     inputValue.setCharAt(idx, 'a');
  33.                     carry = true;
  34.                 }
  35.             }
  36.             if (carry) {
  37.                 inputValue.insert(0, "a");
  38.             }
  39.             final String plainText = inputValue.toString();
  40.             //System.out.println(plainText);
  41.             final String encodedValue = encode(plainText);
  42.             assertEquals("Plain text <" + plainText + ">, encoded as <" + encodedValue + ">;", plainText, decode(encodedValue));
  43.         }
  44.     }
  45.  
  46.     private static void assertFirstEncodesToSecondAndBack(final String plainText, final String encodedString) {
  47.         assertEquals("Plain text string <" + plainText + "> encoding;", encodedString, encode(plainText));
  48.         assertEquals("Encoded text string <\" + encodedString + \"> decoding;", plainText, decode(encodedString));
  49.     }
  50.  
  51.     private static String encode(final String plainText) {
  52.         final StringBuilder encodedValue = new StringBuilder();
  53.         for (final char chr : plainText.toCharArray()) {
  54.             switch (chr) {
  55.                 case 'a': encodedValue.append("0"); break;
  56.                 case 'b': encodedValue.append("01"); break;
  57.                 case 'c': encodedValue.append("11"); break;
  58.             }
  59.         }
  60.         return encodedValue.toString();
  61.     }
  62.  
  63.     private static String decode(String encodedString) {
  64.         final StringBuilder decodedValue = new StringBuilder();
  65.         int currentLength = encodedString.length();
  66.         while (encodedString.length() > 0) {
  67.             if (encodedString.endsWith("0")) {
  68.                 decodedValue.append('a');
  69.                 encodedString = encodedString.substring(0, encodedString.length() - 1);
  70.             } else if (encodedString.endsWith("01")) {
  71.                 decodedValue.append('b');
  72.                 encodedString = encodedString.substring(0, encodedString.length() - 2);
  73.             } else if (encodedString.endsWith("11")) {
  74.                 decodedValue.append('c');
  75.                 encodedString = encodedString.substring(0, encodedString.length() - 2);
  76.             } else {
  77.                 throw new IllegalStateException("Don't know how to process string ending like this <" + encodedString + ">");
  78.             }
  79.  
  80.             if (encodedString.length() == currentLength) {
  81.                 throw new IllegalStateException("Infinate loop on <" + encodedString + ">");
  82.             }
  83.             currentLength = encodedString.length();
  84.         }
  85.         return decodedValue.reverse().toString();
  86.     }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement