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 testKnownEncodings() {
- 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");
- }
- private 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));
- }
- public void testGeneratedStrings() {
- final StringBuilder inputValue = new StringBuilder();
- while (inputValue.length() <= 13) {
- 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));
- }
- }
- public void testBadEncodedString() {
- try {
- decode("1");
- fail("Expected IllegalStateException.");
- } catch (final IllegalStateException ex) {
- assertEquals("No zeros followed by an odd number of ones (1) is not valid.", ex.getMessage());
- }
- }
- public void testBadCharacterInEncodedString() {
- try {
- decode("020");
- fail("Expected IllegalStateException.");
- } catch (final IllegalStateException ex) {
- assertEquals("Not expecting character <2> in value to decode <020>. Only '0' and '1' are valid.", ex.getMessage());
- }
- }
- 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();
- }
- final StringBuilder _decodedValue = new StringBuilder();
- int _leadingZeros = 0;
- int _countOfOnes = 0;
- private String decode(final String encodedString) {
- _decodedValue.setLength(0);
- _leadingZeros = 0;
- _countOfOnes = 0;
- for (final char chr : encodedString.toCharArray()) {
- switch (chr) {
- case '0':
- if (_countOfOnes > 0) {
- appendDecode();
- }
- ++_leadingZeros;
- break;
- case '1':
- ++_countOfOnes;
- break;
- default:
- throw new IllegalStateException("Not expecting character <" + chr + "> in value to decode <" + encodedString + ">. Only '0' and '1' are valid.");
- }
- }
- appendDecode();
- return _decodedValue.toString();
- }
- private void appendDecode() {
- if (_countOfOnes % 2 == 0) { /* even number of ones (possibly zero) */
- appendNofChar(_decodedValue, _leadingZeros, 'a');
- appendNofChar(_decodedValue, _countOfOnes / 2, 'c');
- } else /* odd number of ones */ {
- if (_leadingZeros == 0) {
- throw new IllegalStateException("No zeros followed by an odd number of ones (" + _countOfOnes + ") is not valid.");
- } else {
- // "Take out" the "01", representing the 'b' character "from the middle."
- --_leadingZeros;
- --_countOfOnes;
- appendNofChar(_decodedValue, _leadingZeros, 'a'); // Append the remaining 'a's (if any)
- appendNofChar(_decodedValue, 1, 'b'); // The 'b' for the "'0' to '1'" sequence.
- appendNofChar(_decodedValue, _countOfOnes / 2, 'c'); // Trailing 'c's (if any)
- }
- }
- _leadingZeros = 0;
- _countOfOnes = 0;
- }
- private static void appendNofChar(final StringBuilder stringBuilder, final int numberOfChar, final char chr) {
- for (int count = 0; count < numberOfChar; ++count) {
- stringBuilder.append(chr);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement