Advertisement
JeffGrigg

Untitled

Dec 10th, 2017
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.49 KB | None | 0 0
  1. import junit.framework.TestCase;
  2.  
  3. public class Lucifere20171208Test extends TestCase {
  4.  
  5.     public void testKnownEncodings() {
  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.  
  24.     private void assertFirstEncodesToSecondAndBack(final String plainText, final String encodedString) {
  25.         assertEquals("Plain text string <" + plainText + "> encoding;", encodedString, encode(plainText));
  26.         assertEquals("Encoded text string <" + encodedString + "> decoding;", plainText, decode(encodedString));
  27.     }
  28.  
  29.     public void testGeneratedStrings() {
  30.         final StringBuilder inputValue = new StringBuilder();
  31.         while (inputValue.length() <= 13) {
  32.             boolean carry = true;
  33.             for (int idx = inputValue.length() - 1; idx >= 0 && carry; --idx) {
  34.                 final char nextChar = (char)(inputValue.charAt(idx) + 1);
  35.                 if (nextChar <= 'c') {
  36.                     inputValue.setCharAt(idx, nextChar);
  37.                     carry = false;
  38.                 } else {
  39.                     inputValue.setCharAt(idx, 'a');
  40.                     carry = true;
  41.                 }
  42.             }
  43.             if (carry) {
  44.                 inputValue.insert(0, "a");
  45.             }
  46.             final String plainText = inputValue.toString();
  47.             //System.out.println(plainText);
  48.             final String encodedValue = encode(plainText);
  49.             assertEquals("Plain text <" + plainText + ">, encoded as <" + encodedValue + ">;", plainText, decode(encodedValue));
  50.         }
  51.     }
  52.  
  53.     public void testBadEncodedString() {
  54.         try {
  55.             decode("1");
  56.             fail("Expected IllegalStateException.");
  57.         } catch (final IllegalStateException ex) {
  58.             assertEquals("No zeros followed by an odd number of ones (1) is not valid.", ex.getMessage());
  59.         }
  60.     }
  61.  
  62.     public void testBadCharacterInEncodedString() {
  63.         try {
  64.             decode("020");
  65.             fail("Expected IllegalStateException.");
  66.         } catch (final IllegalStateException ex) {
  67.             assertEquals("Not expecting character <2> in value to decode <020>.  Only '0' and '1' are valid.", ex.getMessage());
  68.         }
  69.     }
  70.  
  71.     private static String encode(final String plainText) {
  72.         final StringBuilder encodedValue = new StringBuilder();
  73.         for (final char chr : plainText.toCharArray()) {
  74.             switch (chr) {
  75.                 case 'a': encodedValue.append("0"); break;
  76.                 case 'b': encodedValue.append("01"); break;
  77.                 case 'c': encodedValue.append("11"); break;
  78.             }
  79.         }
  80.         return encodedValue.toString();
  81.     }
  82.  
  83.     final StringBuilder _decodedValue = new StringBuilder();
  84.     int _leadingZeros = 0;
  85.     int _countOfOnes = 0;
  86.  
  87.     private String decode(final String encodedString) {
  88.  
  89.         _decodedValue.setLength(0);
  90.         _leadingZeros = 0;
  91.         _countOfOnes = 0;
  92.  
  93.         for (final char chr : encodedString.toCharArray()) {
  94.             switch (chr) {
  95.                 case '0':
  96.                     if (_countOfOnes > 0) {
  97.                         appendDecode();
  98.                     }
  99.                     ++_leadingZeros;
  100.                     break;
  101.  
  102.                 case '1':
  103.                     ++_countOfOnes;
  104.                     break;
  105.  
  106.                 default:
  107.                     throw new IllegalStateException("Not expecting character <" + chr + "> in value to decode <" + encodedString + ">.  Only '0' and '1' are valid.");
  108.             }
  109.         }
  110.  
  111.         appendDecode();
  112.  
  113.         return _decodedValue.toString();
  114.     }
  115.  
  116.     private void appendDecode() {
  117.         if (_countOfOnes % 2 == 0) {  /* even number of ones (possibly zero) */
  118.             appendNofChar(_decodedValue, _leadingZeros, 'a');
  119.             appendNofChar(_decodedValue, _countOfOnes / 2, 'c');
  120.         } else /* odd number of ones */ {
  121.             if (_leadingZeros == 0) {
  122.                 throw new IllegalStateException("No zeros followed by an odd number of ones (" + _countOfOnes + ") is not valid.");
  123.             } else {
  124.                 // "Take out" the "01", representing the 'b' character "from the middle."
  125.                 --_leadingZeros;
  126.                 --_countOfOnes;
  127.  
  128.                 appendNofChar(_decodedValue, _leadingZeros, 'a');    // Append the remaining 'a's (if any)
  129.                 appendNofChar(_decodedValue, 1, 'b');   // The 'b' for the "'0' to '1'" sequence.
  130.                 appendNofChar(_decodedValue, _countOfOnes / 2, 'c');    // Trailing 'c's (if any)
  131.             }
  132.         }
  133.  
  134.         _leadingZeros = 0;
  135.         _countOfOnes = 0;
  136.     }
  137.  
  138.     private static void appendNofChar(final StringBuilder stringBuilder, final int numberOfChar, final char chr) {
  139.         for (int count = 0; count < numberOfChar; ++count) {
  140.             stringBuilder.append(chr);
  141.         }
  142.     }
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement